Skip to content

fix(layer): host layers outside the ancestors that break them - #5039

Merged
imdreamrunner merged 6 commits into
mainfrom
fix/layer-inline-safe-content
Aug 15, 2026
Merged

imdreamrunner merged 6 commits into
mainfrom
fix/layer-inline-safe-content

Conversation

@cixzhang

@cixzhang cixzhang commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fix context layers—especially rich HoverCards—rendered inside HTML contexts such as <p>, headings, links, buttons, and structurally restricted containers.

Previously, block content could enter invalid server markup, be reparented by the HTML parser, and leave an empty or broken popover after hydration.

Approach

Context-mode useLayer now renders an inert <template> marker at the layer’s actual JSX position. The marker is identical during SSR and the first client render.

After mounting:

  • Safe positions render the popover inline beside the marker.
  • Unsafe positions portal it to the nearest safe ancestor outside the complete unsafe chain.
  • The marker remains mounted, without an ID, so the host can be recalculated if the render call moves.
  • Open layers are reopened exactly once when relocation replaces their DOM element.
  • Detached transitional elements are never passed to showPopover().

The new lazyMount option defers host discovery and content mounting until show(). HoverCard uses it, so a closed card renders only the inert marker and unmounts its content again after hiding.

Theme, direction, and focus

Corrective portals stay near the intended render position rather than using document.body.

  • CSS custom properties continue inheriting live from the nearby safe host.
  • direction and writing-mode are copied only when the unsafe ancestor chain changes them; otherwise they keep inheriting live.
  • showPopover() receives the trigger as its invoker source.
  • Existing ButtonGroup and InputGroup structural selectors ignore both popovers and inert layer markers.

A block context is still recommended for HoverCard. Paragraph, heading, and link placements are supported when necessary, but corrective portaling can change DOM and tab order.

Scope

Story

Core/Layer → Inline trigger hosting demonstrates a rich card triggered inside a paragraph, including its resolved host, typography, focus order, and scrolling behavior.

Validation

  • All GitHub checks pass.
  • 559 affected-consumer tests pass.
  • Full repository test suite passes in CI.
  • Core and Storybook production builds pass.
  • Typecheck, docs typecheck, lint, formatting, repository checks, accessibility, RTL, smoke, sandbox, and Vercel checks pass.

A context layer used to render where it was placed in the tree. Inline
triggers put it somewhere it cannot survive:

- In a `<p>`, the server markup carries the layer's content inside the
  paragraph, and the HTML parser reparents it out — the card opens empty
  and its content lands in the page as stray text.
- In an `<a>`, a link inside the card closes the wrapping link the same
  way, and every focusable in the card joins the link's tab stop and
  click target.
- Left inline, the layer also inherits the paragraph's typography: a card
  in 13px centered text renders 13px and centered.

Layers are now hosted on the client in the nearest ancestor of the
trigger that can hold them, and are not emitted in server markup at all,
so the parser never sees them and the first client render matches the
server. Hosting near the trigger rather than in `<body>` is what keeps
the theme cascade and the tab order; `show()` also passes the trigger as
the popover invoker `source`.
@vercel

vercel Bot commented Aug 14, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview Aug 15, 2026 6:45am

Request Review

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

github-actions Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

Modified Components

Button (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 534 -
Complexity N/A Very High (63) -
ButtonGroup (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 121 -
Complexity N/A Low (3) -
HoverCard (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 474 -
Complexity N/A Very High (52) -
InputGroup (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 270 -
Complexity N/A Medium (12) -
Layer (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 778 -
Complexity N/A Very High (87) -
Timestamp (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 671 -
Complexity N/A Very High (80) -
Toolbar (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 269 -
Complexity N/A High (17) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.7KB 1.2KB

Accessibility Audit

Status: 2 accessibility violation(s) found — 1 serious, 1 moderate.

Timestamp - 1 issue(s)
  • 🟠 serious: Ensure the contrast between foreground and background colors meets WCAG 2 AA minimum contrast ratio thresholds
    • Rule: color-contrast · Affects 1/19 stories · Learn more
    • WCAG: 1.4.3 (Level AA)
Toolbar - 1 issue(s)
  • 🟡 moderate: Ensure landmarks are unique
    • Rule: landmark-unique · Affects 1/13 stories · Learn more

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

github-actions Bot added a commit that referenced this pull request Aug 14, 2026
… in a story

The mount gate flips state in a layout effect, which trips
@eslint-react/set-state-in-effect and turned CI red.

Both cheaper signals were tried and both are wrong here:

- A ref cannot schedule a render, so a layer whose trigger never
  registers (useKeyboardHint at rest, TopNavMegaMenu outside a <nav>)
  never appears at all — 26 tests.
- useSyncExternalStore reports the client one render too early: it is
  already true on the first client render, before the trigger has
  resolved a host, so the layer renders inline in the paragraph for a
  frame and React logs the nesting error again (seen in Chrome 149).

The flip has to arrive as a render, one commit late. That is the
mechanism, so the rule is suppressed at the call with the reasoning.

The new Layer story hosts a card from an inline mention in a scrolling
article and reads back where the layer landed, plus the scroll offset
before and after every focus move — the browser's scroll-into-view does
not touch the scroll container for focus inside the top layer.
@imdreamrunner

Copy link
Copy Markdown
Contributor

Could we adjust the hosting approach in two ways?

  1. Resolve the location lazily when showing. Instead of resolving and mounting a portal after hydration for every context layer, show() could record the open intent, resolve the current location, mount the layer, then replay showPopover() after its ref attaches. Hiding would unmount it and clear the resolved location. This avoids work for layers that never open and prevents a stored host from becoming stale if the trigger moves.

  2. Use an empty <template> sentinel at the layer.render() call site. Trigger ancestry gives us a safe portal target, but it cannot tell us whether the intended inline location was already safe. An inert template rendered identically during SSR and hydration would expose the actual parentElement without mounting consumer content or affecting layout. When showing:

    • If the sentinel’s parent is safe, replace it with the layer inline.
    • If unsafe, portal the layer to the nearest safe ancestor.

This would preserve the existing DOM position, cascade, and natural tab order when possible, while using a portal only as a corrective fallback. The final layer should still honor as, and showPopover({source: trigger}) remains a useful backstop when portaling.

I’d add focused coverage for React SSR/hydration with <template>, safe inline placement, paragraph/link/table contexts, controlled/default-open behavior, and rapid show/hide before the layer commits.

@github-actions github-actions Bot added the needs:design-review Affects visuals — Design should review label Aug 15, 2026
@imdreamrunner
imdreamrunner enabled auto-merge (squash) August 15, 2026 06:47
@imdreamrunner
imdreamrunner merged commit 5db7f15 into main Aug 15, 2026
19 checks passed

This branch was successfully deployed

1 active deployment
Preview — d2425ca5 Deployed Aug 15, 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. needs:design-review Affects visuals — Design should review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants