Components
Alert dialog
A dialog that stops everything to ask for a yes or no before something is lost.
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from '@dex/shared/components/alert-dialog'
import { Button } from '@dex/shared/components/button'
export function AlertDialogDefault() {
return (
<AlertDialog>
<AlertDialogTrigger render={<Button variant="outline" />}>Close editor</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Discard your changes?</AlertDialogTitle>
<AlertDialogDescription>
You have edited your CV since you last saved it. If you close the editor now, those
edits are lost.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Keep editing</AlertDialogCancel>
<AlertDialogAction>Discard changes</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
)
}
'use client'
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from '@dex/shared/components/alert-dialog'
import { Button } from '@dex/shared/components/button'
import { toast } from '@dex/shared/components/toast'
export function AlertDialogDestructive() {
return (
<AlertDialog>
<AlertDialogTrigger render={<Button variant="destructive" />}>
Delete account
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Delete your account?</AlertDialogTitle>
<AlertDialogDescription>
This deletes your profile, your CVs and your applications. It cannot be undone.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction variant="destructive" onClick={() => toast.success('Account deleted')}>
Delete account
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
)
}
'use client'
import { useState } from 'react'
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from '@dex/shared/components/alert-dialog'
import { Button } from '@dex/shared/components/button'
export function AlertDialogControlled() {
const [isOpen, setIsOpen] = useState(false)
const [isWithdrawn, setIsWithdrawn] = useState(false)
return (
<>
<Button variant="outline" disabled={isWithdrawn} onClick={() => setIsOpen(true)}>
Withdraw application
</Button>
{isWithdrawn && <p className="text-sm text-muted-foreground">Application withdrawn.</p>}
<AlertDialog open={isOpen} onOpenChange={setIsOpen}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Withdraw your application?</AlertDialogTitle>
<AlertDialogDescription>
We will tell QuantumLeap you are no longer interested in the Staff Software Engineer
role.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Keep application</AlertDialogCancel>
<AlertDialogAction onClick={() => setIsWithdrawn(true)}>Withdraw</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</>
)
}
- Use an alert dialog when something would be lost and the person has to answer before going on: discarding edits, withdrawing, deleting. For anything else, use a dialog.
- It has no close button and does not close when the page behind it is pressed. The only ways out are its two buttons and Escape.
- It opens with focus on the cancel button, the choice that loses nothing, wherever that button sits in the content.
- Both buttons close the dialog. Put the work in the action's
onClick. - Name each button for what it does: "Keep editing" and "Discard changes", not "No" and "Yes".