Repository navigation
perf(Layer): a closed layer reads its writing context when it first opens, not on mount - #9012
Conversation
…pens, not on mount
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
PR Analysis Report📚 Storybook Preview🧪 Sandbox PreviewComponent Size
Sizes are gzip. Import counts the JavaScript the import loads eagerly; CSS counts the package stylesheet rules referenced by the modules it can load from its own package, lazily loaded ones included. Bundle Size SummaryGzip figures measure each package's entry file, not the code an import pulls in.
Accessibility AuditStatus: The accessibility audit did not run on this pull request. It runs after merges to Generated by PR Enrichment workflow | Storybook | Sandbox | View full report |
|
AI review status for this pull request.
|
There was a problem hiding this comment.
Reviewed and looks good to approve: deferring the writing-context read to show() keeps the first open frame settled, and the three new tests hold it — two of them fail against the current main implementation.
Checked at this revision: the Layer, Tooltip, Popover and DropdownMenu unit suites and the core typecheck pass, and in Chromium a right-to-left tooltip inside a right-to-left paragraph still opens with direction: rtl both inline and computed, with the open frames byte-identical to main.
The red checks are not this change: the UI test lane passed, test is only the job that joins the others, the test-node failure is the CLI build-theme --icons-specifier watch-mode test (it passes on a rerun here), smoke-test failed on an esm.sh 500 while loading lucide-react for the published CDN template, and test-build, pr-interaction and fixture-contrast were declined by the queue policy rather than run — worth a rerun so the pre-merge signal is real.
One note for whoever merges: the page-level timings in the description come from a temporary story that is not in the repository, so the size of the win is not reproducible — the mechanism behind it is.
[Automated review]
… writing context read before the show)
User impact
When people open a page that mounts many closed popovers, tooltips and menus at once, such as a long comment list where each row has its own menus and tooltips, the page freezes while it mounts. Each closed layer outside an inline or interactive parent read computed style on mount to copy its writing direction. Other components wrote to the DOM between those reads, so every read forced a full style recalculation. After this change, those layers mount without reading style. Open layers look and behave as before.
Problem and solution fit
<p>, a<button>, inline text) portals to a safe ancestor. Mounting it calledreadPortalWritingContext, which readsgetComputedStyleon the marker and on the host. That runs inside React's commit, right after other components' DOM writes, such as each trigger'sanchor-name, so each read paid for a whole-document style recalculation.show()reads it before the layer opens, writes it on the popover at once, and publishes it to the slot, whose render writes the same values. A layer that mounts already opening, such aslazyMountor a remount while open, still reads it on mount.Expected behavior and authority
architecture:layer-runtimehosting: direction and writing mode are copied only when the portal would otherwise lose them. That still holds.spec:AST-059FR8), "the first frame is the settled frame": the deferred read runs synchronously inshow(), beforeshowPopover, so the first open frame already has the direction.spec:AST-038FR2: the writing context survives. A caller's ownstyle.direction/writingModestill wins over the copied value, as it does in render.Smallest restoration
One file in the runtime (
useLayer.tsx).ContextLayerMount.portalStylemay benull(not read yet), andshow()applies the deferred read. No API, visual, default or interaction change.Evidence
Chrome 156, headless, on a 40-row page: 7,954 elements, 403 closed popovers, each row with tooltips inside inline text, a menu, an overflow list and clamped text. Each figure is the median of 7 loads, taken from a timeline trace of the mount's long task. The page was a temporary story, not committed.
mainThe remaining passes come from text-truncation and overflow-list measuring, which have separate fixes.
Reproduction before: 121 forced passes in the trace stack under
readPortalWritingContext, on mount, with every layer closed.Result after: none on mount. A layer reads its writing context once, when it first opens.
Unchanged paths:
main.direction: rtl, both inline and computed, as onmain.Mutation-sensitive tests:
useLayer.test.tsxadds three tests. Two fail onmain:showPopoverruns.The third, "keeps the caller's own direction over a deferred writing context", fails if the imperative write ignores the caller's style.
Scope
Testing
vitest --project ui packages/core/src/Layer packages/core/src/Tooltip packages/core/src/Popover packages/core/src/DropdownMenu: 704 passed.pnpm -F @astryxdesign/core typecheck