Dialog

A confirmation that pops in with a ripple.

Installation

Terminal
npx shadcn@latest add https://fabrickit.org/r/dialog.json

This copies the component into components/fabricui and its engine into lib/fabricui. It's your code from then on. First time? Add the theme once: see Installation.

Code

components/fabricui/dialog.tsx
"use client";

import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { Dialog as DialogEngine } from "@/lib/fabricui/dialog-engine";
import "./dialog.css";

const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;

/** Why the dialog closed: the `data-close` of the button that closed it, or "dismiss" (Escape, the backdrop). */
export type DialogCloseReason = "cancel" | "confirm" | "dismiss" | (string & {});

export type DialogProps = {
  /** The button that opens it (one element, rendered as it is). */
  trigger: React.ReactElement;
  /** The heading. */
components/fabricui/dialog.css
/*
 * dialog.css — Dialog (dialog.js). Moved out of dialog.html.
 */
/*
 * Dialog. A modal confirmation: the backdrop dims the frame and the panel
 * pops in from the centre (cloth-drop.js pop(): a soft scale-up with a
 * fabric ripple running round its edges only, settling flat). It leaves
 * with a quick, quiet fade.
 */


/* ---------- Backdrop ---------- */
.dlg-backdrop:where(.fui-dialog, .fui-dialog *) {
  position: absolute;
  inset: 0;
  z-index: 20;
lib/fabricui/dialog-engine.js
/*
 * dialog-engine.js — Dialog (dialog.css). Moved out of dialog.html so it can be used anywhere.
 */
import { ClothDrop } from "./cloth-drop";

/**
 * Dialog: modal. Opens from its trigger; focus starts on the safe action
 * and stays inside (Tab wraps); Escape or a backdrop click closes; focus
 * goes back to the trigger. The panel pops in from the centre with fabric
 * edges and fades out quietly.
 */
export class Dialog {
  /**
   * @param {HTMLElement} root  the .dlg element
   * @param {{ trigger: HTMLElement, backdrop: HTMLElement, onClose?: (reason: string) => void }} options
   */

Usage

Usage
import { Button } from "@/components/fabricui/button";
import { Dialog } from "@/components/fabricui/dialog";

export function DeleteProject() {
  return (
    <Dialog
      trigger={<Button variant="primary">Delete project</Button>}
      title="Delete project?"
      description="This permanently deletes the project and its files."
      onClose={reason => reason === "confirm" && deleteProject()}
    >
      <Button variant="ghost" data-close="cancel">Cancel</Button>
      <Button variant="danger" data-close="confirm">Delete project</Button>
    </Dialog>
  );
}

Props

PropTypeDefault
trigger

The button that opens it. One element, rendered as it is.

ReactElement—
title

The heading.

ReactNode—
description

The line under it.

ReactNode—
children

The actions. Mark each with data-close: clicking it closes the dialog with that reason. The one marked data-close="cancel" (required) gets focus when it opens.

ReactNode—
open

Open, if you control it. Leave out to let the trigger open it.

boolean—
onOpenChange

Called when it opens or closes.

(open: boolean) => void—
onClose

Called when it closes: the data-close of the button that closed it, or "dismiss" (Escape, or a click on the backdrop).

(reason: string) => void—
className

Added to the panel.

string—