Skip to content
2k
On This Page

Dialog

A window overlaid on either the primary window or another dialog window, rendering the content underneath inert.

import { Button } from "~/components/ui/button"
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger
} from "~/components/ui/dialog"
import { Input } from "~/components/ui/input"
import { Label } from "~/components/ui/label"

export default function DialogDemo() {
  return (
    <Dialog>
      <form
        onSubmit={(event) => {
          event.preventDefault()
        }}
      >
        <DialogTrigger
          render={(partProps) => <Button {...partProps} variant="outline" />}
        >
          Open Dialog
        </DialogTrigger>
        <DialogContent class="sm:max-w-[425px]">
          <DialogHeader>
            <DialogTitle>Edit profile</DialogTitle>
            <DialogDescription>
              Make changes to your profile here. Click save when you&apos;re
              done.
            </DialogDescription>
          </DialogHeader>
          <div class="grid gap-4">
            <div class="grid gap-3">
              <Label for="dialog-demo-name">Name</Label>
              <Input
                id="dialog-demo-name"
                name="name"
                defaultValue="Pedro Duarte"
              />
            </div>
            <div class="grid gap-3">
              <Label for="dialog-demo-username">Username</Label>
              <Input
                id="dialog-demo-username"
                name="username"
                defaultValue="@peduarte"
              />
            </div>
          </div>
          <DialogFooter>
            <DialogClose
              render={(partProps) => (
                <Button {...partProps} variant="outline" />
              )}
            >
              Cancel
            </DialogClose>
            <Button type="submit">Save changes</Button>
          </DialogFooter>
        </DialogContent>
      </form>
    </Dialog>
  )
}

Installation

pnpm dlx solidui-cli@latest add dialog

Install the following dependencies:

pnpm add @ui-sdk/solid @ui-sdk/core

Install the required Solid UI dependencies:

pnpm dlx solidui-cli@latest add button

Copy and paste the following code into your project.

components/ui/dialog.tsx
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import { type ComponentProps } from "@solidjs/web"
import {
  Dialog as SdkDialog,
  DialogTrigger as SdkDialogTrigger,
  DialogClose as SdkDialogClose,
  DialogBackdrop as SdkDialogBackdrop,
  DialogContent as SdkDialogContent,
  DialogTitle as SdkDialogTitle,
  DialogDescription as SdkDialogDescription
} from "@ui-sdk/solid/dialog"
import { omit, Show, type Component } from "solid-js"

import { cn } from "~/lib/utils"
import { Button } from "~/components/ui/button"
type DialogProps = ComponentProps<typeof SdkDialog>
const Dialog: Component<DialogProps> = (props) => <SdkDialog {...props} />
type DialogTriggerProps = Omit<
  ComponentProps<typeof SdkDialogTrigger>,
  "class"
> & {
  class?: string
}
const DialogTrigger = (props: DialogTriggerProps) => (
  <SdkDialogTrigger {...props} />
)
type DialogCloseProps = Omit<ComponentProps<typeof SdkDialogClose>, "class"> & {
  class?: string
}
const DialogClose = (props: DialogCloseProps) => <SdkDialogClose {...props} />
type DialogOverlayProps = Omit<
  ComponentProps<typeof SdkDialogBackdrop>,
  "class"
> & {
  class?: string
}
const DialogOverlay = (props: DialogOverlayProps) => (
  <SdkDialogBackdrop
    {...props}
    class={cn("bg-black/10 duration-100 data-ending-style:animate-out data-ending-style:fade-out-0 data-ending-style:fill-mode-forwards supports-backdrop-filter:backdrop-blur-xs data-open:animate-in data-open:fade-in-0 fixed inset-0 isolate z-50", props.class)}
  />
)
type DialogContentProps = (Omit<
  ComponentProps<typeof SdkDialogContent>,
  "class"
> & {
  class?: string
}) & {
  showCloseButton?: boolean
}
const DialogContent = (props: DialogContentProps) => {
  const others = omit(props, "children", "showCloseButton")
  return (
    <>
      <DialogOverlay />
      <SdkDialogContent
        {...others}
        class={cn(
          "grid max-w-[calc(100%-2rem)] gap-4 rounded-xl bg-popover p-4 text-sm text-popover-foreground ring-1 ring-foreground/10 duration-100 data-ending-style:animate-out data-ending-style:fade-out-0 data-ending-style:fill-mode-forwards data-ending-style:zoom-out-95 sm:max-w-sm data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 fixed top-1/2 left-1/2 z-50 w-full -translate-x-1/2 -translate-y-1/2 outline-none",
          props.class
        )}
      >
        {props.children}
        <Show when={props.showCloseButton !== false}>
          <SdkDialogClose
            render={(partProps) => (
              <Button {...partProps} variant="ghost" size="icon-sm" />
            )}

            class="absolute top-2 right-2"
          >
            <svg
              width="24"
              height="24"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
            >
              <path d="M18 6 6 18" />
              <path d="m6 6 12 12" />
            </svg>
            <span class="sr-only">Close</span>
          </SdkDialogClose>
        </Show>
      </SdkDialogContent>
    </>
  )
}
type DialogHeaderProps = ComponentProps<"div">
const DialogHeader = (props: DialogHeaderProps) => (
  <div
    data-slot="dialog-header"
    {...props}
    class={cn("gap-2 flex flex-col", props.class)}
  />
)
type DialogFooterProps = (Omit<ComponentProps<"div">, "class"> & {
  class?: string
}) & {
  showCloseButton?: boolean
}
const DialogFooter = (props: DialogFooterProps) => {
  const others = omit(props, "children", "showCloseButton")
  return (
    <div
      data-slot="dialog-footer"
      {...others}
      class={cn(
        "-mx-4 -mb-4 rounded-b-xl border-t bg-muted/50 p-4 flex flex-col-reverse gap-2 sm:flex-row sm:justify-end",
        props.class
      )}
    >
      {props.children}
      <Show when={props.showCloseButton}>
        <SdkDialogClose
          render={(partProps) => <Button {...partProps} variant="outline" />}
        >
          Close
        </SdkDialogClose>
      </Show>
    </div>
  )
}
type DialogTitleProps = Omit<ComponentProps<typeof SdkDialogTitle>, "class"> & {
  class?: string
}
const DialogTitle = (props: DialogTitleProps) => (
  <SdkDialogTitle {...props} class={cn("text-base leading-none font-medium", props.class)} />
)
type DialogDescriptionProps = Omit<
  ComponentProps<typeof SdkDialogDescription>,
  "class"
> & {
  class?: string
}
const DialogDescription = (props: DialogDescriptionProps) => (
  <SdkDialogDescription
    {...props}
    class={cn("text-sm text-muted-foreground *:[a]:underline *:[a]:underline-offset-3 *:[a]:hover:text-foreground", props.class)}
  />
)
export {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogOverlay,
  DialogTitle,
  DialogTrigger,
  type DialogCloseProps,
  type DialogContentProps,
  type DialogDescriptionProps,
  type DialogFooterProps,
  type DialogHeaderProps,
  type DialogOverlayProps,
  type DialogProps,
  type DialogTitleProps,
  type DialogTriggerProps
}

Update the import paths to match your project setup.

Usage

import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogTrigger
} from "~/components/ui/dialog"
<Dialog>
  <DialogTrigger>Open</DialogTrigger>
  <DialogContent>
    <DialogHeader>
      <DialogTitle>Are you absolutely sure?</DialogTitle>
      <DialogDescription>
        This action cannot be undone. This will permanently delete your account
        and remove your data from our servers.
      </DialogDescription>
    </DialogHeader>
  </DialogContent>
</Dialog>

Composition

Use the following composition to build a Dialog:

Dialog
โ”œโ”€โ”€ DialogTrigger
โ””โ”€โ”€ DialogContent
    โ”œโ”€โ”€ DialogHeader
    โ”‚   โ”œโ”€โ”€ DialogTitle
    โ”‚   โ””โ”€โ”€ DialogDescription
    โ””โ”€โ”€ DialogFooter

Custom Close Button

Replace the default close control with your own button.

import { Button } from "~/components/ui/button"
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger
} from "~/components/ui/dialog"
import { Input } from "~/components/ui/input"
import { Label } from "~/components/ui/label"

export default function DialogCloseButton() {
  return (
    <Dialog>
      <DialogTrigger
        render={(partProps) => <Button {...partProps} variant="outline" />}
      >
        Share
      </DialogTrigger>
      <DialogContent class="sm:max-w-md" showCloseButton={false}>
        <DialogHeader>
          <DialogTitle>Share link</DialogTitle>
          <DialogDescription>
            Anyone who has this link will be able to view this.
          </DialogDescription>
        </DialogHeader>
        <div class="flex items-center gap-2">
          <div class="grid flex-1 gap-2">
            <Label class="sr-only" for="dialog-close-button-link">
              Link
            </Label>
            <Input
              id="dialog-close-button-link"
              readonly
              value="https://www.solid-ui.com/docs/"
            />
          </div>
        </div>
        <DialogFooter class="sm:justify-start">
          <DialogClose
            render={(partProps) => <Button {...partProps} />}
            type="button"
          >
            Close
          </DialogClose>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}

No Close Button

Use showCloseButton={false} to hide the close button.

import { Button } from "~/components/ui/button"
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogTrigger
} from "~/components/ui/dialog"

export default function DialogNoCloseButton() {
  return (
    <Dialog>
      <DialogTrigger
        render={(partProps) => <Button {...partProps} variant="outline" />}
      >
        No Close Button
      </DialogTrigger>
      <DialogContent showCloseButton={false}>
        <DialogHeader>
          <DialogTitle>No Close Button</DialogTitle>
          <DialogDescription>
            This dialog doesn&apos;t have a close button in the top-right
            corner.
          </DialogDescription>
        </DialogHeader>
      </DialogContent>
    </Dialog>
  )
}

Keep actions visible while the content scrolls.

import { For } from "solid-js"

import { Button } from "~/components/ui/button"
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger
} from "~/components/ui/dialog"

export default function DialogStickyFooter() {
  return (
    <Dialog>
      <DialogTrigger
        render={(partProps) => <Button {...partProps} variant="outline" />}
      >
        Sticky Footer
      </DialogTrigger>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>Sticky Footer</DialogTitle>
          <DialogDescription>
            This dialog keeps actions visible while the content scrolls.
          </DialogDescription>
        </DialogHeader>
        <div class="-mx-4 max-h-[50vh] overflow-y-auto px-4">
          <For each={Array.from({ length: 10 })}>
            {() => (
              <p class="mb-4 leading-normal">
                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do
                eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
                enim ad minim veniam, quis nostrud exercitation ullamco laboris
                nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
                in reprehenderit in voluptate velit esse cillum dolore eu fugiat
                nulla pariatur. Excepteur sint occaecat cupidatat non proident,
                sunt in culpa qui officia deserunt mollit anim id est laborum.
              </p>
            )}
          </For>
        </div>
        <DialogFooter>
          <DialogClose
            render={(partProps) => <Button {...partProps} variant="outline" />}
          >
            Close
          </DialogClose>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}

Scrollable Content

Long content can scroll while the header stays in view.

import { For } from "solid-js"

import { Button } from "~/components/ui/button"
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogTrigger
} from "~/components/ui/dialog"

export default function DialogScrollableContent() {
  return (
    <Dialog>
      <DialogTrigger
        render={(partProps) => <Button {...partProps} variant="outline" />}
      >
        Scrollable Content
      </DialogTrigger>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>Scrollable Content</DialogTitle>
          <DialogDescription>
            This is a dialog with scrollable content.
          </DialogDescription>
        </DialogHeader>
        <div class="-mx-4 no-scrollbar max-h-[50vh] overflow-y-auto px-4">
          <For each={Array.from({ length: 10 })}>
            {() => (
              <p class="mb-4 leading-normal">
                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do
                eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
                enim ad minim veniam, quis nostrud exercitation ullamco laboris
                nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
                in reprehenderit in voluptate velit esse cillum dolore eu fugiat
                nulla pariatur. Excepteur sint occaecat cupidatat non proident,
                sunt in culpa qui officia deserunt mollit anim id est laborum.
              </p>
            )}
          </For>
        </div>
      </DialogContent>
    </Dialog>
  )
}

API Reference

PartDescription
DialogRoot component that manages open state and focus behavior.
DialogTriggerInteractive element used to open the dialog.
DialogContentOverlay + content container rendered in a portal.
DialogHeaderLayout wrapper for title and description content.
DialogTitleAccessible heading for the dialog content.
DialogDescriptionSupporting text that describes the dialog purpose.
DialogFooterFooter container for actions, usually confirm/cancel buttons.
DialogCloseInteractive element used to close the dialog.

See the UI SDK documentation for more information.