Skip to content

Plan bars with a headless fit plan shared by every UI - #106

Merged
agorischek merged 5 commits into
mainfrom
review-f15-headless-fit-plan
Nov 21, 2026
Merged

agorischek merged 5 commits into
mainfrom
review-f15-headless-fit-plan

Conversation

@agorischek

@agorischek agorischek commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Code review, finding 15: responsive toolbar machinery has forked across UI layers.

Evidence

  • Private hosted/host-primer/src/layout.ts:132-196,222-326 keeps its own measurement cache, collapse selection, resize observation, hysteresis and focus recovery.
  • Public packages/host/core/src/ui/fit-plan.ts:35-89,118-156,159-236 has a generic fit-plan engine with region observation, a settled marker and a focus/pointer interaction lock.

The two UIs may legitimately choose different collapse plans and spacing. The trouble is that each one has to work out the hard measurement and interaction lifecycle on its own. Primer restores focus after a remount but has no guard against replanning while a control is pressed. The default UI has that guard but no focus restore.

The user approved this: "playground will never move to Shad Cn that's always gonna do a little bit of its custom thing and so I think that moving our layout logic into a headless shared whatever thing um makes sense".

The module

packages/host/core/src/fit-plan.ts (with fit-plan/choose.ts, fit-plan/fit-width.ts and fit-plan/focus.ts) has no shadcn, no ui/ imports and no styling; it uses React. It is exported from the main @githubnext/projections-host entry:

useFitPlan<T>(bar, plans, options: FitPlanOptions<T>): {plan: T; index: number; width: number}
chooseFitPlan<T>(choice: FitChoice<T>): number   // pure, no DOM
fitWidth(element): number                        // the default measure
toolbarHysteresis                                // 24, the default hysteresis
type FitChoice<T>, FitHysteresis<T>, FitPlanOptions<T>

The shared module owns the lifecycle:

  • needs measured as rendered and before paint, cached by signature until key (the content) changes;
  • a ResizeObserver on the bar and its data-toolbar-region elements, replanning on the next frame;
  • data-fit-settled on the bar once a plan settles;
  • the focus/pointer interaction lock, so there is no replanning between a press and its click;
  • the bounded burst that keeps disagreeing measurements from oscillating;
  • new: focus retention. When a plan remounts the focused data-focus-id control, focus moves to its new form, or else to the first of focusFallbacks[id] that the bar holds.

As a state machine

The lifecycle is an explicit machine, fitPlanLifecycle (fit-plan/lifecycle.ts, built on @githubnext/projections-machine; see docs/guides/state-machines.md), rather than refs, effect timers and setState from timer callbacks. That's what the machines lifecycle check (tools/scripts/check-machines.mjs) asks for: the four patterns moved out of ui/fit-plan.ts would otherwise have been new debt in fit-plan.ts. The baseline can only shrink, so --update won't move debt from one file to another.

  • States: planning (the bar is measured as rendered and other plans are tried), settled (locked while a lock holds the plan), held (focus moved during a press, so the replan waits for the release) and releasing (the replan once focus has finished moving).
  • Parallel fields: hand: 'free' | 'pressing' and frame (a resize waits for the next frame).
  • Activities: the frame after a resize is an activity (requestAnimationFrame, rendered synchronously with flushSync as before), and the replan after focus moves is an after timer (0 ms).
  • What the layout effect sends: it still measures, but now sends what it finds (resized, tried, fitted). data-fit-settled follows the state.
  • What stays with the hook: the measured needs, ceilings and the burst bound.
  • One tightening: a press that comes after focus moved, but before the replan timer fired, now holds the replan until the release too.

The explore test walks every state and checks that no replan is pending while a pointer is pressed, that the frame activity runs exactly while a resize waits, and that every listed handler is reachable. ui/fit-plan.ts's baseline drops from 4 to none, and fit-plan.ts has none.

What stays UI-owned

Plans and their order, and these hooks in FitPlanOptions:

  • measure(element, plan), defaulting to fitWidth(element);
  • hysteresis?: number | ((from, to, element) => number), defaulting to toolbarHysteresis (24). Each UI keeps its own value; Primer's is 32. With from === to, the function sets how far past its ceiling the settled plan may stay.
  • the existing signature, locate, lock and ceiling;
  • focusFallbacks and the data-focus-id markers.

How Primer's rules map onto the options

  • rowNeed (two-row plans with the steps inline must also leave the slider playbackRowShare of row 2). measure returns Math.max(need, rowNeed) for those plans and need for the rest, using Primer's existing requiredWidth. A plan that hasn't been measured is tried first, just as Primer tries a step whose rowNeed is unknown.
  • Sparse first row (a less collapsed one-row step skips the hysteresis while row 1 of a two-row bar is under firstRowShare full). hysteresis: (from, to, element) => from.rows === 2 && to.rows === 1 && sparseFirstRow(element) ? 0 : 32. The bar is passed as rendered, so sparseFirstRow reads it unchanged. The chooser only asks about plans fuller than the settled one, which is exactly Primer's index < current.
  • Focus recovery: focusFallbacks takes Primer's table as it is, and its data-focus-id markers stay.

Default UI

ui/fit-plan.ts now only re-exports from ../fit-plan.ts, and ui/toolbar-layout.ts imports from the shared module. fitWidth, toolbarHysteresis, useFitPlan and FitPlanOptions are still exported from ./ui and ./ui/components as before. Behaviour doesn't change: the default UI and the Inspector title bar mark no data-focus-id controls, so focus retention does nothing for them, and the chooser is the same logic, extracted.

Tests

  • packages/host/core/test/fit-plan.test.mjs (node:test, against ../dist), all on the pure chooseFitPlan:
    • resizing around a collapse threshold: a plan collapses as soon as it stops fitting and expands only with hysteresis to spare; the last plan is the fallback, and an unmeasured plan is tried;
    • the per-transition hysteresis hook, modelled on Primer's sparse-row rule;
    • a growing label replanning, then hysteresis holding when it shrinks back;
    • fitPlanLifecycle: a press holds a replan until its release, a resize waits for its frame, and an explore of every state.
  • packages/host/core/browser/fit-plan.spec.ts (browser, desktop Chromium and WebKit; it now waits for the story to render before sizing the bar, since Storybook renders after the page loads) runs on a new headless harness story, Harness/Default UI/Fit plan (tools/storybook/harness/host/fit-plan.tsx, ?lock for the lock):
    • focus follows a control that a new plan remounts, or moves to its fallback when the control goes into an overflow;
    • a press that moves focus out of the bar (as WebKit does) isn't replanned away before its click, and the bar replans once the pointer is released.

Run locally, rebased on main: tools check --fast passed (including the machines gate), node tools/scripts/check-machines.mjs passed, and the core's node tests passed (139). fit-plan.spec.ts passed on chromium-desktop. WebKit and the full browser suites are left to CI.

Follow-up

  • Private repo, after the pin bump: move hosted/host-primer/src/layout.ts's useToolbarLayout onto useFitPlan from @githubnext/projections-host, keeping toolbarSteps as its plans and its two-row/slider and sparse-row behaviour through the options above:

    • pass a key from content.key and the ladder;
    • pass measure built on requiredWidth, plus rowNeed;
    • pass the hysteresis function with 32;
    • pass focusFallbacks;
    • optionally pass a lock (that also brings the press-to-click guard);
    • delete Primer's own ResizeObserver, chooseToolbarStep loop and focus effect.

    One difference: Primer shifts its known needs on a content change rather than measuring again, but the shared cache measures again when key changes.

  • The public VS Code UI (packages/host/vscode-ui/src/layout/) carries the same Primer-style fork and could move over the same way.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Intentional blur can be undone by stale focus restoration, and the fallback-focus test can pass without exercising restoration because React reuses the button node.

2 open issues
What changed in this PR

Extracts responsive bar planning into the headless host core so different UIs can share measurement and interaction handling while keeping their own layout policies. Existing default UI exports remain compatible; Primer and VS Code adoption are follow-ups.

Changes:

  • Exposes the shared hook, pure plan chooser, width measurement, and configurable layout policies.
  • Adds focus retention for remounted controls and overflow fallbacks.
  • Adds unit tests, browser coverage, a Storybook harness, and API documentation.
File Description
tools/​storybook/​harness/​host/​fit-plan.tsx Adds a plain-control fit-plan harness.
tools/​storybook/​harness/​host.stories.tsx Registers the harness story.
packages/​host/​core/​test/​fit-plan.test.mjs Tests thresholds, hysteresis, and content changes.
packages/​host/​core/​src/​ui/​toolbar-layout.ts Imports the shared core hook.
packages/​host/​core/​src/​ui/​fit-plan.ts Preserves existing UI exports through re-exports.
packages/​host/​core/​src/​ui/​components/​README.md Documents the hook’s new core location.
packages/​host/​core/​src/​index.ts Exports the shared planning API.
packages/​host/​core/​src/​fit-plan/​focus.ts Adds focus tracking and restoration.
packages/​host/​core/​src/​fit-plan/​fit-width.ts Extracts DOM width measurement.
packages/​host/​core/​src/​fit-plan/​choose.ts Extracts pure plan selection with configurable hysteresis.
packages/​host/​core/​src/​fit-plan.ts Centralizes the hook and configurable lifecycle.
packages/​host/​core/​browser/​README.md Describes interaction coverage.
packages/​host/​core/​browser/​fit-plan.spec.ts Tests focus retention and press-to-click behavior.
docs/​guides/​host-core.md Documents planned bars and UI-owned options.

Review effort: Balanced

agorischek and others added 4 commits November 21, 2026 08:17
The default UI's fit-plan lifecycle (the measurement cache, the observed bar
and regions, the settled marker and the press-to-click lock) moves to the
host's headless core as `useFitPlan`, with focus retention for controls marked
`data-focus-id` (`focusFallbacks`), a `measure` hook, a per-transition
`hysteresis` and the pure `chooseFitPlan`. The default UI's names stay
exported from `./ui` and `./ui/components`. Code review, finding 15.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Focus retention restores focus only after a remount: focus that leaves the bar
while its control stays mounted clears the remembered control. The harness keys
Zoom and More, so the overflow fallback is really exercised, and the spec checks
an intentional blur stays put.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Storybook renders a story after the page loads, so querying `.fit-sized` right
after `goto` could find nothing. The locator's `evaluate` waits for it.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
`fitPlanLifecycle` names what `useFitPlan` kept in refs, effect timers and
deferred setState: planning, settled (locked while a lock holds the plan), held
(focus moved during a press, so the replan waits for the release) and releasing
(the replan after focus has finished moving, an `after` timer). The frame after a
resize is an activity, and the settled marker follows the state. The measured
needs stay with the hook. Its explore test checks that no replan comes while a
pointer is pressed, and the lifecycle baseline drops `ui/fit-plan.ts` to none.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@agorischek
agorischek force-pushed the review-f15-headless-fit-plan branch from 3d438ed to b47beb3 Compare November 21, 2026 16:17
@agorischek
agorischek merged commit 6046adc into main Nov 21, 2026
53 checks passed
@agorischek
agorischek deleted the review-f15-headless-fit-plan branch November 21, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants