diff --git a/src/App.tsx b/src/App.tsx index 6f74d94..280d6c4 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,5 +1,32 @@ +import { useRef } from 'react'; +import Dialog from '../src/components/Dialog.tsx'; + +interface DialogElement { + openModal: () => void; + closeModal: () => void; +} + function App() { - return
; + const dialogRef1 = useRef(null); + const dialogRef2 = useRef(null); + + const openModal = (dialogRef: React.RefObject) => { + if (dialogRef.current) { + dialogRef.current?.openModal(); + } + }; + + return ( +
+ + +
+ + +

children

+
+
+ ); } export default App; diff --git a/src/components/Dialog.stories.ts b/src/components/Dialog.stories.ts new file mode 100644 index 0000000..80dff2b --- /dev/null +++ b/src/components/Dialog.stories.ts @@ -0,0 +1,13 @@ +import Dialog from './Dialog.tsx'; +import { Meta, StoryObj } from '@storybook/react'; + +const meta: Meta = { + title: 'Dialog', + component: Dialog, +}; + +export default meta; + +export type Story = StoryObj; + +export const Default: Story = {}; diff --git a/src/components/Dialog.tsx b/src/components/Dialog.tsx new file mode 100644 index 0000000..d7c1265 --- /dev/null +++ b/src/components/Dialog.tsx @@ -0,0 +1,49 @@ +import { ReactNode, forwardRef, useImperativeHandle, useRef } from 'react'; + +interface Props { + children?: ReactNode; //선택 + title?: string; //선택 + desc: string; //필수 +} + +interface DialogElement { + openModal: () => void; + closeModal: () => void; +} + +const Dialog = forwardRef((props, ref) => { + const { children, title, desc } = props; + const dialogRef = useRef(null); + + useImperativeHandle(ref, () => ({ + openModal: () => dialogRef.current?.showModal(), + closeModal: () => dialogRef.current?.close(), + })); + + //동작 : 어두운 바닥 클릭시 닫기 + const handleClose = (event: React.MouseEvent) => { + if (dialogRef.current && event.target === dialogRef.current) { + dialogRef.current?.close(); + } + }; + + //텍스트 정렬 : title이 없을경우 desc 가운데 정렬 + const descClassName = `py-4 text-lg ${title ? '' : 'text-center'}`; + + return ( + +
e.stopPropagation()}> + {title &&

{title}

} +

{desc}

+ {children &&
{children}
} +
+ +
+
+
+ ); +}); + +export default Dialog;