Repository navigation
perf(Switch): measure the track at the first press, not at mount - #12700
Conversation
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.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
|
/approve-spec 760231c |
PR Analysis Report🔎 Preview ·
|
| 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
There was a problem hiding this comment.
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, becausesetValuemeasures after the new circle has rendered andrescaleThumbthen 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
mainfor 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.tsxmount test: 200 reads onmain, 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 (
touchDownmeasures before the drag reads the geometry). - The two new tests fail on
mainand 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.
| // The boxes are read at the first press, not here: a read at mount forces | ||
| // a layout for every switch on the page. |
There was a problem hiding this comment.
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.
|
/approve-spec 3ad29a6 |
There was a problem hiding this comment.
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.tsxmount test: 200 reads onmain, 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 (
touchDownreads 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'sattach). - 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). attachandsetGeometryonly 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_SIZEfeeds 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
mainand 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.mjsre-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.
…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.
|
/approve-spec 4c0ac27 |
There was a problem hiding this comment.
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.tslines 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.mjsonly re-quotes threelabelIconstrings; 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'sattachreads 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 (
touchDownreads 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).
attachandsetGeometryonly 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,
attachandsetGeometry,touchDown,setValue, thethumbSizeoption, the size table, the changeset): verified, plus theSwitch.doc.mjsre-quote it does not list. - The mount-cost test fails on
mainand passes here: verified. - The track-geometry test (no read at attach, one per press) fails on
mainand 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
mainand 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
mainalready 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.mjsre-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.
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
Switchmeasured 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:
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.value) animates the thumb from the previous state's default circle. That circle comes from Switch's ownTHUMB_SIZE, the same table the size styles read. Default themes animate exactly as onmain(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
useSwitchTouch's layout effect attaches the controller.attachcalledmeasureGeometry, which readstrack.getBoundingClientRect(),clientLeftandclientTop. That is one synchronous layout per Switch at mount, in both presentations.attachandsetGeometryonly mark the geometry as stale.touchDown), once per press, before the drag uses them.THUMB_SIZE(passed asthumbSize), as the rescale's start.Expected behavior and authority
architecture:react-component-runtimeINV18: a track measurement at mount is not an allowed read.useSwitchTouch).component:Switch's drag, flip and RTL behavior is unchanged.Smallest restoration
packages/core/src/Switch/useSwitchTouch.ts:isGeometryMeasuredflag;attachandsetGeometrystop measuring;touchDownmeasures;setValuestarts from the default circle when nothing was measured yet;thumbSizeoption.packages/core/src/Switch/Switch.tsx: aTHUMB_SIZEtable that the thumb size styles anduseSwitchTouchboth read.[perf]changeset.Evidence
Switch.test.tsx, "mount cost": mounting 200 Switches calls nogetBoundingClientRect. It fails onmainand passes here.useSwitchTouch.test.ts, "the track geometry":It fails on
mainand passes here.useSwitchTouch.test.ts, "the first value change before any measurement":maingives 24 px, the stale mount-time circle, and this head gives 20 px.Switch/plusLayer/touchTriggerTargets: 150 passed.mainwithin 0.05 px, frame by frame.main.mainjumps 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
Testing
vitest --project ui packages/core/src/Switch packages/core/src/Layer/touchTriggerTargets.test.tsx: 150 passed.main.tsc --noEmitfor 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)