Dialog
A modal window over the page. Use it for decisions that need an answer now, short forms, previews, and search. Focus stays inside until it closes, then returns to where it was.
Installation
pnpm dlx shadcn@latest add https://sprout.tulipfarm.ai/r/dialog.jsonCopies Dialog and the components it uses (Button) into your app, installs the packages, and adds the styles. Then import it from @/components/ui/dialog. Needs Tailwind CSS 4 and a components.json from shadcn init.
Install the following dependencies:
pnpm add clsx tailwind-merge @base-ui/react @hugeicons/core-free-icons @hugeicons/reactAdd Sprout base once per app: the
cn()helper and the design tokens.lib/sprout/classes.ts- Design tokens, for your global CSS
Add the components it uses: Button.
Copy and paste the following code into your project.
'use client'; import { Dialog as DialogPrimitive } from '@base-ui/react/dialog'; import { mergeProps } from '@base-ui/react/merge-props'; import { useRender } from '@base-ui/react/use-render'; import { CloseIcon } from '@/lib/sprout/icons/close-icon'; import { cn, stateClassName } from '@/lib/sprout/classes'; import { Button } from '@/components/ui/button'; export const dialogSizeNames = ['default', 'form', 'split', 'fullscreen'] as const; export const dialogPlacementNames = ['center', 'top'] as const; export type DialogSize = (typeof dialogSizeNames)[number]; export type DialogPlacement = (typeof dialogPlacementNames)[number]; export type DialogProps = DialogPrimitive.Root.Props; export function Dialog(props: DialogProps) { return <DialogPrimitive.Root {...props} />; } export type DialogTriggerProps = DialogPrimitive.Trigger.Props; export function DialogTrigger(props: DialogTriggerProps) { return <DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} />; } export type DialogPortalProps = DialogPrimitive.Portal.Props; export function DialogPortal(props: DialogPortalProps) { return <DialogPrimitive.Portal data-slot="dialog-portal" {...props} />; } export type DialogCloseProps = DialogPrimitive.Close.Props; export function DialogClose(props: DialogCloseProps) { return <DialogPrimitive.Close data-slot="dialog-close" {...props} />; } export type DialogOverlayProps = DialogPrimitive.Backdrop.Props; export function DialogOverlay({ className, ...props }: DialogOverlayProps) { return ( <DialogPrimitive.Backdrop data-slot="dialog-overlay" {...props} className={stateClassName<DialogPrimitive.Backdrop.State>('sprout-dialog-overlay', className)} /> ); } export interface DialogContentProps extends DialogPrimitive.Popup.Props { showCloseButton?: boolean; closeLabel?: string; size?: DialogSize; placement?: DialogPlacement; container?: DialogPrimitive.Portal.Props['container']; } export function DialogContent({ className, children, showCloseButton = true, closeLabel = 'Close', size = 'default', placement = 'center', container, ...props }: DialogContentProps) { return ( <DialogPortal container={container}> <DialogOverlay /> <DialogPrimitive.Popup data-slot="dialog-content" data-size={size} data-placement={size === 'fullscreen' ? undefined : placement} {...props} className={stateClassName<DialogPrimitive.Popup.State>( cn('sprout-dialog-content', `sprout-dialog-${size}`), className, )} > {children} {showCloseButton && ( <DialogPrimitive.Close data-slot="dialog-close" className="sprout-dialog-close" aria-label={closeLabel} render={<Button variant="ghost" size="icon-sm" />} > <CloseIcon size="md" aria-hidden="true" /> </DialogPrimitive.Close> )} </DialogPrimitive.Popup> </DialogPortal> ); } type PartSlot = 'dialog-header' | 'dialog-body'; export interface DialogPartState extends Record<string, unknown> { slot: PartSlot | 'dialog-footer'; } export type DialogPartProps = useRender.ComponentProps<'div', DialogPartState>; function part(slot: PartSlot, base: string) { return function DialogPart({ className, render, ref, ...props }: DialogPartProps) { return useRender({ defaultTagName: 'div', ref, render, props: mergeProps<'div'>({ 'data-slot': slot, className: cn(base, className) } as object, props), state: { slot }, }); }; } export const DialogHeader = part('dialog-header', 'sprout-dialog-header'); export const DialogBody = part('dialog-body', 'sprout-dialog-body'); export interface DialogFooterProps extends DialogPartProps { showCloseButton?: boolean; closeLabel?: string; } export function DialogFooter({ className, render, ref, showCloseButton = false, closeLabel = 'Close', children, ...props }: DialogFooterProps) { return useRender({ defaultTagName: 'div', ref, render, props: mergeProps<'div'>( { 'data-slot': 'dialog-footer', className: cn('sprout-dialog-footer', className), children: ( <> {children} {showCloseButton && ( <DialogPrimitive.Close render={<Button variant="outline" />}>{closeLabel}</DialogPrimitive.Close> )} </> ), } as object, props, ), state: { slot: 'dialog-footer' }, }); } export type DialogTitleProps = DialogPrimitive.Title.Props; export function DialogTitle({ className, ...props }: DialogTitleProps) { return ( <DialogPrimitive.Title data-slot="dialog-title" {...props} className={stateClassName<DialogPrimitive.Title.State>('sprout-dialog-title', className)} /> ); } export type DialogDescriptionProps = DialogPrimitive.Description.Props; export function DialogDescription({ className, ...props }: DialogDescriptionProps) { return ( <DialogPrimitive.Description data-slot="dialog-description" {...props} className={stateClassName<DialogPrimitive.Description.State>('sprout-dialog-description', className)} /> ); }And the files it imports:
lib/sprout/icons/close-icon.tsxlib/sprout/icons/create-icon.tsx
Add the styles to your global CSS, after
@import "tailwindcss".@layer components { .sprout-dialog-overlay { z-index: 50; isolation: isolate; background: var(--sprout-overlay); position: fixed; inset: 0; } .sprout-dialog-content { --sprout-dialog-padding: 16px; z-index: 50; box-sizing: border-box; width: 100%; min-width: 0; max-width: calc(100% - 2rem); max-height: calc(100dvh - 2rem); padding: var(--sprout-dialog-padding); border: 1px solid var(--sprout-line); border-radius: calc(var(--sprout-radius-panel) + 4px); background: var(--sprout-surface); color: var(--sprout-text); box-shadow: var(--sprout-shadow-overlay); font-family: inherit; font-size: var(--sprout-text-sm); outline: none; gap: 16px; line-height: 1.25rem; display: grid; position: fixed; top: 50%; left: 50%; translate: -50% -50%; } .sprout-dialog-content[data-placement="top"] { top: var(--sprout-dialog-top, 12dvh); max-height: min(76dvh, calc(100dvh - var(--sprout-dialog-top, 12dvh) - 1rem)); translate: -50%; } .sprout-dialog-default { overscroll-behavior: contain; overflow-wrap: break-word; overflow-y: auto; } .sprout-dialog-form { grid-template-rows: auto minmax(0, 1fr); } .sprout-dialog-split { --sprout-dialog-padding: 0px; overscroll-behavior: contain; width: calc(100% - 2rem); overflow-y: auto; } .sprout-dialog-fullscreen { --sprout-dialog-padding: 0px; width: 100%; max-width: none; height: 100dvh; max-height: none; box-shadow: none; border: 0; border-radius: 0; flex-direction: column; gap: 0; display: flex; inset: 0; overflow: hidden; translate: none; } .sprout-dialog-header { flex-direction: column; gap: 6px; min-width: 0; display: flex; } .sprout-dialog-content:has( > .sprout-dialog-close) > .sprout-dialog-header { padding-inline-end: 28px; } :is(.sprout-dialog-split, .sprout-dialog-fullscreen) > .sprout-dialog-header { padding: 16px 16px 0; } :is(.sprout-dialog-split, .sprout-dialog-fullscreen):has( > .sprout-dialog-close) > .sprout-dialog-header { padding-inline-end: 44px; } .sprout-dialog-title { min-width: 0; font-size: var(--sprout-text-base); letter-spacing: -.005em; overflow-wrap: anywhere; margin: 0; font-weight: 550; line-height: 1.375rem; } .sprout-dialog-description { min-width: 0; color: var(--sprout-muted); font-size: var(--sprout-text-sm); text-wrap: pretty; margin: 0; line-height: 1.25rem; } .sprout-dialog-description a:not([data-slot]) { color: inherit; text-underline-offset: 3px; border-radius: 2px; text-decoration: underline; } .sprout-dialog-description a:not([data-slot]):focus-visible { outline: var(--sprout-focus-width) solid var(--sprout-focus-ring); outline-offset: 0; } .sprout-dialog-body { min-width: 0; min-height: 0; margin-inline: calc(var(--sprout-dialog-padding) * -1); padding: 0 var(--sprout-dialog-padding) 4px; overscroll-behavior: contain; flex-direction: column; gap: 12px; display: flex; overflow-y: auto; } :is(.sprout-dialog-split, .sprout-dialog-fullscreen) > .sprout-dialog-body { padding: 0 16px 16px; } .sprout-dialog-fullscreen > .sprout-dialog-body { flex: 1; } .sprout-dialog-footer { margin: 0 calc(var(--sprout-dialog-padding) * -1) calc(var(--sprout-dialog-padding) * -1); border-top: 1px solid var(--sprout-line); background: color-mix(in oklab, var(--sprout-canvas) 70%, var(--sprout-surface)); border-end-end-radius: inherit; border-end-start-radius: inherit; flex-direction: column-reverse; gap: 8px; padding: 12px 16px; display: flex; } .sprout-dialog-fullscreen > .sprout-dialog-footer { margin-top: auto; } .sprout-dialog-footer > p { color: var(--sprout-muted); font-size: var(--sprout-text-sm); margin: 0; line-height: 1.25rem; } .sprout-dialog-close { top: 8px; position: absolute; inset-inline-end: 8px; } @media (width >= 40rem) { .sprout-dialog-content { max-width: 24rem; } .sprout-dialog-form { max-width: 36rem; } .sprout-dialog-split { max-width: 64rem; } .sprout-dialog-fullscreen { max-width: none; } .sprout-dialog-footer { flex-flow: wrap; justify-content: flex-end; } .sprout-dialog-footer > p { align-self: center; margin-inline-end: auto; } } @media (hover: hover) { .sprout-dialog-description a:not([data-slot]):hover { color: var(--sprout-text); } } @media (prefers-reduced-motion: no-preference) { .sprout-dialog-overlay { transition: opacity var(--sprout-motion-normal) var(--sprout-motion-ease); } .sprout-dialog-overlay[data-starting-style], .sprout-dialog-overlay[data-ending-style] { opacity: 0; } .sprout-dialog-content { transition: opacity var(--sprout-motion-normal) var(--sprout-motion-ease), scale var(--sprout-motion-normal) var(--sprout-motion-ease); } .sprout-dialog-content[data-ending-style], .sprout-dialog-overlay[data-ending-style] { transition-duration: var(--sprout-motion-fast); } .sprout-dialog-content[data-starting-style], .sprout-dialog-content[data-ending-style] { opacity: 0; scale: .96; } .sprout-dialog-fullscreen[data-starting-style], .sprout-dialog-fullscreen[data-ending-style] { scale: 1; } .sprout-dialog-description a:not([data-slot]) { transition: color var(--sprout-motion-fast) ease-out; } } }Import it from
@/components/ui/dialog. Update the paths if your@/alias points elsewhere.
pnpm add @tulipfarm/sprout@import "tailwindcss";
@import "@tulipfarm/sprout/styles.css";The package keeps the source and ships fixes with each release. Import from @tulipfarm/sprout/dialog.
Usage
import { Button } from "@tulipfarm/sprout/button";
import {
Dialog,
DialogBody,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@tulipfarm/sprout/dialog";
<Dialog open={open} onOpenChange={(next) => !pending && setOpen(next)}>
<DialogTrigger render={<Button variant="destructive" size="sm" />}>
Remove member
</DialogTrigger>
<DialogContent finalFocus={() => trigger.current ?? heading.current}>
<DialogHeader>
<DialogTitle>Remove [email protected]?</DialogTitle>
<DialogDescription>
They lose access to this workspace now.
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button variant="outline" disabled={pending} onClick={() => setOpen(false)}>
Cancel
</Button>
<Button variant="destructive" disabled={pending} aria-busy={pending} onClick={remove}>
{pending ? "Saving change…" : "Confirm removal"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>Dialog works controlled with open and onOpenChange, or uncontrolled with defaultOpen. To block Escape and outside clicks while work is saving, ignore the change in onOpenChange. You can also mount the dialog only while it is open.
DialogContent portals an overlay and the popup, and adds a corner close button. Turn it off with showCloseButton={false}. DialogFooter showCloseButton adds an outline Close button after your actions.
Sizes
size is "default" (24rem, scrolls as a whole), "form" (36rem, the header stays put and DialogBody scrolls), "split" (64rem, no padding, for two panes), or "fullscreen" (covers the viewport). Below 40rem every size fills the width minus a 1rem gutter.
Sizes
Each size with the content it is used for. Default fits short forms and confirmations, form scrolls its middle, split holds two panes, and fullscreen covers the viewport.
Confirmations
Pass finalFocus a function so focus goes back to the trigger, or to a section heading with tabIndex={-1} if the row is gone. Errors stay inside the dialog as an Alert, so the person can try again.
Confirmations
Default-size dialogs opened from row actions. Escape and outside clicks are ignored while a change is saving, and focus returns to the trigger or to the section heading if the row disappears.
Members and teams
- Ana Ruiz[email protected] · Owner
- Kofi Mensah[email protected] · Member
- Priya Natarajan[email protected] · Admin
- Field operationsTeam · 6 members
States
Dialogs often open before their data arrives. Show a skeleton with role="status", an error with a retry, and an empty StatePanel when there is nothing to choose. Leave out the description until it has something to say.
Loading, empty, error, and blocked states
Pick a starting state, then open the dialog. The body scrolls between a fixed header and footer, and the footer offers the recovery that fits the state.
Flows
The footer can hold a hint as a direct <p>, a submit button tied to a form with form="…", or a back step. Without a footer, put actions in a right-aligned row at the end of the body. A second dialog can open on top; when it closes, move focus to the control the person needs next.
Form dialogs
Form-size dialogs with a scrolling body and a footer bar. Multi-step flows swap the body and footer in place; a second dialog can open on top and hand focus back.
Danger zone
Search
<DialogContent
data-slot="search-dialog"
placement="top"
showCloseButton={false}
className="flex max-w-2xl flex-col gap-0 p-0"
>
<DialogTitle className="sr-only">Search Meadow</DialogTitle>
…
</DialogContent>
<DialogContent
placement="top"
style={{ "--sprout-dialog-top": "33.333%" }}
className="gap-0 p-0"
showCloseButton={false}
>
<DialogHeader className="sr-only">…</DialogHeader>
…
</DialogContent>Search and command palettes
placement="top" pins the dialog near the top so results grow downward. The corner button is off; search closes from its input row and both titles are screen-reader only.
API
Dialog, DialogTrigger, DialogPortal, DialogClose, DialogOverlay, DialogTitle, and DialogDescription wrap the Base UI dialog parts and take their props, including render. DialogHeader, DialogBody, and DialogFooter render a div and accept render, so a body can be the form.
DialogContent takes size, placement("center" or "top"; top reads --sprout-dialog-top, default 12dvh), showCloseButton, closeLabel, container, and every Base UI popup prop such as initialFocus and finalFocus. It sets data-slot="dialog-content" (overridable), data-size, and data-placement.
Accessibility
The popup has role="dialog" and is named by DialogTitle. Every dialog needs a title, even if it is visually hidden with sr-only. Tab and Shift+Tab stay inside. Escape and outside clicks close it unless you ignore the change. Focus moves to the first focusable element, or to initialFocus.
Opening fades and scales in over 180ms; closing takes 120ms. Both turn off when reduced motion is requested, and fullscreen dialogs only fade.
Theme tokens
Dialogs use --sprout-overlay, --sprout-surface, --sprout-canvas, --sprout-line, --sprout-text, --sprout-muted, --sprout-shadow-overlay, and --sprout-radius-panel. They need no brand color, so they look the same with the neutral and TulipFarm themes in light and dark mode.