The AlertDialog component is a modal dialog that appears on top of the application content to present critical information or require a confirmation from the user. It typically includes a title, an optional description, and action buttons.
Usage
To implement an AlertDialog, import the component and its sub-components. You’ll need a state variable to control the visibility of the dialog.
import { AlertDialog } from " @rhinolabs/ui " ;
import { useState } from " react " ;
export default function AlertDialogDemo () {
const [ open , setOpen ] = useState ( false );
< AlertDialog open = { open } onOpenChange = { setOpen } >
< AlertDialog.Trigger asChild >
< button className = " rounded-md bg-destructive px-4 py-2 text-sm font-medium text-destructive-foreground hover:bg-destructive/90 focus:outline-none focus:ring-2 focus:ring-ring focus-offset-2 " >
< AlertDialog.Title > Are you absolutely sure? </ AlertDialog.Title >
< AlertDialog.Description >
This action cannot be undone. This will permanently delete your account
and remove your data from our servers.
</ AlertDialog.Description >
< AlertDialog.Cancel asChild >
< button className = " rounded-md border border-input bg-background px-4 py-2 text-sm font-medium shadow-sm transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 " >
< AlertDialog.Action asChild >
< button className = " inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 bg-destructive text-destructive-foreground hover:bg-destructive/90 " >
Examples
Basic AlertDialog
This example demonstrates a simple alert dialog with a title, description, cancel, and confirm action.
import { AlertDialog } from " @rhinolabs/ui " ;
import { useState } from " react " ;
export function SingleActionAlertDialogDemo () {
const [ open , setOpen ] = useState ( false );
< AlertDialog open = { open } onOpenChange = { setOpen } >
< AlertDialog.Trigger asChild >
< button className = " rounded-md bg-muted px-4 py-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground focus:outline-none focus:ring-2 focus:ring-ring focus-offset-2 " >
< AlertDialog.Title > Information </ AlertDialog.Title >
< AlertDialog.Description > This is an important message for you. </ AlertDialog.Description >
< AlertDialog.Action asChild >
< button className = " inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 bg-primary text-primary-foreground hover:bg-primary/90 " >
Props
AlertDialog Props
Prop Type Default Description openbooleanfalseControls the visibility state of the alert dialog. onOpenChange(open: boolean) => void- Callback function called when the open state changes. childrenReact.ReactNode- The content to be rendered within the AlertDialog component. ...propsReact.HTMLAttributes<HTMLDivElement>- All other standard HTML attributes for the root element.
AlertDialog.Trigger Props
Prop Type Default Description asChildbooleanfalseRenders the trigger as a child of the AlertDialog.Trigger component. childrenReact.ReactNode- The element that will trigger the dialog. ...propsReact.HTMLAttributes<HTMLElement>- All other standard HTML attributes for the trigger element.
AlertDialog.Portal Props
Prop Type Default Description childrenReact.ReactNode- The content to be rendered within the portal. ...propsReact.HTMLAttributes<HTMLDivElement>- All other standard HTML attributes for the portal container.
AlertDialog.Overlay Props
Prop Type Default Description classNamestring- Additional CSS classes to apply to the overlay. ...propsReact.HTMLAttributes<HTMLDivElement>- All other standard HTML attributes for the overlay.
AlertDialog.Content Props
Prop Type Default Description classNamestring- Additional CSS classes to apply to the content container. ...propsReact.HTMLAttributes<HTMLDivElement>- All other standard HTML attributes for the content container.
Prop Type Default Description classNamestring- Additional CSS classes to apply to the header container. ...propsReact.HTMLAttributes<HTMLDivElement>- All other standard HTML attributes for the header container.
Prop Type Default Description classNamestring- Additional CSS classes to apply to the footer container. ...propsReact.HTMLAttributes<HTMLDivElement>- All other standard HTML attributes for the footer container.
AlertDialog.Title Props
Prop Type Default Description classNamestring- Additional CSS classes to apply to the title element. ...propsReact.HTMLAttributes<HTMLHeadingElement>- All other standard HTML attributes for the title element.
AlertDialog.Description Props
Prop Type Default Description classNamestring- Additional CSS classes to apply to the description element. ...propsReact.HTMLAttributes<HTMLParagraphElement>- All other standard HTML attributes for the description element.
Design Considerations
Use AlertDialog for critical actions that users need to acknowledge or confirm.
Keep the title clear and concise, explaining the purpose of the dialog.
Provide a descriptive message to give users enough context to make a decision.
Clearly label action buttons to indicate their outcome.
Ensure the primary action is visually distinct.
Avoid using alert dialogs for non-critical information or as a primary means of navigation.
Accessibility
The AlertDialog component uses the appropriate ARIA roles (role="alertdialog", aria-modal="true") to identify it as an alert dialog.
Focus is managed automatically, typically being placed on the first interactive element within the dialog.
Users can typically close the dialog using the Escape key.
Ensure that the title and description are programmatically associated with the dialog using aria-labelledby and aria-describedby.