Dialog

A modal window that holds focus until it closes.

The dialog opens in the browser’s top layer, so it needs no portal. DialogHeader and DialogFooter are layout helpers; the title and description name and describe it for screen readers.

Preview
basic.tsx
import { Button } from "~/components/ui/button"
import {
  Dialog,
  DialogBackdrop,
  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 Example() {
  return (
    <Dialog>
      <DialogTrigger render={<Button variant="outline" />}>Rename project</DialogTrigger>
      <DialogBackdrop />
      <DialogContent>
        <DialogHeader>
          <DialogTitle>Rename project</DialogTitle>
          <DialogDescription>The new name shows everywhere the project does.</DialogDescription>
        </DialogHeader>
        <Field>
          <FieldLabel>Name</FieldLabel>
          <Input defaultValue="Roadmap 2027" />
        </Field>
        <DialogFooter>
          <DialogClose render={<Button variant="outline" />}>Cancel</DialogClose>
          <DialogClose render={<Button />}>Save</DialogClose>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}

Dependencies

What it's built from and what's built from it in Mocha, read from the imports in its source.

UsesScroll AreaprimitiveTextprimitive
Used byNothing yet.
Packages @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20

Installation

Set the project up for Mocha once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/mocha/dialog.json

Manually

Install its packages:

terminal
npm install @ui-sdk/core@0.1.0-beta.20 @ui-sdk/react@0.1.0-beta.20

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/ui/dialog.tsx
import {
  Dialog,
  DialogBackdrop as DialogBackdropPrimitive,
  DialogClose,
  DialogContent as DialogContentPrimitive,
  DialogDescription as DialogDescriptionPrimitive,
  DialogTitle as DialogTitlePrimitive,
  DialogTrigger,
} from "@ui-sdk/react/dialog"
import type * as React from "react"

import { ScrollArea } from "~/components/ui/scroll-area"
import { textVariants } from "~/components/ui/text"
import { cn, cnState } from "~/lib/utils"

/** Dims the page behind an open dialog. */
function DialogBackdrop({
  className,
  ...props
}: React.ComponentProps<typeof DialogBackdropPrimitive>) {
  return (
    <DialogBackdropPrimitive
      className={cnState(
        "fixed inset-0 bg-backdrop transition-opacity duration-150 data-ending-style:opacity-0 data-starting-style:opacity-0 motion-reduce:transition-none",
        className,
      )}
      {...props}
    />
  )
}

/** The dialog itself. Give it a `DialogTitle`, and a `DialogClose` unless it has its own way out. */
function DialogContent({
  className,
  children,
  ...props
}: React.ComponentProps<typeof DialogContentPrimitive>) {
  return (
    <DialogContentPrimitive
      className={cnState(
        [
          "fixed top-1/2 left-1/2 m-0 flex max-h-[calc(100dvh-2rem)] w-[calc(100%-2rem)] max-w-lg -translate-x-1/2 -translate-y-1/2 flex-col overflow-hidden rounded-lg border border-border bg-overlay text-overlay-foreground shadow-overlay",
          "origin-(--transform-origin) transition-[opacity,scale] duration-150 ease-out data-starting-style:scale-95 data-starting-style:opacity-0 data-ending-style:scale-95 data-ending-style:opacity-0 motion-reduce:transition-none",
        ].join(" "),
        className,
      )}
      {...props}
    >
      {(state) => (
        // The dialog never outgrows the screen: its content scrolls inside it.
        <ScrollArea className="min-h-0">
          <div className="grid gap-lg p-xl">
            {typeof children === "function" ? children(state) : children}
          </div>
        </ScrollArea>
      )}
    </DialogContentPrimitive>
  )
}

/** Groups the title and description at the top of a dialog. */
function DialogHeader({ className, ...props }: React.ComponentProps<"div">) {
  return <div data-slot="dialog-header" className={cn("grid gap-xs", className)} {...props} />
}

/** Holds the dialog's actions, last one first on small screens. */
function DialogFooter({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="dialog-footer"
      className={cn("flex flex-col-reverse gap-sm sm:flex-row sm:justify-end", className)}
      {...props}
    />
  )
}

/** Names the dialog. */
function DialogTitle({ className, ...props }: React.ComponentProps<typeof DialogTitlePrimitive>) {
  return (
    <DialogTitlePrimitive
      className={cnState(
        textVariants({ size: "lg", weight: "semibold", family: "heading", leading: "tight" }),
        className,
      )}
      {...props}
    />
  )
}

/** Describes the dialog, read out when it opens. */
function DialogDescription({
  className,
  ...props
}: React.ComponentProps<typeof DialogDescriptionPrimitive>) {
  return (
    <DialogDescriptionPrimitive
      className={cnState([textVariants({ size: "sm", tone: "muted" })], className)}
      {...props}
    />
  )
}

export {
  Dialog,
  DialogBackdrop,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
}

API reference

Generated from Mocha's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

DialogBackdrop

Dims the page behind an open dialog.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: DialogBackdropState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: DialogBackdropState) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<DialogBackdropState>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: DialogBackdropState) => CSSProperties)

DialogContent

The dialog itself. Give it a DialogTitle, and a DialogClose unless it has its own way out.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: DialogContentState<unknown>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: DialogContentState<unknown>) => string)

container

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type Element | DocumentFragment | null

finalFocus

Where focus goes when the dialog closes while focus is inside it.

Type PopupFocusTarget · Default "trigger"

initialFocus

Where focus goes when the dialog opens. Opening with touch focuses the dialog itself, so no on-screen keyboard appears, unless this is set to something other than "auto".

Type DialogInitialFocus · Default "auto"

inline

Renders the dialog and its backdrop where they are in the page instead of above it, so it can stay inside a frame, like an app preview. Pair it with modal="trap-focus" to keep the rest of the page usable. A container on the dialog still moves its backdrop, so leave it off.

Type boolean · Default false

keepMounted

Whether the dialog stays in the page, hidden, while closed. Its content keeps its state between opens.

Type boolean · Default false

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<DialogContentState<unknown>>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: DialogContentState<unknown>) => CSSProperties)

DialogDescription

Describes the dialog, read out when it opens.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: {}) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: {}) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<{}>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: {}) => CSSProperties)

DialogFooter

Holds the dialog's actions, last one first on small screens.

Takes the props of a native div.

DialogHeader

Groups the title and description at the top of a dialog.

Takes the props of a native div.

DialogTitle

Names the dialog.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: {}) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: {}) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<{}>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: {}) => CSSProperties)

DialogTrigger

The button that opens the dialog. A trigger outside the dialog, like one per table row, connects through controller.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: DialogTriggerState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: DialogTriggerState) => string)

controller

Connects a trigger outside the dialog, like one per table row, to the dialog with this controller.

Type PopupController<any, DialogOpenChangeReason, {}, any, DialogPayloadChangeReason>

disabled

Stops presses. The button is natively disabled, so it leaves the tab order and its form doesn't submit through it, unless focusableWhenDisabled is set.

Type boolean · Default false

focusableWhenDisabled

Keeps a disabled trigger in the tab order with aria-disabled instead of the native attribute, so it can host a tooltip that says why. Presses still do nothing.

Type boolean · Default false

payload

Data for the content to render when this trigger opens the dialog, like the row it belongs to.

Type unknown

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<DialogTriggerState>

shortcut

A keyboard shortcut that presses the trigger, like "mod+k". mod is Command on Apple platforms and Control elsewhere. Sets aria-keyshortcuts, and a Shortcut part inside shows it. A modal dialog pauses the page's shortcuts while it's open.

Type string

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: DialogTriggerState) => CSSProperties)