Repository navigation
perf(Item): load the swipe gesture only when swipeActions is set - #7296
Conversation
Item imported useSwipeAction unconditionally and called it on every render, so every consumer rendering a row without swipeActions shipped the swipe gesture and the panel markup on its first load. The gesture and the panels now live in ItemSwipeLayer, which Item reaches only through a dynamic import, rendered inside the root when swipeActions resolve to a served swipe. The root keeps the swipe styles and forwards its pointer, click-capture and key events into a bridge the layer publishes, so a swipeable row keeps its DOM (no element added) and React's event model on its root. Item's public props and types are unchanged. Two at-rest rows importing Item from the package root: initial load 11,641 -> 8,963 B gzip (-2,678 B); the swipe hook, setPointerCapture and the panel marker are gone from it. The same rows with swipeActions: 8,993 B initial + a 3,529 B gzip chunk carrying the hook and the panels. Item.source-build.test.mjs pins the compiled import graph; Item.swipeLayer .test.tsx proves a row without swipeActions never renders the layer. The existing swipe suites are unchanged in substance: one warm-up render makes the lazy layer synchronous for them, and the browser spec waits for the panel before it drags.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
There was a problem hiding this comment.
Thanks — this looks good to land.
The split does what it promises: the gesture and panels move verbatim into a module Item only reaches through a dynamic import when a row has swipe actions, the root keeps its styles and forwards events into the layer, and the rendered DOM once loaded is the one from before (panel straight into the root, no wrapper). The new tests guard the important part well — the import-graph test means this can't quietly regress to a static import, and the runtime test shows rows without swipeActions never touch the layer. Existing swipe and List suites pass.
The one trade-off is already called out in the changeset and docs: on a cold load a swipe row paints immediately but only becomes swipeable once the chunk arrives, and swipe consumers pick up a small extra request. That's the same shape as the other lazy affordances in core, and since panels show nothing at rest there's no visual change while it loads.
[Automated review]
PR Analysis Report📚 Storybook Preview🧪 Sandbox PreviewModified ComponentsItem (@astryxdesign/core) · View in Storybook
Bundle Size Summary
Accessibility AuditStatus: No accessibility violations detected. Visual Regression24 of 444 shot(s) changed. A change here is a question, not a failure: check whether the after is the
and 4 more. Generated by PR Enrichment workflow | Storybook | Sandbox | View full report |
cixzhang
left a comment
There was a problem hiding this comment.
CodeBunny approved at this head; CI green. Approved.
Primary intent
An
ItemwithoutswipeActionsshould ship none of the swipe gesture.Item.tsximportsuseSwipeActionunconditionally and calls it on every render, so every row carries the gesture and the panels on first load. A bundler cannot drop a statically imported module a component may render, so paying nothing withoutswipeActionsmeans loading the gesture on demand. Nothing is broken and no capability is added, so no other template fits.User or maintainer impact
Every consumer rendering
ItemwithoutswipeActions(a two-row sidebar surfaced it) loses the gesture and the panels from its initial load. A consumer withswipeActionskeeps the same row DOM, props, types and behavior; the gesture arrives in a chunk requested during the row's first render, so on a cold load the row paints at once (the panels show nothing at rest) and becomes swipeable when the chunk lands.Problem and solution fit
A hook cannot be conditional, so the gesture ran for every row: about 2.7 KB gzip no at-rest row executes, paid everywhere because
Itemis the row primitive behind lists, menus and navigation.The gesture and the panels move to
Item/ItemSwipeLayer.tsx, reached only throughlazy(() => import('./ItemSwipeLayer'))inside<Suspense fallback={null}>and rendered into the root whenswipeActionsresolve to a served swipe, as today. The root staysItem's: it keeps the swipe root styles and forwards its pointer, click-capture and key events into a bridge the layer publishes, so no element is added and React's event model on the root is unchanged, the wayProgressBarandTimestampalready load an affordance. No public API change.Authority
spec:AST-057(current) governs; FR2 and DEC-5 (no element added; the root translates, the panels counter-translate) still hold and are tested. No new policy.Change and evidence
esbuild, minified, React external, two rows imported from
@astryxdesign/core:swipeActionsswipeActionsuseSwipeAction,setPointerCaptureand[data-swipe-panel]are absent from the initial load after and present in the chunk.Item.source-build.test.mjs: the compiledItem.tsxreaches the layer only by a dynamic import and the gesture not at all.Item.swipeLayer.test.tsx: rows withoutswipeActionsnever render the layer; a row with them renders the panel into the root, no wrapper.Item.test.tsxare unchanged in substance (one warm-up render makes the layer synchronous);List.test.tsxand the Chromium spec wait for the panel.Scope
Testing
pnpm vitest run --maxWorkers=4 packages/core/src/Item packages/core/src/List: 5 files, 146 passed.pnpm vitest run packages/core: 369 files / 10,915 tests passed; the 28 failures are locale cases (Calendar, DateInput, NumberInput parsing, collation) on a Node built withsmall-icu, unrelated to this change.pnpm -F @astryxdesign/core build,tsc --noEmit(core),pnpm check:repo: clean. ESLint on the touched files: no new warnings.[perf], patch.