Skip to content

Commit

Permalink
feat(modal): add a .close() function to smoothly animate modal away i…
Browse files Browse the repository at this point in the history
…nstead of unmounting

Use a ref passed to the Modal to get access to the function, and use like: modalRef.current.close()
  • Loading branch information
aVileBroker committed Jun 20, 2022
1 parent 24b34bc commit ebcb993
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 140 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@babel/preset-react": "^7.9.4",
"@babel/preset-typescript": "^7.9.0",
"@juggle/resize-observer": "^3.1.3",
"@react-spring/web": "^9.2.4",
"@react-spring/web": "^9.4.5",
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/git": "^9.0.0",
"@storybook/addon-a11y": "^6.5.9",
Expand All @@ -55,7 +55,7 @@
"@typescript-eslint/eslint-plugin": "^3.0.0",
"@typescript-eslint/parser": "^3.0.0",
"babel-loader": "^8.1.0",
"commitizen": "^4.1.2",
"commitizen": "^4.2.4",
"css-loader": "^3.5.3",
"cz-conventional-changelog": "3.2.0",
"eslint": "^7.0.0",
Expand Down Expand Up @@ -97,6 +97,7 @@
"@mdi/js": "^5.9.55",
"@mdi/react": "^1.4.0",
"@react-stately/toggle": "^3.2.3",
"detect-gpu": "^4.0.27",
"fuse.js": "^6.4.6",
"lodash.debounce": "^4.0.8",
"polished": "^4.1.1",
Expand Down
15 changes: 12 additions & 3 deletions src/components/Modal/Modal.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { useState } from 'react';
import React, { useCallback, useRef, useState } from 'react';
import styled from 'styled-components';
import { Story, Meta } from '@storybook/react';

import colors from '../../enums/colors';
import Modal, { ModalProps } from './Modal';
import Modal, { ModalProps, ModalApi } from './Modal';
import Button from '../Button/Button';
import Card from '../Card';
import { withFoundryContext } from '../../../.storybook/decorators';
Expand Down Expand Up @@ -38,6 +38,14 @@ export const Default: Story<DefaultProps> = ({
onClose();
};

const modalRef = useRef<ModalApi>();

const customCloseButtonClick = useCallback(() => {
if (modalRef.current) {
modalRef.current.close();
}
}, [modalRef]);

return (
<Background>
<Card elevation={1} header="Use this button to open the modal again">
Expand All @@ -55,11 +63,12 @@ export const Default: Story<DefaultProps> = ({
backgroundBlur={`${backgroundBlur}rem`}
onClickOutside={onClickOutside ? handleClose : undefined}
onClose={handleClose}
ref={modalRef}
>
<Card
header="Hello world!"
footer={
<Button color={colors.primaryDark} onClick={handleClose}>
<Button color={colors.primaryDark} onClick={customCloseButtonClick}>
Okay...
</Button>
}
Expand Down
Loading

0 comments on commit ebcb993

Please sign in to comment.