Dialog

Accessibility

Accessibility props

NameTypeDescription
titleReact.NodeProvides a visible title that is associated with the dialog as its accessible name.
titleAs"h1" \| "h2" \| "h3" \| "h4" \| "h5" \| "h6" \| "div"Defines the semantic HTML element for the title while maintaining its visual appearance.
descriptionReact.NodeProvides additional context about the dialog’s purpose, which is automatically associated with the dialog via aria-describedby.
triggerRefReact.RefObject<HTMLElement>References the element that triggered the dialog, allowing focus to return to it when the dialog closes.

Automatic Accessibility Features

Focus Management

Best practices

    1. Apply aria-expanded="true" to the trigger element when the dialog is open and aria-expanded="false" when it’s closed.
    2. Use aria-controls on the trigger element to associate it with the dialog via Dialog component’s id.

Keyboard Navigation

  • Escape key closes the dialog
  • Tab key navigates through focusable elements within the dialog, with focus being contained within the dialog while it’s open
  • Enter/Space keys activate buttons and other interactive elements within the dialog

Examples

Basic Dialog with accessible title and semantic heading level

<Dialog
title="Edit your profile information"
titleAs="h2"
description="Update your personal details to keep your account information current."
primaryAction={<Button type="critical">Save changes</Button>}
secondaryAction={<Button type="secondary">Cancel</Button>}
onClose={() => handleClose()}
/>

Dialog with proper trigger

function ExampleComponent() {
const [isOpen, setIsOpen] = React.useState(false);
const buttonRef = React.useRef(null);
return (
<>
<Button ref={buttonRef} onClick={() => setIsOpen(true)} aria-expanded={isOpen}>
Change email preferences
</Button>
{isOpen && (
<Dialog
title="Email notification settings"
description="Choose which email notifications you'd like to receive from us."
primaryAction={
<Button
onClick={() => {
savePreferences();
setIsOpen(false);
}}
>
Save preferences
</Button>
}
secondaryAction={
<Button type="secondary" onClick={() => setIsOpen(false)}>
Cancel
</Button>
}
onClose={() => setIsOpen(false)}
triggerRef={buttonRef}
/>
)}
</>
);
}