An open source design system that's fully customizable and agent ready
TypeScript
fix(theme): route physical block padding through the container expansion (#5244)
Cindy Zhanga7ba3a09aef6
diff --git a/.changeset/physical-block-padding-container-expansion.md b/.changeset/physical-block-padding-container-expansion.mdnew file mode 100644index 0000000000..d9caa1bced--- /dev/null+++ b/.changeset/physical-block-padding-container-expansion.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Theming: a physical `paddingTop`/`paddingBottom` now reaches the container padding expansion, so `card`, `dialog`, `section` and `number-input` track it the way they already track the logical spellings. A physical block longhand matched none of the padding property names the expansion recognizes, so it landed raw on the element while the component's internals kept reading the default — the NumberInput stepper column came up ~10px short of the field edges, and container bleed compensated by the wrong amount. Mixing spellings was worse than either alone: `padding: '10px'` plus `paddingTop: '14px'` published 10px in the tokens while the element painted 14px on top. `padding-top` and `padding-bottom` ARE the block edges in every horizontal writing mode, so this normalization assumes no direction. `paddingLeft`/`paddingRight` are deliberately unchanged: they are direction-relative — left is inline-start in LTR and inline-end in RTL — and the tokens are consumed by logical properties, so routing them would silently move the padding to the opposite edge under RTL. They keep their physical meaning, exactly as before.++@cixzhangdiff --git a/packages/core/src/NumberInput/NumberInput.test.tsx b/packages/core/src/NumberInput/NumberInput.test.tsxindex b09a6e4441..164b29a8f6 100644--- a/packages/core/src/NumberInput/NumberInput.test.tsx+++ b/packages/core/src/NumberInput/NumberInput.test.tsx@@ -1842,6 +1842,17 @@ describe('NumberInput stepper padding coupling', () => {'--astryx-number-input-padding-block-end: 12px',],],+ [+ // `paddingTop` is `paddingBlockStart` in every horizontal writing+ // mode, so it normalizes onto the same tokens. The physical inline+ // pair is deliberately not mapped — see generateThemeRules.+ 'the physical block longhands',+ {paddingTop: '14px', paddingBottom: '6px'},+ [+ '--astryx-number-input-padding-block-start: 14px',+ '--astryx-number-input-padding-block-end: 6px',+ ],+ ],])('%s', (_label, base, expected) => {const theme = defineTheme({name: 'number-input-padding-spelling-test',@@ -1853,7 +1864,9 @@ describe('NumberInput stepper padding coupling', () => {}// The expansion consumes the padding: a raw declaration left on the// wrapper is padding the column cannot see, which is the gap itself.- expect(css).not.toMatch(/^\s*padding(-block|-inline)?(-start|-end)?:/m);+ expect(css).not.toMatch(+ /^\s*padding(-block|-inline|-top|-bottom)?(-start|-end)?:/m,+ );});});diff --git a/packages/core/src/theme/generateThemeRules.test.ts b/packages/core/src/theme/generateThemeRules.test.tsindex 091bc3897d..a21bb2e564 100644--- a/packages/core/src/theme/generateThemeRules.test.ts+++ b/packages/core/src/theme/generateThemeRules.test.ts@@ -584,3 +584,82 @@ describe('brutalist-style derived expansion', () => {expect(rule).toContain('--_dropdown-menu-padding: 4px');});});++describe('physical padding longhands', () => {+ const ruleFor = (+ component: string,+ base: Record<string, string>,+ name = 'test-physical-padding',+ ) =>+ generateThemeRules(+ defineTheme({name, components: {[component]: {base}}}),+ ).find(r => r.includes(`.astryx-${component}`));++ // `padding-top`/`padding-bottom` ARE the block edges in every horizontal+ // writing mode, so the expansion can normalize them with no direction+ // assumption. Without that, the padding lands raw on the element and the+ // component's internals — the NumberInput stepper column, container bleed —+ // read the default instead of what the theme set.+ it.each([+ ['paddingTop', {paddingTop: '14px'}, 'block-start'],+ ['paddingBottom', {paddingBottom: '14px'}, 'block-end'],+ ])('routes %s through the container expansion', (_label, base, edge) => {+ const rule = ruleFor('card', base);+ expect(rule).toContain(`--astryx-card-padding-${edge}: 14px`);+ expect(rule).not.toMatch(/[{;]\s*padding-(top|bottom):/);+ });++ it('normalizes both block edges together, asymmetrically', () => {+ const rule = ruleFor('number-input', {+ paddingTop: '14px',+ paddingBottom: '6px',+ });+ expect(rule).toContain('--astryx-number-input-padding-block-start: 14px');+ expect(rule).toContain('--astryx-number-input-padding-block-end: 6px');+ });++ it.each(['card', 'dialog', 'section', 'number-input'])(+ 'reaches %s, which expands its padding',+ component => {+ expect(ruleFor(component, {paddingTop: '14px'})).toContain(+ `--astryx-${component}-padding-block-start: 14px`,+ );+ },+ );++ // THE RTL GUARD. `paddingLeft` is inline-start in LTR and inline-end in RTL,+ // and the tokens are consumed by logical properties, so mapping it would+ // silently move the padding to the other edge in RTL. It stays physical —+ // which is what the author wrote — and does not reach the tokens.+ it('leaves the direction-relative inline pair physical', () => {+ const rule = ruleFor('card', {paddingLeft: '20px', paddingRight: '8px'});+ expect(rule).toContain('padding-left: 20px');+ expect(rule).toContain('padding-right: 8px');+ expect(rule).not.toContain('--astryx-card-padding-inline');+ });++ it('expands the block edges while leaving left physical', () => {+ const rule = ruleFor('card', {paddingTop: '14px', paddingLeft: '20px'});+ expect(rule).toContain('--astryx-card-padding-block-start: 14px');+ expect(rule).toContain('padding-left: 20px');+ });++ // The shorthand-plus-override case was the worst one: the tokens carried+ // 10px while the element painted 14px on top, so every internal compensated+ // by the wrong amount.+ it('lets a physical longhand override the shorthand per edge', () => {+ const rule = ruleFor('card', {padding: '10px', paddingTop: '14px'});+ expect(rule).toContain('--astryx-card-padding-block-start: 14px');+ expect(rule).toContain('--astryx-card-padding-block-end: 10px');+ expect(rule).toContain('--astryx-card-padding-inline: 10px');+ expect(rule).not.toMatch(/[{;]\s*padding(-top)?:/);+ });++ // A `vars` entry carries one value for the whole box, so a single physical+ // edge must not feed it — only the container expansion takes these.+ it('does not feed a single edge to a whole-box derived var', () => {+ const rule = ruleFor('dropdown-menu', {paddingTop: '14px'});+ expect(rule).toContain('padding-top: 14px');+ expect(rule).not.toContain('--_dropdown-menu-padding');+ });+});diff --git a/packages/core/src/theme/generateThemeRules.ts b/packages/core/src/theme/generateThemeRules.tsindex f48f317902..892b16c352 100644--- a/packages/core/src/theme/generateThemeRules.ts+++ b/packages/core/src/theme/generateThemeRules.ts@@ -128,6 +128,34 @@ const PADDING_PROPS = new Set(['paddingInlineEnd',]);+/**+ * Physical block-axis longhands, and the logical longhand each one *is* in+ * every horizontal writing mode. Normalizing them costs no direction+ * assumption, which is why they can join the container expansion.+ *+ * `paddingLeft`/`paddingRight` are deliberately absent. They are+ * direction-relative — left is inline-start in LTR and inline-end in RTL — and+ * the expansion's tokens are consumed by logical properties, so mapping them+ * would put the padding on the opposite edge in RTL. They keep their physical+ * meaning and land on the element as `padding-left`/`padding-right`; the cost+ * is that a component's internals cannot see them.+ */+const PHYSICAL_BLOCK_PADDING_PROPS: Record<string, string> = {+ paddingTop: 'paddingBlockStart',+ paddingBottom: 'paddingBlockEnd',+};++/**+ * Every padding spelling the container expansion consumes. Kept separate from+ * PADDING_PROPS, which also routes longhands to `vars`-style derived entries —+ * those carry one value for the whole box, so a single physical edge must not+ * reach them.+ */+const CONTAINER_PADDING_PROPS = new Set([+ ...PADDING_PROPS,+ ...Object.keys(PHYSICAL_BLOCK_PADDING_PROPS),+]);+interface ParsedPadding {blockStart?: string;blockEnd?: string;@@ -138,12 +166,14 @@ interface ParsedPadding {/*** Parse CSS padding shorthand/longhand into block/inline values.- * Supports 1-3 value shorthands and logical properties.+ * Supports 1-3 value shorthands, logical properties, and the physical block+ * longhands normalized by PHYSICAL_BLOCK_PADDING_PROPS.*/function parsePadding(props: [string, string][]): ParsedPadding {const result: ParsedPadding = {};- for (const [prop, value] of props) {+ for (const [rawProp, value] of props) {+ const prop = PHYSICAL_BLOCK_PADDING_PROPS[rawProp] ?? rawProp;switch (prop) {case 'padding': {const parts = value.trim().split(/\s+/);@@ -377,13 +407,28 @@ function generateComponentRules(}}}+ // A physical block longhand reaches the container expansion only. It+ // names one edge, so it must not feed a `vars` entry above, which+ // carries the padding for the whole box.+ if (+ prop in PHYSICAL_BLOCK_PADDING_PROPS &&+ getDerivedVars(component, 'padding').some(+ e => e.expand === 'container',+ )+ ) {+ containerExpanded = true;+ }}// Container padding expansion: replace padding props with// component-scoped container tokens for layout integration.if (containerExpanded) {- const paddingProps = props.filter(([p]) => PADDING_PROPS.has(p));- const nonPaddingProps = props.filter(([p]) => !PADDING_PROPS.has(p));+ const paddingProps = props.filter(([p]) =>+ CONTAINER_PADDING_PROPS.has(p),+ );+ const nonPaddingProps = props.filter(+ ([p]) => !CONTAINER_PADDING_PROPS.has(p),+ );const parsed = parsePadding(paddingProps);const containerTokens = expandContainerPadding(component, parsed);finalProps = [...nonPaddingProps, ...containerTokens];