Dialog (Compound Component)
The dialog component informs users about a task and can contain critical information, require decisions, or involve multiple tasks. The Dialog component can be used to create a modal, alert, or other custom dialog and can be fully customized to fit your needs.
Anatomy
This component is comprised of several elements that work together to create a dialog. The following are the elements that make up the Dialog component.
API Reference
Root
The root element of the Dialog component. This element wraps the content and trigger elements and provides the scripting logic to open and close the dialog.
Content
The content of the Dialog component. This component is based on the <dialog>
element and can take in any additional HTML attributes. The following are the custom props that can be passed to the component.
Prop | Type | Default |
---|---|---|
hideClose | boolean | false |
Trigger
The trigger that opens the Dialog component. This element inherits from the Button component.
Prop | Type | Default |
---|---|---|
variant | "contained" | "outlined" | "subtle" | "text" | "contained" |
size | "sm" | "md" | "lg" | "md" |
title | string | — |
icon | boolean | false |
href | string | — |
disabled | boolean | false |
Accessibility
The <Dialog>
component leverages the <dialog>
element which is natively accessible. The following are some additional accessibility features that are built into the component:
- Clicking off from the dialog will close it. This is done by listening for a click event on the dialog element and checking if the target is the dialog itself.
- When the dialog element is opened, the body element will receive the
overflow-hidden
class to prevent scrolling. - When the dialog includes a form, we suggest passing
autofocus
to the first input field to ensure that the user can start interacting with the form immediately. - Ensure forms are validated before submitting. This can be done by adding the
required
attribute to the input fields and setting theformnovalidate
attribute on the cancel button.
Examples
Hide Close Button
You can hide the default close button by passing the hideClose
prop to the <Dialog.Content>
component.
Form Dialog
You can use a dialog to handle form data. The following example demonstrates how to create a custom form dialog using the <Dialog>
component.
Custom Trigger & Dialog
The dialog element can be customized to fit whatever context it is being used in. The following example demonstrates how to create a custom trigger and dialog.
Astro Component
It’s recommended that an element like this is made as a reusable component. In this case, we have put together the following Astro compound component to serve as a blueprint for how you could possibly set up a Dialog component, and what properties to consider.