Re-show a natively closed dialog only when its consumer answered 'kept'; returnFocus; ignore a held Escape; Dialog honours reduced motion; useBeforeUnload extracted; focusRow reports found only when the row holds focus.
93 lines
2.9 KiB
TypeScript
93 lines
2.9 KiB
TypeScript
import { useId, type ReactNode } from 'react'
|
|
import { AnimatePresence, motion, useReducedMotion } from 'motion/react'
|
|
import ModalDialog, { type ModalDialogProps } from './ModalDialog'
|
|
import { useT } from '@/i18n/useT'
|
|
|
|
export interface DialogProps {
|
|
open: boolean
|
|
/** Asked on Escape, the close button and a backdrop click; the dialog leaves when `open` turns false. */
|
|
onClose: () => unknown
|
|
/** Where the focus goes after the close (ModalDialog): a target, or null when a move owns it. */
|
|
returnFocus?: ModalDialogProps['returnFocus']
|
|
title: string
|
|
/** The dialog's message, announced with its name (aria-describedby). */
|
|
description?: ReactNode
|
|
children: ReactNode
|
|
size?: 'sm' | 'md' | 'lg' | 'xl'
|
|
}
|
|
|
|
const SIZE: Record<NonNullable<DialogProps['size']>, string> = {
|
|
sm: 'max-w-sm',
|
|
md: 'max-w-md',
|
|
lg: 'max-w-2xl',
|
|
xl: 'max-w-4xl',
|
|
}
|
|
|
|
export default function Dialog({ open, ...surface }: DialogProps) {
|
|
return (
|
|
<AnimatePresence>{open && <DialogSurface {...surface} />}</AnimatePresence>
|
|
)
|
|
}
|
|
|
|
function DialogSurface({
|
|
onClose,
|
|
returnFocus,
|
|
title,
|
|
description,
|
|
children,
|
|
size = 'md',
|
|
}: Omit<DialogProps, 'open'>) {
|
|
const t = useT()
|
|
const reduced = useReducedMotion()
|
|
const scale = reduced ? 1 : 0.96
|
|
const titleId = useId()
|
|
const descriptionId = useId()
|
|
return (
|
|
<ModalDialog
|
|
onClose={onClose}
|
|
returnFocus={returnFocus}
|
|
labelledBy={titleId}
|
|
describedBy={description ? descriptionId : undefined}
|
|
className="open:flex items-center justify-center p-3 sm:p-4"
|
|
>
|
|
<motion.div
|
|
data-testid="dialog-backdrop"
|
|
className="absolute inset-0 bg-black/40"
|
|
initial={{ opacity: 0 }}
|
|
animate={{ opacity: 1 }}
|
|
exit={{ opacity: 0 }}
|
|
transition={{ duration: reduced ? 0 : 0.15 }}
|
|
onClick={onClose}
|
|
/>
|
|
<motion.div
|
|
initial={{ opacity: 0, scale }}
|
|
animate={{ opacity: 1, scale: 1 }}
|
|
exit={{ opacity: 0, scale }}
|
|
transition={{ duration: reduced ? 0 : 0.18 }}
|
|
className={`relative w-full ${SIZE[size]} max-h-[85vh] overflow-y-auto overscroll-contain rounded-2xl bg-[var(--color-surface)] shadow-xl`}
|
|
>
|
|
<header className="flex items-center justify-between px-4 sm:px-6 py-3 sm:py-4 border-b border-[var(--color-border)]">
|
|
<h2
|
|
id={titleId}
|
|
className="text-lg font-semibold text-[var(--color-ink)]"
|
|
>
|
|
{title}
|
|
</h2>
|
|
<button
|
|
type="button"
|
|
aria-label={t('common.close')}
|
|
onClick={onClose}
|
|
className="rounded-md p-1 text-[var(--color-ink-muted)] hover:bg-[var(--color-surface-strong)]"
|
|
>
|
|
✕
|
|
</button>
|
|
</header>
|
|
<div className="px-4 sm:px-6 py-3 sm:py-4">
|
|
{description && <div id={descriptionId}>{description}</div>}
|
|
{children}
|
|
</div>
|
|
</motion.div>
|
|
</ModalDialog>
|
|
)
|
|
}
|