An open source design system that's fully customizable and agent ready
TypeScript
fix(Selector): measure compact preset to its size token (#6774)
Harjoth Kharae41ab7a0be0b
diff --git a/.changeset/selector-keeps-compact-single-line-triggers-alig-1e63.md b/.changeset/selector-keeps-compact-single-line-triggers-alig-1e63.mdnew file mode 100644index 0000000000..dcb09b4270--- /dev/null+++ b/.changeset/selector-keeps-compact-single-line-triggers-alig-1e63.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] Selector keeps compact single-line triggers aligned with their size tokens, including icons and clear controls.+@harjothkharadiff --git a/.github/scripts/story-play-guard.js b/.github/scripts/story-play-guard.jsindex 6b411e2a7f..01570c2c0d 100644--- a/.github/scripts/story-play-guard.js+++ b/.github/scripts/story-play-guard.js@@ -38,6 +38,16 @@ const port = Number(getArg('port') || 6010);// Stories whose play assertions are load-bearing. Adding a story here is the// whole cost of promoting its play function into required CI.const TARGETS = [+ {+ component: 'Selector',+ story: 'core-selector--size-variants',+ guards: 'compact trigger variants match their size tokens and multiline values grow',+ },+ {+ component: 'MultiSelector',+ story: 'core-multiselector--sizes',+ guards: 'compact trigger variants match their size tokens',+ },{component: 'ChartTooltip',story: 'charts-chrome-tooltip--modal-layering',diff --git a/apps/storybook/stories/MultiSelector.stories.tsx b/apps/storybook/stories/MultiSelector.stories.tsxindex 0fb31a75c8..5d134a4fa3 100644--- a/apps/storybook/stories/MultiSelector.stories.tsx+++ b/apps/storybook/stories/MultiSelector.stories.tsx@@ -1,6 +1,7 @@// Copyright (c) Meta Platforms, Inc. and affiliates.import type {Meta, StoryObj} from '@storybook/react';+import {expect} from 'storybook/test';import {useState} from 'react';import {Button} from '@astryxdesign/core/Button';import {MultiSelector} from '@astryxdesign/core/MultiSelector';@@ -433,6 +434,16 @@ export const Status: Story = {};// Size variants+const compactSizingTheme = defineTheme({+ name: 'multi-selector-compact-sizing',+ tokens: {+ '--spacing-5': '10px',+ '--size-element-sm': '24px',+ '--size-element-md': '28px',+ '--size-element-lg': '32px',+ },+});+export const Sizes: Story = {render: () => {const [value1, setValue1] = useState<string[]>([]);@@ -465,10 +476,71 @@ export const Sizes: Story = {onChange={setValue3}placeholder="Large (36px)"/>+ <Theme theme={compactSizingTheme}>+ {(['sm', 'md', 'lg'] as const).map(size => (+ <div key={size} style={{display: 'grid', gap: 8}}>+ {(+ [+ 'plain',+ 'start',+ 'option',+ 'status',+ 'tooltip',+ 'clear',+ 'loading',+ 'readonly',+ ] as const+ ).map(state => (+ <MultiSelector+ key={state}+ label={`Compact ${size} ${state}`}+ size={size}+ options={[+ {+ value: 'name',+ label: 'Name',+ icon: state === 'option' ? 'search' : undefined,+ },+ ]}+ value={['name']}+ onChange={() => {}}+ startIcon={state === 'start' ? 'search' : undefined}+ status={+ state === 'status' || state === 'tooltip'+ ? {type: 'warning', message: 'Check selection'}+ : undefined+ }+ statusVariant={state === 'tooltip' ? 'tooltip' : 'attached'}+ hasClear={state === 'clear'}+ isLoading={state === 'loading'}+ isReadOnly={state === 'readonly'}+ />+ ))}+ </div>+ ))}+ </Theme></div>);},decorators: [Story => <Story />],+ play: async ({canvasElement}) => {+ await document.fonts.ready;+ const triggers = canvasElement.querySelectorAll<HTMLElement>(+ '.astryx-multi-selector',+ );+ expect(triggers).toHaveLength(27);+ for (const trigger of triggers) {+ const size = Number.parseFloat(+ getComputedStyle(trigger).getPropertyValue(+ `--size-element-${trigger.dataset.size}`,+ ),+ );+ expect(+ trigger.getBoundingClientRect().height,+ trigger.textContent ?? '',+ ).toBeCloseTo(size, 1);+ }+ },};// Form compositiondiff --git a/apps/storybook/stories/Selector.stories.tsx b/apps/storybook/stories/Selector.stories.tsxindex ddcf292854..9993665ad1 100644--- a/apps/storybook/stories/Selector.stories.tsx+++ b/apps/storybook/stories/Selector.stories.tsx@@ -573,6 +573,16 @@ export const OptionDescriptions: Story = {};// Size variants+const compactSizingTheme = defineTheme({+ name: 'selector-compact-sizing',+ tokens: {+ '--spacing-5': '10px',+ '--size-element-sm': '24px',+ '--size-element-md': '28px',+ '--size-element-lg': '32px',+ },+});+export const SizeVariants: Story = {render: () => {const [value1, setValue1] = useState<string | undefined>();@@ -605,10 +615,91 @@ export const SizeVariants: Story = {onChange={setValue3}placeholder="Large size (36px)"/>+ <Theme theme={compactSizingTheme}>+ {(['sm', 'md', 'lg'] as const).map(size => (+ <div key={size} style={{display: 'grid', gap: 8}}>+ {(+ [+ 'plain',+ 'start',+ 'option',+ 'status',+ 'tooltip',+ 'clear',+ 'loading',+ 'custom',+ 'readonly',+ ] as const+ ).map(state => (+ <Selector+ key={state}+ label={`Compact ${size} ${state}`}+ size={size}+ options={[+ {+ value: 'apple',+ label: 'Apple',+ icon: state === 'option' ? UserIcon : undefined,+ },+ ]}+ value="apple"+ onChange={() => {}}+ startIcon={state === 'start' ? UserIcon : undefined}+ status={+ state === 'status' || state === 'tooltip'+ ? {type: 'warning', message: 'Check selection'}+ : undefined+ }+ statusVariant={state === 'tooltip' ? 'tooltip' : 'attached'}+ hasClear={state === 'clear'}+ isLoading={state === 'loading'}+ isReadOnly={state === 'readonly'}+ renderValue={+ state === 'custom'+ ? option => <span>{option.label}</span>+ : undefined+ }+ />+ ))}+ <Selector+ label={`Compact ${size} multiline`}+ data-testid="compact-multiline"+ size={size}+ options={['Apple']}+ value="Apple"+ onChange={() => {}}+ renderValue={option => (+ <>+ <div>{option.label}</div>+ <div>Second line</div>+ </>+ )}+ />+ </div>+ ))}+ </Theme></div>);},decorators: [Story => <Story />],+ play: async ({canvasElement}) => {+ await document.fonts.ready;+ const triggers =+ canvasElement.querySelectorAll<HTMLElement>('.astryx-selector');+ expect(triggers).toHaveLength(33);+ for (const trigger of triggers) {+ const styles = getComputedStyle(trigger);+ const size = Number.parseFloat(+ styles.getPropertyValue(`--size-element-${trigger.dataset.size}`),+ );+ const height = trigger.getBoundingClientRect().height;+ if (trigger.dataset.testid === 'compact-multiline') {+ expect(height).toBeGreaterThan(size);+ } else {+ expect(height, trigger.textContent ?? '').toBeCloseTo(size, 1);+ }+ }+ },};// Ghost variant for toolbar compositiondiff --git a/packages/core/src/Selector/Selector.doc.mjs b/packages/core/src/Selector/Selector.doc.mjsindex fb3b05bd81..cead9c4c1e 100644--- a/packages/core/src/Selector/Selector.doc.mjs+++ b/packages/core/src/Selector/Selector.doc.mjs@@ -324,7 +324,7 @@ export const docs = {name: 'renderValue',type: '(option: SelectorOptionData) => ReactNode',description:- 'Custom render function for the selected option inside the closed trigger. The trigger is sized by padding, so it is the size token for a one-line value (28/32/36) and exactly one text line taller for a two-line one (48/52/56), always on the 4px rhythm, always aligned with the buttons and inputs beside it. Inside an InputGroup the group owns the row height: a SelectorOption folds onto one line and ellipsizes, and any taller node is cut off at the row.',+ 'Custom render function for the selected option inside the closed trigger. A one-line value follows the active size token, including compact spacing with icons or a clear control. Padding accommodates the trigger line box, and each extra line grows the control by one line (28/32/36 for one line and 48/52/56 for two with the default tokens). Inside an InputGroup the group owns the row height: a SelectorOption folds onto one line and ellipsizes, and any taller node is cut off at the row.',},{name: 'indicatorPosition',diff --git a/packages/core/src/Selector/Selector.source-build.test.mjs b/packages/core/src/Selector/Selector.source-build.test.mjsindex 9ebc01c61a..b90528e7c6 100644--- a/packages/core/src/Selector/Selector.source-build.test.mjs+++ b/packages/core/src/Selector/Selector.source-build.test.mjs@@ -19,9 +19,9 @@ const ROOT = path.resolve(__dirname, '../../../..');const SELECTOR_SOURCE = path.join(__dirname, 'Selector.tsx');const EXPECTED_PADDING_DECLARATIONS = [- 'padding-block:calc((var(--size-element-sm) - var(--spacing-5) - 2 * var(--border-width)) / 2)',- 'padding-block:calc((var(--size-element-md) - var(--spacing-5) - 2 * var(--border-width)) / 2)',- 'padding-block:calc((var(--size-element-lg) - var(--spacing-5) - 2 * var(--border-width)) / 2)',+ 'padding-block:calc((var(--size-element-sm) - max(var(--spacing-5),20px,1rem) - 2 * var(--border-width)) / 2)',+ 'padding-block:calc((var(--size-element-md) - max(var(--spacing-5),20px,1rem) - 2 * var(--border-width)) / 2)',+ 'padding-block:calc((var(--size-element-lg) - max(var(--spacing-5),20px,1rem) - 2 * var(--border-width)) / 2)',];describe('Selector source-build compatibility (#5464)', () => {diff --git a/packages/core/src/Selector/Selector.tsx b/packages/core/src/Selector/Selector.tsxindex 0461fc49c7..4ecbc01138 100644--- a/packages/core/src/Selector/Selector.tsx+++ b/packages/core/src/Selector/Selector.tsx@@ -6,7 +6,7 @@* @file Selector.tsx* @input Uses React, StyleX, adaptive selection surfaces, theme-resolved* indicators, Field, and InputGroup context- * @output Exports Selector with content-derived option-mark layout+ * @output Exports Selector with content-derived option-mark layout and token-sized single-line triggers* @position Core implementation; consumed by index.ts** SYNC: When modified, update:@@ -113,13 +113,10 @@ const styles = stylex.create({'@supports (-webkit-touch-callout: none)': `max(1rem, ${typeScaleVars['--text-label-size']})`,},},- // A FIXED line box, not the ratio: the trigger's padding is derived from- // one line being `--spacing-5` tall, and a ratio makes the line box track- // the font — which the coarse-pointer bump above (and any theme that- // changes `--font-size-base`) then moves, taking the control off its size- // token. The glyphs still grow for touch; only the box they sit in is- // pinned. Item's own rows set their line heights and are unaffected.- lineHeight: spacingVars['--spacing-5'],+ // Keep the line box and padding in step, including the 20px clear+ // control and 1rem icons when the spacing scale is compact. A fixed+ // length also keeps the iOS font floor from changing trigger height.+ lineHeight: `max(${spacingVars['--spacing-5']}, 20px, 1rem)`,color: colorVars['--color-text-primary'],cursor: {default: 'pointer',@@ -409,9 +406,8 @@ const styles = stylex.create({// and inputs beside it. No prop picks the height — the content does, and it// can only land on the grid.//-// `--spacing-5` is one line here because `triggerContainer` pins its-// line-height to exactly that; the two must stay in step, which is why both-// read the same token rather than one hardcoding 20px.+// Match triggerContainer's line box, which also accommodates the fixed-size+// clear control and icons independently of the theme's spacing scale.// Keep these calculations inline: a consumer's Babel preset can lower a// module-scope helper to a function expression before StyleX evaluates this// object, and StyleX cannot constant-evaluate that transformed helper.@@ -419,15 +415,15 @@ const styles = stylex.create({const sizeStyles = stylex.create({sm: {minHeight: sizeVars['--size-element-sm'],- paddingBlock: `calc((${sizeVars['--size-element-sm']} - ${spacingVars['--spacing-5']} - 2 * ${borderVars['--border-width']}) / 2)`,+ paddingBlock: `calc((${sizeVars['--size-element-sm']} - max(${spacingVars['--spacing-5']}, 20px, 1rem) - 2 * ${borderVars['--border-width']}) / 2)`,},md: {minHeight: sizeVars['--size-element-md'],- paddingBlock: `calc((${sizeVars['--size-element-md']} - ${spacingVars['--spacing-5']} - 2 * ${borderVars['--border-width']}) / 2)`,+ paddingBlock: `calc((${sizeVars['--size-element-md']} - max(${spacingVars['--spacing-5']}, 20px, 1rem) - 2 * ${borderVars['--border-width']}) / 2)`,},lg: {minHeight: sizeVars['--size-element-lg'],- paddingBlock: `calc((${sizeVars['--size-element-lg']} - ${spacingVars['--spacing-5']} - 2 * ${borderVars['--border-width']}) / 2)`,+ paddingBlock: `calc((${sizeVars['--size-element-lg']} - max(${spacingVars['--spacing-5']}, 20px, 1rem) - 2 * ${borderVars['--border-width']}) / 2)`,},});