An open source design system that's fully customizable and agent ready
TypeScript
fix(core): collapse the Switch label gap when isLabelHidden (#5112)
Cindy Zhangfd131157cce9
diff --git a/.changeset/switch-hidden-label-gap.md b/.changeset/switch-hidden-label-gap.mdnew file mode 100644index 0000000000..896ec330e2--- /dev/null+++ b/.changeset/switch-hidden-label-gap.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] `Switch` with `isLabelHidden` no longer reserves the label gap. The hidden label is `sr-only`, but its wrapper stayed a flex item, so the row still painted the 8px gap beside it: the field box measured 8px wider than the track it contains, and a hidden-label switch stopped 8px inside the edge every neighbouring control lined up on. The gap now collapses with the label, so the field is exactly as wide as the painted track — matching `CheckboxInput`, which already did this.++@cixzhangdiff --git a/packages/core/src/Switch/Switch.test.tsx b/packages/core/src/Switch/Switch.test.tsxindex 986f099102..d6330bf54b 100644--- a/packages/core/src/Switch/Switch.test.tsx+++ b/packages/core/src/Switch/Switch.test.tsx@@ -262,6 +262,22 @@ describe('Switch', () => {expect(screen.getByLabelText('Toggle row')).toBeInTheDocument();});+ it('drops the label gap when isLabelHidden so the row is only as wide as the track', () => {+ const {rerender} = render(+ <Switch+ label="Toggle row"+ isLabelHidden+ value={false}+ onChange={() => {}}+ />,+ );+ const row = screen.getByRole('switch').parentElement!.parentElement!;+ expect(getComputedStyle(row).gap).toMatch(/^0(px)?$/);++ rerender(<Switch label="Toggle row" value={false} onChange={() => {}} />);+ expect(getComputedStyle(row).gap).not.toMatch(/^0(px)?$/);+ });+it('keeps description linked via aria-describedby when isLabelHidden', () => {render(<Switchdiff --git a/packages/core/src/Switch/Switch.tsx b/packages/core/src/Switch/Switch.tsxindex fba33a2212..0a3ed2ef7a 100644--- a/packages/core/src/Switch/Switch.tsx+++ b/packages/core/src/Switch/Switch.tsx@@ -135,6 +135,12 @@ const styles = stylex.create({alignItems: 'center',gap: spacingVars['--spacing-2'],},+ // A hidden label is sr-only, so its wrapper is a zero-width flex item — the+ // row gap would still be painted beside the track, making the field box+ // wider than the control it contains. Matches CheckboxInput.+ containerLabelHidden: {+ gap: 0,+ },containerSpread: {justifyContent: 'space-between',width: '100%',@@ -655,6 +661,7 @@ export function Switch({}}{...stylex.props(styles.container,+ isLabelHidden && styles.containerLabelHidden,labelSpacing === 'spread' && styles.containerSpread,!isDisabled && switchScope,)}>