Skip to content

perf(core): render Text, Heading, Kbd, Button, IconButton and TopNav in React Server Components - #8443

Draft
rhagigi wants to merge 5 commits into
meta:mainfrom
rhagigi:perf/server-safe-shell
Draft

rhagigi wants to merge 5 commits into
meta:mainfrom
rhagigi:perf/server-safe-shell

Conversation

@rhagigi

@rhagigi rhagigi commented Oct 25, 2026

Copy link
Copy Markdown

Primary intent

Performance: let an app that renders its header from React Server Components ship no component JavaScript for the parts of it that only present. This PR makes Text, Heading, Kbd, Button, IconButton, TopNav and TopNavItem render on the server. None of the other templates fits: it is not a defect, a visual change, or a new public capability (no prop, type or export changes), and unlike pure maintenance it changes where these components render and when their code loads.

The rule: presentational paths render on the server; interactive behavior lives in client parts, loaded only when the props (or the client providers) that need it are used. Server-render tests are the evidence.

User or maintainer impact

Today every one of these components carries 'use client', so a Server Component that renders <TopNav>, a few <TopNavItem> links and a <Button href> ships and hydrates the whole client implementation of each: the client TopNav with MobileNav, the drawer toggle and the menus (the TopNav barrel), and the client Button with Tooltip, Spinner, the loading live region and the i18n catalog.

After this change, in a Server Component:

  • Text, Heading and Kbd render on the server with no client code. Truncation with a tooltip (an explicit maxLines) and the platform mod glyph are small client parts, rendered only when used.
  • Button, IconButton, the TopNav desktop bar and TopNavItem render their markup on the server. Each sits in a small client part that reads the client context a Server Component cannot, and the interactive component loads lazily only where it is needed.

Client imports are unchanged: index.ts stays the default entry, so client code renders exactly what it rendered before.

Problem and solution fit

  • Why does a server-rendered header ship this code? Each component is a 'use client' module, so the server can only emit a client reference to it, and the browser loads and hydrates the full component.
  • Why does that hurt? The header is on every page's critical path, and its JavaScript competes with first paint and hydration.
  • Why does it matter? The interactive parts (menus, tooltips, loading state, the mobile drawer) are the larger part of that code, and most header renders use none of them.

Why not just drop 'use client' (as Text, Heading and Kbd do)? Button, TopNav and TopNavItem read client context that changes their output, and current authority requires it:

  • Button.spec.md FR4/ORD1 and architecture:component-size-cascade INV3 to INV5: size comes from the nearest provider (Toolbar, SideNav, ButtonGroup);
  • FR10 and FR13: a ButtonGroup disables its members and owns their corners and elevation;
  • FR11: links resolve as, then LinkProvider, then a native anchor;
  • FR15: touch press feedback;
  • AppShell renders its topNav three times and tells each copy its mode (desktop, mobile bar, drawer) through context.

A Server Component cannot read context, and a directive-free module is also imported by client code, where dropping the context reads would break all of the above. So these three use the react-server export condition from #823: the condition resolves a server shell, and the default condition keeps the client component.

Deltas:

  • Primary
    • react-server entries for ./Button, ./IconButton and ./TopNav, which resolve server shells (ButtonShell.tsx, IconButtonShell.tsx, TopNavShell.tsx, TopNavItemShell.tsx).
    • The client parts listed below.
    • Text, Heading and Kbd as server modules (first commit).
  • Supporting
    • Shared, server-safe style and markup modules, so both renderers produce the same output: buttonStyles.stylex.ts, ButtonContent.tsx, topNavStyles.ts, topNavItemStyles.stylex.ts, TopNavMobileBar.tsx.
    • utils/pressable.ts: the press marker, split out of the controller module that holds module state.
    • scripts/sync-exports.js generates the condition.
    • pressableCoverage.test.ts recognizes a server shell's marker.
    • Tests and a changeset.
  • Tagalongs: none. The Text/Heading fix (commit 2) keeps the first commit from remounting the element when maxLines toggles.

Authority

No current record governs RSC rendering; #823 is the design this follows (the react-server conditional export, server-safe modules where nothing reads context). Every component keeps its current contract: Button.spec.md FR1 to FR17 and AR1 to AR6, family:buttons, family:navigation-destinations, architecture:component-size-cascade INV3 to INV5, and the public API (architecture:public-component-api INV1, INV9, INV12). The client parts exist so that a server-rendered component meets those rows.

Review receipt: global baselines from node scripts/review-global-baselines.mjs, authority b7ea471a8fc7704cc5ad256f2c4d749962a433f5, head 1f33e4a371ab4160e70fc498be7f0cbbc2ac4a35, triggers behavior, public-api and react-runtime.

Record Trigger Claims matched Applies to this delta
architecture:public-component-api public-api INV1-INV9, INV12, INV16 Yes. Same exports through both entries; no prop, type or default change; the shells' helpers are unreachable from any export (INV1, INV9, INV12).
architecture:react-component-runtime react-runtime INV1-INV6, INV8, INV10, INV11 Yes. The gates hold no state; the press controller keeps its single shared install (INV5); the lazy hand-over is covered under Known limitations (INV1).
architecture:internationalization behavior, public-api, react-runtime INV1, INV1a, INV2-INV5, INV7-INV13 Yes, for the default TopNav label. It is still translated on the client by TopNavLocalizedFrame and follows the provider (INV1a, INV3, INV4).
design:bidirectionality react-runtime DR9 Yes. The markup is unchanged, so no direction flash.
architecture:container-padding public-api INV3, INV7, INV9-INV11 Unchanged. The ghost Button keeps its edge-compensation marker on the server.
spec:AST-002 public-api DEC-1-3, DEC-6-8, FR4, FR15 No public input change.
spec:AST-055, spec:AST-056, spec:AST-057, spec:AST-058, spec:AST-059 behavior, public-api listed claims Not touched: triggers, search, swipe actions, option actions and layer insets.

Direct owners resolved first: component:Button (Button.spec.md), component:IconButton, family:buttons, family:navigation-destinations and architecture:component-size-cascade (inherited by Button). No knowledge record is created or changed.

Change and evidence

Client parts and why each exists

Every client part below is a 'use client' module the server graph references. serverSafeComponents.test.ts fails if a shell reaches any client module not in __tests__/serverEntryClientParts.ts, or reaches client code without one.

Button and IconButton (IconButton is Button with isIconOnly, the same composition as IconButton.tsx)

Client part Loaded when Why it needs the client (source)
Button.tsx (the client Button) clickAction useTransition plus an in-flight ref guard, for fire-once dedupe: Button.tsx:334, :339, :405
isLoading Spinner ('use client', :503); the live region holds its text for one frame with useState/useEffect (:346, :349); the localized loading string comes from useTranslator (:329)
tooltip useTooltip attaches triggers to the element (:400, :592)
ref, an event handler, any other function prop, or a component as functions and refs cannot be rendered on the server (useMergedRefs :536, handleClick :405, handleKeyDown :437)
inside a size provider, ButtonGroup, InputGroup or (for a link) LinkProvider, lazily via the gate useSize :330, useButtonGroup :331, useInputGroup :332, useLinkComponent :379
ButtonContextGate.tsx every server-rendered Button reads those four contexts (ButtonContextGate.tsx:53 to :56) and installs the document touch press controller (:52; Button.tsx:328, FR15)
IconSizeScope.tsx icon is set the icon slot's default size is a context provider (FR3; IconSizeScope.tsx:31); it adds no DOM

There is no ripple. Press feedback is the shared document-level controller; the server markup carries its data-astryx-pressable marker, and the gate installs it.

TopNav and TopNavItem

Client part Loaded when Why it needs the client (source)
TopNavFrame.tsx (TopNavFrame) every server-rendered TopNav AppShell tells each copy of topNav its mode through context (AppShell.tsx:720, :922; TopNav.tsx:111). Desktop: the landmark around the server-rendered bar. Mobile bar: TopNavMobileBar at once, reading useTopNavMobileContent and useAppShellMobile (TopNavFrame.tsx:71, :72), with the toggle AppShell has already loaded. Drawer: the client TopNav, lazily (:37)
TopNavFrame.tsx (TopNavSlotScope) every slot section the slot context the menus read (TopNav.tsx:197 to :216; TopNavMenu.tsx:364, TopNavMegaMenu.tsx:415); it adds no DOM
TopNavLocalizedFrame.tsx a TopNav with no label the default label is localized (useTranslator, TopNav.tsx:109). A labelled TopNav never loads the message catalog
TopNav.tsx (the client TopNav) the drawer (lazily, via the frame); a ref or a function prop MobileNav :155, Divider :163, drawer content from context :112
TopNavItemGate.tsx every server-rendered TopNavItem reads the render mode and LinkProvider (TopNavItemGate.tsx:51, :52; TopNavItem.tsx:141, :142), installs the touch press controller (:50; TopNavItem.tsx:140)
TopNavItem.tsx (the client item) the drawer or a LinkProvider (lazily, via the gate); onClick, a ref or a component as the drawer item closes the drawer (closeMobileNav, TopNavItem.tsx:165); handlers (:211)

TopNavHeading, TopNavMenu, TopNavMegaMenu and their items stay client modules, re-exported unchanged from the server entry.

Text, Heading and Kbd (no react-server entry: they read no context, so the same module serves both)

Client part Loaded when Why
Text/TruncatedText.tsx an explicit maxLines with the tooltip on (Text.tsx:306, Heading.tsx:303) measures overflow (useTruncation, TruncatedText.tsx:62) and lazily shows the full-text tooltip (:33). At maxLines={0} it measures nothing, so toggling keeps the element mounted
Kbd/PlatformKbd.tsx the shortcut contains mod (Kbd.tsx:43) picks ⌘ or Ctrl after hydration (useSyncExternalStore, PlatformKbd.tsx:34); server and hydration markup agree

Bundle

esbuild over the built package (minified, React and StyleX external): the JavaScript that loads eagerly for what a Server Component references. The client reference today is the 'use client' entry; after this change it is the client part.

What a Server Component renders Before (gzip) After (gzip)
A Button with data props 37.4 KB (client Button) 3.4 KB (ButtonContextGate)
... with an icon 37.4 KB 3.6 KB (+ IconSizeScope)
A labelled TopNav with TopNavItems 57.0 KB (the TopNav barrel) 5.7 KB (TopNavFrame + TopNavItemGate)
A header: labelled TopNav, items, a Button link 58.1 KB 6.0 KB
The same header, TopNav without label 58.1 KB 22.8 KB: TopNavLocalizedFrame brings the message catalog, which any other localized client component on the page already loads

Client imports cost a little more, because the shared modules are now separate files: the Button entry goes from 37.4 KB to 37.7 KB and the TopNav entry from 57.0 KB to 57.6 KB.

The lazy client components load only on the paths in the tables above.

Tests

  • serverRender.test.ts (new): renders under the react-server condition.
    • It loads React's react-server build and swaps every 'use client' module for a client reference, as an RSC bundler does.
    • react-dom/server refuses to load under that condition, so a small resolver renders the tree instead. Under the server build any hook call throws, and the test asserts that useState, useContext and createContext are absent.
    • Presentational Buttons, IconButtons, TopNav bars and items render with no hook call and reference only their gates. Interactive props reference the client component.
    • Every prop crossing into a client part is serializable. A Server Action is accepted; an event handler is rejected, as React Flight rejects it for the client Button today.
    • Text, Heading and Kbd reference no client code unless truncation or mod needs it.
  • serverSafeComponents.test.ts (extended). For each react-server entry:
    • the condition comes first and points at index.server.ts;
    • it exports the same names as index.ts;
    • its server graph reaches no client React API, react-dom or module state;
    • it reaches exactly the client parts listed, each with its reason.
  • ButtonShell.test.tsx and TopNavShell.test.tsx (new): markup parity with the client components.
    • Static markup is identical for 25 Button prop shapes (variants, sizes, icon and icon-only, end content, children vs label, widths, elevation, disabled, form attributes, links, rejected destinations, caller DOM props) and for IconButton.
    • It is also identical inside a horizontal and a disabled vertical ButtonGroup, a size provider, a null size provider, an InputGroup and a LinkProvider.
    • The TopNav bar matches in five header shapes; in AppShell's mobile bar and drawer; with a localized default label; and for TopNavItem, including the drawer and a LinkProvider.
    • The mobile bar shows on the first commit, before any chunk loads, and the drawer never shows a horizontal item.
  • Text/TruncatedText.test.tsx (new): Text and Heading keep their element mounted when maxLines toggles.
  • Mutation-checked. Each new invariant fails when its rule is broken: a hook added to a shell, a gate that never hands over, a frame that ignores the mode, a missing press marker, and a presentational Button routed to the client.
  • pressableCoverage.test.ts: a server shell counts as marked when it spreads pressableProps and renders a gate that calls usePressFeedback().

Public exports

  • dist/index.d.ts, dist/Button/index.d.ts, dist/IconButton/index.d.ts and dist/TopNav/index.d.ts are byte-identical before and after.
  • ButtonSize and ButtonVariant moved to buttonStyles.stylex.ts and are re-exported from Button.tsx. A type-equality probe against the previous build passes for ButtonSize, keyof ButtonProps, and a ButtonVariantMap module augmentation.
  • Each index.server.ts exports the same names as its index.ts (tested). The shells' helpers are not reachable from any package export.

Known limitations

  • Soft navigation inside a provider. On a client render (a soft navigation, not a server-rendered page) of a server-rendered Button inside a provider, the server markup shows until the client Button chunk arrives. On server-rendered pages, hydration keeps the HTML, which was rendered with context.
  • Remount when maxLines toggles through undefined. Toggling maxLines between a number and undefined on Text or Heading (rather than 0) still swaps the client part for the plain element, and so remounts it. So does toggling hasTruncateTooltip. An explicit maxLines={0} in a Server Component loads the truncation part, which measures nothing at 0.
  • Toggle may lag one frame. On AppShell's first switch to the mobile bar, the drawer toggle may appear one frame after the bar: it is lazily referenced so non-AppShell pages never load it, and AppShell has already loaded its module.
  • withAstryx() source builds. withAstryx() aliases every @astryxdesign/core/* request to its source entry for every webpack layer, so its source builds still resolve the client components in Server Components (today's behavior). Apps on the published package resolve the react-server entry through standard conditions. See follow-ups.

Follow-ups (not in this PR)

  • Avatar and the Link variants, then the remaining Tier 2 components from feat: RSC-native components via react-server conditional exports #823.
  • TopNavHeading, TopNavMenu and TopNavMegaMenu server shells, where their content is static.
  • Layer-aware aliasing in withAstryx() (issuerLayer rules for the React Server layers), so source builds resolve the react-server entries, with a build test.
  • A react-server entry for the package root (@astryxdesign/core), whose barrel is 'use client'.
  • A React Flight round-trip test once a Flight server is available to the test project.
  • Consumer docs for using these components from Server Components.

Scope

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

Testing

Commits (each passes its targeted suites, tsc and sync-exports --check on its own):

  1. perf(core): render Text, Heading and Kbd on the server; truncation and the platform glyph become client parts
  2. fix(core): keep Text and Heading mounted when truncation toggles
  3. perf(core): render Button and IconButton in React Server Components
  4. perf(core): render the TopNav desktop bar in React Server Components
  5. test(core): prove the react-server entries render on the server

Results at the head commit:

  • pnpm test:ui: 599 of 600 files and 14,828 tests pass. The one failure, DropdownMenuLazySheet.test.tsx, is a hook timeout under full parallel load; it passes alone.
  • Targeted core suites: 20 files, 675 tests. These are Text, Heading, Kbd, Button, IconButton, TopNav, ButtonGroup, AppShell, serverRender, serverSafeComponents and pressableCoverage, plus the new ButtonShell, TopNavShell and TruncatedText tests.
  • pnpm test:node:
    • Under full parallel load on this machine, many CLI suites time out.
    • Rerun with --maxWorkers=4, 65 of 71 of those files pass.
    • The remaining 6 files (8 tests) shell out to npm pack or a fixture pnpm install. They fail identically on main at b7ea471a8 on this machine (setup-workspace, theme-checks, pack-check.lifecycle-output, integration-real-world, release/cache, global-local).
    • packages/cli/api/render/render.test.mjs (render --reverse maps the button target to Button) passes.
  • pnpm lint:strict: 0 errors. The warnings are pre-existing, and none is in a changed file.
  • pnpm -F @astryxdesign/core typecheck: passes (tsc and the docs typecheck).
  • pnpm -F @astryxdesign/core build: passes. dist/*/index.d.ts is unchanged, and dist/*/index.server.js is emitted for the three entries.
  • node scripts/sync-exports.js --check and pnpm check:repo: pass.
  • Repository pre-commit hooks: pass.
  • Changeset: .changeset/server-render-header-shell.md ([perf], patch).
  • Independent review: two rounds of adversarial review. Every finding is fixed, or listed above under Known limitations or Follow-ups.

…d the platform glyph become client parts

Text and Heading carried 'use client' only for the truncation tooltip, which
measures overflow when maxLines > 0. That moves into a TruncatedText client
part, rendered only when maxLines > 0 with the tooltip on. Kbd's only
client-side work is picking the 'mod' glyph after hydration; shortcuts with
'mod' render a PlatformKbd client part, all others render on the server.
Public APIs are unchanged; both client parts are listed as client islands in
serverSafeComponents.test.ts.
Text and Heading rendered the TruncatedText client part only for
maxLines > 0, so a caller toggling maxLines between a number and 0
(show more / less) switched the root element type and remounted the
element and its children. Any explicit maxLines now takes the client
part, which measures nothing at 0, so the element stays mounted; Text
and Heading without maxLines still render on the server with no client
code. KbdView's comment now says where platform detection happens.
`@astryxdesign/core/Button` and `/IconButton` gain a `react-server` export
condition (first, so a bundler that also honors `source` picks it, and
generated by scripts/sync-exports.js for any directory with an
index.server.ts). It resolves a server shell, ButtonShell.tsx, which
imports no client React API.

A Button whose props are data renders on the server: the root element,
classes, tokens, theme attributes and content the client Button renders
for those props. Both renderers share buttonStyles.stylex.ts and
ButtonContent.tsx. The client Button loads only for what needs it:
- clickAction, isLoading, tooltip, a ref or any function prop render the
  client Button directly;
- every server-rendered Button sits in ButtonContextGate, which reads the
  size, ButtonGroup, InputGroup and LinkProvider contexts a Server
  Component cannot, hands over to a lazily loaded client Button when one
  applies, and installs the shared touch press controller;
- an icon in the slot is sized through IconSizeScope.

pressable.ts holds the press marker without the controller's module
state, and pressableCoverage.test.ts counts a server shell that spreads
the marker and renders a marked gate as marked. Client imports and the
public API are unchanged.
`@astryxdesign/core/TopNav` gains a `react-server` entry. TopNav and
TopNavItem resolve server shells (TopNavShell.tsx, TopNavItemShell.tsx)
that render the desktop bar and its items on the server with the markup
the client components render in their default mode; the heading, menus,
mega menu and render context stay client modules, as through index.ts.

AppShell renders its `topNav` three times and tells each copy its mode
through context, which a Server Component cannot read, so the shells sit
in small client parts:
- TopNavFrame renders the landmark, the mobile bar at once
  (TopNavMobileBar, shared with TopNav.tsx; AppShell has already loaded
  the drawer toggle) and, in the drawer, a lazily loaded client TopNav;
- TopNavLocalizedFrame translates the default label, only for a TopNav
  without one, so a labelled TopNav loads no message catalog;
- TopNavSlotScope sets the slot context menus read;
- TopNavItemGate hands an item to a lazily loaded client TopNavItem in
  the drawer or under a LinkProvider, and installs the touch press
  controller.

A ref or a function prop renders the client component. Client imports
and the public API are unchanged.
serverSafeComponents.test.ts now checks every `react-server` entry: the
condition comes first and points at index.server.ts, the entry exports
the same names as index.ts, and its server graph runs no client React
API and reaches exactly the client parts __tests__/serverEntryClientParts.ts
lists, with the reason for each.

serverRender.test.ts renders the entries under React's `react-server`
build, with every `'use client'` module swapped for a client reference
as an RSC bundler does: presentational Buttons, IconButtons, TopNav bars
and items render with no hook call and reference only their gates;
interactive props reference the client component; props crossing into a
client part are serializable; Text, Heading and Kbd render with no client
reference unless truncation or the `mod` key needs one.
@vercel

vercel Bot commented Oct 25, 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 25, 2026 7:04pm UTC

Request Review

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

astracat-bot Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
❓ Needs maintainer input (for maintainers only) Oct 30, 2026, 4:42 PM UTC

@github-actions github-actions Bot added community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge needs:design-review Affects visuals — Design should review labels Oct 25, 2026

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

This change waits on an engineering-owner decision: no current record governs React Server Component entry points or 'use client' placement (architecture:component-style-authoring leaves them to a future RSC/module-boundary record), so an engineering owner needs to provide or identify that spec, or record the decision by commenting /approve-spec <full head SHA> on this pull request, which the author may do if they are an engineering owner. Whatever that decision, these changes are still needed:

  • Split the 4,759-line change by component family (Text/Heading/Kbd, Button/IconButton, TopNav), each part with its own tests.
  • Inside a ButtonGroup, size provider or LinkProvider, the first client render shows the provider-free markup until the client Button loads (ButtonContextGate.tsx:78), so a disabled group's link stays navigable; render that fallback with the provider's state and add a first-commit test.
  • Rebase onto main, where Kbd now resolves its labels through useTranslator(), and either keep Kbd a client module or give its labels a client part, since a directive-free Kbd cannot read the translator.

[Automated review]

// and hydration keeps it. On a client render (a soft navigation) it shows
// standalone styling until the chunk arrives, which beats a missing Button.
return (
<Suspense fallback={children}>

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.

Once isProvided is true, this fallback is the provider-free server element. On a client render before the chunk loads, a member of a disabled ButtonGroup commits as an enabled <a href> or submit <button>, and a member under SizeProvider value="sm" as data-size="md" (Button FR10, FR4). Rendering the fallback from getButtonRootProps/ButtonContent with the group's disabled state and the inherited size the gate has already read would close that window.

* accessible name for the shortcut is built from these words instead.
*/
const KEY_LABEL = new Map<string, string>([
['ctrl', 'Control'],

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.

On main these labels are catalog keys resolved by useTranslator() (#7849, architecture:internationalization INV1a). A directive-free Kbd cannot read the translator (INV13), so on rebase this split either drops that localization or Kbd has to stay a client module, or move its labels into a client part.

@github-actions

Copy link
Copy Markdown
Contributor

PR Analysis Report

Current run: The current CI run concluded failure. Current analysis is unavailable.


Generated by PR Enrichment workflow | View current CI run

This branch was successfully deployed

1 active deployment
Preview — 1f33e4a3 Deployed Oct 25, 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. community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge needs:design-review Affects visuals — Design should review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant