A modal dialog component that overlays the main UI. Dialogs trap focus and prevent interaction with the underlying content, following accessibility best practices.
| Prop | Type | Required | Description |
|---|---|---|---|
| label | string | Yes | Dialog title displayed in the header |
| children | ReactNode | Yes | Dialog content |
| close | () => void | Yes | Function to close the dialog |
| Parameter | Type | Description |
|---|---|---|
| DialogComponent | (props: { close: () => void } & Input) => JSX.Element | Component to render inside the dialog |
| options.closeOnOutsideClick | boolean | Whether clicking outside closes the dialog |
Returns:
Click the button to open a dialog:
import { Dialog, useDialog } from "aidos-ui";
function MyDialog({ close }) {
return (
<Dialog close={close} label="Settings">
<Row padding="medium" align="center" justify="space-between">
<Span>Dark mode</Span>
<DarkModeToggle />
</Row>
</Dialog>
);
}
function App() {
const { open } = useDialog(
({ close }) => <MyDialog close={close} />,
{ closeOnOutsideClick: true }
);
return (
<Button color="primary" onClick={() => open()}>
Open Settings
</Button>
);
}
Use TypeScript generics to pass data when opening the dialog:
interface UserData {
name: string;
email: string;
}
function EditUserDialog({ close, name, email }: { close: () => void } & UserData) {
return (
<Dialog close={close} label="Edit User">
<Column padding="medium" gap="medium">
<TextInput value={name} onValueChange={() => {}} />
<TextInput value={email} onValueChange={() => {}} />
<Button color="primary" onClick={close}>
Save
</Button>
</Column>
</Dialog>
);
}
function App() {
const { open } = useDialog<UserData>(
({ close, name, email }) => (
<EditUserDialog close={close} name={name} email={email} />
),
{ closeOnOutsideClick: false }
);
return (
<Button
color="secondary"
onClick={() => open({ name: "John", email: "john@example.com" })}
>
Edit User
</Button>
);
}
Control whether clicking the backdrop closes the dialog:
// Dialog closes when clicking outside
const { open } = useDialog(
({ close }) => <MyDialog close={close} />,
{ closeOnOutsideClick: true }
);
// Dialog stays open until explicitly closed
const { open } = useDialog(
({ close }) => <MyDialog close={close} />,
{ closeOnOutsideClick: false }
);
Dialogs can be closed in several ways:
function ConfirmDialog({ close, onConfirm }) {
return (
<Dialog close={close} label="Confirm">
<Column padding="medium" gap="medium">
<P>Are you sure you want to proceed?</P>
<Row gap="medium" justify="end">
<Button bare color="secondary" onClick={close}>
Cancel
</Button>
<Button
color="primary"
onClick={() => {
onConfirm();
close();
}}
>
Confirm
</Button>
</Row>
</Column>
</Dialog>
);
}
The DialogProvider must wrap your app to enable dialogs. This is typically included in the Providers component:
import { Providers } from "aidos-ui";
function App() {
return (
<Providers>
{/* Your app content */}
</Providers>
);
}