|
| 1 | +# Headless composables for an OVERLAY family (Menu) — #2723 |
| 2 | + |
| 3 | +> **Status: overlay archetype validated + core Menu parts converted** (parity-gated, |
| 4 | +> 51 consumer tests green). Began as a spike; graduated to a real conversion. This |
| 5 | +> establishes the overlay contract that the deferred overlay work (**joint with |
| 6 | +> #2724**) should build on. Scope: `MenuRoot`/`MenuItem`/`MenuItemImpl`/ |
| 7 | +> `MenuContentImpl`; `MenuSubTrigger`/`Checkbox`/`Radio`/`Sub` reuse the same builders |
| 8 | +> as follow-ups (see "Scope NOT covered"). |
| 9 | +
|
| 10 | +## TL;DR |
| 11 | + |
| 12 | +The pattern extends — but overlays **bend two axioms** from the Switch/Tabs recipe, |
| 13 | +and the recipe's stated reason for deferring overlays ("overlay logic lives in |
| 14 | +Content-part wrappers") is **wrong by half**. The wrappers are a thin shell; the |
| 15 | +overlay *brain* (typeahead, arrow-nav, the pointer-grace machine, highlight |
| 16 | +ownership) is plain state+handlers and extracts cleanly — **now demonstrated, not |
| 17 | +just argued**: `useMenuContent()` pulls ~215 lines out of `MenuContentImpl.vue` |
| 18 | +(**405 → 191 lines**), leaving the SFC as the four wrappers + positioning props. |
| 19 | +The single thing that couldn't move is `RovingFocusGroup.getItems()` (a template- |
| 20 | +instance ref), injected as one `getItems` option. That reframe is the most useful |
| 21 | +input to #2724. |
| 22 | + |
| 23 | +## What was built |
| 24 | + |
| 25 | +In `packages/core/src/Menu/useMenu.ts`: |
| 26 | + |
| 27 | +1. **`useMenuRoot()`** — the state-only overlay root. Renders no attrs, so **no |
| 28 | + `PartSurface`**; returns `{ open, onOpenChange, onClose, menuContext, menuRootContext }` |
| 29 | + — the two context objects the SFC provides verbatim. `MenuRoot.vue` now composes it. |
| 30 | +2. **`getMenuItemBaseSurface(contentContext, { disabled, currentElement })`** — the |
| 31 | + attr-bearing item render surface (role/tabindex/aria-disabled + the |
| 32 | + pointermove/leave/focus/blur hover-highlight). `MenuItemImpl.vue` composes it. |
| 33 | +3. **`getMenuItemSelectSurface(rootContext, { disabled, currentElement, onSelect, searchRef })`** |
| 34 | + — the select protocol (click/pointerdown/pointerup/keydown + close-on-select). |
| 35 | + `MenuItem.vue` composes it. |
| 36 | +4. **`useMenuContent(options)`** — the overlay **brain**: typeahead, arrow-nav |
| 37 | + (`handleKeyDown`/`onKeydownNavigation`), the pointer-grace/direction machine, and |
| 38 | + `highlightedElement` ownership. Returns the `role=menu` `PartSurface` + the |
| 39 | + `MenuContentContext` value. `MenuContentImpl.vue` composes it, keeping the four |
| 40 | + wrappers + the two mount side-effects. **Extracted ~215 lines (405 → 191).** |
| 41 | + |
| 42 | +**Verification:** `Menu` + `DropdownMenu` + `ContextMenu` + `Menubar` suites (the |
| 43 | +compatibility oracle for these SFCs) **51/51 green**; full suite **2042 green**; |
| 44 | +`type-check` clean; **19** POC unit tests across the four shapes. |
| 45 | + |
| 46 | +## Findings — the questions overlays force |
| 47 | + |
| 48 | +### 1. Overlay-root contract: `{ state, context }`, no surface |
| 49 | +`useMenuRoot()` returns state + the context objects, no `PartSurface`. **The family |
| 50 | +is not surface-free — only the root is.** `DropdownMenuTrigger`/`Content` have real |
| 51 | +surfaces; the surfaces just begin one level below the root. Recipe addition (one line): |
| 52 | + |
| 53 | +> *Overlay roots are state-only (`{ state, context }`, no surface); positioning |
| 54 | +> (`PopperRoot`) and portal stay wrappers, so a standalone overlay `useX()` yields |
| 55 | +> the state machine and the trigger/item surfaces but not placement.* |
| 56 | +
|
| 57 | +The two-context shape (`MenuContext` keyed `['MenuRoot','MenuSub']` + `MenuRootContext`) |
| 58 | +is reused by `MenuSub` — a future `useMenuSub()` returns a `MenuContext`-shaped |
| 59 | +object, so the contract is symmetric. |
| 60 | + |
| 61 | +### 2. HEADLINE: the item builder is a context-SCOPED FACTORY, not a pure builder |
| 62 | +Tabs' per-item builders are **pure, idempotent derivations** of `(context, value)` — |
| 63 | +safe to call many times. `getMenuItemBaseSurface` **cannot** be: it creates |
| 64 | +per-instance state (`isFocused`) and closes over the item's own `currentElement` |
| 65 | +(the highlight identity check + pointer handlers compare against it). So it must be |
| 66 | +called **exactly once per item instance**, and callers pass an element ref they |
| 67 | +populate after mount. |
| 68 | + |
| 69 | +> **The "context-pure builder" axiom does not survive contact with overlays.** |
| 70 | +> Overlay item builders are *context-scoped factories*: `state`/`props` computeds |
| 71 | +> stay pure derivations, but the factory itself instantiates state. If both the |
| 72 | +> standalone `useMenu().getItemSurface` path and the SFC inject-and-call path |
| 73 | +> instantiated it, you'd get two divergent `isFocused` states — so memoize one |
| 74 | +> factory call per instance. |
| 75 | +
|
| 76 | +### 3. The select protocol moves — as a callback channel |
| 77 | +`MenuItem`'s close-on-select is **not** a DOM dispatch — it builds |
| 78 | +`new CustomEvent(ITEM_SELECT, { cancelable: true })` as a cancelable **token**, emits |
| 79 | +it, `await nextTick()`, and closes unless `defaultPrevented`. That whole flow lives |
| 80 | +in the composable behind an **`onSelect: (event) => void`** option; the SFC passes |
| 81 | +`e => emits('select', e)`. `onSelect` stays a callback — it is never a merged DOM |
| 82 | +listener. The async `defaultPrevented`-after-`nextTick` veto survives `mergeProps` |
| 83 | +binding as long as surface handlers come first in the merge. |
| 84 | + |
| 85 | +### 4. base + variant layering (so SubTrigger can reuse) |
| 86 | +The SFC split (`MenuItemImpl` = render/highlight; `MenuItem` = select) maps to two |
| 87 | +builders. **Base** (`getMenuItemBaseSurface`) is what every item variant renders |
| 88 | +through, so `MenuSubTrigger`/`MenuCheckboxItem`/`MenuRadioItem` can reuse it and add |
| 89 | +their own layer. Not layering the base would strand SubTrigger. |
| 90 | + |
| 91 | +### 5. What CANNOT move (stays wrapper/SFC) |
| 92 | +- **`CollectionItem` registration** — vnode-bound provide/inject; the `<CollectionItem>` |
| 93 | + wrapper stays in the SFC. The composable documents it as a required host. |
| 94 | +- **`RovingFocusGroup.getItems()`** — a template-instance-ref call (typeahead |
| 95 | + candidates); a standalone `useMenuContent()` must **accept an injected `getItems`**. |
| 96 | +- **The pointer-grace machine** — CAN move, but only into a **content** composable, |
| 97 | + never the item's. Items already consume it purely via the `onItemEnter`/`onItemLeave`/ |
| 98 | + `onTriggerLeave` closure trio on `MenuContentContext` — that trio IS the contract, |
| 99 | + keep it verbatim. It's pinned near the DOM only by lazy `getBoundingClientRect()`/ |
| 100 | + `dataset.side` reads (SSR-safe as-is). |
| 101 | +- **`highlightedElement` is an `HTMLElement`** — the stale-leave guard, `isHighlighted`, |
| 102 | + and arrow-nav all depend on **element identity**. Do NOT normalize it to an id/index |
| 103 | + in a refactor — that's a behavior rewrite. |
| 104 | +- **Mount-lifecycle composables** (`useBodyScrollLock`, `useFocusGuards`, |
| 105 | + `useIsUsingKeyboard`) — must run inside the mounted content/root, so an overlay |
| 106 | + `useX()` is **not callable outside `setup()`** (unlike Switch/Tabs). Expected for |
| 107 | + overlays; test in a mount harness. |
| 108 | + |
| 109 | +### 6. Two recipe amendments overlays require |
| 110 | +- **Functional `data-*` selectors are exempt from the no-`data-*` rule.** |
| 111 | + `data-reka-menu-content` (submenu-scoping `closest()` check), `data-reka-collection-item`, |
| 112 | + and `[data-disabled]` (read by `useArrowNavigation`'s selector) are *functional |
| 113 | + selectors*, not semantic state — they belong in `props`, not routed through |
| 114 | + `stateToDataAttrs`. Silently dropping one degrades UX with no test failure. |
| 115 | +- **Overlay item builders are context-scoped factories** (finding #2) — the recipe's |
| 116 | + "pure builder" wording needs the factory caveat for overlays. |
| 117 | + |
| 118 | +## The reframe for #2724 (most valuable output) — now PROVEN |
| 119 | + |
| 120 | +The deferral said "overlay logic lives in Content-part component wrappers." Reality |
| 121 | +for Menu: `MenuContentImpl` composes **4** wrappers (FocusScope/DismissableLayer/ |
| 122 | +RovingFocusGroup/PopperContent), but the **majority** of its logic — typeahead, |
| 123 | +`onKeydownNavigation`, `handleKeyDown`, `handleBlur`, the pointer-direction tracker, |
| 124 | +the grace machine (`isPointerMovingToSubmenu`, `pointerGraceIntentRef`, |
| 125 | +`pointerDirRef`, `lastPointerXRef`), and `highlightedElement` ownership — is |
| 126 | +**wrapper-independent**. `useMenuContent()` extracts it: it returns the `role=menu` |
| 127 | +surface and the `MenuContentContext` value, and **`MenuContentImpl.vue` drops from |
| 128 | +405 to 191 lines** — the residue is the 4 wrappers, the positioning props, and the |
| 129 | +two mount side-effects (`useFocusGuards`/`useBodyScrollLock`). |
| 130 | + |
| 131 | +**The wrappers are the shell; the overlay brain is the composable.** The only piece |
| 132 | +that resisted extraction is `RovingFocusGroup.getItems()` (a template-instance ref |
| 133 | +for typeahead candidates), injected as a single `getItems` option — a clean, |
| 134 | +named seam. That is exactly the boundary #2724 should build the overlay contract on. |
| 135 | + |
| 136 | +## Scope NOT covered (recommended next depth) |
| 137 | +- Reuse `getMenuItemBaseSurface` in **`MenuSubTrigger`/`MenuCheckboxItem`/`MenuRadioItem`** |
| 138 | + (proves the base+variant layering end-to-end; SubTrigger adds the open-timer + |
| 139 | + grace-polygon layer). |
| 140 | +- **`useMenuSub()`** — the `MenuContext` symmetry (`MenuSub` re-provides a |
| 141 | + `MenuContext`-shaped object). |
| 142 | +- Fold `useFocusGuards`/`useBodyScrollLock` into `useMenuContent` behind a flag, so a |
| 143 | + standalone consumer opts into the mount side-effects (kept in the SFC here so the |
| 144 | + brain stays side-effect-free). |
| 145 | +- `PartSurface` is imported from `@/shared` (relocated there by #2795). |
| 146 | + |
| 147 | +## Bottom line |
| 148 | +`useX()` works for overlays. It costs two axiom amendments (state-only root; |
| 149 | +context-scoped factories) and one exemption (functional `data-*`). The real prize |
| 150 | +isn't the item surface — it's that the overlay brain in the Content part extracts, |
| 151 | +and this POC **proves it**: `useMenuContent()` takes 405→191 lines out of the SFC |
| 152 | +with one named seam (`getItems`), all 51 consumer tests still green. That is the |
| 153 | +concrete evidence #2724 needs before it rewrites overlay internals — the overlay |
| 154 | +contract can be "state-only root + brain composable + wrappers stay wrappers." |
0 commit comments