Dialog

A modal with a header, content and two actions, plus a small one that asks before deleting, both opened from a project row. It is built on the dialog element, so focus stays inside and Escape closes it, and on a phone it becomes a bottom sheet.

Q4 launch plan12 files · Edited 2 days ago

Rename project

The new name shows up for everyone on your team.

Delete this project?

Q4 launch plan and its 12 files will be gone for good. This cannot be undone.

Options

Size
Colour

Code

import { useEffect, useId, useRef, useState } from "react";
import "./dialog.css";

/* A modal on the dialog element. Give it open and onClose; size="small" and
   danger make the confirmation that asks before deleting.

   <Dialog open={open} onClose={close} title="Rename project" confirm="Save" onConfirm={save}>…</Dialog> */
export function Dialog({
  open,
  onClose,
  title,
  text,
  size,
  danger = false,
  cancel = "Cancel",
  confirm = "Save",
  onConfirm,
  children,
}) {
  const dialog = useRef(null);
  const id = useId();

  /* Opening is at once. Closing plays the way out first. */
  useEffect(() => {
    const element = dialog.current;
    if (open) {
      element.removeAttribute("data-closing");
      if (!element.open) element.showModal();
      return;
    }
    if (!element.open) return;
    element.setAttribute("data-closing", "");
    const timer = setTimeout(() => {
      element.removeAttribute("data-closing");
      element.close();
    }, 200);
    return () => clearTimeout(timer);
  }, [open]);

  const submit = (event) => {
    event.preventDefault();
    if (onConfirm) onConfirm(event);
    onClose();
  };

  return (
    <dialog
      className="dh-dialog"
      ref={dialog}
      data-size={size}
      role={danger ? "alertdialog" : undefined}
      aria-labelledby={id + "-title"}
      aria-describedby={text ? id + "-text" : undefined}
      onCancel={(event) => {
        event.preventDefault();
        onClose();
      }}
      onClick={(event) => {
        if (event.target === dialog.current) onClose();
      }}
    >
      <form method="dialog" onSubmit={submit}>
        <header>
          <h2 id={id + "-title"}>{title}</h2>
          {text && <p id={id + "-text"}>{text}</p>}
        </header>
        {children}
        <footer>
          <button className="dh-dialog-btn" type="button" onClick={onClose}>
            {cancel}
          </button>
          <button className="dh-dialog-btn" type="submit" data-tone={danger ? "danger" : "primary"}>
            {confirm}
          </button>
        </footer>
      </form>
    </dialog>
  );
}

export function Example() {
  const [shown, setShown] = useState(null);
  const close = () => setShown(null);

  return (
    <div className="dh-dialog-demo">
      <div className="dh-dialog-card">
        <i aria-hidden="true" />
        <p>
          <b>Q4 launch plan</b>
          <span>12 files · Edited 2 days ago</span>
        </p>
        <button className="dh-dialog-btn" type="button" onClick={() => setShown("edit")}>
          Rename
        </button>
        <button className="dh-dialog-btn" type="button" data-tone="quiet-danger" onClick={() => setShown("delete")}>
          Delete
        </button>
      </div>

      <Dialog
        open={shown === "edit"}
        onClose={close}
        title="Rename project"
        text="The new name shows up for everyone on your team."
        confirm="Save"
      >
        <label>
          <span>Name</span>
          <input type="text" defaultValue="Q4 launch plan" />
        </label>
      </Dialog>

      <Dialog
        open={shown === "delete"}
        onClose={close}
        size="small"
        danger
        title="Delete this project?"
        text="Q4 launch plan and its 12 files will be gone for good. This cannot be undone."
        confirm="Delete project"
      />
    </div>
  );
}

Q4 launch plan12 files · Edited 2 days ago

Rename project

The new name shows up for everyone on your team.

Delete this project?

Q4 launch plan and its 12 files will be gone for good. This cannot be undone.