Skip to content

Commit

Permalink
Merge pull request #113 from imaginer-dev/83--ํŒ์—…modal
Browse files Browse the repository at this point in the history
feat: ํŒ์—…(modal)์ถ”๊ฐ€
  • Loading branch information
gihwan-dev authored May 1, 2024
2 parents 231b818 + 8fe4a0c commit 83d61d0
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
import { useRef } from 'react';
import Dialog from '../src/components/Dialog.tsx';

interface DialogElement {
openModal: () => void;
closeModal: () => void;
}

function App() {
return <div></div>;
const dialogRef1 = useRef<DialogElement | null>(null);
const dialogRef2 = useRef<DialogElement | null>(null);

const openModal = (dialogRef: React.RefObject<DialogElement>) => {
if (dialogRef.current) {
dialogRef.current?.openModal();
}
};

return (
<div>
<button onClick={() => openModal(dialogRef1)}>ํŒ์—… ์—ด๊ธฐ 1</button>
<Dialog ref={dialogRef1} desc="ํŒ์—…์„ค๋ช…"></Dialog>
<br />
<button onClick={() => openModal(dialogRef2)}>ํŒ์—… ์—ด๊ธฐ 2</button>
<Dialog ref={dialogRef2} title="ํŒ์—…์ œ๋ชฉ" desc="ํŒ์—…์„ค๋ช…">
<p>children</p>
</Dialog>
</div>
);
}

export default App;
13 changes: 13 additions & 0 deletions src/components/Dialog.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Dialog from './Dialog.tsx';
import { Meta, StoryObj } from '@storybook/react';

const meta: Meta<typeof Dialog> = {
title: 'Dialog',
component: Dialog,
};

export default meta;

export type Story = StoryObj<typeof Dialog>;

export const Default: Story = {};
49 changes: 49 additions & 0 deletions src/components/Dialog.tsx
Original file line number Diff line number Diff line change
@@ -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<DialogElement, Props>((props, ref) => {
const { children, title, desc } = props;
const dialogRef = useRef<HTMLDialogElement | null>(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 (
<dialog ref={dialogRef} className="modal modal-middle" onClick={handleClose}>
<div className="modal-box" onClick={(e) => e.stopPropagation()}>
{title && <h3 className="text-lg font-bold">{title}</h3>}
<p className={descClassName}>{desc}</p>
{children && <div>{children}</div>}
<form method="dialog" className="modal-action flex justify-center">
<button type="button" className="btn bg-primary text-base-100" onClick={() => dialogRef.current?.close()}>
๋‹ซ๊ธฐ
</button>
</form>
</div>
</dialog>
);
});

export default Dialog;

0 comments on commit 83d61d0

Please sign in to comment.