Files
Ho Ngoc Hai d7961e297c feat(a11y): add DialogContext auto-labelling with aria-labelledby/describedby
Introduce DialogContext using React.useId() that auto-wires aria-labelledby
and aria-describedby on DialogContent, with matching ids on DialogTitle and
DialogDescription. Adds role="dialog" and aria-modal="true". All 12+ existing
consumers get proper ARIA labels without any call-site changes.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-24 10:33:54 +07:00

125 lines
3.3 KiB
TypeScript

'use client';
import * as React from 'react';
import { cn } from '@/lib/utils';
/* ------------------------------------------------------------------ */
/* DialogContext — auto-wires aria-labelledby / aria-describedby */
/* ------------------------------------------------------------------ */
interface DialogContextValue {
titleId: string;
descriptionId: string;
}
const DialogContext = React.createContext<DialogContextValue | null>(null);
function useDialogContext() {
return React.useContext(DialogContext);
}
/* ------------------------------------------------------------------ */
interface DialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
children: React.ReactNode;
}
function Dialog({ open, onOpenChange, children }: DialogProps) {
const reactId = React.useId();
const titleId = `${reactId}-dialog-title`;
const descriptionId = `${reactId}-dialog-desc`;
React.useEffect(() => {
if (open) {
document.body.style.overflow = 'hidden';
} else {
document.body.style.overflow = '';
}
return () => {
document.body.style.overflow = '';
};
}, [open]);
if (!open) return null;
return (
<DialogContext.Provider value={{ titleId, descriptionId }}>
<div className="fixed inset-0 z-50">
<div
className="fixed inset-0 bg-black/80 animate-in fade-in-0"
onClick={() => onOpenChange(false)}
/>
<div className="fixed inset-0 flex items-center justify-center p-4">
{children}
</div>
</div>
</DialogContext.Provider>
);
}
const DialogContent = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, children, ...props }, ref) => {
const ctx = useDialogContext();
return (
<div
ref={ref}
role="dialog"
aria-modal="true"
aria-labelledby={ctx?.titleId}
aria-describedby={ctx?.descriptionId}
className={cn(
'relative z-50 w-full max-w-lg rounded-lg border bg-background p-6 shadow-lg animate-in fade-in-0 zoom-in-95',
className,
)}
onClick={(e) => e.stopPropagation()}
{...props}
>
{children}
</div>
);
});
DialogContent.displayName = 'DialogContent';
function DialogHeader({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) {
return (
<div className={cn('flex flex-col space-y-1.5 text-center sm:text-left', className)} {...props} />
);
}
function DialogTitle({ className, id, ...props }: React.HTMLAttributes<HTMLHeadingElement>) {
const ctx = useDialogContext();
return (
<h2
id={id ?? ctx?.titleId}
className={cn('text-lg font-semibold leading-none tracking-tight', className)}
{...props}
/>
);
}
function DialogDescription({ className, id, ...props }: React.HTMLAttributes<HTMLParagraphElement>) {
const ctx = useDialogContext();
return (
<p
id={id ?? ctx?.descriptionId}
className={cn('text-sm text-muted-foreground', className)}
{...props}
/>
);
}
function DialogFooter({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) {
return (
<div
className={cn('flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2 mt-4', className)}
{...props}
/>
);
}
export { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter };