Skip to content

perf(useLayer): placing a context layer re-renders its slot, not its host - #8519

Merged
cixzhang merged 2 commits into
meta:mainfrom
vjeux:layer-slot-second-render
Oct 26, 2026
Merged

cixzhang merged 2 commits into
meta:mainfrom
vjeux:layer-slot-second-render

Conversation

@vjeux

@vjeux vjeux commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Primary intent

Performance of every context-mode useLayer host. A Tooltip, Popover, DropdownMenu, Selector or ComplexSelector that mounts closed renders twice: once with the inert marker, then again when the marker's ref callback resolves the mount and sets it as state on the hook, re-rendering the host and everything under it. Nothing visual or new, so no other template fits.

User or maintainer impact

Every application mounting closed layers pays a second render of each host's subtree: a list of rows with menus renders every row twice. In one application's end-to-end suite (7,600 cases, production build), 110,000 of 1.3 million rendering commits had this origin, about 150 components each.

After this change the host renders once. The second render still happens, in an internal slot owning only the <template> marker and the popover element. The DOM, server markup, hydration, lazyMount and the public API are unchanged.

Problem and solution fit

A context layer cannot know its JSX parent until the marker is in the DOM, so the hook learns the mount in a ref callback and publishes it through useState on itself; that state re-renders the whole host for a fact that only places one element.

The mount now reaches a ContextLayerSlot child through a store the hook holds; the slot subscribes with useSyncExternalStore and renders the marker and the layer. The hook keeps its synchronous ref for show() and bumps a mountGeneration only when a resolved mount is replaced (a render call that moved), so the effects bound to the layer element still re-run then.

Rejected: an inline first render corrected in the same commit trips React's development nesting validation (<div> under <p>) and breaks the marker-only server markup of #5039; lazy mounting by default removes the closed layer from the DOM, dangles aria-controls and aria-describedby, and changes a documented default.

Authority

architecture:layer-runtime INV1: inline when safe, a corrective portal beyond the outermost unsafe ancestor, as before. architecture:react-component-runtime INV1 and INV4: no marker-only state repaired from a ref callback, and the mount is read through a subscription, not a ref. No record changes; useLayer's exports and options are unchanged.

Change and evidence

  • useLayer.tsx: ContextLayerMountStore, ContextLayerSlot; requestContextMount writes the store and the generation; the effects keyed on contextMount key on mountGeneration.
  • useLayer.test.tsx: the host renders once on a closed mount while the run commits twice; the host renders once with the marker in a <p>, the slot portaling the layer out; server markup carries the marker alone, hydrates with no React error, then mounts the layer. The first two fail on main.
  • useLayer.doc.mjs: one sentence on what the resolution re-renders. Changeset [perf], patch.

Scope

  • This PR has one primary intent.
  • Independently removable deltas were removed or split.
  • Public text and artifacts contain no internal Meta context.

Testing

  • pnpm exec vitest run packages/core/src/Layer/useLayer.test.tsx: 88 passed (3 new).
  • pnpm exec vitest run packages/core/src/{Layer,Tooltip,HoverCard,Popover,DropdownMenu,ContextMenu,Selector,ComplexSelector,MultiSelector,Combobox,Toast,Breadcrumbs}: 47 files, 1483 passed.
  • pnpm -F @astryxdesign/core typecheck, pnpm exec eslint --max-warnings=0 on the touched files, pnpm exec prettier --check, pnpm check:changesets: clean.
  • Red arm: with main's useLayer.tsx, "renders the host once on a closed mount" and "renders the host once when the marker sits in an unsafe parent" fail (expected 2 to be 1); the hydration case passes on both.

…host

A context-mode layer resolves where it is hosted from its inert marker once
the marker is in the DOM. That mount used to be state on the hook, so every
Tooltip, Popover, DropdownMenu, Selector and ComplexSelector rendered twice
on a closed mount, the second time with everything under it. The mount now
reaches an internal ContextLayerSlot through a store the hook holds; the
slot renders the marker and the layer, and the second render is its alone.
The DOM, the server markup, hydration and lazyMount are unchanged.
@vercel

vercel Bot commented Oct 26, 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 26, 2026 12:13pm UTC

Request Review

@astracat-bot

astracat-bot Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Oct 26, 2026, 12:56 PM UTC

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

github-actions Bot commented Oct 26, 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
core/AlertDialog 54.4KB 54.6KB +168B 12.4KB 12.4KB 0B
core/AppShell 55.5KB 55.7KB +152B 11.6KB 11.6KB 0B
core/Avatar 37.3KB 37.5KB +145B 6.3KB 6.3KB 0B
core/AvatarGroup 39.6KB 39.8KB +150B 6.4KB 6.4KB 0B
core/AvatarGroupOverflow 39.7KB 39.9KB +169B 6.4KB 6.4KB 0B
core/Banner 47.7KB 47.8KB +138B 9.9KB 9.9KB 0B
core/BaseTypeahead 53.7KB 53.8KB +152B 9.9KB 9.9KB 0B
core/BreadcrumbItem 65.7KB 65.9KB +152B 11.0KB 11.0KB 0B
core/Button 43.1KB 43.3KB +146B 9.2KB 9.2KB 0B
core/Calendar 54.7KB 54.9KB +135B 10.0KB 10.0KB 0B
core/Carousel 50.1KB 50.2KB +137B 10.2KB 10.2KB 0B
core/ChatComposer 60.3KB 60.4KB +148B 10.8KB 10.8KB 0B
core/ChatComposerInput 55.3KB 55.5KB +158B 10.1KB 10.1KB 0B
core/ChatComposerTokenElement 55.3KB 55.5KB +158B 10.1KB 10.1KB 0B
core/ChatDictationButton 46.4KB 46.5KB +133B 9.5KB 9.5KB 0B
core/ChatLayout 48.7KB 48.8KB +135B 9.9KB 9.9KB 0B
core/ChatLayoutScrollButton 46.3KB 46.4KB +134B 9.6KB 9.6KB 0B
core/ChatMessageList 28.4KB 28.4KB +1B 7.8KB 7.8KB 0B
core/ChatSendButton 45.7KB 45.8KB +137B 9.2KB 9.2KB 0B
core/ChatToolCalls 31.9KB 31.9KB 0B 8.9KB 8.9KB 0B
core/CheckboxInput 49.3KB 49.4KB +134B 9.3KB 9.3KB 0B
core/CheckboxList 45.8KB 46.0KB +137B 8.1KB 8.1KB 0B
core/CheckboxListItem 53.4KB 53.6KB +125B 10.5KB 10.5KB 0B
core/CodeBlock 60.2KB 60.4KB +133B 10.7KB 10.7KB 0B
core/CommandPalette 49.2KB 49.2KB 0B 11.8KB 11.8KB 0B
core/CommandPaletteInput 31.9KB 31.9KB 0B 8.1KB 8.1KB 0B
core/ComplexSelector 55.1KB 55.3KB +168B 10.6KB 10.6KB 0B
core/ContextMenu 77.1KB 77.3KB +147B 14.5KB 14.5KB 0B
core/DateInput 92.3KB 92.5KB +176B 14.6KB 14.6KB 0B
core/DateRangeInput 68.8KB 69.0KB +148B 11.2KB 11.2KB 0B
core/DateTimeInput 102.5KB 102.6KB +155B 15.1KB 15.1KB 0B
core/DialogHeader 48.6KB 48.8KB +173B 9.8KB 9.8KB 0B
core/DrawerHeader 48.7KB 48.9KB +182B 11.2KB 11.2KB 0B
core/DropdownMenu 72.1KB 72.2KB +147B 14.6KB 14.6KB 0B
core/DropdownMenuSubMenu 57.4KB 57.5KB +153B 9.5KB 9.5KB 0B
core/Field 44.0KB 44.2KB +142B 8.0KB 8.0KB 0B
core/FieldLabel 39.6KB 39.8KB +150B 6.5KB 6.5KB 0B
core/FileInput 53.6KB 53.7KB +150B 10.3KB 10.3KB 0B
core/Heading 9.1KB 9.1KB -1B 6.8KB 6.8KB 0B
core/HoverCard 17.0KB 17.2KB +146B 4.9KB 4.9KB 0B
core/IconButton 43.2KB 43.3KB +144B 9.2KB 9.2KB 0B
core/InputGroup 44.6KB 44.8KB +147B 8.0KB 8.0KB 0B
core/LayerProvider 63.0KB 63.2KB +162B 10.1KB 10.1KB 0B
core/Lightbox 49.4KB 49.5KB +122B 9.8KB 9.8KB 0B
core/Link 43.4KB 43.5KB +137B 7.6KB 7.6KB 0B
core/ListItem 52.9KB 53.0KB +127B 10.5KB 10.5KB 0B
core/Markdown 143.6KB 143.7KB +147B 17.2KB 17.2KB 0B
core/MobileNav 48.7KB 48.8KB +131B 10.1KB 10.1KB 0B
core/MobileNavToggle 46.1KB 46.2KB +137B 9.4KB 9.4KB 0B
core/MoreMenu 72.3KB 72.5KB +136B 14.6KB 14.6KB 0B
core/MultiSelector 75.5KB 75.7KB +150B 14.5KB 14.5KB 0B
core/NumberInput 55.5KB 55.6KB +137B 10.7KB 10.7KB 0B
core/Pagination 87.2KB 87.3KB +160B 15.2KB 15.2KB 0B
core/Popover 49.7KB 49.8KB +139B 12.0KB 12.0KB 0B
core/PowerSearch 147.6KB 147.7KB +128B 18.5KB 18.5KB 0B
core/ProgressBar 7.7KB 7.7KB -1B 6.6KB 6.6KB 0B
core/RadioList 45.4KB 45.5KB +152B 8.0KB 8.0KB 0B
core/RadioListItem 52.0KB 52.1KB +125B 9.7KB 9.7KB 0B
core/SegmentedControl 38.7KB 38.8KB +145B 6.3KB 6.3KB 0B
core/Selector 78.6KB 78.7KB +152B 14.8KB 14.8KB 0B
core/SideNav 55.7KB 55.8KB +130B 10.6KB 10.6KB 0B
core/SideNavCollapseButton 46.5KB 46.6KB +131B 9.5KB 9.5KB 0B
core/SideNavHeading 54.6KB 54.7KB +162B 10.1KB 10.1KB 0B
core/SideNavItem 54.3KB 54.4KB +152B 10.1KB 10.1KB 0B
core/Slider 48.3KB 48.4KB +142B 8.5KB 8.5KB 0B
core/Spinner 26.3KB 26.3KB 0B 7.5KB 7.5KB 0B
core/StatusDot 18.9KB 19.0KB +132B 6.2KB 6.2KB 0B
core/Stepper 47.3KB 47.4KB +124B 9.6KB 9.6KB 0B
core/Switch 48.2KB 48.4KB +136B 9.3KB 9.3KB 0B
core/TabList 40.0KB 40.2KB +162B 7.2KB 7.2KB 0B
core/TabMenu 51.5KB 51.6KB +143B 10.0KB 10.0KB 0B
core/Table 83.3KB 83.5KB +148B 15.0KB 15.0KB 0B
core/TableCell 78.2KB 78.4KB +148B 14.6KB 14.6KB 0B
core/TableHeaderCell 78.0KB 78.1KB +147B 14.6KB 14.6KB 0B
core/TableSelectionToolbar 52.2KB 52.4KB +155B 11.2KB 11.2KB 0B
core/Text 8.6KB 8.6KB 0B 6.8KB 6.8KB 0B
core/TextArea 48.8KB 48.9KB +142B 9.4KB 9.4KB 0B
core/TextInput 53.0KB 53.1KB +148B 10.5KB 10.5KB 0B
core/Thumbnail 49.9KB 50.1KB +146B 11.0KB 11.0KB 0B
core/TimeInput 79.2KB 79.3KB +159B 13.6KB 13.6KB 0B
core/Timer 9.2KB 9.2KB -1B 6.8KB 6.8KB 0B
core/Timestamp 27.5KB 27.5KB -2B 9.5KB 9.5KB 0B
core/Toast 59.7KB 59.8KB +159B 9.7KB 9.7KB 0B
core/ToggleButton 44.0KB 44.1KB +142B 9.3KB 9.3KB 0B
core/Tokenizer 64.2KB 64.3KB +177B 11.7KB 11.7KB 0B
core/Toolbar 39.6KB 39.8KB +152B 7.7KB 7.7KB 0B
core/Tooltip 16.7KB 16.8KB +144B 4.9KB 4.9KB 0B
core/TopNav 49.5KB 49.7KB +133B 10.4KB 10.4KB 0B
core/TopNavHeading 54.0KB 54.2KB +166B 9.9KB 9.9KB 0B
core/TopNavMegaMenu 54.0KB 54.2KB +146B 10.7KB 10.7KB 0B
core/TopNavMenu 53.5KB 53.6KB +147B 10.1KB 10.1KB 0B
core/Typeahead 60.8KB 61.0KB +162B 11.3KB 11.3KB 0B

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 34.3KB 34.3KB 0B

Accessibility Audit

Status: No accessibility violations detected.

Visual Regression

55 of 502 shot(s) changed.

A change here is a question, not a failure: check whether the after is the
picture you intended. Record that review on the PR. Baseline maintenance is an
explicit dispatch of CI; this report never rewrites the baseline or adds a release gate.

component story theme mode pixels
Selector Indicator Space Bottom Sheet Narrow End RTL neutral light 753,933
Selector Indicator Space Bottom Sheet Wide Start neutral light 740,072
RichTextEditor Markdown parity: side by side probe dark 558,840
RichTextEditor Markdown parity: side by side probe light 540,756
Chat Full AI Chat probe light 230,823
Chat Full AI Chat probe dark 229,805
AppShell With Mobile Nav probe light 217,010
AppShell With Mobile Nav probe dark 216,960
Collapsible Dividers probe dark 66,445
Collapsible Dividers probe light 66,445
AppShell Playground neutral dark 65,198
AppShell Playground neutral light 63,788
Chat Default neutral dark 36,304
Chat Default neutral light 35,767
Chat Empty State probe light 23,531
Chat Empty State probe dark 22,622
ChatAutoScroll Scroll Behavior Comparison neutral dark 15,378
ChatAutoScroll Scroll Behavior Comparison neutral light 14,107
Chat With Attachments probe light 13,406
Chat With Attachments probe dark 11,232

and 35 more.


Generated by PR Enrichment workflow | Storybook | Sandbox | 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.

Requesting changes: the slot cuts the closed-mount host render from two to one for Tooltip, Selector and ComplexSelector, but the slide-rules effect in useLayer.tsx is now keyed on mountGeneration, which the first mount resolution doesn't bump. A layer whose render() call mounts after its host (a Popover leaving its bottom-sheet presentation, a conditional layer.render()) therefore never installs the @position-try sheet, so it cannot slide and on a narrow viewport can cover its own trigger. Please install the sheet where the mount resolves (in requestContextMount or ContextLayerSlot) and add a test that mounts the render call after its host with the sheet removed.

  • The changeset lists Popover among components that now render once, but a closed Popover still renders three times on mount, and the doc's "never the component that called the hook" skips the replaced-mount path.

[Automated review]

Comment thread packages/core/src/Layer/useLayer.tsx Outdated
ensureSlideRules(doc);
}
}, [mode, contextMount]);
}, [mode, mountGeneration]);

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.

This effect now re-runs only when mountGeneration changes, and the first resolution doesn't bump it. When the host's first commit doesn't include layer.render(), doc is undefined here and the slide-rules sheet is never installed. That happens for a Popover leaving its sheet presentation, or for a conditional render call, and the layer then loses its slide options. Installing the sheet where the mount resolves would cover it, for example in requestContextMount from the marker's document before mountStore.set(mount). A test that removes #astryx-layer-slide-rules and mounts the render call late would pin it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right: the first resolution never bumped the generation, so a render call mounting after its host got no sheet. In fcfd008 the effect is gone and requestContextMount installs the sheet on sentinel.ownerDocument as the mount resolves, the one event every mount has. New test "installs the slide rules for a render call that mounts after its host" removes the sheet, mounts the host without layer.render(), then adds the render call; it fails on 0c2a642 (expected null not to be null) and passes on main and now.

Comment thread .changeset/layer-slot-second-render.md Outdated
'@astryxdesign/core': patch
---

[perf] useLayer: placing a context layer no longer re-renders its host. The hook still resolves where the layer goes from its inert marker once the marker is in the DOM, but it now hands that mount to an internal slot that renders the marker and the layer, so the second render on mount is the slot's alone: a Tooltip, Popover, DropdownMenu, Selector or ComplexSelector and everything under it render once when they mount closed. The DOM, server markup, hydration and `lazyMount` are unchanged.

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.

A closed Popover still renders three times on mount, before and after this change, because of its own mount-time state updates. Tooltip, Selector and ComplexSelector do go from two renders to one. Consider naming only the components that change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed. The changeset now says the render that places the layer is the slot's alone, that a Tooltip, Selector or ComplexSelector that mounts closed renders once, and that a Popover or DropdownMenu keeps only the renders its own state costs.

type: '(children: ReactNode, props: ContextRenderProps | FixedRenderProps) => ReactNode',
description:
'Render function for the popover element. Pass placement/alignment in context mode or x/y in fixed mode. Placement/alignment are logical: they map to the self-* position-area keyword family, which resolves against the popover\'s own inherited direction, so RTL contexts mirror automatically in pure CSS. Pass `positioning: "custom"` in context mode to author position styles yourself via `style` (e.g. explicit anchor() insets or an anchor-size() cover): the hook keeps the popover behavior and position-anchor wiring but derives no position styles, including the automatic RTL mirroring, which becomes your responsibility. Pass `offset` (a CSS length; a number is px) in context mode for clearance from the anchor: it applies to both edges of the placement axis, so the gap survives a flip. Layers are flush by default. Context mode first renders an inert `<template>` marker in matching server and client markup. The final layer stays at that JSX position if its parent is safe; otherwise it is portaled to the nearest ancestor outside paragraphs, links, buttons, inline formatting, and structurally restricted containers. The nearest safe host keeps CSS custom properties inheriting live, while the layer preserves direction and writing mode from its JSX position. By default this resolution occurs after hydration so closed-layer DOM remains available; `lazyMount` defers it until `show()` and unmounts the content again on hide while the marker remains. The Popover API promotes the layer to the top layer when shown, so it escapes ancestor clipping and stacking wherever it is hosted. When the layer would overflow the viewport, position-try fallbacks flip it to the opposite side; centered layers additionally slide along the alignment axis (span fallbacks) so they stay on-screen near viewport edges.',
'Render function for the popover element. Pass placement/alignment in context mode or x/y in fixed mode. Placement/alignment are logical: they map to the self-* position-area keyword family, which resolves against the popover\'s own inherited direction, so RTL contexts mirror automatically in pure CSS. Pass `positioning: "custom"` in context mode to author position styles yourself via `style` (e.g. explicit anchor() insets or an anchor-size() cover): the hook keeps the popover behavior and position-anchor wiring but derives no position styles, including the automatic RTL mirroring, which becomes your responsibility. Pass `offset` (a CSS length; a number is px) in context mode for clearance from the anchor: it applies to both edges of the placement axis, so the gap survives a flip. Layers are flush by default. Context mode first renders an inert `<template>` marker in matching server and client markup. The final layer stays at that JSX position if its parent is safe; otherwise it is portaled to the nearest ancestor outside paragraphs, links, buttons, inline formatting, and structurally restricted containers. The nearest safe host keeps CSS custom properties inheriting live, while the layer preserves direction and writing mode from its JSX position. By default this resolution occurs after hydration so closed-layer DOM remains available, and it re-renders only the slot that holds the marker and the layer, never the component that called the hook; `lazyMount` defers it until `show()` and unmounts the content again on hide while the marker remains. The Popover API promotes the layer to the top layer when shown, so it escapes ancestor clipping and stacking wherever it is hosted. When the layer would overflow the viewport, position-try fallbacks flip it to the opposite side; centered layers additionally slide along the alignment axis (span fallbacks) so they stay on-screen near viewport edges.',

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.

When a resolved mount is replaced, for example when the render call moves, setMountGeneration re-renders the host, so "never the component that called the hook" is too strong. Scoping it to the first resolution would be accurate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Scoped in fcfd008: the doc now says the resolution re-renders the slot, not the component that called the hook, which renders again only when a resolved mount is replaced by a render call that moved to another host.

…changeset names what renders once

The slide-rules effect was keyed on mountGeneration, which the first
resolution does not bump, so a render call that mounts after its host
(a conditional layer.render()) never installed the @position-try sheet.
The sheet is now installed in requestContextMount, the one event every
mount has. A test mounts the render call after its host with the sheet
removed. The changeset and doc stop claiming a Popover renders once: a
Popover or DropdownMenu keeps the renders its own state costs, and the
host renders again when a resolved mount is replaced.

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

Recommending approval: the new slot keeps the marker, corrective-portal and slide-rule behavior intact while closed Tooltip, Selector and ComplexSelector hosts now render once on mount, and the earlier slide-rules and wording findings are fixed. One optional follow-up: under React StrictMode in development the marker's ref replay counts as a replaced mount, so the host still renders twice there; either bump mountGeneration only when a different marker element resolves the mount, or mention that replay in the useLayer.doc.mjs sentence.

[Automated review]

? readPortalWritingContext(sentinel, portalTarget)
: {},
};
const replaced = contextMountRef.current !== null;

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: under StrictMode, React 19 attaches, detaches and re-attaches this marker's ref on mount, so the second requestContextMount finds a resolved mount here, bumps mountGeneration and re-renders the host; a StrictMode mount still renders the host twice. Remembering the marker element that produced contextMountRef.current, and bumping only when a different marker resolves the mount, would match the comment above and the new useLayer.doc.mjs sentence; otherwise that sentence could name StrictMode's development replay.

@cixzhang
cixzhang merged commit 87a9e77 into meta:main Oct 26, 2026
77 checks passed
@vjeux
vjeux deleted the layer-slot-second-render branch October 26, 2026 15:52
vjeux added a commit to vjeux/astryx that referenced this pull request Oct 30, 2026
…s tooltip

Three tests read a code block header's text right after it appeared. Since meta#8519 the copy button's tooltip arrives one render later, from its own layer slot, so on a busy runner the read could miss it. Each test now waits for the full text inside waitFor.
cixzhang pushed a commit that referenced this pull request Oct 31, 2026
…s tooltip (#8569)

## Maintainer impact

Found when the Spinner and CircularProgress fixes (#8177, #8232) hit this race in CI. Since #8519, `CodeBlockHeaderPlugin.test.tsx` can fail on a busy CI runner with no rich-text change: a header's text read `['ts', 'js']` instead of `['tsCopy code', 'jsCopy code']` on pull requests that touch neither rich text nor layers, which then needed a re-run.

## Intended invariant

Each code block header shows its language label and a copy button whose tooltip reads "Copy code" (`spec:AST-061` FR8). The tests assert that text; product behavior stays as it is.

## Change

What led to it: a header's text ends with its copy button's tooltip. Since #8519 the tooltip's layer is placed by its own slot one render after the header (the slot gets its mount from a store subscription). Three tests waited for the header to exist, then read its text, so a late slot render left the tooltip out.

Options:

- Wait for the full text inside `waitFor` (this PR): the tests stop assuming one render.
- Render the tooltip in the header's render: a product change that would undo #8519's performance gain.
- Retry or skip the flaky tests: that hides the race.

This PR takes the first, since the one-render assumption was the tests', not the product's. The first test already finds the button by its accessible name, present from the start, so it is unchanged.

## Evidence

- Failure before the change: with the slot seeing its mount 30 ms late (a temporary edit in `useLayer.tsx`), the old tests fail as the failing runs did: `expected '' to be 'Copy code'`, and `['', 'Plaintext']` for `['Copy code', 'PlaintextCopy code']`.
- Success after the change: with the same delay the new tests pass, and without it too.
- Product/runtime behavior verified unchanged: only the test file changes.

## Scope

- [x] No intended public API, product behavior, visual direction, or policy change.
- [x] Product changes discovered during the work were removed or split.
- [x] Public text and artifacts contain no internal Meta context.

## Testing

What I ran and what it showed:

1. `pnpm exec vitest run --project ui packages/richtext/src/CodeBlockHeaderPlugin.test.tsx`: all four tests pass.
2. With the layer mount store's update in `useLayer.tsx` delayed 30 ms, its value and its listeners both (a temporary edit): main's file fails three of the four tests; this file passes all four.
3. Local gates at 7d01151: root build, typecheck, ESLint, Prettier, the duration guard and `pnpm test:ui` (16,446 tests) pass; in the node project, the only failures are `cache.test.mjs`'s two install-lock cases, a known failure on this machine (#8520).

This branch was successfully deployed

1 active deployment
Preview — fcfd008f Deployed Oct 26, 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.

2 participants