Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(Selector): keep one-line triggers on their size token under a wide spacing scale (#6854)

Xinzi Zhoue8a5938381fe
diff --git a/.changeset/selector-caps-spacing-row-to-size-token.md b/.changeset/selector-caps-spacing-row-to-size-token.md
new file mode 100644
index 0000000000..e0ff2598ce
--- /dev/null
+++ b/.changeset/selector-caps-spacing-row-to-size-token.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Selector's one-line trigger matches its size token when the theme's `--spacing-5` is taller than the token can hold, instead of overshooting it.
+@imdreamrunner
diff --git a/.github/scripts/story-play-guard.js b/.github/scripts/story-play-guard.js
index cab4b09d27..3b18a5e5c5 100644
--- a/.github/scripts/story-play-guard.js
+++ b/.github/scripts/story-play-guard.js
@@ -41,7 +41,7 @@ const TARGETS = [
{
component: 'Selector',
story: 'core-selector--size-variants',
- guards: 'compact trigger variants match their size tokens and multiline values grow',
+ guards: 'compact and wide-spacing triggers match their size tokens, large label text stays unclipped, and multiline values grow by one text row',
},
{
component: 'MultiSelector',
diff --git a/apps/storybook/stories/Selector.stories.tsx b/apps/storybook/stories/Selector.stories.tsx
index 9993665ad1..94a4c5e3a9 100644
--- a/apps/storybook/stories/Selector.stories.tsx
+++ b/apps/storybook/stories/Selector.stories.tsx
@@ -583,6 +583,35 @@ const compactSizingTheme = defineTheme({
},
});
+// A spacing scale taller than the small token can hold, like the docsite
+// Playground's largest preset: `--spacing-5` (40px) equals the small size
+// token, so an uncapped spacing row plus borders would overshoot it.
+const wideSpacingSizingTheme = defineTheme({
+ name: 'selector-wide-spacing-sizing',
+ typography: {scale: {base: 18, ratio: 1.414}},
+ tokens: {
+ '--spacing-5': '40px',
+ '--size-element-sm': '40px',
+ '--size-element-md': '48px',
+ '--size-element-lg': '56px',
+ },
+});
+
+// Label text too large for the small token: the trigger must keep it visible
+// rather than cap its row to the token.
+const largeTextSizingTheme = defineTheme({
+ name: 'selector-large-text-sizing',
+ tokens: {'--spacing-5': '40px', '--text-label-size': '32px'},
+});
+
+// Wide spacing with the default size ramp and label: a built-in one-line value
+// lands on its token, and a caller-rendered value that sets a larger font with
+// its own line height grows the trigger to fit.
+const wideSpacingDefaultSizesTheme = defineTheme({
+ name: 'selector-wide-spacing-default-sizes',
+ tokens: {'--spacing-5': '40px'},
+});
+
export const SizeVariants: Story = {
render: () => {
const [value1, setValue1] = useState<string | undefined>();
@@ -678,6 +707,67 @@ export const SizeVariants: Story = {
</div>
))}
</Theme>
+ <Theme theme={wideSpacingSizingTheme}>
+ {(['sm', 'md', 'lg'] as const).map(size => (
+ <div key={size} style={{display: 'grid', gap: 8}}>
+ {(['plain', 'clear'] as const).map(state => (
+ <Selector
+ key={state}
+ label={`Wide spacing ${size} ${state}`}
+ size={size}
+ options={['Apple']}
+ value="Apple"
+ onChange={() => {}}
+ hasClear={state === 'clear'}
+ />
+ ))}
+ <Selector
+ label={`Wide spacing ${size} multiline`}
+ data-testid="wide-multiline"
+ size={size}
+ options={['Apple']}
+ value="Apple"
+ onChange={() => {}}
+ renderValue={option => (
+ <>
+ <div>{option.label}</div>
+ <div>Second line</div>
+ </>
+ )}
+ />
+ </div>
+ ))}
+ </Theme>
+ <Theme theme={largeTextSizingTheme}>
+ <Selector
+ label="Large text small"
+ data-testid="large-text"
+ size="sm"
+ options={['Ågypj']}
+ value="Ågypj"
+ onChange={() => {}}
+ />
+ </Theme>
+ <Theme theme={wideSpacingDefaultSizesTheme}>
+ <Selector
+ label="Wide spacing default small"
+ size="sm"
+ options={['Apple']}
+ value="Apple"
+ onChange={() => {}}
+ />
+ <Selector
+ label="Wide spacing large custom value"
+ data-testid="large-custom"
+ size="sm"
+ options={['Ågypj']}
+ value="Ågypj"
+ onChange={() => {}}
+ renderValue={option => (
+ <div style={{fontSize: 32, lineHeight: 1.25}}>{option.label}</div>
+ )}
+ />
+ </Theme>
</div>
);
},
@@ -686,15 +776,55 @@ export const SizeVariants: Story = {
await document.fonts.ready;
const triggers =
canvasElement.querySelectorAll<HTMLElement>('.astryx-selector');
- expect(triggers).toHaveLength(33);
+ expect(triggers).toHaveLength(45);
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') {
+ if (
+ trigger.dataset.testid === 'large-text' ||
+ trigger.dataset.testid === 'large-custom'
+ ) {
+ // Text larger than the token can hold keeps its whole line box.
+ const walker = document.createTreeWalker(trigger, NodeFilter.SHOW_TEXT);
+ let text: Node | null = walker.nextNode();
+ while (text && !text.textContent?.includes('Ågypj')) {
+ text = walker.nextNode();
+ }
+ // The nearest ancestor that clips: the built-in label, or the
+ // renderValue wrapper around caller content.
+ let clipElement = text?.parentElement ?? null;
+ while (
+ clipElement &&
+ clipElement !== trigger &&
+ getComputedStyle(clipElement).overflowY === 'visible'
+ ) {
+ clipElement = clipElement.parentElement;
+ }
+ expect(clipElement).toBeTruthy();
+ if (!text || !clipElement) {
+ continue;
+ }
+ const range = document.createRange();
+ range.selectNodeContents(text);
+ const textBox = range.getBoundingClientRect();
+ const clip = clipElement.getBoundingClientRect();
+ expect(textBox.top).toBeGreaterThanOrEqual(clip.top - 0.5);
+ expect(textBox.bottom).toBeLessThanOrEqual(clip.bottom + 0.5);
expect(height).toBeGreaterThan(size);
+ } else if (trigger.dataset.testid?.endsWith('-multiline')) {
+ // Two text rows, plus whatever padding remains once the first row
+ // fills the token (padding never goes below zero).
+ const row = Number.parseFloat(styles.lineHeight);
+ const borders =
+ Number.parseFloat(styles.borderTopWidth) +
+ Number.parseFloat(styles.borderBottomWidth);
+ expect(height, trigger.textContent ?? '').toBeCloseTo(
+ Math.max(size + row, 2 * row + borders),
+ 1,
+ );
} else {
expect(height, trigger.textContent ?? '').toBeCloseTo(size, 1);
}
diff --git a/packages/core/src/Selector/Selector.doc.mjs b/packages/core/src/Selector/Selector.doc.mjs
index cead9c4c1e..21dd9ad2e7 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. 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.',
+ 'Custom render function for the selected option inside the closed trigger. A one-line value follows the active size token, including compact or wide spacing scales 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). Content inherits the trigger line height; content that sets a larger font should set its own line height so the trigger grows to fit 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.',
},
{
name: 'indicatorPosition',
diff --git a/packages/core/src/Selector/Selector.source-build.test.mjs b/packages/core/src/Selector/Selector.source-build.test.mjs
index b90528e7c6..342cb87f8b 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) - 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)',
+ 'padding-block:calc((var(--size-element-sm) - max(min(var(--spacing-5),max(var(--size-element-sm) - 2 * var(--border-width),1.5em)),20px,1rem) - 2 * var(--border-width)) / 2)',
+ 'padding-block:calc((var(--size-element-md) - max(min(var(--spacing-5),max(var(--size-element-md) - 2 * var(--border-width),1.5em)),20px,1rem) - 2 * var(--border-width)) / 2)',
+ 'padding-block:calc((var(--size-element-lg) - max(min(var(--spacing-5),max(var(--size-element-lg) - 2 * var(--border-width),1.5em)),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.tsx
index ec2c96da92..ffb5eee0e7 100644
--- a/packages/core/src/Selector/Selector.tsx
+++ b/packages/core/src/Selector/Selector.tsx
@@ -113,10 +113,8 @@ const styles = stylex.create({
'@supports (-webkit-touch-callout: none)': `max(1rem, ${typeScaleVars['--text-label-size']})`,
},
},
- // 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)`,
+ // The line box is set per size in sizeStyles below, where it can be capped
+ // to what that size token holds.
color: colorVars['--color-text-primary'],
cursor: {
default: 'pointer',
@@ -168,10 +166,13 @@ const styles = stylex.create({
// trigger stops asserting a floor of its own — otherwise a control sized
// above its group (`<InputGroup size="md"><Selector size="lg">`) grows the
// row it was supposed to sit in. The padding goes with it: the row is
- // already the size token, and the value box is centred in it.
+ // already the size token, and the value box is centred in it. The line box
+ // likewise stops reading the control's own size token (sizeStyles caps it
+ // there), so grouped triggers keep one line box whatever their `size`.
triggerInGroup: {
minHeight: 0,
paddingBlock: 0,
+ lineHeight: `max(${spacingVars['--spacing-5']}, 20px, 1rem)`,
},
// Wrapper for `renderValue` output. Takes the free width and clips
// horizontally so a long value ellipsizes rather than widening the trigger;
@@ -221,9 +222,12 @@ const styles = stylex.create({
triggerIconOpen: {
transform: 'rotate(180deg)',
},
+ // Ghost has no borders, so its spacing row never overshoots the token; it
+ // keeps the uncapped line box from before the sizeStyles cap.
triggerGhost: {
width: 'auto',
borderWidth: 0,
+ lineHeight: `max(${spacingVars['--spacing-5']}, 20px, 1rem)`,
backgroundColor: 'transparent',
boxShadow: {
default: 'none',
@@ -402,13 +406,21 @@ const styles = stylex.create({
// The trigger is sized by PADDING, not by a fixed height, so it is the size
// token plus one text line for each extra line the value uses: 28/32/36 for
-// one line, 48/52/56 for two. The token and a text line are both multiples of
-// 4, so every trigger lands on the 4px rhythm and lines up with the Buttons
-// and inputs beside it. No prop picks the height — the content does, and it
-// can only land on the grid.
+// one line, 48/52/56 for two with the default tokens, where the token and a
+// text line are both multiples of 4 and the trigger lines up with the Buttons
+// and inputs beside it. No prop picks the height — the content does.
//
-// Match triggerContainer's line box, which also accommodates the fixed-size
-// clear control and icons independently of the theme's spacing scale.
+// The line box is `--spacing-5`, floored at the 20px clear control and 1rem
+// icons. When that row would not fit inside the size token's borders, it is
+// capped at what the token holds, so a spacing scale taller than the token
+// cannot push a one-line trigger past it. The cap never goes below 1.5em of
+// the trigger's own font — the type scale's loosest target leading, a
+// conservative bound above a label's glyph extent — so label text too large
+// for the token keeps room rather than being clipped. Line box and padding
+// read the same value; nothing changes wherever the row already fit. Ghost and
+// grouped triggers keep the uncapped line box. `renderValue` content inherits
+// this row like the built-in label; content that sets a larger font should set
+// its own line height, as it must at any 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.
@@ -416,15 +428,18 @@ const styles = stylex.create({
const sizeStyles = stylex.create({
sm: {
minHeight: sizeVars['--size-element-sm'],
- paddingBlock: `calc((${sizeVars['--size-element-sm']} - max(${spacingVars['--spacing-5']}, 20px, 1rem) - 2 * ${borderVars['--border-width']}) / 2)`,
+ lineHeight: `max(min(${spacingVars['--spacing-5']}, max(${sizeVars['--size-element-sm']} - 2 * ${borderVars['--border-width']}, 1.5em)), 20px, 1rem)`,
+ paddingBlock: `calc((${sizeVars['--size-element-sm']} - max(min(${spacingVars['--spacing-5']}, max(${sizeVars['--size-element-sm']} - 2 * ${borderVars['--border-width']}, 1.5em)), 20px, 1rem) - 2 * ${borderVars['--border-width']}) / 2)`,
},
md: {
minHeight: sizeVars['--size-element-md'],
- paddingBlock: `calc((${sizeVars['--size-element-md']} - max(${spacingVars['--spacing-5']}, 20px, 1rem) - 2 * ${borderVars['--border-width']}) / 2)`,
+ lineHeight: `max(min(${spacingVars['--spacing-5']}, max(${sizeVars['--size-element-md']} - 2 * ${borderVars['--border-width']}, 1.5em)), 20px, 1rem)`,
+ paddingBlock: `calc((${sizeVars['--size-element-md']} - max(min(${spacingVars['--spacing-5']}, max(${sizeVars['--size-element-md']} - 2 * ${borderVars['--border-width']}, 1.5em)), 20px, 1rem) - 2 * ${borderVars['--border-width']}) / 2)`,
},
lg: {
minHeight: sizeVars['--size-element-lg'],
- paddingBlock: `calc((${sizeVars['--size-element-lg']} - max(${spacingVars['--spacing-5']}, 20px, 1rem) - 2 * ${borderVars['--border-width']}) / 2)`,
+ lineHeight: `max(min(${spacingVars['--spacing-5']}, max(${sizeVars['--size-element-lg']} - 2 * ${borderVars['--border-width']}, 1.5em)), 20px, 1rem)`,
+ paddingBlock: `calc((${sizeVars['--size-element-lg']} - max(min(${spacingVars['--spacing-5']}, max(${sizeVars['--size-element-lg']} - 2 * ${borderVars['--border-width']}, 1.5em)), 20px, 1rem) - 2 * ${borderVars['--border-width']}) / 2)`,
},
});