Drawer

A panel that slides in from the side for filters or settings, opened from a project list. On a narrow screen it becomes a sheet that rises from the bottom, with a handle to close it.

Projects

  • Q4 launch planMaya OkaforActive
  • Brand refreshJonas WeberActive
  • Help centreMaya OkaforPaused

Filters

Status
Owner

Options

Size
Colour

Code

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

/* A panel that slides in from the right, and from the bottom on a narrow
   screen. Give it open and onClose; children go in the scrolling middle.

   <Drawer open={open} onClose={close} title="Filters" confirm="Show results" onConfirm={apply}>…</Drawer> */
export function Drawer({ open, onClose, title, reset = "Reset", confirm = "Show results", onConfirm, children }) {
  const drawer = useRef(null);
  const id = useId();

  /* Opening is at once. Closing slides away first. */
  useEffect(() => {
    const element = drawer.current;
    if (open) {
      element.removeAttribute("data-closing");
      if (!element.open) element.showModal();
      /* Focus starts on the title, not on the first button. */
      element.querySelector("h2").focus();
      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(new FormData(event.currentTarget));
    onClose();
  };

  return (
    <dialog
      className="dh-drawer"
      ref={drawer}
      aria-labelledby={id}
      onCancel={(event) => {
        event.preventDefault();
        onClose();
      }}
      onClick={(event) => {
        if (event.target === drawer.current) onClose();
      }}
    >
      <form method="dialog" onSubmit={submit}>
        <button className="dh-drawer-grab" type="button" aria-label="Close" onClick={onClose} />
        <header>
          <h2 id={id} tabIndex={-1}>{title}</h2>
          <button className="dh-drawer-x" type="button" aria-label="Close" onClick={onClose}>
            <svg viewBox="0 0 12 12" aria-hidden="true">
              <path d="M3 3l6 6M9 3l-6 6" />
            </svg>
          </button>
        </header>
        <div className="dh-drawer-body">{children}</div>
        <footer>
          <button className="dh-drawer-btn" type="reset">
            {reset}
          </button>
          <button className="dh-drawer-btn" type="submit" data-tone="primary">
            {confirm}
          </button>
        </footer>
      </form>
    </dialog>
  );
}

const projects = [
  { name: "Q4 launch plan", owner: "Maya Okafor", status: "Active", kind: "good" },
  { name: "Brand refresh", owner: "Jonas Weber", status: "Active", kind: "good" },
  { name: "Help centre", owner: "Maya Okafor", status: "Paused", kind: "warn" },
];

export function Example() {
  const [open, setOpen] = useState(false);

  return (
    <div className="dh-drawer-demo">
      <div className="dh-drawer-card">
        <header>
          <h3>Projects</h3>
          <button className="dh-drawer-btn" type="button" onClick={() => setOpen(true)}>
            <svg viewBox="0 0 20 20" aria-hidden="true">
              <path d="M3 5.750A.75.750 0 0 1 3.750 5h12.500a.75.750 0 0 1 0 1.500H3.750A.75.750 0 0 1 3 5.750Zm2.500 4.250a.75.750 0 0 1 .75-.75h7.500a.75.750 0 0 1 0 1.500h-7.500A.75.750 0 0 1 5.500 10Zm2.500 4.250a.75.750 0 0 1 .75-.75h2.500a.75.750 0 0 1 0 1.500h-2.500a.75.750 0 0 1-.75-.75Z" />
            </svg>
            Filters
            <em>2</em>
          </button>
        </header>
        <ul>
          {projects.map((project) => (
            <li key={project.name}>
              <b>{project.name}</b>
              <span>{project.owner}</span>
              <small data-kind={project.kind}>{project.status}</small>
            </li>
          ))}
        </ul>
      </div>

      <Drawer open={open} onClose={() => setOpen(false)} title="Filters">
        <fieldset>
          <legend>Status</legend>
          <label>
            <input type="checkbox" name="status" value="active" defaultChecked /> Active <small>24</small>
          </label>
          <label>
            <input type="checkbox" name="status" value="paused" defaultChecked /> Paused <small>6</small>
          </label>
          <label>
            <input type="checkbox" name="status" value="archived" /> Archived <small>112</small>
          </label>
        </fieldset>
        <fieldset>
          <legend>Owner</legend>
          <label>
            <input type="radio" name="owner" value="anyone" defaultChecked /> Anyone
          </label>
          <label>
            <input type="radio" name="owner" value="maya" /> Maya Okafor
          </label>
          <label>
            <input type="radio" name="owner" value="team" /> My team
          </label>
        </fieldset>
      </Drawer>
    </div>
  );
}

Projects

  • Q4 launch planMaya OkaforActive
  • Brand refreshJonas WeberActive
  • Help centreMaya OkaforPaused

Filters

Status
Owner