Components
Alert dialog
A confirmation that asks one question and waits for an answer. Clicking outside does not count as one.
import * as AlertDialog from "@sideby/ui/alert-dialog";
import { buttonVariants } from "@sideby/ui/button";
<AlertDialog.Root>
<AlertDialog.Trigger class={buttonVariants({ variant: "destructive" })}>Kick Leo</AlertDialog.Trigger>
<AlertDialog.Content destructive>
<AlertDialog.Header>
<AlertDialog.Title>Kick Leo?</AlertDialog.Title>
<AlertDialog.Description>
They'll leave the room right away and can only come back with a fresh link.
</AlertDialog.Description>
</AlertDialog.Header>
<AlertDialog.Footer>
<AlertDialog.Cancel>Keep Leo</AlertDialog.Cancel>
<AlertDialog.Action variant="destructive" onclick={kick}>Kick Leo</AlertDialog.Action>
</AlertDialog.Footer>
</AlertDialog.Content>
</AlertDialog.Root> When to use it
Use an alert dialog only to confirm something that cannot be undone: kicking someone from the room, handing the remote to someone else. It interrupts on purpose, so save it for the moments that earn it. If the action can be undone, skip the question and offer an undo afterwards instead.
For anything that is not a yes-or-no question, use a dialog.
Destructive confirmations
Set destructive on AlertDialog.Content when confirming removes something. Two things change:
- Cancel gets the first focus. Bits UI’s
onOpenAutoFocushook would focus the dialog itself;destructivesends focus to the Cancel button instead. Someone who opens the dialog by accident and presses Enter keeps Leo in the room. - The action uses the destructive skin. Pass
variant="destructive"toAlertDialog.Action, so the red reads as a warning before anyone reads the words.
The kick confirmation above is the pattern: the trigger and the action both say Kick Leo, and the dismiss action says Keep Leo.
A default action
A confirmation that does not remove anything keeps the default action, the page’s one accent fill, and the focus starts on the dialog.
import * as AlertDialog from "@sideby/ui/alert-dialog";
import { buttonVariants } from "@sideby/ui/button";
<AlertDialog.Root>
<AlertDialog.Trigger class={buttonVariants({ variant: "outline" })}>Make host</AlertDialog.Trigger>
<AlertDialog.Content>
<AlertDialog.Header>
<AlertDialog.Title>Make Mia the host?</AlertDialog.Title>
<AlertDialog.Description>
Mia gets the remote: play, pause and seek. You'll watch along like everyone else, and only Mia can hand it back.
</AlertDialog.Description>
</AlertDialog.Header>
<AlertDialog.Footer>
<AlertDialog.Cancel>Keep the remote</AlertDialog.Cancel>
<AlertDialog.Action onclick={makeHost}>Make host</AlertDialog.Action>
</AlertDialog.Footer>
</AlertDialog.Content>
</AlertDialog.Root> Parts
AlertDialog.Rootholds the open state (bind:open).AlertDialog.Triggeropens it; style it withbuttonVariants.AlertDialog.Contenthas the same surface, size and motion as a dialog, without a close button.destructivemoves the first focus to Cancel.AlertDialog.HeaderstacksAlertDialog.Title(the question) andAlertDialog.Description(what happens next).AlertDialog.FooterholdsAlertDialog.Cancel(always the outline skin) and thenAlertDialog.Action(variantisdefaultordestructive). Both close the dialog; run the action in the Action’sonclick.
Keyboard
- Enter or Space on the trigger opens it. Focus goes to Cancel when
destructiveis set, and to the dialog otherwise. - Tab and Shift+Tab move between Cancel and the action. Focus never leaves while it is open.
- Escape cancels. Clicking the scrim does nothing.
- When it closes, focus goes back to the trigger.
Accessibility
- Bits UI owns the behaviour: the
alertdialogrole,aria-modal, the focus trap, Escape, the scroll lock and focus return. - No scrim dismissal. A tap outside is not an answer, so the dialog ignores it. The only ways out are Cancel, the action and Escape, which cancels.
- The title is the question and names the dialog; the description is read with it, so a screen reader hears the consequence before the choices.
- Below
smthe action stacks on top of Cancel; fromsmthey sit in a row with the action last.
Copy
- The title asks one question with a question mark: “Kick Leo?”, “Make Mia the host?“.
- The description says what happens next, in plain words: “They’ll leave the room right away and can only come back with a fresh link.”
- The action repeats the verb and the object from the title: “Kick Leo”, not “Yes” or “OK”. Someone skimming the buttons should know what each one does.
- Cancel says what stays the same: “Keep Leo”, “Keep the remote”. A verb and a noun, like every button.