An open source design system that's fully customizable and agent ready
TypeScript
fix(Layout): collapse header/footer padding with sibling markers, not :has() followed by * (#8417)
Cindy Zhang28317b0545b1
diff --git a/.changeset/layout-edge-padding-no-has-universal.md b/.changeset/layout-edge-padding-no-has-universal.mdnew file mode 100644index 0000000000..6b22d28fc0--- /dev/null+++ b/.changeset/layout-edge-padding-no-has-universal.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] `Layout` no longer ships a `:has()` on the layout followed by a universal `*` selector for the padding that collapses next to a `LayoutHeader` or `LayoutFooter` without a divider. That selector shape can make a browser restyle everything inside the layout on each change within it, which costs the most in long documents. `LayoutHeader` and `LayoutFooter` now carry internal markers, and Layout's middle row reads them with sibling selectors. Rendering is unchanged for a Layout's own content, and a Layout nested in another Layout's content or panels still inherits the outer collapse. One case changes: a Layout nested inside a `LayoutHeader` or `LayoutFooter` that has no divider no longer takes that outer collapse, so its content keeps its own inner padding beside its own divider (16px at the default spacing) instead of 0px.++@cixzhangdiff --git a/packages/core/src/Layout/Layout.tsx b/packages/core/src/Layout/Layout.tsxindex c30c23f30e..ebd52dd7f7 100644--- a/packages/core/src/Layout/Layout.tsx+++ b/packages/core/src/Layout/Layout.tsx@@ -25,6 +25,7 @@ import {LayoutDividerContext} from './LayoutDividerContext';import {stack} from '../Stack/stack.stylex';import {stackItem} from '../Stack/stackItem.stylex';import {mergeProps} from '../utils';+import {layoutFooterMarker, layoutHeaderMarker} from './layoutEdges.stylex';import type {BaseProps} from '../BaseProps';import type {SizeValue, SpacingStep} from '../utils/types';import {themeProps} from '../utils/themeProps';@@ -101,6 +102,23 @@ const styles = stylex.create({middle: {flex: 1,minHeight: 0,+ // A LayoutHeader without a divider right before this row, or a+ // LayoutFooter without one right after it, removes the matching inner+ // padding of LayoutContent (it reads these, falling back to its padding).+ // Sibling relations on this row only: a `:has()` on the layout followed+ // by `*` restyles everything inside it on any change. Unset otherwise, so+ // a nested Layout's content inherits an outer row's value, as the+ // ancestor rule this replaces matched any enclosing layout.+ '--_layout-flush-block-start': {+ default: null,+ [stylex.when.siblingBefore(':not([data-divider])', layoutHeaderMarker)]:+ '0px',+ },+ '--_layout-flush-block-end': {+ default: null,+ [stylex.when.siblingAfter(':not([data-divider])', layoutFooterMarker)]:+ '0px',+ },},middleQuery: {containerType: {diff --git a/packages/core/src/Layout/LayoutContent.tsx b/packages/core/src/Layout/LayoutContent.tsxindex 68f5ffc87b..7f901854fd 100644--- a/packages/core/src/Layout/LayoutContent.tsx+++ b/packages/core/src/Layout/LayoutContent.tsx@@ -41,20 +41,11 @@ const styles = stylex.create({// Default: inner padding on all sides (will be overridden by position-specific styles)paddingInlineStart: `var(--layout-padding-inner-x, ${spacingVars['--spacing-4']})`,paddingInlineEnd: `var(--layout-padding-inner-x, ${spacingVars['--spacing-4']})`,- paddingBlockStart: {- default: `var(--layout-padding-inner-y, ${spacingVars['--spacing-4']})`,- // When header has no divider, collapse top padding for seamless visual flow- [stylex.when.ancestor(- ':has(> .astryx-layout-header:not([data-divider]))',- )]: 0,- },- paddingBlockEnd: {- default: `var(--layout-padding-inner-y, ${spacingVars['--spacing-4']})`,- // When footer has no divider, collapse bottom padding for seamless visual flow- [stylex.when.ancestor(- ':has(> .astryx-layout-footer:not([data-divider]))',- )]: 0,- },+ // A header or footer without a divider collapses the adjacent padding+ // for a seamless visual flow: Layout's middle row sets these to 0px then+ // (see Layout.tsx `middle`).+ paddingBlockStart: `var(--_layout-flush-block-start, var(--layout-padding-inner-y, ${spacingVars['--spacing-4']}))`,+ paddingBlockEnd: `var(--_layout-flush-block-end, var(--layout-padding-inner-y, ${spacingVars['--spacing-4']}))`,// Publish container padding vars for bleed children (Table, Divider, etc.)'--container-padding-inline-start': `var(--layout-padding-inner-x, ${spacingVars['--spacing-4']})`,'--container-padding-inline-end': `var(--layout-padding-inner-x, ${spacingVars['--spacing-4']})`,diff --git a/packages/core/src/Layout/LayoutFooter.tsx b/packages/core/src/Layout/LayoutFooter.tsxindex c0ddb20a94..666c8c6eaa 100644--- a/packages/core/src/Layout/LayoutFooter.tsx+++ b/packages/core/src/Layout/LayoutFooter.tsx@@ -19,6 +19,7 @@ import type {AriaRole, ReactNode} from 'react';import type {BaseProps} from '../BaseProps';import {use} from 'react';import * as stylex from '@stylexjs/stylex';+import {layoutFooterMarker} from './layoutEdges.stylex';import {LayoutDividerContext} from './LayoutDividerContext';import {colorVars, spacingVars} from '../theme/tokens.stylex';import {mergeProps} from '../utils';@@ -161,6 +162,7 @@ export function LayoutFooter({{...mergeProps(themeProps('layout-footer'),stylex.props(+ layoutFooterMarker,styles.footer,dynamicStyles.sizing(height ?? null),diff --git a/packages/core/src/Layout/LayoutHeader.tsx b/packages/core/src/Layout/LayoutHeader.tsxindex 8246f4e136..68913e1bfc 100644--- a/packages/core/src/Layout/LayoutHeader.tsx+++ b/packages/core/src/Layout/LayoutHeader.tsx@@ -20,6 +20,7 @@ import type {AriaRole, ReactNode} from 'react';import type {BaseProps} from '../BaseProps';import {use} from 'react';import * as stylex from '@stylexjs/stylex';+import {layoutHeaderMarker} from './layoutEdges.stylex';import {LayoutDividerContext} from './LayoutDividerContext';import {colorVars, spacingVars} from '../theme/tokens.stylex';import {mergeProps} from '../utils';@@ -173,6 +174,7 @@ export function LayoutHeader({{...mergeProps(themeProps('layout-header'),stylex.props(+ layoutHeaderMarker,styles.header,dynamicStyles.sizing(height ?? null),diff --git a/packages/core/src/Layout/layoutEdges.stylex.ts b/packages/core/src/Layout/layoutEdges.stylex.tsnew file mode 100644index 0000000000..eab2f3d971--- /dev/null+++ b/packages/core/src/Layout/layoutEdges.stylex.ts@@ -0,0 +1,25 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file layoutEdges.stylex.ts+ * @input StyleX markers+ * @output Exports layoutHeaderMarker and layoutFooterMarker+ * @position Internal markers LayoutHeader and LayoutFooter carry on their own+ * element, so Layout's middle row can tell whether a divider-less header+ * precedes it or a divider-less footer follows it with sibling relations+ * alone, instead of a `:has()` on the layout followed by `*`, which makes+ * engines restyle the whole layout on any change inside it+ *+ * SYNC: When modified, update:+ * - /packages/core/src/Layout/Layout.tsx+ * - /packages/core/src/Layout/LayoutHeader.tsx+ * - /packages/core/src/Layout/LayoutFooter.tsx+ * - /packages/core/src/Layout/LayoutContent.tsx+ */++import * as stylex from '@stylexjs/stylex';++export const layoutHeaderMarker: ReturnType<typeof stylex.defineMarker> =+ stylex.defineMarker();+export const layoutFooterMarker: ReturnType<typeof stylex.defineMarker> =+ stylex.defineMarker();diff --git a/packages/core/src/theme/derivedVarRegistry.test.ts b/packages/core/src/theme/derivedVarRegistry.test.tsindex 681fddf513..5c012ed463 100644--- a/packages/core/src/theme/derivedVarRegistry.test.ts+++ b/packages/core/src/theme/derivedVarRegistry.test.ts@@ -91,6 +91,11 @@ const STRUCTURAL_VARS = new Set([// Private counterpart of the public token above: one ancestor Section's// padding, propagated down the tree. Structural, never authored by a theme.'--_section-padding-propagated',+ // Layout's middle row tells LayoutContent that a divider-less header or+ // footer sits beside it (Layout/layoutEdges.stylex.ts). Structural, never+ // authored by a theme.+ '--_layout-flush-block-start',+ '--_layout-flush-block-end',]);/**diff --git a/scripts/build-css.test.mjs b/scripts/build-css.test.mjsindex cc45dee66c..57f3db2ac4 100644--- a/scripts/build-css.test.mjs+++ b/scripts/build-css.test.mjs@@ -324,4 +324,18 @@ describe('build-css astryx.css', () => {);});});++ describe('Layout header/footer padding collapse (Layout/layoutEdges.stylex.ts)', () => {+ it('ships no :has() on the layout followed by a descendant * for the header or footer', () => {+ // `.anchor:has(…) *` makes engines restyle everything inside the layout+ // on any change in it; the collapse uses sibling markers instead.+ const costly = astryxCss+ .split('\n')+ .filter(+ line =>+ /astryx-layout-(header|footer)/.test(line) && /:has\(/.test(line),+ );+ expect(costly).toEqual([]);+ });+ });});