Dialog
AlphaAn accessible modal dialog with focus trapping, portal rendering, and compound component API.
Import
import { Dialog } from "@compa11y/react";Usage
Example.tsx
import { useState } from "react";
import { Button, Dialog } from "@compa11y/react";
function Example() {
const [open, setOpen] = useState(false);
return (
<>
<Button onClick={() => setOpen(true)}>
Open Dialog
</Button>
<Dialog open={open} onOpenChange={setOpen}>
<Dialog.Title>Confirm Action</Dialog.Title>
<Dialog.Description>
Are you sure you want to proceed? This action cannot be undone.
</Dialog.Description>
<Dialog.Actions>
<Button variant="outline" onClick={() => setOpen(false)}>Cancel</Button>
<Button variant="danger" onClick={() => setOpen(false)}>Confirm</Button>
</Dialog.Actions>
</Dialog>
</>
);
}Features
- Focus trapping within the dialog
- Focus restoration on close
- Escape key to close
- Click outside to close
- Portal rendering to document body
- Screen reader announcements
- Dev warnings for missing title/description
- Compound component API
Props
| Prop | Type | Default | Description |
|---|---|---|---|
open* | boolean | - | Controls dialog visibility |
onOpenChange* | (open: boolean) => void | - | Called when open state changes |
aria-label | string | - | Accessible label (alternative to Dialog.Title) |
children* | ReactNode | - | Dialog content |
Sub-components
Dialog.Title
| Prop | Type | Default | Description |
|---|---|---|---|
children* | ReactNode | - | Title text |
Dialog.Description
| Prop | Type | Default | Description |
|---|---|---|---|
children* | ReactNode | - | Description text |
Dialog.Actions
| Prop | Type | Default | Description |
|---|---|---|---|
children* | ReactNode | - | Action buttons |
Dialog.Close
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | - | Close button content |
Keyboard Interactions
Accessibility
All keyboard interactions follow WAI-ARIA best practices and work without any additional configuration.
| Key | Action |
|---|---|
| Tab | Moves focus between focusable elements within the dialog |
| Shift+Tab | Moves focus backwards |
| Escape | Closes the dialog |