feat(ui): modal dialogs on the native dialog element (D13)

ModalDialog wraps showModal(): the page behind is inert and does not scroll, focus starts on the first control and returns to the opener, or to the first control of its nearest container when the opener is gone. Escape and close requests only ask the consumer. Dialog gains aria-labelledby and an optional described message; the confirmations pass theirs. The mobile drawer uses the same primitive and closes at the sidebar breakpoint.
This commit is contained in:
bchanot committed 2026-10-25 22:36:31 +01:00
1 parent 57922e0902
commit 6f885eccb2
9 files changed
+450 -118

No files matched your search

+50 -48
View File
@@ -3,6 +3,7 @@ import { Link } from 'react-router-dom'
import { AnimatePresence, motion, useReducedMotion } from 'motion/react'
import Brand from '@/components/brand/Brand'
import Icon from '@/components/ui/Icon'
import ModalDialog from '@/components/ui/ModalDialog'
import SidebarNav from './SidebarNav'
import SignOutButton from './SignOutButton'
import UserBadge from './UserBadge'
@@ -13,64 +14,65 @@ export interface MobileDrawerProps {
onClose: () => void
}
/** The sidebar's breakpoint (Tailwind `lg`): from there the drawer has no reason to stay open. */
const LARGE_SCREEN = '(min-width: 64rem)'
/** Left-side navigation drawer for small screens (transform + opacity only). */
export default function MobileDrawer({ open, onClose }: MobileDrawerProps) {
return <AnimatePresence>{open && <DrawerSurface onClose={onClose} />}</AnimatePresence>
}
function DrawerSurface({ onClose }: Pick<MobileDrawerProps, 'onClose'>) {
const t = useT()
const reduced = useReducedMotion()
const slide = reduced ? { x: 0 } : { x: '-100%' }
// A modal drawer left open while the window widens would keep the page inert.
useEffect(() => {
if (!open) return
const onKey = (e: KeyboardEvent) => e.key === 'Escape' && onClose()
window.addEventListener('keydown', onKey)
return () => window.removeEventListener('keydown', onKey)
}, [open, onClose])
const query = window.matchMedia(LARGE_SCREEN)
const onChange = (event: MediaQueryListEvent) => event.matches && onClose()
query.addEventListener('change', onChange)
return () => query.removeEventListener('change', onChange)
}, [onClose])
return (
<AnimatePresence>
{open && (
<div className="fixed inset-0 z-50 lg:hidden">
<motion.div
data-testid="drawer-backdrop"
className="absolute inset-0 bg-black/40"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.15 }}
<ModalDialog onClose={onClose} label={t('shell.nav_label')} className="open:block">
<motion.div
data-testid="drawer-backdrop"
className="absolute inset-0 bg-black/40"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.15 }}
onClick={onClose}
/>
<motion.aside
initial={slide}
animate={{ x: 0 }}
exit={slide}
transition={{ duration: reduced ? 0 : 0.2, ease: 'easeOut' }}
className="absolute inset-y-0 left-0 flex w-[min(20rem,85vw)] flex-col bg-[var(--color-surface)] shadow-xl pt-[env(safe-area-inset-top)] pb-[env(safe-area-inset-bottom)]"
>
<div className="flex items-center justify-between px-4 py-3 border-b border-[var(--color-border)]">
<Link to="/app" onClick={onClose} aria-label={t('shell.nav.dashboard')}>
<Brand size="md" />
</Link>
<button
type="button"
onClick={onClose}
/>
<motion.aside
role="dialog"
aria-modal="true"
aria-label={t('shell.nav_label')}
initial={slide}
animate={{ x: 0 }}
exit={slide}
transition={{ duration: reduced ? 0 : 0.2, ease: 'easeOut' }}
className="absolute inset-y-0 left-0 flex w-[min(20rem,85vw)] flex-col bg-[var(--color-surface)] shadow-xl pt-[env(safe-area-inset-top)] pb-[env(safe-area-inset-bottom)]"
aria-label={t('shell.close_menu')}
className="grid h-11 w-11 place-items-center rounded-lg text-[var(--color-ink-muted)] hover:bg-[var(--color-surface-strong)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)]"
>
<div className="flex items-center justify-between px-4 py-3 border-b border-[var(--color-border)]">
<Link to="/app" onClick={onClose} aria-label={t('shell.nav.dashboard')}>
<Brand size="md" />
</Link>
<button
type="button"
onClick={onClose}
aria-label={t('shell.close_menu')}
className="grid h-11 w-11 place-items-center rounded-lg text-[var(--color-ink-muted)] hover:bg-[var(--color-surface-strong)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)]"
>
<Icon name="close" />
</button>
</div>
<div className="flex-1 overflow-y-auto px-3 py-4">
<SidebarNav onNavigate={onClose} />
</div>
<div className="space-y-3 border-t border-[var(--color-border)] px-4 py-4">
<UserBadge />
<SignOutButton className="w-full" />
</div>
</motion.aside>
<Icon name="close" />
</button>
</div>
)}
</AnimatePresence>
<div className="flex-1 overflow-y-auto px-3 py-4">
<SidebarNav onNavigate={onClose} />
</div>
<div className="space-y-3 border-t border-[var(--color-border)] px-4 py-4">
<UserBadge />
<SignOutButton className="w-full" />
</div>
</motion.aside>
</ModalDialog>
)
}