Documentation / Hooks & utilities
useDialogA11y
The hook that turns a plain container into an accessible modal dialog: it powers Modal, Drawer and the mobile Sidebar, and it is exported for your own dialogs. It returns a dialogRef to set on the container and dialogProps to spread onto it (role="dialog", aria-modal, tabIndex={-1}). The accessible name is up to you: aria-labelledby pointing at the title, or aria-label.
While open is true, it:
- sets the initial focus (
initialFocus: the dialog, the first focusable element, a ref or nothing); - keeps Tab / Shift + Tab inside the dialog (
trapFocus) and closes on Escape (closeOnEscape→onClose); - makes the rest of the page
inert+aria-hidden(hideBackground, except the elements inkeepInteractive); - locks page scrolling (
lockScroll) and returns focus to the trigger on close (returnFocus).
Nested dialogs stack: only the topmost reacts to the keyboard. Its listener sits on window, so a menu or a dropdown list open inside the dialog consumes Escape before it does. "use client" hook.
Examples
Custom dialog
The container receives dialogRef and dialogProps; the title names it through aria-labelledby.
import { useId, useState } from "react";
import { Button, useDialogA11y } from "@synertech/ui";
export function CustomDialog() {
const [open, setOpen] = useState(false);
const titleId = useId();
const { dialogRef, dialogProps } = useDialogA11y<HTMLDivElement>({
open,
onClose: () => setOpen(false),
});
return (
<>
<Button onClick={() => setOpen(true)}>Ouvrir</Button>
{open && (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/40">
<div
ref={dialogRef}
{...dialogProps}
aria-labelledby={titleId}
className="w-80 rounded-lg bg-white p-5 shadow-xl"
>
<h2 id={titleId} className="font-semibold">
Session expirée
</h2>
<p className="mt-2 text-sm text-gray-600">Reconnectez-vous pour continuer.</p>
<Button className="mt-4" onClick={() => setOpen(false)}>
Fermer
</Button>
</div>
</div>
)}
</>
);
}Initial focus and focus return
initialFocus targets a specific field; returnFocus sends focus somewhere other than the trigger.
import { useRef, useState } from "react";
import { Button, useDialogA11y } from "@synertech/ui";
export function FocusControl() {
const [open, setOpen] = useState(false);
const inputRef = useRef<HTMLInputElement>(null);
const searchRef = useRef<HTMLInputElement>(null);
const { dialogRef, dialogProps } = useDialogA11y<HTMLFormElement>({
open,
onClose: () => setOpen(false),
initialFocus: inputRef,
returnFocus: searchRef,
});
return (
<div className="flex gap-2">
<input
ref={searchRef}
placeholder="Rechercher…"
className="rounded-md border px-3 py-2 text-sm"
/>
<Button onClick={() => setOpen(true)}>Renommer</Button>
{open && (
<form
ref={dialogRef}
{...dialogProps}
aria-label="Renommer le dossier"
onSubmit={(event) => {
event.preventDefault();
setOpen(false);
}}
className="fixed left-1/2 top-24 z-50 w-80 -translate-x-1/2 space-y-3 rounded-lg bg-white p-5 shadow-xl"
>
<input
ref={inputRef}
defaultValue="Factures 2025"
className="w-full rounded-md border px-3 py-2 text-sm"
/>
<Button type="submit">Valider</Button>
</form>
)}
</div>
);
}Clickable backdrop kept interactive
The backdrop sits outside the dialog: keepInteractive spares it so that a click on it closes the panel.
import { useRef, useState } from "react";
import { Button, useDialogA11y } from "@synertech/ui";
export function ClickableOverlay() {
const [open, setOpen] = useState(false);
const overlayRef = useRef<HTMLDivElement>(null);
const { dialogRef, dialogProps } = useDialogA11y<HTMLElement>({
open,
onClose: () => setOpen(false),
initialFocus: "first",
keepInteractive: [overlayRef],
});
return (
<>
<Button onClick={() => setOpen(true)}>Filtres</Button>
{open && (
<>
<div
ref={overlayRef}
className="fixed inset-0 z-40 bg-black/30"
onClick={() => setOpen(false)}
aria-hidden="true"
/>
<aside
ref={dialogRef}
{...dialogProps}
aria-label="Filtres"
className="fixed inset-y-0 right-0 z-50 w-72 bg-white p-5 shadow-xl"
>
<label className="flex items-center gap-2 text-sm">
<input type="checkbox" /> En stock uniquement
</label>
</aside>
</>
)}
</>
);
}Blocking dialog
closeOnEscape={false} forces an explicit choice; lockScroll={false} lets the page scroll.
import { useState } from "react";
import { Button, useDialogA11y } from "@synertech/ui";
export function Blocking() {
const [open, setOpen] = useState(false);
const { dialogRef, dialogProps } = useDialogA11y<HTMLDivElement>({
open,
closeOnEscape: false,
lockScroll: false,
});
return (
<>
<Button onClick={() => setOpen(true)}>Afficher les conditions</Button>
{open && (
<div
ref={dialogRef}
{...dialogProps}
aria-label="Nouvelles conditions"
className="fixed bottom-4 left-4 right-4 z-50 rounded-lg bg-white p-5 shadow-xl"
>
<p className="text-sm">Nos conditions d’utilisation évoluent au 1er janvier.</p>
<div className="mt-3 flex gap-2">
<Button size="sm" onClick={() => setOpen(false)}>
Accepter
</Button>
<Button
size="sm"
onClick={() => setOpen(false)}
bgColor="bg-gray-100 hover:bg-gray-200"
textColor="text-gray-800"
>
Refuser
</Button>
</div>
</div>
)}
</>
);
}API reference
Hooks
Turns any container into an accessible modal dialog: role="dialog" + aria-modal, initial focus, Tab / Shift+Tab containment, Escape, focus returned to the trigger, inert background and nested dialog support (only the topmost one reacts to the keyboard).
const { dialogRef, dialogProps } = useDialogA11y({ open, onClose });
return open ? <div ref={dialogRef} {...dialogProps} aria-labelledby={titleId}>…</div> : null;import { useDialogA11y } from "@synertech/ui";useDialogA11y<T extends HTMLElement = HTMLDivElement>(options: UseDialogA11yOptions): UseDialogA11yResult<T>Types
DialogA11yProps
InterfaceProps to spread on the dialog container.
interface DialogA11yProps| Field | Type | Default | Description |
|---|---|---|---|
aria-modal• |
true |
— | |
role• |
"dialog" |
— | |
tabIndex• |
-1 |
— | Required so the container itself can receive the initial focus. |
UseDialogA11yOptions
Interfaceinterface UseDialogA11yOptions| Field | Type | Default | Description |
|---|---|---|---|
open• |
boolean |
— | Whether the dialog is currently rendered. |
closeOnEscape |
boolean |
— | Close the dialog on Escape. Default |
hideBackground |
boolean |
— | Mark the rest of the page |
initialFocus |
DialogInitialFocus |
— | Element focused when the dialog opens. Default |
keepInteractive |
Array<RefObject<HTMLElement | null> | HTMLElement | null | undefined> |
— | Elements outside the dialog that must stay interactive while it is open (a clickable overlay, a portalled popover, ...). |
lockScroll |
boolean |
— | Prevent the page behind the dialog from scrolling. Default |
onClose |
() => void |
— | Called when Escape is pressed on the topmost dialog. |
returnFocus |
DialogReturnFocus |
— | Element focused when the dialog closes. Default |
trapFocus |
boolean |
— | Keep Tab / Shift+Tab inside the dialog. Default |
UseDialogA11yResult
Interfaceinterface UseDialogA11yResult<T extends HTMLElement>| Field | Type | Default | Description |
|---|---|---|---|
dialogProps• |
DialogA11yProps |
— | |
dialogRef• |
{ current: T | null } |
— | Attach to the dialog container (the element holding |
Where the focus goes when the dialog opens.
"dialog"(default): the dialog container itself, so screen readers announce its name and description before the content."first": the first focusable element inside the dialog."none": the focus is left untouched.- a ref: that specific element (falls back to the dialog if unmounted).
type DialogInitialFocus = "dialog" | "first" | "none" | RefObject<HTMLElement | null>Where the focus goes when the dialog closes.
true(default): back to the element that was focused before opening.false: nothing is restored.- a ref: that specific element.
type DialogReturnFocus = boolean | RefObject<HTMLElement | null>