diff --git a/src/components/ui/Dialog.tsx b/src/components/ui/Dialog.tsx index 6a40c1d9..eb14fc12 100644 --- a/src/components/ui/Dialog.tsx +++ b/src/components/ui/Dialog.tsx @@ -1,3 +1,67 @@ -export default function Dialog() { - return
Dialog placeholder
+import { useEffect, type ReactNode } from 'react' +import { AnimatePresence, motion } from 'motion/react' + +export interface DialogProps { + open: boolean + onClose: () => void + title: string + children: ReactNode + size?: 'sm' | 'md' | 'lg' | 'xl' +} + +const SIZE: Record, string> = { + sm: 'max-w-sm', + md: 'max-w-md', + lg: 'max-w-2xl', + xl: 'max-w-4xl', +} + +export default function Dialog({ open, onClose, title, children, size = 'md' }: DialogProps) { + useEffect(() => { + if (!open) return + const onKey = (e: KeyboardEvent) => e.key === 'Escape' && onClose() + window.addEventListener('keydown', onKey) + return () => window.removeEventListener('keydown', onKey) + }, [open, onClose]) + + return ( + + {open && ( +
+ + +
+

{title}

+ +
+
{children}
+
+
+ )} +
+ ) } diff --git a/tests/unit/ui.Dialog.test.tsx b/tests/unit/ui.Dialog.test.tsx new file mode 100644 index 00000000..32ea6659 --- /dev/null +++ b/tests/unit/ui.Dialog.test.tsx @@ -0,0 +1,35 @@ +import { describe, it, expect, vi } from 'vitest' +import { render, screen } from '@testing-library/react' +import userEvent from '@testing-library/user-event' +import Dialog from '@/components/ui/Dialog' + +describe('Dialog', () => { + it('renders nothing when open=false', () => { + render( + {}} title="X"> + body + , + ) + expect(screen.queryByText('body')).toBeNull() + }) + + it('renders content when open=true', () => { + render( + {}} title="X"> + body + , + ) + expect(screen.getByText('body')).toBeInTheDocument() + }) + + it('calls onClose on backdrop click', async () => { + const onClose = vi.fn() + render( + + body + , + ) + await userEvent.click(screen.getByTestId('dialog-backdrop')) + expect(onClose).toHaveBeenCalled() + }) +})