Notch toolbar

A dark toolbar joined to the bottom edge by two curved feet, with buttons, their keyboard shortcuts and a search that opens in place, shown over a grainy sunlit scene.

Options

Screen

Code

import { useEffect, useRef, useState } from "react";
import "./notch-toolbar.css";

const icon = { viewBox: "0 0 16 16", fill: "none", stroke: "currentColor", strokeWidth: 1.6, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": true };

const items = [
  { name: "Dashboard", kind: "Page" },
  { name: "Projects", kind: "Page" },
  { name: "Invite a teammate", kind: "Action" },
  { name: "Billing", kind: "Settings" },
  { name: "Keyboard shortcuts", kind: "Help" },
  { name: "Sign out", kind: "Action" },
];

export function NotchToolbar({ results = items, onPick }) {
  const [open, setOpen] = useState(false);
  const [query, setQuery] = useState("");
  const [active, setActive] = useState(0);
  const input = useRef(null);

  const shown = results.filter((item) => item.name.toLowerCase().includes(query.trim().toLowerCase()));

  useEffect(() => {
    if (open) input.current?.focus();
  }, [open]);

  function start() {
    setQuery("");
    setActive(0);
    setOpen(true);
  }

  function keys(event) {
    if (event.key === "/" && event.target !== input.current) {
      event.preventDefault();
      start();
    }
    if (!open) return;
    if (event.key === "Escape") setOpen(false);
    if (event.key === "ArrowDown" || event.key === "ArrowUp") {
      event.preventDefault();
      const step = event.key === "ArrowDown" ? 1 : -1;
      setActive((value) => (shown.length ? (value + step + shown.length) % shown.length : 0));
    }
    if (event.key === "Enter" && shown[active]) onPick?.(shown[active]);
  }

  return (
    <div className="dh-notch-frame" onKeyDown={keys}>
      {open && (
        <div className="dh-notch-results" role="listbox" aria-label="Results" style={{ width: 380 }}>
          {shown.map((item, index) => (
            <a
              key={item.name}
              href="#"
              role="option"
              aria-selected={index === active}
              onPointerMove={() => setActive(index)}
              onClick={() => onPick?.(item)}
            >
              {item.name} <span>{item.kind}</span>
            </a>
          ))}
          {shown.length === 0 && <p>Nothing matches that search.</p>}
        </div>
      )}
      <div className="dh-notch" role="toolbar" aria-label="Quick actions" style={{ width: open ? 380 : undefined }}>
        {open ? (
          <label className="dh-notch-field">
            <svg viewBox="0 0 14 14" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" aria-hidden>
              <circle cx="6" cy="6" r="4.25" />
              <path d="M9.5 9.5 12.5 12.5" />
            </svg>
            <input
              ref={input}
              type="text"
              placeholder="Search"
              aria-label="Search"
              autoComplete="off"
              spellCheck={false}
              value={query}
              onChange={(event) => {
                setQuery(event.target.value);
                setActive(0);
              }}
            />
            <button type="button" aria-label="Close search" onClick={() => setOpen(false)}><kbd>esc</kbd></button>
          </label>
        ) : (
          <div className="dh-notch-row">
            <button type="button" aria-label="Menu">
              <svg {...icon}><path d="M3 4.5h10M3 8h7M3 11.5h4" /></svg>
            </button>
            <button type="button" onClick={start}>Search <kbd>/</kbd></button>
            <i />
            <button type="button" aria-label="Previous">
              <svg {...icon}><path d="M9.5 3.5 5 8l4.5 4.5" /></svg>
            </button>
            <button type="button" aria-label="Next">
              <svg {...icon}><path d="M6.5 3.5 11 8l-4.5 4.5" /></svg>
            </button>
            <i />
            <button type="button">Copy <kbd>C</kbd></button>
          </div>
        )}
      </div>
    </div>
  );
}