|
| 1 | +import { Canvas, Meta, Description } from '@storybook/blocks'; |
| 2 | +import * as Modal from './Modal.stories'; |
| 3 | + |
| 4 | +<Meta of={Modal} /> |
| 5 | + |
| 6 | +# useModal |
| 7 | + |
| 8 | +애니메이션이 적용된 Modal을 portal을 통해 간편하게 관리하기 위한 훅입니다. |
| 9 | + |
| 10 | +## 함수 인자 |
| 11 | + |
| 12 | +modalProps객체를 받습니다. 해당 객체는 아래와 같이 구성됩니다. |
| 13 | + |
| 14 | +```ts |
| 15 | +interface UseModalProps { |
| 16 | + modalRoot?: ModalRoot; |
| 17 | + overlayClose?: boolean; |
| 18 | + overlayAnimation?: { |
| 19 | + showClassName?: string; |
| 20 | + hideClassName?: string; |
| 21 | + }; |
| 22 | + modalAnimation?: { |
| 23 | + showClassName?: string; |
| 24 | + hideClassName?: string; |
| 25 | + }; |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +`modalRoot`: 모달을 렌더링할 HTMLElement입니다. default는 `document.body`입니다. |
| 30 | + |
| 31 | +`overlayClose`: overlay를 눌러 modal을 닫을지를 설정합니다. default는 `true`입니다. |
| 32 | + |
| 33 | +`overlayAnimation`: Overlay에 적용될 애니메이션 className입니다. `showClassName`과 `hideClassName` 두 가지 key-value를 받을 수 있습니다. |
| 34 | + |
| 35 | +`modalAnimation`: Modal에 적용될 애니메이션 className입니다. `showClassName`과 `hideClassName` 두 가지 key-value를 받을 수 있습니다. |
| 36 | + |
| 37 | +## 반환값 |
| 38 | + |
| 39 | +`Modal`: 컴포넌트로,해당 컴포넌트로 감싸진 children이 지정한 root에 portal을 통해 렌더링 됩니다. |
| 40 | + |
| 41 | +`show`: 모달을 엽니다. |
| 42 | + |
| 43 | +`hide`: 모달을 닫습니다. |
| 44 | + |
| 45 | +`isShow`: 모달이 열려있는지 상태를 나타냅니다. |
| 46 | + |
| 47 | +```tsx |
| 48 | +function Modal() { |
| 49 | + const { Modal, show, isShow, hide } = useModal({ |
| 50 | + modalAnimation: { |
| 51 | + showClassName: showStyle, |
| 52 | + hideClassName: hideStyle, |
| 53 | + }, |
| 54 | + overlayAnimation: { |
| 55 | + showClassName: overlayShow, |
| 56 | + hideClassName: overlayHide, |
| 57 | + }, |
| 58 | + }); |
| 59 | + |
| 60 | + const handleClick = () => { |
| 61 | + if (isShow) hide(); |
| 62 | + show(); |
| 63 | + }; |
| 64 | + |
| 65 | + return ( |
| 66 | + <div> |
| 67 | + <button onClick={handleClick}>{isShow ? 'hide' : 'show'}</button> |
| 68 | + <Modal overlayClassName={Overlay} modalClassName={ModalContainer}> |
| 69 | + <div>모달!</div> |
| 70 | + <button onClick={hide}>닫기</button> |
| 71 | + </Modal> |
| 72 | + </div> |
| 73 | + ); |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +<Canvas of={Modal.defaultStory} /> |
0 commit comments