Dialog
A sheet of paper laid over the page: pencilled round, lifted off a hatched shadow, drawn in as it lands.
import { Button } from "@/components/ui/button";
import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
import { Field, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
export default function DialogDemo() {
return (
<Dialog>
<DialogTrigger render={<Button variant="outline" seed="dialog-open" />}>Edit profile</DialogTrigger>
<DialogContent seed="dialog">
<DialogHeader>
<DialogTitle>Edit profile</DialogTitle>
<DialogDescription>How your name and handle appear on your notes. Save when you're done.</DialogDescription>
</DialogHeader>
<div className="grid gap-4">
<Field>
<FieldLabel htmlFor="dialog-name">Name</FieldLabel>
<Input id="dialog-name" defaultValue="Ada Lovelace" seed="dialog-name" />
</Field>
<Field>
<FieldLabel htmlFor="dialog-handle">Handle</FieldLabel>
<Input id="dialog-handle" defaultValue="@ada" seed="dialog-handle" />
</Field>
</div>
<DialogFooter>
<DialogClose render={<Button variant="outline" seed="dialog-cancel" />}>Cancel</DialogClose>
<DialogClose render={<Button seed="dialog-save" />}>Save changes</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ dialog"use client";
import type { ComponentProps } from "react";
import { Dialog as DialogPrimitive } from "@base-ui/react/dialog";
import { cn } from "@/lib/utils";
import { usePen, type Pen } from "@/hooks/use-ink-box";
import { inkClassName } from "@/lib/ink";
import { InkGlyph } from "@/lib/ink-glyphs";
import { InkPanel } from "@/lib/ink-panel";
import { Button } from "@/components/ui/button";
function Dialog({ ...props }: DialogPrimitive.Root.Props) {
return <DialogPrimitive.Root data-slot="dialog" {...props} />;
}
function DialogTrigger({ ...props }: DialogPrimitive.Trigger.Props) {
return <DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} />;
}
function DialogPortal({ ...props }: DialogPrimitive.Portal.Props) {
return <DialogPrimitive.Portal data-slot="dialog-portal" {...props} />;
}
function DialogClose({ ...props }: DialogPrimitive.Close.Props) {
return <DialogPrimitive.Close data-slot="dialog-close" {...props} />;
}
/** Tracing paper over the page: it dims and softens what's underneath. */
function DialogOverlay({ className, ...props }: DialogPrimitive.Backdrop.Props) {
return (
<DialogPrimitive.Backdrop
data-slot="dialog-overlay"
className={inkClassName(
"fixed inset-0 z-50 bg-paper/65 backdrop-blur-[1.5px] transition-opacity duration-(--dur-state) ease-out",
"data-ending-style:opacity-0 data-ending-style:ease-in data-starting-style:opacity-0",
// iOS: cover the whole visible viewport.
"supports-[-webkit-touch-callout:none]:absolute",
className,
)}
{...props}
/>
);
}
type DialogPen = Omit<Pen, "fill"> & { seed?: string | number };
/**
* A sheet of paper laid over the page: pencilled round, lifted off its
* hatched shadow, and drawn in as it lands. `size` sets how wide it is.
*/
function DialogContent({
className,
children,
showCloseButton = true,
size = "default",
seed,
roughness,
passes,
radius,
corners,
shadow,
draw,
weight,
speed,
...props
}: DialogPrimitive.Popup.Props & DialogPen & { showCloseButton?: boolean; size?: "sm" | "default" | "lg" | "xl" }) {
const pen = usePen({ roughness, passes, radius, corners, shadow, draw, weight, speed });
return (
<DialogPortal>
<DialogOverlay />
<DialogPrimitive.Viewport data-slot="dialog-viewport" className="fixed inset-0 z-50 flex items-center justify-center p-5">
<DialogPrimitive.Popup
data-slot="dialog-content"
data-size={size}
className={inkClassName(
"ink-paper relative isolate grid w-full max-w-md gap-5 data-[size=lg]:max-w-2xl data-[size=sm]:max-w-sm data-[size=xl]:max-w-4xl p-6 text-base text-popover-foreground outline-none",
// It lands: a little low and askew, then settles flat.
"transition-[opacity,translate,rotate] duration-(--dur-enter) ease-out-expo data-starting-style:opacity-0 data-ending-style:opacity-0",
"motion-safe:data-starting-style:translate-y-3 motion-safe:data-starting-style:-rotate-1",
"data-ending-style:duration-(--dur-state) data-ending-style:ease-in motion-safe:data-ending-style:translate-y-1.5",
className,
)}
{...props}
>
<InkPanel pen={pen} seed={seed} estimate={[448, 240]} offset={7} className="text-ink-3" />
{children}
{showCloseButton && (
<DialogPrimitive.Close data-slot="dialog-close" render={<Button variant="ghost" size="icon-sm" className="absolute top-3 right-3" />}>
<InkGlyph name="close" />
<span className="sr-only">Close</span>
</DialogPrimitive.Close>
)}
</DialogPrimitive.Popup>
</DialogPrimitive.Viewport>
</DialogPortal>
);
}
function DialogHeader({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="dialog-header" className={cn("flex flex-col gap-1.5 pr-8", className)} {...props} />;
}
function DialogFooter({ className, showCloseButton = false, children, ...props }: ComponentProps<"div"> & { showCloseButton?: boolean }) {
return (
<div data-slot="dialog-footer" className={cn("flex flex-col-reverse gap-3 pt-1 sm:flex-row sm:justify-end", className)} {...props}>
{children}
{showCloseButton && <DialogPrimitive.Close render={<Button variant="outline" />}>Close</DialogPrimitive.Close>}
</div>
);
}
function DialogTitle({ className, ...props }: DialogPrimitive.Title.Props) {
return <DialogPrimitive.Title data-slot="dialog-title" className={inkClassName("text-xl leading-snug font-bold", className)} {...props} />;
}
function DialogDescription({ className, ...props }: DialogPrimitive.Description.Props) {
return (
<DialogPrimitive.Description
data-slot="dialog-description"
className={inkClassName("text-base text-ink-3 *:[a]:underline *:[a]:underline-offset-3 *:[a]:hover:text-ink", className)}
{...props}
/>
);
}
export { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogOverlay, DialogPortal, DialogTitle, DialogTrigger };Usage
import { Button } from "@/components/ui/button"
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog"
<Dialog>
<DialogTrigger render={<Button variant="outline" />}>Edit profile</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Edit profile</DialogTitle>
<DialogDescription>Save when you're done.</DialogDescription>
</DialogHeader>
<DialogFooter>
<DialogClose render={<Button />}>Save</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>Examples
Controlled
Own the open state to close the dialog from code, here when the form is submitted.
"use client";
import { useState, type FormEvent } from "react";
import { Button } from "@/components/ui/button";
import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
import { Field, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
// Own the open state to close the dialog from your own code, here when the form is submitted.
export default function DialogControlled() {
const [open, setOpen] = useState(false);
const [name, setName] = useState("Untitled");
const [saved, setSaved] = useState<string>();
const submit = (e: FormEvent) => {
e.preventDefault();
setSaved(name);
setOpen(false);
};
return (
<div className="flex flex-col items-center gap-4">
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger render={<Button variant="outline" seed="dc-open" />}>Rename notebook</DialogTrigger>
<DialogContent seed="dc">
<form onSubmit={submit} className="grid gap-5">
<DialogHeader>
<DialogTitle>Rename notebook</DialogTitle>
<DialogDescription>Press Enter to save.</DialogDescription>
</DialogHeader>
<Field>
<FieldLabel htmlFor="dc-name">Name</FieldLabel>
<Input id="dc-name" value={name} onChange={(e) => setName(e.target.value)} seed="dc-name" />
</Field>
<DialogFooter>
<DialogClose render={<Button variant="outline" type="button" seed="dc-cancel" />}>Cancel</DialogClose>
<Button type="submit" seed="dc-save">
Save
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
{saved && <p className="text-ink-2">Saved as "{saved}".</p>}
</div>
);
}Long content
A scrolling region inside a dialog, reachable from the keyboard.
import { Button } from "@/components/ui/button";
import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
const clauses = Array.from({ length: 12 }, (_, i) => `${i + 1}. The pen stays with its owner, and any lines drawn with it are drawn with care.`);
export default function DialogScroll() {
return (
<Dialog>
<DialogTrigger render={<Button variant="outline" seed="dsc-open" />}>Read the terms</DialogTrigger>
<DialogContent seed="dsc">
<DialogHeader>
<DialogTitle>Terms of the pen</DialogTitle>
<DialogDescription>Scroll to the end, then accept.</DialogDescription>
</DialogHeader>
<div tabIndex={0} aria-label="Terms" className="max-h-60 overflow-y-auto pr-2 text-ink-2">
{clauses.map((clause) => (
<p key={clause} className="mb-3">
{clause}
</p>
))}
</div>
<DialogFooter>
<DialogClose render={<Button seed="dsc-accept" />}>I accept</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>
);
}A form
A wider dialog holding two columns of fields and a textarea.
"use client";
import { Button } from "@/components/ui/button";
import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
export default function DialogForm() {
return (
<Dialog>
<DialogTrigger render={<Button seed="df-open" />}>Edit profile</DialogTrigger>
<DialogContent size="lg" seed="df">
<form className="grid gap-5" onSubmit={(event) => event.preventDefault()}>
<DialogHeader>
<DialogTitle>Edit profile</DialogTitle>
<DialogDescription>How you appear on the letters you send.</DialogDescription>
</DialogHeader>
<FieldGroup>
<div className="grid gap-5 sm:grid-cols-2">
<Field>
<FieldLabel htmlFor="df-first">First name</FieldLabel>
<Input id="df-first" defaultValue="Grace" seed="df-first" />
</Field>
<Field>
<FieldLabel htmlFor="df-last">Last name</FieldLabel>
<Input id="df-last" defaultValue="Hopper" seed="df-last" />
</Field>
</div>
<Field>
<FieldLabel htmlFor="df-bio">About you</FieldLabel>
<Textarea id="df-bio" rows={3} defaultValue="Writing since 1952." seed="df-bio" />
</Field>
</FieldGroup>
<DialogFooter>
<DialogClose render={<Button variant="outline" type="button" seed="df-cancel" />}>Cancel</DialogClose>
<DialogClose render={<Button type="submit" seed="df-save" />}>Save changes</DialogClose>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
);
}Share a link
A small dialog with a read-only link and a copy button.
import { Button } from "@/components/ui/button";
import { CopyButton } from "@/components/ui/copy-button";
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
export default function DialogShare() {
return (
<Dialog>
<DialogTrigger render={<Button variant="outline" seed="dsh-open" />}>Share</DialogTrigger>
<DialogContent size="sm" seed="dsh">
<DialogHeader>
<DialogTitle>Share this notebook</DialogTitle>
<DialogDescription>Anyone with the link can read it. Nobody can write in it.</DialogDescription>
</DialogHeader>
<div className="flex items-center gap-2">
<Input readOnly aria-label="Link" defaultValue="https://example.com/n/4fK9q" seed="dsh-link" className="min-w-0 flex-1" />
<CopyButton value="https://example.com/n/4fK9q" variant="outline" size="icon" seed="dsh-copy">
Copy link
</CopyButton>
</div>
<DialogFooter showCloseButton />
</DialogContent>
</Dialog>
);
}Sizes
From a small confirmation to a wide editor.
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
const sizes = ["sm", "default", "lg", "xl"] as const;
export default function DialogSizes() {
return (
<div className="flex flex-wrap items-center justify-center gap-4">
{sizes.map((size) => (
<Dialog key={size}>
<DialogTrigger render={<Button variant="outline" seed={`dz-${size}`} />}>{size}</DialogTrigger>
<DialogContent size={size} seed={`dz-c-${size}`}>
<DialogHeader>
<DialogTitle>size="{size}"</DialogTitle>
<DialogDescription>The dialog is as wide as it needs to be, and never wider than the screen.</DialogDescription>
</DialogHeader>
<DialogFooter showCloseButton />
</DialogContent>
</Dialog>
))}
</div>
);
}API reference
- DialogContent showCloseButtondefault
true booleanA drawn cross in the top-right corner.
- DialogFooter showCloseButtondefault
false booleanAdds an outlined Close button to the footer.
- DialogTrigger render
ReactElementRenders the trigger as your own element, usually a Button.
- DialogContent sizedefault
"default" "sm" | "default" | "lg" | "xl"How wide it is: 24, 28, 42 or 56rem at most, and never wider than the screen.
- seed
string | numberOn DialogContent: pins the drawing.
Every other prop goes to Base UI's Dialog.
Pen settings
The drawing also takes draw, roughness, passes, radius, corners, shadow, weight and speed, as props or from the nearest InkProvider. What each one does.