Context menu

Right-click for actions. It drops from the pointer like cloth.

Q4 launch4 files
Landing page.fig4.2 MB · edited 2h ago
Brand guidelines.pdf12.8 MB · edited yesterday
Hero shot.png2.1 MB · edited Mon
Icon set.zip860 KB · edited Nov 21

Right-click a file, or focus one and press ⇧ F10

Hero card
Circle

Drag a frame to move it, or right-click it or the canvas

Installation

Terminal
npx shadcn@latest add https://fabrickit.org/r/context-menu.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/context-menu.tsx
"use client";

import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { ContextMenu as ContextMenuEngine } from "@/lib/fabricui/context-menu-engine";
import "./context-menu.css";

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

/** An action. `icon` is the inner markup of a 24×24 stroke icon (Hugeicons' `<path d="…" />`s). */
export type ContextMenuAction = {
  id: string;
  text: string;
  icon?: string;
  /** A shortcut shown at the row's end, like "⌘ D". */
  kbd?: string;
components/fabricui/context-menu.css
/*
 * Context menu — actions for whatever you right-clicked (or long-pressed, or
 * pressed Shift+F10 / the Menu key on). Shared by every page that has one;
 * context-menu.js builds and drives it.
 *
 * It drops from the pointer as cloth (cloth-drop.js, hung from its own top
 * edge) and leaves with a quick, quiet fade. The submenu opens at once,
 * flipping to the left when there's no room on the right.
 *
 * The menu is a shade lighter than the cards it opens over (raised), so it
 * reads as on top without a border or shadow.
 */
.cm:where(.fui-context-menu, .fui-context-menu *) {
  --w: 236px;
  position: fixed;
  left: 0;
lib/fabricui/context-menu-engine.js
import { ClothDrop } from "./cloth-drop";

const esc = s => String(s).replace(/[&<>"]/g, c => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c]);

/*
 * context-menu-engine.js — a menu of actions for whatever was right-clicked (see
 * context-menu.css). Needs cloth-drop.js.
 *
 *   const menu = new ContextMenu({
 *     label: "File",
 *     items: [
 *       { id: "open", text: "Open", icon: "<path …/>", kbd: "⌘ O" },
 *       { id: "share", text: "Share", icon, items: [ …a submenu… ] },
 *       { id: "grid", text: "Show grid", icon, checked: true },   // a toggle
 *       { id: "paste", text: "Paste", icon, disabled: true },
 *       { sep: true },

Usage

Usage
import { ContextMenu } from "@/components/fabricui/context-menu";

const ITEMS = [
  { id: "open", text: "Open", kbd: "⌘ O" },
  { id: "rename", text: "Rename", kbd: "↵" },
  { id: "share", text: "Share", items: [{ id: "link", text: "Copy link" }, { id: "email", text: "Email" }] },
  { sep: true as const },
  { id: "trash", text: "Move to trash", danger: true },
];

export function Files({ files }: { files: string[] }) {
  return (
    <ContextMenu label="File" items={ITEMS} selector=".file" onSelect={(item, el) => run(item.id, el.dataset.name)}>
      <ul>{files.map(f => <li className="file" tabIndex={0} data-name={f} key={f}>{f}</li>)}</ul>
    </ContextMenu>
  );
}

Props

PropTypeDefault
items

The rows. icon is the inner markup of a 24×24 stroke icon (Hugeicons <path>s); kbd a shortcut shown at the end; checked makes it a toggle; items a submenu (one level); sep a line; group a small heading.

({ id, text, icon?, kbd?, danger?, disabled?, checked?, items? } | { sep: true } | { group: string })[]—
children

The area right-clicks open it on: one element, given a ref, with nothing wrapped round it. A long press opens it on touch; Shift+F10 or the Menu key from the keyboard.

ReactElement—
selector

Open only on elements matching this inside the area (".file", "tr"). Leave out to open on the area itself.

string—
ignore

Never open on elements matching this (another menu's targets, so two menus can share an area).

string—
onSelect

Called with the chosen action (a toggle arrives already flipped) and the element it was opened on.

(item, target: HTMLElement) => void—
onOpen

Called as it opens: the last chance to change items with menu.set(id, { … }).

(target, menu) => void—
label

The menu's accessible name.

string"Actions"
menuRef

The running menu, to call set(id, { … }) on or read items from.

Ref—