suiss/ui
  • Docs
  • Components
  • Patterns
  • Interactions
  • Charts
  • Blocks
  • AI
GitHubCreate
DocsComponentsPatternsInteractionsChartsBlocksAICreate
Patterns19
  • Overview
  • Action Bar
  • Action Menu
  • App Shell
  • Command Palette
  • Confirm
  • Context Actions
  • Date Picker
  • Flyout
  • Modal
  • Navbar
  • Paged List
  • Panel
  • Picker
  • Preview
  • Search Picker
  • Segment Picker
  • Split View
  • Table List
  • Trail

suiss/ui ยท Pattern

Modal

A modal window that opens as a centered dialog on desktop and a bottom drawer on mobile.

Modal is an open-source pattern from suiss/ui for React. The @esuiss/ui CLI copies its source into your project, so you own the code.

import {
  Modal,
  ModalBody,
  ModalClose,
  ModalContent,
  ModalDescription,
  ModalFooter,
  ModalHeader,
  ModalTitle,
  ModalTrigger,
} from "@/components/patterns/modal"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"

export default function ModalDemo() {

Modal presents focused content that blocks the rest of the page, such as a form or a share sheet. On desktop it renders a Dialog. Below the 48rem breakpoint it renders a bottom Drawer with a swipe handle, and each part switches to its drawer counterpart.

Use it for tasks that need the user's full attention before returning to the page.

Installation

$ pnpm dlx shadcn@latest add @esuiss/modal

Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:

$ pnpm dlx @esuiss/ui@latest add patterns modal
  1. Add the cn helper.

  2. Add the pieces it builds on: Dialog, Drawer, use-mobile.

  3. Copy and paste the following code into your project.

components/patterns/modal.tsx
"use client"

import * as React from "react"
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"
import {
  Drawer,
  DrawerClose,
  1. Update the import paths to match your project setup.

Usage

import {
  Modal,
  ModalBody,
  ModalClose,
  ModalContent,
  ModalDescription,
  ModalFooter,
  ModalHeader,
  ModalTitle,
  ModalTrigger,
} from "@/components/patterns/modal"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
<div className="flex min-h-svh items-center justify-center">
  <Modal>
    <ModalTrigger render={<Button variant="outline" />}>
      Edit profile
    </ModalTrigger>
    <ModalContent>
      <ModalHeader>
        <ModalTitle>Edit profile</ModalTitle>
        <ModalDescription>
          Update your name and username. Click save when you are done.
        </ModalDescription>
      </ModalHeader>
      <ModalBody className="grid gap-4">
        <div className="grid gap-2">
          <Label htmlFor="modal-name">Name</Label>
          <Input id="modal-name" defaultValue="Ada Lovelace" />
        </div>
        <div className="grid gap-2">
          <Label htmlFor="modal-username">Username</Label>
          <Input id="modal-username" defaultValue="@ada" />
        </div>
      </ModalBody>
      <ModalFooter>
        <ModalClose render={<Button variant="secondary" />}>
          Cancel
        </ModalClose>
        <Button>Save changes</Button>
      </ModalFooter>
    </ModalContent>
  </Modal>
</div>
<div className="flex min-h-svh items-center justify-center">
  <Modal>
    <ModalTrigger asChild>
      <Button variant="outline">Edit profile</Button>
    </ModalTrigger>
    <ModalContent>
      <ModalHeader>
        <ModalTitle>Edit profile</ModalTitle>
        <ModalDescription>
          Update your name and username. Click save when you are done.
        </ModalDescription>
      </ModalHeader>
      <ModalBody className="grid gap-4">
        <div className="grid gap-2">
          <Label htmlFor="modal-name">Name</Label>
          <Input id="modal-name" defaultValue="Ada Lovelace" />
        </div>
        <div className="grid gap-2">
          <Label htmlFor="modal-username">Username</Label>
          <Input id="modal-username" defaultValue="@ada" />
        </div>
      </ModalBody>
      <ModalFooter>
        <ModalClose asChild>
          <Button variant="secondary">Cancel</Button>
        </ModalClose>
        <Button>Save changes</Button>
      </ModalFooter>
    </ModalContent>
  </Modal>
</div>

Composition

Use the following composition to build a Modal:

Modal
โ”œโ”€โ”€ ModalTrigger
โ””โ”€โ”€ ModalContent
    โ”œโ”€โ”€ ModalHeader
    โ”‚   โ”œโ”€โ”€ ModalTitle
    โ”‚   โ””โ”€โ”€ ModalDescription
    โ”œโ”€โ”€ ModalBody
    โ””โ”€โ”€ ModalFooter
        โ””โ”€โ”€ ModalClose

Examples

With close button

ModalContent shows a corner close button by default; here it is the only way to dismiss besides Escape. Set showCloseButton to false to hide it.

import {
  Modal,
  ModalBody,
  ModalContent,
  ModalDescription,
  ModalFooter,
  ModalHeader,
  ModalTitle,
  ModalTrigger,
} from "@/components/patterns/modal"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"

export default function ModalWithCloseButton() {
  return (
    <div className="flex min-h-svh items-center justify-center">

With close label

Set closeLabel to show a text button instead of the close icon.

import {
  Modal,
  ModalBody,
  ModalContent,
  ModalDescription,
  ModalFooter,
  ModalHeader,
  ModalTitle,
  ModalTrigger,
} from "@/components/patterns/modal"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"

export default function ModalWithCloseLabel() {
  return (
    <div className="flex min-h-svh items-center justify-center">

Floating

Set floating to inset the mobile drawer from the screen edges with fully rounded corners.

import {
  Modal,
  ModalBody,
  ModalClose,
  ModalContent,
  ModalDescription,
  ModalFooter,
  ModalHeader,
  ModalTitle,
  ModalTrigger,
} from "@/components/patterns/modal"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"

export default function ModalFloating() {

Accessibility

  • The popup has the dialog role, named by ModalTitle and described by ModalDescription.
  • Escape closes the modal and returns focus to the trigger.
  • The default close button is labelled Close.

API Reference

Modal

The root that renders a Dialog on desktop and a Drawer on mobile.

PropTypeDefault
openboolean-
defaultOpenboolean-
onOpenChange(open: boolean) => void-
floatingboolean-
childrenReact.ReactNode-

The modal can be controlled with open and onOpenChange or uncontrolled with defaultOpen.

ModalTrigger

The button that opens the modal.

PropTypeDefault
classNamestring-

Also accepts the props of React.ComponentProps<typeof DialogTrigger>.

Also accepts className, children, id and aria-label.

Pass render to replace the button with your own element, such as a Button.

Set asChild to use your own element, such as a Button.

ModalContent

The popup window, with a close button by default.

PropTypeDefault
classNamestring-
styleReact.CSSProperties-

Also accepts the props of React.ComponentProps<typeof DialogContent>.

Also accepts showCloseButton, closeLabel, children, id and aria-label.

className and style apply to the dialog or drawer popup. They are plain values only, because the same props are forwarded to either popup.

ModalHeader

Groups the title and description.

Also accepts the props of React.ComponentProps<"div">.

ModalBody

The main content area, padded on mobile.

Also accepts the props of React.ComponentProps<"div">.

ModalFooter

Holds the modal's actions; stacked with the primary action on top on mobile.

Also accepts the props of React.ComponentProps<"div">.

On mobile it stacks its buttons in reverse order so the primary action sits on top.

ModalTitle

The modal's title, used as its accessible name.

Also accepts the props of React.ComponentProps<typeof DialogTitle>.

Also accepts className, children and id.

ModalDescription

Supporting text, used as the modal's accessible description.

Also accepts the props of React.ComponentProps<typeof DialogDescription>.

Also accepts className, children and id.

ModalClose

A button that closes the modal.

Also accepts the props of React.ComponentProps<typeof DialogClose>.

Also accepts className, children, id and aria-label.

Pass render to replace the button with your own element, such as a Button.

Set asChild to use your own element, such as a Button.

Each part sets a data-slot, such as modal-content or modal-close, for styling.

NextNavbarPreviousFlyout

On this page

  1. 01Installation
  2. 02Usage
  3. 03Composition
  4. 04Examples
    • With close button
    • With close label
    • Floating
  5. 05Accessibility
  6. 06API Reference
    • Modal
    • ModalTrigger
    • ModalContent
    • ModalHeader
    • ModalBody
    • ModalFooter
    • ModalTitle
    • ModalDescription
    • ModalClose