Files
faunosteo/apps/app/src/components/shell/MobileDrawer.tsx
T
bchanot 6f885eccb2 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.
2026-10-25 22:36:31 +01:00

79 lines
3.1 KiB
TypeScript

import { useEffect } from 'react'
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'
import { useT } from '@/i18n/useT'
export interface MobileDrawerProps {
open: boolean
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(() => {
const query = window.matchMedia(LARGE_SCREEN)
const onChange = (event: MediaQueryListEvent) => event.matches && onClose()
query.addEventListener('change', onChange)
return () => query.removeEventListener('change', onChange)
}, [onClose])
return (
<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}
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>
</ModalDialog>
)
}