Skip to the content

Capsid menu

components/command-menu/examples.tsx

42 lines and 10 functions in DrDustinEdwards/capsomer, at commit d3966cc on 2026-10-24. This file on GitHub.

Functions

The functions of examples.tsx
FunctionLinesComplexityCoveredCRAPMutation
Live9 to 411No data: no coverage report in this mapNo data: no coverage report in this mapNo data: mutation testing has not run on this repo yet
ran12 to 121No data: no coverage report in this mapNo data: no coverage report in this mapNo data: mutation testing has not run on this repo yet
<anonymous:12>12 to 121No data: no coverage report in this mapNo data: no coverage report in this mapNo data: mutation testing has not run on this repo yet
search17 to 241No data: no coverage report in this mapNo data: no coverage report in this mapNo data: mutation testing has not run on this repo yet
<anonymous:18>18 to 241No data: no coverage report in this mapNo data: no coverage report in this mapNo data: mutation testing has not run on this repo yet
<anonymous:19>19 to 221No data: no coverage report in this mapNo data: no coverage report in this mapNo data: mutation testing has not run on this repo yet
<anonymous:20>20 to 201No data: no coverage report in this mapNo data: no coverage report in this mapNo data: mutation testing has not run on this repo yet
<anonymous:21>21 to 211No data: no coverage report in this mapNo data: no coverage report in this mapNo data: mutation testing has not run on this repo yet
<anonymous:23>23 to 231No data: no coverage report in this mapNo data: no coverage report in this mapNo data: mutation testing has not run on this repo yet
<anonymous:30>30 to 301No data: no coverage report in this mapNo data: no coverage report in this mapNo data: mutation testing has not run on this repo yet

Imports

It imports components/command-menu/command-menu.react.tsx. No module of this repo imports it.

Source

// Mounts the React command menu's live-search specimen: the catalog is searched as the person
// types, through a fake source that answers after 300 ms. Titles are samples.
import { StrictMode, useState } from "react";
import { createRoot } from "react-dom/client";
import { CommandMenu, type Command } from "./command-menu.react.tsx";

const CATALOG = ["The Left Hand of Darkness", "The Dispossessed", "A Wizard of Earthsea", "Middlemarch", "The Remains of the Day", "Piranesi"];

function Live() {
  const [open, setOpen] = useState(true);
  const [said, setSaid] = useState("");
  const ran = (label: string) => () => setSaid(`Ran: ${label}`);
  const commands: Command[] = [
    { id: "go-library", label: "Your library", group: "Go to", run: ran("Your library") },
    { id: "go-diary", label: "Diary", group: "Go to", run: ran("Diary") },
  ];
  const search = (q: string, signal: AbortSignal) =>
    new Promise<Command[]>((resolve, reject) => {
      const t = setTimeout(() => {
        const hits = CATALOG.filter((b) => b.toLowerCase().includes(q.toLowerCase()));
        resolve([{ id: "search-all", label: `Search all results for “${q}”`, group: "Search", run: ran(`Search ${q}`) }, ...hits.map((b) => ({ id: `book-${b}`, label: b, group: "Books", run: ran(b) }))]);
      }, 300);
      signal.addEventListener("abort", () => (clearTimeout(t), reject(signal.reason)));
    });
  return (
    <>
      <main className="specimen-page" tabIndex={-1}>
        <h1>Home</h1>
        <p>
          <button type="button" className="cap-btn" onClick={() => setOpen(true)}>
            Search
          </button>
        </p>
        <p role="status" data-specimen-part="said">
          {said}
        </p>
      </main>
      <CommandMenu open={open} onOpenChange={setOpen} commands={commands} search={search} label="Search books" placeholder="Search books and pages" bindKeys={false} />
    </>
  );
}

for (const el of Array.from(document.querySelectorAll<HTMLElement>("[data-mount]"))) {
  if (el.dataset.mount === "live") createRoot(el).render(<StrictMode><Live /></StrictMode>);
}