// --------------------------------------------------------------------------- // sheet/CellContextMenu.tsx — S67-T07 (QI19, R31, 2026-09-28): right-clicking a CELL shows Excel // desktop's cell menu, with its mini toolbar above it. // // ⭐⭐ R31: the labels are Excel's own, Title Case with Excel's ellipses ("Clear Contents", // "Format Cells..."), an exception to DESIGN.md §2 for these menus only. Order and separators // follow Excel desktop: Cut, Copy, Paste Options, Paste Special... | Insert..., Delete..., Clear // Contents | Filter >, Sort > | New Comment, New Note | Format Cells..., Pick From Drop-down // List..., Link. Smart Lookup, Translate, Get Data from Table/Range..., Quick Analysis and Define // Name... have no Loopable backend and are left out (R31). // // ⛔ EVERY ITEM RUNS A DOOR THAT ALREADY EXISTS. This file decides nothing about the document: it // reports `onAct()` and `SheetGrid.tsx` routes each act to the clipboard, structure, style, // sort, filter, note, link or validation door the ribbon already uses. A second write path here // would be two answers to one act. // // ⚠ A read-only sheet DISABLES the edit items rather than hiding them (the done-when's NC), and an // empty clipboard disables Paste; both say why in the item's title. // --------------------------------------------------------------------------- import { useEffect, useLayoutEffect, useRef, useState } from "react"; import type { ComponentProps, KeyboardEvent as ReactKeyboardEvent, ReactNode } from "react"; import { AnchoredOverlay } from "../customer-grid/OverlaySurface"; import { IconAccounting, IconAlignCenter, IconBorderBottom, IconCellFormat, IconClear, IconCommaStyle, IconCopy, IconCut, IconDecimalLess, IconDecimalMore, IconDelete, IconFillColor, IconFontColor, IconFormatPainter, IconGrowFont, IconInsert, IconMenuFilter, IconMenuLink, IconMenuSort, IconMergeCenter, IconNewComment, IconNewNote, IconPaste, IconPasteFormatting, IconPasteFormulas, IconPasteLink, IconPasteSpecial, IconPasteTranspose, IconPasteValues, IconPercentStyle, IconPickList, IconRowGroup, IconShrinkFont, IconRowHeight, IconColumnWidth, IconHideBand, IconUnhideBand, } from "./ribbonIcons"; import "../styles/wave67/cell-menu.css"; /** Every act the menu can report. `SheetGrid.tsx::onCellMenuAct` owns what each one does. */ export type CellMenuAct = | "cut" | "copy" | "paste" | "paste-values" | "paste-formulas" | "paste-formatting" | "paste-special" | "paste-transpose" | "paste-link" | "insert-right" | "insert-down" | "insert-row" | "insert-col" | "delete-left" | "delete-up" | "delete-row" | "delete-col" | "clear-contents" | "filter-reapply" | "filter-clear" | "filter-value" | "sort-asc" | "sort-desc" | "new-comment" | "new-note" | "format-cells" | "pick-list" | "link" | "bold" | "italic" | "center" | "accounting" | "percent" | "comma" | "decimal-more" | "decimal-less" | "grow-font" | "shrink-font" | "format-painter" | "fill" | "font-color" | "borders" | "merge-center" // S67-T08: the row and column header menus' own acts. | "row-height" | "col-width" | "hide-rows" | "hide-cols" | "unhide" | "group-rows" | "ungroup-rows"; /** Excel's font size ladder, which Increase and Decrease Font Size step along. */ export const FONT_SIZE_LADDER = [8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72] as const; export interface CellContextMenuProps { anchor: ComponentProps["anchor"]; /** * S67-T08: which surface was right clicked. A cell gets Excel's cell menu; a row or column * header gets Excel's row or column menu (Insert, Delete, Row Height... or Column Width..., * Hide, Unhide), and the frozen band's headers the same. */ kind?: "cell" | "row" | "column"; /** Row menu only: the sheet can group rows here, and some rows are grouped already. */ canGroup?: boolean; canUngroup?: boolean; /** The workbook is read only for this reader: edit items are shown, disabled. */ readOnly: boolean; /** The browser clipboard is known empty. Unknown or denied reads keep Paste available. */ clipboardEmpty: boolean; /** A sheet filter is on, so Reapply and Clear Filter have something to act on. */ filterActive: boolean; /** The selection's anchor font and size, shown in the mini toolbar. */ fontName: string; fontSize: number; fonts: readonly string[]; onFont: (name: string) => void; onSize: (size: number) => void; onAct: (act: CellMenuAct) => void; onClose: () => void; } type Sub = "filter" | "sort" | null; type Ask = "insert" | "delete" | null; const READ_ONLY_WHY = "You can view this workbook but not change it."; /** The accessible name of each menu, which is also how a person's screen reader tells them apart. */ const MENU_LABEL = { cell: "Cell", row: "Row", column: "Column" } as const; /** The existing in-menu Insert and Delete choices. Paste Special is owned by the grid. */ const ASK_TITLE: Record, string> = { insert: "Insert", delete: "Delete", }; const SIDEWAYS_WHY = "Cells shift down or up in Loopable today; a sideways shift is not there yet."; const ASK_OPTIONS: Record, [string, string, string?][]> = { insert: [["right", "Shift cells right", SIDEWAYS_WHY], ["down", "Shift cells down"], ["row", "Entire row"], ["col", "Entire column"]], delete: [["left", "Shift cells left", SIDEWAYS_WHY], ["up", "Shift cells up"], ["row", "Entire row"], ["col", "Entire column"]], }; export default function CellContextMenu({ anchor, kind = "cell", canGroup = false, canUngroup = false, readOnly, clipboardEmpty, filterActive, fontName, fontSize, fonts, onFont, onSize, onAct, onClose, }: CellContextMenuProps) { const [sub, setSub] = useState(null); const [ask, setAsk] = useState(null); const [shift, setShift] = useState(""); const listRef = useRef(null); const subRef = useRef(null); const hoverTimer = useRef(0); useEffect(() => () => window.clearTimeout(hoverTimer.current), []); const act = (a: CellMenuAct) => { onClose(); onAct(a); }; // Submenu hover intent: open after a short dwell, so a pointer crossing the list does not // flicker every submenu it passes (Interaction layer). const hoverOpen = (next: Sub) => { window.clearTimeout(hoverTimer.current); hoverTimer.current = window.setTimeout(() => setSub(next), next ? 160 : 220); }; // Keyboard: Up and Down walk the enabled items, Right opens a submenu, Left or Escape closes // it, Home and End jump. Escape on the main list is the overlay's own dismiss. const items = (root: HTMLElement | null) => { if (!root) return []; const home = root.classList.contains("sheet-cell-submenu") ? root : null; return [...root.querySelectorAll("[role=menuitem]:not([disabled])")] .filter((el) => el.closest(".sheet-cell-submenu") === home); }; const walk = (root: HTMLElement | null, e: ReactKeyboardEvent) => { const list = items(root); const at = list.indexOf(document.activeElement as HTMLButtonElement); const go = (i: number) => { e.preventDefault(); list[(i + list.length) % list.length]?.focus(); }; if (e.key === "ArrowDown") return go(at + 1); if (e.key === "ArrowUp") return go(at < 0 ? list.length - 1 : at - 1); if (e.key === "Home") return go(0); if (e.key === "End") return go(list.length - 1); }; const onListKey = (e: ReactKeyboardEvent) => { if (ask) return; if (sub && subRef.current?.contains(document.activeElement)) { if (e.key === "ArrowLeft" || e.key === "Escape") { e.preventDefault(); e.stopPropagation(); const owner = listRef.current?.querySelector(`[data-sub="${sub}"]`); setSub(null); owner?.focus(); return; } walk(subRef.current, e); return; } const owner = (document.activeElement as HTMLElement | null)?.dataset?.sub as Sub | undefined; if (owner && (e.key === "ArrowRight" || e.key === "Enter" || e.key === " ")) { e.preventDefault(); setSub(owner); return; } walk(listRef.current, e); }; useLayoutEffect(() => { if (sub) items(subRef.current)[0]?.focus(); }, [sub]); useLayoutEffect(() => { if (!ask) return; setShift(ask === "insert" ? "down" : "up"); }, [ask]); const item = ( label: string, icon: ReactNode, a: CellMenuAct | null, opts: { disabled?: boolean; why?: string; sub?: Sub; shortcut?: string } = {}, ) => ( ); const sep = (key: string) =>
; const edit = { disabled: readOnly, why: READ_ONLY_WHY }; const pasteGate = readOnly ? { disabled: true, why: READ_ONLY_WHY } : clipboardEmpty ? { disabled: true, why: "There is nothing on the clipboard to paste." } : { disabled: false }; const pasteOption = (label: string, icon: ReactNode, a: CellMenuAct | null, why?: string) => ( ); const mini = (label: string, icon: ReactNode, a: CellMenuAct) => ( ); return ( {/* ⭐ EXCEL'S MINI TOOLBAR, ABOVE THE MENU: font, size, grow and shrink, Accounting, Percent, Comma, Format Painter on the first row; Bold, Italic, Center, Borders, Fill, Font Color, the two decimals and Merge and Center on the second. */}
{mini("Increase Font Size", , "grow-font")} {mini("Decrease Font Size", , "shrink-font")} {mini("Accounting Number Format", , "accounting")} {mini("Percent Style", , "percent")} {mini("Comma Style", , "comma")} {mini("Format Painter", , "format-painter")}
{mini("Center", , "center")} {mini("Borders", , "borders")} {mini("Fill Color", , "fill")} {mini("Font Color", , "font-color")} {mini("Increase Decimal", , "decimal-more")} {mini("Decrease Decimal", , "decimal-less")} {mini("Merge and Center", , "merge-center")}
{ask ? (
{ASK_TITLE[ask]}
{ASK_OPTIONS[ask].map(([value, label, why]) => ( ))}
) : kind !== "cell" ? (
{item("Cut", , "cut", edit)} {item("Copy", , "copy")}
Paste Options:
{pasteOption("Paste", , "paste")} {pasteOption("Formulas", , "paste-formulas")} {pasteOption("Values", , "paste-values")} {pasteOption("Formatting", , "paste-formatting")} {pasteOption("Transpose", , "paste-transpose")} {pasteOption("Paste Link", , "paste-link")}
{sep("h1")} {item("Insert", , kind === "row" ? "insert-row" : "insert-col", edit)} {item("Delete", , kind === "row" ? "delete-row" : "delete-col", edit)} {item("Clear Contents", , "clear-contents", edit)} {sep("h2")} {item("Format Cells...", , "format-cells", { ...edit, shortcut: "Ctrl+1" })} {kind === "row" ? item("Row Height...", , "row-height", edit) : item("Column Width...", , "col-width", edit)} {item("Hide", , kind === "row" ? "hide-rows" : "hide-cols", edit)} {item("Unhide", , "unhide", edit)} {kind === "row" && (canGroup || canUngroup) && sep("h3")} {/* ⚠ NOT IN EXCEL'S ROW MENU. Kept because this tree's Data ribbon has no Group control (its Outline group holds Hide and Unhide only), so this is the only mouse door to row grouping; S67-T08's record names it. */} {kind === "row" && canGroup && item("Group rows", , "group-rows", edit)} {kind === "row" && canUngroup && item("Ungroup rows", , "ungroup-rows", edit)}
) : (
window.clearTimeout(hoverTimer.current)}> {item("Cut", , "cut", edit)} {item("Copy", , "copy")}
hoverOpen(null)}> Paste Options:
{pasteOption("Paste", , "paste")} {pasteOption("Formulas", , "paste-formulas")} {pasteOption("Values", , "paste-values")} {pasteOption("Formatting", , "paste-formatting")} {pasteOption("Transpose", , "paste-transpose")} {pasteOption("Paste Link", , "paste-link")}
{sep("s1")} {item("Clear Contents", , "clear-contents", edit)} {sep("s2")}
{item("Filter", , null, { sub: "filter" })} {sub === "filter" && (
window.clearTimeout(hoverTimer.current)}> {item("Reapply", , "filter-reapply", { disabled: !filterActive, why: "No filter is on in this sheet." })} {item("Clear Filter", , "filter-clear", { disabled: !filterActive, why: "No filter is on in this sheet." })} {item("Filter by Selected Cell's Value", , "filter-value")}
)}
{item("Sort", , null, { sub: "sort" })} {sub === "sort" && (
window.clearTimeout(hoverTimer.current)}> {item("Sort A to Z", , "sort-asc", edit)} {item("Sort Z to A", , "sort-desc", edit)}
)}
{sep("s3")} {item("New Comment", , "new-comment", edit)} {item("Add note", , "new-note", edit)} {sep("s4")} {item("Format Cells...", , "format-cells", { ...edit, shortcut: "Ctrl+1" })} {item("Pick From Drop-down List...", , "pick-list", edit)} {item("Link", , "link", edit)}
)}
); }