Skip to content

perf(Layer): a closed layer reads its writing context when it first opens, not on mount - #9012

Merged
cixzhang merged 1 commit into
mainfrom
perf/layer-lazy-writing-context
Oct 30, 2026
Merged

cixzhang merged 1 commit into
mainfrom
perf/layer-lazy-writing-context

Conversation

@cixzhang

Copy link
Copy Markdown
Contributor

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

  • Why it is slow: a context layer whose JSX position is unsafe (inside a <p>, a <button>, inline text) portals to a safe ancestor. Mounting it called readPortalWritingContext, which reads getComputedStyle on the marker and on the host. That runs inside React's commit, right after other components' DOM writes, such as each trigger's anchor-name, so each read paid for a whole-document style recalculation.
  • Why it matters: a closed layer never paints, so its writing direction matters only when it opens. Reading it on mount puts one recalculation per closed layer on the page's first frame.
  • Fix: a closed layer's mount leaves its writing context unread. 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 as lazyMount or a remount while open, still reads it on mount.

Expected behavior and authority

  • architecture:layer-runtime hosting: direction and writing mode are copied only when the portal would otherwise lose them. That still holds.
  • INV12 (spec:AST-059 FR8), "the first frame is the settled frame": the deferred read runs synchronously in show(), before showPopover, so the first open frame already has the direction.
  • spec:AST-038 FR2: the writing context survives. A caller's own style.direction / writingMode still wins over the copied value, as it does in render.

Smallest restoration

One file in the runtime (useLayer.tsx). ContextLayerMount.portalStyle may be null (not read yet), and show() 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.

Build Mount long task Forced style/layout passes in it Of them, from the writing-context read
main 340 ms 285 121
this PR 309 ms 164 0

The 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:

    • Full-page screenshots of the settled page are byte-identical to main.
    • A right-to-left tooltip inside an inline paragraph opens with direction: rtl, both inline and computed, as on main.
    • Truncated text still shows its full-text tooltip on hover.
  • Mutation-sensitive tests: useLayer.test.tsx adds three tests. Two fail on main:

    • "reads no computed style for a closed layer mounted ahead of its first show";
    • "applies a closed layer's deferred writing context before it opens", which checks the popover's inline direction at the moment showPopover runs.

    The third, "keeps the caller's own direction over a deferred writing context", fails if the imperative write ignores the caller's style.

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/Layer packages/core/src/Tooltip packages/core/src/Popover packages/core/src/DropdownMenu: 704 passed.
  • pnpm -F @astryxdesign/core typecheck
  • The real-browser trace and checks above.

@vercel

vercel Bot commented Oct 30, 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 Oct 30, 2026 3:21pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 30, 2026
@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR

🧪 Sandbox Preview

View Sandbox for this PR

Component Size

Component Import before Import after Delta CSS before CSS after Delta
charts/Chart N/A 65.1KB N/A N/A 20B N/A
charts/ChartLegend N/A 54.4KB N/A N/A 20B N/A
charts/ChartTooltip N/A 54.4KB N/A N/A 20B N/A
core/AlertDialog 58.1KB 58.2KB +100B 12.5KB 12.5KB 0B
core/AppShell 67.4KB 67.5KB +103B 11.8KB 11.8KB 0B
core/Avatar 37.8KB 37.9KB +110B 6.3KB 6.3KB 0B
core/AvatarGroup 40.2KB 40.3KB +98B 6.4KB 6.4KB 0B
core/AvatarGroupOverflow 41.1KB 41.2KB +106B 6.4KB 6.4KB 0B
core/Banner 49.3KB 49.4KB +102B 10.1KB 10.1KB 0B
core/BaseTypeahead 55.8KB 55.9KB +108B 10.1KB 10.1KB 0B
core/BreadcrumbItem 68.1KB 68.2KB +100B 11.2KB 11.2KB 0B
core/Button 44.5KB 44.6KB +99B 9.3KB 9.3KB 0B
core/Calendar 56.4KB 56.5KB +102B 10.2KB 10.2KB 0B
core/Carousel 51.7KB 51.8KB +112B 10.3KB 10.3KB 0B
core/ChatComposer 62.2KB 62.3KB +113B 11.0KB 11.0KB 0B
core/ChatComposerInput 56.9KB 57.0KB +101B 10.2KB 10.2KB 0B
core/ChatComposerTokenElement 56.9KB 57.0KB +100B 10.2KB 10.2KB 0B
core/ChatDictationButton 48.0KB 48.0KB +89B 9.6KB 9.6KB 0B
core/ChatLayout 50.4KB 50.5KB +104B 10.1KB 10.1KB 0B
core/ChatLayoutScrollButton 47.9KB 48.0KB +104B 9.7KB 9.7KB 0B
core/ChatMessageList 29.3KB 29.3KB -1B 7.8KB 7.8KB 0B
core/ChatSendButton 47.2KB 47.3KB +93B 9.3KB 9.3KB 0B
core/ChatToolCalls 32.4KB 32.4KB 0B 9.1KB 9.1KB 0B
core/CheckboxInput 51.3KB 51.5KB +114B 9.5KB 9.5KB 0B
core/CheckboxList 46.6KB 46.7KB +107B 8.3KB 8.3KB 0B
core/CheckboxListItem 55.5KB 55.6KB +104B 10.6KB 10.6KB 0B
core/CodeBlock 62.3KB 62.4KB +103B 10.8KB 10.8KB 0B
core/CommandPalette 52.8KB 52.8KB 0B 12.0KB 12.0KB 0B
core/CommandPaletteInput 32.9KB 32.9KB +1B 8.3KB 8.3KB 0B
core/ComplexSelector 57.3KB 57.4KB +101B 10.8KB 10.8KB 0B
core/ContextMenu 75.8KB 75.9KB +102B 14.8KB 14.8KB 0B
core/DateInput 101.4KB 101.5KB +98B 14.9KB 14.9KB 0B
core/DateRangeInput 71.3KB 71.4KB +103B 11.4KB 11.4KB 0B
core/DateTimeInput 111.6KB 111.7KB +96B 15.4KB 15.4KB 0B
core/DialogHeader 50.3KB 50.4KB +102B 10.0KB 10.0KB 0B
core/DrawerHeader 50.4KB 50.5KB +96B 11.4KB 11.4KB 0B
core/DropdownMenu 73.9KB 74.0KB +115B 15.0KB 15.0KB 0B
core/DropdownMenuSubMenu 59.5KB 59.7KB +119B 9.7KB 9.7KB 0B
core/Field 44.8KB 44.9KB +108B 8.1KB 8.1KB 0B
core/FieldLabel 40.3KB 40.4KB +105B 6.7KB 6.7KB 0B
core/FileInput 55.6KB 55.7KB +100B 10.5KB 10.5KB 0B
core/Heading 9.1KB 9.1KB +1B 6.8KB 6.8KB 0B
core/HoverCard 17.2KB 17.3KB +111B 4.9KB 4.9KB 0B
core/IconButton 44.5KB 44.6KB +107B 9.3KB 9.3KB 0B
core/InputGroup 45.4KB 45.5KB +97B 8.2KB 8.2KB 0B
core/LayerProvider 64.8KB 64.9KB +94B 10.3KB 10.3KB 0B
core/Lightbox 52.7KB 52.8KB +98B 10.0KB 10.0KB 0B
core/Link 44.9KB 45.0KB +103B 7.7KB 7.7KB 0B
core/ListItem 54.9KB 55.0KB +105B 10.6KB 10.6KB 0B
core/Markdown 146.0KB 146.1KB +116B 17.8KB 17.8KB 0B
core/MobileNav 52.5KB 52.6KB +101B 10.2KB 10.2KB 0B
core/MobileNavToggle 47.7KB 47.8KB +102B 9.6KB 9.6KB 0B
core/MoreMenu 74.2KB 74.3KB +110B 15.0KB 15.0KB 0B
core/MultiSelector 74.2KB 74.3KB +90B 14.8KB 14.8KB 0B
core/NumberInput 57.3KB 57.4KB +101B 10.9KB 10.9KB 0B
core/Pagination 86.4KB 86.5KB +108B 15.5KB 15.5KB 0B
core/Popover 50.9KB 51.0KB +117B 12.2KB 12.2KB 0B
core/PowerSearch 168.0KB 168.0KB +83B 20.1KB 20.1KB 0B
core/ProgressBar 7.7KB 7.7KB 0B 6.6KB 6.6KB 0B
core/RadioList 46.2KB 46.3KB +102B 8.2KB 8.2KB 0B
core/RadioListItem 53.6KB 53.8KB +106B 9.8KB 9.8KB 0B
core/SegmentedControl 39.1KB 39.2KB +115B 6.3KB 6.3KB 0B
core/Selector 78.0KB 78.1KB +105B 15.2KB 15.2KB 0B
core/SideNav 65.4KB 65.5KB +104B 10.7KB 10.7KB 0B
core/SideNavCollapseButton 48.1KB 48.2KB +114B 9.6KB 9.6KB 0B
core/SideNavHeading 60.0KB 60.1KB +106B 10.4KB 10.4KB 0B
core/SideNavItem 56.1KB 56.2KB +95B 10.3KB 10.3KB 0B
core/Slider 49.1KB 49.2KB +96B 8.7KB 8.7KB 0B
core/Spinner 26.7KB 26.7KB 0B 7.5KB 7.5KB 0B
core/StatusDot 19.0KB 19.1KB +107B 6.2KB 6.2KB 0B
core/Stepper 48.9KB 49.0KB +109B 9.7KB 9.7KB 0B
core/Switch 49.8KB 49.9KB +106B 9.5KB 9.5KB 0B
core/TabList 40.6KB 40.8KB +108B 7.3KB 7.3KB 0B
core/TabMenu 56.7KB 56.8KB +112B 10.2KB 10.2KB 0B
core/Table 85.4KB 85.5KB +117B 15.6KB 15.6KB 0B
core/TableCell 77.0KB 77.1KB +103B 15.3KB 15.3KB 0B
core/TableHeaderCell 76.6KB 76.7KB +101B 15.0KB 15.0KB 0B
core/TableSelectionToolbar 53.6KB 53.7KB +100B 11.3KB 11.3KB 0B
core/Text 8.6KB 8.6KB 0B 6.8KB 6.8KB 0B
core/TextArea 50.0KB 50.1KB +105B 9.5KB 9.5KB 0B
core/TextInput 55.1KB 55.2KB +97B 10.6KB 10.6KB 0B
core/Thumbnail 52.1KB 52.2KB +101B 11.1KB 11.1KB 0B
core/TimeInput 88.6KB 88.7KB +110B 13.9KB 13.9KB 0B
core/Timer 9.2KB 9.2KB +1B 6.8KB 6.8KB 0B
core/Timestamp 27.9KB 27.9KB -2B 9.7KB 9.7KB 0B
core/Toast 61.4KB 61.5KB +97B 9.9KB 9.9KB 0B
core/ToggleButton 45.3KB 45.4KB +101B 9.4KB 9.4KB 0B
core/Tokenizer 69.8KB 69.9KB +124B 16.7KB 16.7KB 0B
core/Toolbar 40.1KB 40.2KB +106B 7.7KB 7.7KB 0B
core/Tooltip 16.8KB 16.9KB +103B 4.9KB 4.9KB 0B
core/TopNav 53.3KB 53.4KB +105B 10.5KB 10.5KB 0B
core/TopNavHeading 59.4KB 59.5KB +106B 10.2KB 10.2KB 0B
core/TopNavMegaMenu 55.8KB 55.9KB +108B 11.0KB 11.0KB 0B
core/TopNavMenu 59.1KB 59.2KB +107B 10.3KB 10.3KB 0B
core/Typeahead 63.0KB 63.1KB +100B 11.4KB 11.4KB 0B
lab/ChatEmojiPicker N/A 66.8KB N/A N/A 773B N/A
lab/ChatReactionBar N/A 67.4KB N/A N/A 1.1KB N/A
lab/CommentComposer N/A 95.8KB N/A N/A 1.9KB N/A
lab/CommentPin N/A 48.8KB N/A N/A 1.2KB N/A
lab/CommentThread N/A 217.8KB N/A N/A 2.4KB N/A
lab/Comments N/A 222.3KB N/A N/A 2.5KB N/A
lab/EmojiPicker N/A 64.8KB N/A N/A 722B N/A
lab/InfoTip N/A 22.0KB N/A N/A 458B N/A
lab/ListInput N/A 76.8KB N/A N/A 1.6KB N/A
lab/MobilePowerSearch N/A 154.6KB N/A N/A 1.2KB N/A
lab/MobileTokenizer N/A 94.1KB N/A N/A 880B N/A
lab/Schedule N/A 61.6KB N/A N/A 3.9KB N/A
lab/TourStep N/A 56.4KB N/A N/A 590B N/A
lab/TransferList N/A 68.6KB N/A N/A 1.7KB N/A
lab/TransferListSelector N/A 74.2KB N/A N/A 1.9KB N/A
richtext/RichTextEditor N/A 272.4KB N/A N/A 2.6KB N/A
richtext/RichTextEditorToolbar N/A 214.1KB N/A N/A 454B N/A
richtext/RichTextView N/A 252.8KB N/A N/A 2.3KB N/A

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 Summary

Gzip figures measure each package's entry file, not the code an import pulls in.

Package Entry (ESM) Entry gzip before Entry gzip after Delta
@astryxdesign/core 4.9KB 1.2KB 1.2KB 0B
Package stylesheet Gzip before Gzip after Delta
@astryxdesign/core 35.4KB 35.4KB 0B

Accessibility Audit

Status: The accessibility audit did not run on this pull request. It runs after merges to main and before each release; pull requests that touch components run it with the ci:heavy-lanes label.


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

@astracat-bot

astracat-bot Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Oct 30, 2026, 5:27 PM UTC

@cixzhang
cixzhang marked this pull request as ready for review October 30, 2026 16:52

@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.

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]

@cixzhang
cixzhang merged commit e9ac835 into main Oct 30, 2026
142 of 149 checks passed
cixzhang added a commit to vjeux/astryx that referenced this pull request Oct 31, 2026
… writing context read before the show)
@github-actions
github-actions Bot deleted the perf/layer-lazy-writing-context branch October 31, 2026 06:09

This branch was successfully deployed

1 active deployment
Preview — 53c08ba1 Deployed Oct 30, 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant