Skip to content

perf(Switch): measure the track at the first press, not at mount - #12700

Merged
cixzhang merged 3 commits into
mainfrom
fix/switch-measure-on-press
Nov 19, 2026
Merged

cixzhang merged 3 commits into
mainfrom
fix/switch-measure-on-press

Conversation

@cixzhang

@cixzhang cixzhang commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

User impact

A settings page with many switches froze while it rendered. On one app's page of 220 switches, about 2.75 s of a 2.9 s task went to layout at 4× CPU slowdown. Each Switch measured its track when it mounted, which forced one layout per switch.

Now a switch measures its track at the first press. Mounting a page of switches forces no layout. Dragging the thumb, right-to-left layouts, and a press right after mount work as before.

Two small behavior differences:

  • Mouse or pen flip point after a toggle. On main, a mouse or pen drag on a switch that was toggled since it mounted flips at the mount-time circle's distance. At the medium size, after turning it on, that is 24 px of travel where the 20 px circle now sits. Now it flips at the current circle (20 px), so the flip point matches where the thumb is. This is a fix, as the Specification session ruled.
  • Theme-resized thumbs on the first change. On the touch presentation, a value change before any press (a label tap, a key, or an app-driven value) animates the thumb from the previous state's default circle. That circle comes from Switch's own THUMB_SIZE, the same table the size styles read. Default themes animate exactly as on main (16 px to 20 px at the medium size). A theme that resizes the thumb animates its first change from the default size; every later change uses the measured circle.

Problem and solution fit

  1. Why it occurs. useSwitchTouch's layout effect attaches the controller. attach called measureGeometry, which reads track.getBoundingClientRect(), clientLeft and clientTop. That is one synchronous layout per Switch at mount, in both presentations.
  2. Fix. attach and setGeometry only mark the geometry as stale.
    • The controller reads the boxes when a press begins (touchDown), once per press, before the drag uses them.
    • A value change that comes before any measurement reads the boxes once, before the thumb's rebase. So the rebase uses the rendered track, a themed size included. Only the previous circle, which the CSS has already replaced, comes from Switch's THUMB_SIZE (passed as thumbSize), as the rescale's start.
    • There is no ResizeObserver and no public API change.

Expected behavior and authority

  • architecture:react-component-runtime INV18: a track measurement at mount is not an allowed read.
  • INV13: the fix lives once, in the shared touch module (useSwitchTouch).
  • component:Switch's drag, flip and RTL behavior is unchanged.

Smallest restoration

  • packages/core/src/Switch/useSwitchTouch.ts:
    • an isGeometryMeasured flag;
    • attach and setGeometry stop measuring;
    • touchDown measures;
    • setValue starts from the default circle when nothing was measured yet;
    • an internal thumbSize option.
  • packages/core/src/Switch/Switch.tsx: a THUMB_SIZE table that the thumb size styles and useSwitchTouch both read.
  • A [perf] changeset.

Evidence

  • Switch.test.tsx, "mount cost": mounting 200 Switches calls no getBoundingClientRect. It fails on main and passes here.
  • useSwitchTouch.test.ts, "the track geometry":
    • attaching the controller reads no box;
    • each press reads the track once.
      It fails on main and passes here.
  • useSwitchTouch.test.ts, "the first value change before any measurement":
    • with no read at mount, the first change plays the thumb's scale from the 16 px default circle to the 20 px one;
    • the second change starts from the measured circle.
  • "flips a mouse drag after a toggle at the current circle": main gives 24 px, the stale mount-time circle, and this head gives 20 px.
  • "shifts the thumb by the rendered track on the first change": a 36 px themed track rebases by its own travel (−12 px), not the default 40 px track's (−16 px). It is red at the previous head 3ad29a6.
  • The existing Switch touch tests (drag tracking, the flip threshold, the press lift and settle, the colour layer on a value change) pass unchanged. Switch/ plus Layer/touchTriggerTargets: 150 passed.
  • The Switch interaction browser spec (thumb 1:1, flip threshold, RTL) runs in CI.
  • First-toggle frame strip (Chromium touch emulation, 390 × 844, DPR 3, coarse pointer; a tap on the label, never the track; a fresh load per take, 3 takes per case, at this head against its merge base):
Case Result
Core md track (40 × 24) The thumb rests at x 4, w 16 on the first on-frame, then travels and grows to x 18, w 20. No first-frame jump. The head matches main within 0.05 px, frame by frame.
Track sized 36 × 24 before mount Rests at x 4, w 16, then travels to x 14, w 20. No jump. The head matches main.
Track resized to 36 px after mount main jumps 4 px on its first on-frame (x 4 → 0, 3 of 3), because it measured the mount-time track. This head does not jump.

Scope

  • One defect is restored.
  • Separable contradictory or unsettled tagalongs were removed or split.
  • No new public API, visual representation, default, or interaction model is hidden under the bug-fix label.
  • Public text and artifacts contain no internal Meta context.

Testing

  • vitest --project ui packages/core/src/Switch packages/core/src/Layer/touchTriggerTargets.test.tsx: 150 passed.
  • The two new tests fail on main.
  • tsc --noEmit for core: clean. ESLint on the changed files: clean.

Overlap checked: #12589 (Switch thumb resting inset; styles only, not useSwitchTouch), #11450 (defers the touch runtime's loading; touches switchTouchPart.tsx, not the measurement), #12455 (checkbox and switch label gap; styles only)

useSwitchTouch read the track's box (getBoundingClientRect, clientLeft,
clientTop) when it attached, one forced layout per Switch at mount. It now
reads the boxes when a press begins, once per press, and before a value
change that comes first. No ResizeObserver; no API change.
@vercel

vercel Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Nov 19, 2026 12:56am UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 18, 2026
@astracat-bot

astracat-bot Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 19, 2026, 3:22 AM UTC

@cixzhang

Copy link
Copy Markdown
Contributor Author

/approve-spec 760231c

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · 4c0ac27

Storybook · Sandbox · built from this PR's head

Modified components

Component Size LoC Visual · A11y · RTL
Switch · Storybook 109.7 kB (+162 B) · 10.0 kB CSS (0) 1,772 (+39) ✓ · ✓ · ✓

Package size: @astryxdesign/core 7.68 MB packed (+1.5 kB); no new dependencies.

Touched components checked; for the full suites, add ci:run-visual for Visual, ci:run-a11y for Accessibility, ci:run-rtl for RTL.


Generated by PR Enrichment workflow | View full report

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changes requested. The mount-time measurement is gone (the two new tests fail on main and pass here, and Core's typecheck and ESLint on the changed files are clean), but measuring lazily changes two behaviors the pull request does not announce.

  • Under a touch presentation, the first toggle with no press before it (a tap on the label, a key, or an app-driven value) now shows the on thumb at full size in the first frame, a 16 px circle jumping to 20 px, because setValue measures after the new circle has rendered and rescaleThumb then sees no size change (useSwitchTouch.ts:454-469).
  • Desktop behavior change needs an owner decision: on a switch whose value changed since it mounted, a mouse or pen drag now flips at the track width less the current thumb (4 px sooner at the default size) while the changeset says the flip point works as before, so add a test that fails on main for this flip distance and describe the change with mouse before and after results, or get an owner decision (useSwitchTouch.ts:508-510).

Next: keep the previous circle for the first unmeasured value change and cover it with a test, then settle the flip distance as above and update the changeset.

Claims and files checked

Review mode: full; reason: no-earlier-review

Claims:

  • No track measurement at mount: verified (Switch.test.tsx mount test: 200 reads on main, 0 here; useSwitchTouch.ts:279-281).
  • The track is read at the first press instead: verified, and at every press plus a value change before any press (useSwitchTouch.ts:454-456, :508-510).
  • A page of hundreds of switches renders without one forced layout per switch: verified for Switch's touch module by read counts; the page timings were not reproduced.
  • Dragging, the flip point and right-to-left layouts work as before: false for the flip point under a mouse or pen after a value change (second finding); verified for touch drags and right-to-left.
  • Attaching and a track-size change only mark the geometry stale: verified (useSwitchTouch.ts:281, :393).
  • A press reads the boxes once, before the drag uses them: verified (useSwitchTouch.ts:510; one read per press in the new test).
  • A value change before any press reads the boxes first, so the rebase uses the rendered travel: it reads, but the read sees the new circle and drops the size carry-over (first finding).
  • The existing value-change read is unchanged: verified (useSwitchTouch.ts:465-466).
  • No ResizeObserver and no API change: verified (no observer added; the API report lists no changed rows).
  • The layout-read rule forbids a mount-time read: overstated; the rule asks for as few reads as possible, and the change conforms to it.
  • The fix lives once, in the touch module: verified (one controller, no copy).
  • A press right after mount works as before: verified (touchDown measures before the drag reads the geometry).
  • The two new tests fail on main and pass here: verified (red-first run).
  • 147 of 147 Switch and touch-target tests pass: verified (147 passed).
  • The interaction browser spec runs in CI: not verified; CI's browser jobs were skipped on this draft head.
  • Core typecheck and ESLint on the changed files are clean: verified.

Files:

  • .changeset/switch-measure-on-press.md: second finding (the flip-point sentence).
  • packages/core/src/Switch/Switch.test.tsx: clear.
  • packages/core/src/Switch/useSwitchTouch.test.ts: clear.
  • packages/core/src/Switch/useSwitchTouch.ts: first and second findings.

Automated review (maintainer link)

Comment thread .changeset/switch-measure-on-press.md Resolved
Comment on lines +279 to +280
// The boxes are read at the first press, not here: a read at mount forces
// a layout for every switch on the page.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Non-blocking (comment style): this is passive, and a value change before any press also reads. Suggested:

// A read here forces a layout for every switch on the page. A press, or a
// value change before one, reads the boxes.

…rcle

A value change before any press starts from the previous state's default
thumb circle (Switch's THUMB_SIZE, which the size styles also read), then
measures. A mouse or pen drag after a toggle flips at the current circle.
Tests for both; the changeset names the two differences.
@github-actions github-actions Bot added the needs:design-review Affects visuals — Design should review label Nov 18, 2026
@cixzhang

Copy link
Copy Markdown
Contributor Author

/approve-spec 3ad29a6

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changes requested. The mount-time read is gone, the first toggle with no press before it grows the thumb as on main again, and the mouse and pen flip distance after a toggle now has a test that fails on main and a line in the changeset.

  • With a theme that sizes the Switch track, the first toggle with no press before it rebases the thumb by the default 40 × 24 track's travel, so on a touch screen the thumb jumps before it travels (4 px at a 36 px track in Chromium); take the one read before the rebase and build only the rescale's starting circle from THUMB_SIZE (useSwitchTouch.ts:466-475).
  • The description's Testing section says the Switch and touch-target run gives 147 passed, but at this head the same command gives 149 (121 in Switch/ plus 28), so update the count.

CI has not run on this draft head yet.

Claims and files checked

Review mode: incremental; reason: none

Claims:

  • No track read at mount: verified (Switch.test.tsx mount test: 200 reads on main, 0 here; useSwitchTouch.ts:287-289).
  • The track is read at the first press instead: verified (useSwitchTouch.ts:529), plus one read in a value change before any press (:485).
  • A page of hundreds of switches renders without a forced layout per switch: verified for Switch's reads by the mount test; the page timing was not reproduced.
  • Dragging, right-to-left and a press right after mount work as before: verified (touchDown reads before the drag uses the boxes; direction is still read at attach, :290).
  • After a toggle, a mouse or pen drag flips at the current circle, up to 4 px different at the medium size: verified (new test: 24 px on main, 20 px here).
  • A theme that resizes the thumb animates its first change from the default size: verified (the scale starts at 16/18 instead of 12/18 in a probe).
  • About 2.75 s of a 2.9 s task went to layout on a 220-switch page: not reproduced (the author's measurement).
  • Each switch read its track at mount, one layout per switch, in both presentations: verified (main's attach).
  • Default themes animate the first change exactly as on main, and later changes use the measured circle: verified (Chromium per-frame thumb boxes match; the new test's second change).
  • attach and setGeometry only mark the geometry stale: verified (:287-289, :405-409).
  • A press reads the boxes once, before the drag uses them: verified (:527-536; one read per press in the new test).
  • A change before any read starts from the default circle, then uses the existing read: verified (:466-488); its travel also comes from the default track (first finding).
  • No ResizeObserver and no public API change: verified (no observer; the API generator lists 0 changed rows).
  • The layout-read rule covers dropping the mount read: verified (it asks that layout be read as little as possible).
  • The fix lives once, in the touch module: verified (one copy, in useSwitchTouch.ts, Switch's own module on the shared gesture and animator modules).
  • The record's drag and right-to-left behavior is unchanged: verified; its flip rule is restored after a toggle.
  • THUMB_SIZE feeds both the size styles and the controller: verified (Switch.tsx:86-93, :154-195, :999; the built stylesheet is byte-identical).
  • The mount and track-geometry tests fail on main and pass here: verified (red-first run).
  • The first-change test plays the scale from 16 to 20 px, then uses the measured circle: verified (passes here).
  • The existing Switch tests pass, 121 of 121 in Switch/: verified.
  • The Switch interaction browser spec runs in CI: not run at this head (CI skipped on the draft).
  • The Switch and touch-target run gives 147 passed: false, it gives 149 (second finding).
  • Core typecheck and ESLint on the changed files are clean: verified (typecheck exit 0; ESLint 0 errors).
  • The three overlapping pull requests do not touch the measurement: verified (none changes useSwitchTouch.ts).
  • One defect, no hidden API, default or interaction change, no internal context: verified, apart from the Switch.doc.mjs re-quote (non-blocking).

Files:

  • .changeset/switch-measure-on-press.md: clear; it does not list the first finding's case.
  • packages/core/src/Switch/Switch.doc.mjs: non-blocking (formatting only).
  • packages/core/src/Switch/Switch.test.tsx: clear.
  • packages/core/src/Switch/Switch.tsx: clear.
  • packages/core/src/Switch/useSwitchTouch.test.ts: clear; its first-change test mocks only a 40 × 24 track.
  • packages/core/src/Switch/useSwitchTouch.ts: first finding; non-blocking comment wording.

Automated review (maintainer link)

…dered track

The one read before the rebase gives the rendered track; only the rescale's
starting circle comes from THUMB_SIZE. A themed track no longer makes the
thumb jump before it travels. Test red at the previous head.
@cixzhang

Copy link
Copy Markdown
Contributor Author

/approve-spec 4c0ac27

@astracat-bot astracat-bot Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approved. Every check at this revision has now finished: 51 passed and none failed, including both Switch interaction legs and the visual-regression run, whose two Switch shots are unchanged.

Nothing is blocking. Two non-blocking notes stand from the previous review:

  • The comment at useSwitchTouch.ts lines 287-288 is passive and leaves out the read a value change before any press makes; the suggested wording is on that line, and the field doc at line 216 reads better as "since the last attach or size change".
  • Switch.doc.mjs only re-quotes three labelIcon strings; name that in the description or drop it.
Claims and files checked

Review mode: same-head; reason: none

changed rules (0): none

Claims:

  • A settings page with many switches froze while rendering, most of a long task in layout: not reproduced (the author's measurement); the per-switch read it describes is verified.
  • Each switch measured its track when it mounted, one forced layout per switch, in both presentations: verified (main's attach reads the boxes, then writes styles before the next switch reads).
  • A switch now measures at the first press, and mounting a page of switches forces no layout: verified (mount test: 200 reads on main, 0 here).
  • Dragging, right-to-left layouts and a press right after mount work as before: verified (touchDown reads before the drag uses the boxes; direction is still read at attach).
  • After a toggle, a mouse or pen drag flips at the current circle (20 px instead of 24 px at the medium size): verified (new test: 24 px on main, 20 px here).
  • That flip change is a fix the spec owners ruled on: consistent with the engineering owner's spec approval for this revision and the record's flip rule.
  • On the touch presentation, a value change before any press animates from the previous state's default circle, from the same table the size styles read: verified (useSwitchTouch.ts:470-478, Switch.tsx:86-93, :154-195).
  • Default themes animate exactly as on main (16 px to 20 px at the medium size): verified (Chromium per-frame thumb boxes match on both trees).
  • A theme that resizes the thumb animates its first change from the default size, and later changes use the measured circle: verified (code path; the second-change test).
  • attach and setGeometry only mark the geometry stale: verified (useSwitchTouch.ts:287-289, :405-409).
  • The controller reads the boxes when a press begins, once per press, before the drag uses them: verified (:535-537; one read per press in the new test).
  • A value change before any measurement reads once, before the rebase, so the rebase uses the rendered track, a themed size included: verified (:470-484; new test; Chromium at a 36 px track).
  • Only the previous circle comes from the size table, as the rescale's start: verified (:473-478, :488-496).
  • No ResizeObserver and no public API change: verified (no observer; the API generator lists 0 changed rows, and the repository's own API check at this revision agrees for all eleven published packages).
  • The layout-read rule does not allow this mount read: verified (it asks that layout be read as little as possible and fails a read interleaved with writes, which each mount read was).
  • The fix lives once, in the touch module: verified (one copy, in Switch's own module built on the shared gesture and animator modules).
  • The Switch record's drag, flip and right-to-left behaviour is unchanged: verified; its flip rule now also holds after a toggle.
  • The smallest-restoration list (the flag, attach and setGeometry, touchDown, setValue, the thumbSize option, the size table, the changeset): verified, plus the Switch.doc.mjs re-quote it does not list.
  • The mount-cost test fails on main and passes here: verified.
  • The track-geometry test (no read at attach, one per press) fails on main and passes here: verified.
  • The first-change test plays the scale from the 16 px to the 20 px circle, then uses the measured circle: verified (passes here, fails on main).
  • The flip test gives 24 px on main and 20 px here: verified.
  • The themed-track test rebases by −12 px, not −16 px, and is red at the previous revision: verified (−16 px there).
  • The existing Switch touch tests pass unchanged, and the Switch and touch-target run gives 150 passed: verified (150 passed).
  • The Switch interaction browser spec runs in CI: verified at this revision (both interaction legs passed).
  • Core typecheck and ESLint on the changed files are clean: verified (typecheck exit 0; ESLint 0 errors, one unused-import warning that main already has; the repository's typecheck and lint checks also passed at this revision).
  • The three overlapping pull requests do not touch the measurement: verified (none changes useSwitchTouch.ts).
  • One defect, no hidden API, default or interaction change, no internal context: verified, apart from the Switch.doc.mjs re-quote (non-blocking).
  • Changeset: no measurement at mount, the track read at the first press: verified (a value change before any press also reads once).
  • Changeset: hundreds of switches render without one forced layout per switch: verified for Switch's own reads.
  • Changeset: dragging, right-to-left and a press right after mount work as before: verified.
  • Changeset: a mouse or pen drag after a toggle flips at the thumb's current size, up to 4 px different at the medium size: verified.
  • Changeset: a theme that resizes the thumb animates its first value change from the default size: verified.

Files:

  • .changeset/switch-measure-on-press.md: clear.
  • packages/core/src/Switch/Switch.doc.mjs: non-blocking (formatting only).
  • packages/core/src/Switch/Switch.test.tsx: clear.
  • packages/core/src/Switch/Switch.tsx: clear (the size table feeds both the styles and the controller; the built stylesheet is byte-identical).
  • packages/core/src/Switch/useSwitchTouch.test.ts: clear.
  • packages/core/src/Switch/useSwitchTouch.ts: non-blocking comment wording.

Automated review (maintainer link)

@cixzhang
cixzhang marked this pull request as ready for review November 19, 2026 02:55
@cixzhang
cixzhang merged commit 5c887ba into main Nov 19, 2026
150 checks passed
@github-actions
github-actions Bot deleted the fix/switch-measure-on-press branch November 19, 2026 06:04

This branch was successfully deployed

1 active deployment
Preview — 4c0ac27e Deployed Nov 19, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. needs:design-review Affects visuals — Design should review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant