Repository navigation
fix(layer): host layers outside the ancestors that break them - #5039
Conversation
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`.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
PR Analysis Report📚 Storybook PreviewView Storybook for this PR 🧪 Sandbox PreviewView Sandbox for this PR Modified ComponentsButton (@astryxdesign/core) · View in Storybook
ButtonGroup (@astryxdesign/core) · View in Storybook
HoverCard (@astryxdesign/core) · View in Storybook
InputGroup (@astryxdesign/core) · View in Storybook
Layer (@astryxdesign/core) · View in Storybook
Timestamp (@astryxdesign/core) · View in Storybook
Toolbar (@astryxdesign/core) · View in Storybook
Bundle Size Summary
Accessibility AuditStatus: 2 accessibility violation(s) found — 1 serious, 1 moderate. Timestamp - 1 issue(s)
Toolbar - 1 issue(s)
Generated by PR Enrichment workflow | Storybook | Sandbox | View full report |
… 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.
|
Could we adjust the hosting approach in two ways?
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 I’d add focused coverage for React SSR/hydration with |
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
useLayernow 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:
showPopover().The new
lazyMountoption defers host discovery and content mounting untilshow(). 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.directionandwriting-modeare copied only when the unsafe ancestor chain changes them; otherwise they keep inheriting live.showPopover()receives the trigger as its invokersource.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
useLayerconsumers.lazyMount?: booleancontext-layer option.LayerProviderbehavior is unchanged.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