89 lines
3.3 KiB
TypeScript
89 lines
3.3 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 { DEFAULT_SHELL } from './ShellChrome'
|
|
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` or `xl`): from there the drawer has no reason
|
|
* to stay open. */
|
|
closesFrom?: '64rem' | '80rem'
|
|
}
|
|
|
|
/** Left-side navigation drawer for small screens (transform + opacity only). */
|
|
export default function MobileDrawer({
|
|
open,
|
|
onClose,
|
|
closesFrom = DEFAULT_SHELL.closesFrom,
|
|
}: MobileDrawerProps) {
|
|
return (
|
|
<AnimatePresence>
|
|
{open && <DrawerSurface onClose={onClose} closesFrom={closesFrom} />}
|
|
</AnimatePresence>
|
|
)
|
|
}
|
|
|
|
function DrawerSurface({ onClose, closesFrom }: Required<Omit<MobileDrawerProps, 'open'>>) {
|
|
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(`(min-width: ${closesFrom})`)
|
|
if (query.matches) onClose()
|
|
const onChange = (event: MediaQueryListEvent) => event.matches && onClose()
|
|
query.addEventListener('change', onChange)
|
|
return () => query.removeEventListener('change', onChange)
|
|
}, [onClose, closesFrom])
|
|
|
|
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>
|
|
)
|
|
}
|