Components
Dialog
A panel over the page for a short task or a piece of detail, which dims what is behind it.
import { Button } from '@dex/shared/components/button'
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from '@dex/shared/components/dialog'
export function DialogDefault() {
return (
<Dialog>
<DialogTrigger render={<Button variant="outline" />}>Interview details</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>System design interview</DialogTitle>
<DialogDescription>Monday 9 March, 10:00 to 11:00, on a video call.</DialogDescription>
</DialogHeader>
<p className="text-sm">
You will design a payments ledger with two engineers from the platform team. There is
nothing to prepare, and you can use any diagramming tool you like.
</p>
<DialogFooter>
<DialogClose render={<Button variant="outline" />}>Close</DialogClose>
<Button>Add to calendar</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)
}
'use client'
import { type SubmitEvent, useState } from 'react'
import { Button } from '@dex/shared/components/button'
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from '@dex/shared/components/dialog'
import { Field, FieldControl, FieldLabel, FieldMessage } from '@dex/shared/components/field'
import { Input } from '@dex/shared/components/input'
export function DialogWithForm() {
const [isOpen, setIsOpen] = useState(false)
const save = (event: SubmitEvent<HTMLFormElement>) => {
event.preventDefault()
setIsOpen(false)
}
return (
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogTrigger render={<Button variant="outline" />}>Edit preferences</DialogTrigger>
<DialogContent className="sm:max-w-md">
<form onSubmit={save} className="grid gap-4">
<DialogHeader>
<DialogTitle>Your preferences</DialogTitle>
<DialogDescription>Dex uses these to choose which roles to show you.</DialogDescription>
</DialogHeader>
<Field>
<FieldLabel>Minimum salary</FieldLabel>
<FieldControl>
<Input name="salary" defaultValue="£120,000" />
</FieldControl>
</Field>
<Field>
<FieldLabel>Notice period</FieldLabel>
<FieldControl>
<Input name="notice" defaultValue="3 months" />
</FieldControl>
<FieldMessage description="We tell companies this before your first interview." />
</Field>
<DialogFooter>
<DialogClose render={<Button type="button" variant="outline" />}>Cancel</DialogClose>
<Button type="submit">Save changes</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
)
}
import { Button } from '@dex/shared/components/button'
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from '@dex/shared/components/dialog'
export function DialogWithoutCloseButton() {
return (
<Dialog>
<DialogTrigger render={<Button variant="outline" />}>Application sent</DialogTrigger>
<DialogContent showCloseButton={false} className="sm:max-w-sm">
<DialogHeader>
<DialogTitle>Your application is in</DialogTitle>
<DialogDescription>
We have sent your CV to InnoWave. Most companies reply within a week.
</DialogDescription>
</DialogHeader>
<DialogFooter>
<DialogClose render={<Button />}>Done</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>
)
}
'use client'
import { useState } from 'react'
import { Button } from '@dex/shared/components/button'
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
} from '@dex/shared/components/dialog'
export function DialogControlled() {
const [isOpen, setIsOpen] = useState(false)
return (
<>
<Button variant="outline" onClick={() => setIsOpen(true)}>
How Dex scores a role
</Button>
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogContent>
<DialogHeader>
<DialogTitle>How Dex scores a role</DialogTitle>
<DialogDescription>
Dex compares the role with your experience, the salary you asked for and where you
want to work.
</DialogDescription>
</DialogHeader>
</DialogContent>
</Dialog>
</>
)
}
import { Button } from '@dex/shared/components/button'
import { Dialog, DialogTrigger } from '@dex/shared/components/dialog'
import { PageDialogContent } from '@dex/shared/components/page-dialog'
const SECTIONS = [
{ title: 'Account', body: 'Your name, email address and how you sign in.' },
{ title: 'Emails', body: 'Which emails Dex sends you, and how often.' },
{ title: 'Appearance', body: 'Light, dark or the same as your device.' },
]
export function DialogPageDialog() {
return (
<Dialog>
<DialogTrigger render={<Button variant="outline" />}>Settings</DialogTrigger>
<PageDialogContent title="Settings">
<div className="mx-auto grid w-full max-w-3xl gap-8 pt-6">
{SECTIONS.map((section) => (
<section key={section.title} className="grid gap-1">
<h3 className="text-base font-bold">{section.title}</h3>
<p className="text-sm text-muted-foreground">{section.body}</p>
</section>
))}
</div>
</PageDialogContent>
</Dialog>
)
}
- Every dialog has a
DialogTitle. It is what a screen reader announces when the dialog opens. Add aDialogDescriptionwhen one line under the title helps. - A dialog closes on Escape, on a press outside it and from the close button in its corner. If closing would lose something, ask first with an alert dialog.
- Close from inside with
DialogClose, drawn as a button withrender. A form that saves closes the dialog itself once the save has worked, by holdingopenin state. - In the footer the main action is last. On a phone the buttons stack with the main action on top.
- Use the page dialog for a whole screen of content, such as settings, that should return the person to exactly where they were when it closes. It has a title and a close button built in.
- Dialogs sit at
z-[80], so one opened from the full-screen chat on a phone shows above it.