An open source design system that's fully customizable and agent ready
TypeScript
feat(Switch): add size prop scale (sm / md) matching boolean control vertical rhythm (#4230) (#4322)
Ojas Mutreja25dddc36f41a
diff --git a/.changeset/switch-size-prop-tjsk.md b/.changeset/switch-size-prop-tjsk.mdnew file mode 100644index 0000000000..d50737705f--- /dev/null+++ b/.changeset/switch-size-prop-tjsk.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[feat] add size prop (sm / md) to Switch to match CheckboxInput and RadioList boolean control scales (#4230)++@HelloOjasMutrejadiff --git a/packages/core/src/Switch/Switch.doc.mjs b/packages/core/src/Switch/Switch.doc.mjsindex 785936efc0..e077df0b07 100644--- a/packages/core/src/Switch/Switch.doc.mjs+++ b/packages/core/src/Switch/Switch.doc.mjs@@ -62,6 +62,12 @@ export const docs = {description: 'Whether the switch is disabled.',default: 'false',},+ {+ name: 'size',+ type: "'sm' | 'md'",+ description: 'Size variant controlling track and thumb dimensions. sm (34x20px) matches sm checkbox/radio vertical rhythm; md (40x24px, default) matches md checkbox/radio vertical rhythm.',+ default: "'md'",+ },{name: 'htmlName',type: 'string',@@ -132,8 +138,8 @@ export const docs = {],theming: {targets: [- {className: 'astryx-switch', states: ['checked', 'disabled']},- {className: 'astryx-switch-thumb', states: ['checked']},+ {className: 'astryx-switch', visualProps: ['size'], states: ['checked', 'disabled']},+ {className: 'astryx-switch-thumb', visualProps: ['size'], states: ['checked']},{className: 'astryx-switch-field', visualProps: ['labelPosition', 'labelSpacing']},],},@@ -278,8 +284,8 @@ export const docsZh = {],theming: {targets: [- {className: 'astryx-switch', states: ['checked', 'disabled']},- {className: 'astryx-switch-thumb', states: ['checked']},+ {className: 'astryx-switch', visualProps: ['size'], states: ['checked', 'disabled']},+ {className: 'astryx-switch-thumb', visualProps: ['size'], states: ['checked']},{className: 'astryx-switch-field', visualProps: ['labelPosition', 'labelSpacing']},],},diff --git a/packages/core/src/Switch/Switch.test.tsx b/packages/core/src/Switch/Switch.test.tsxindex fea564c91c..ae62e639cc 100644--- a/packages/core/src/Switch/Switch.test.tsx+++ b/packages/core/src/Switch/Switch.test.tsx@@ -68,6 +68,28 @@ describe('Switch', () => {expect(screen.getByRole('switch')).toBeChecked();});+ it('renders with custom size prop (sm / md)', () => {+ const {rerender} = render(+ <Switch+ label="Small switch"+ value={false}+ size="sm"+ onChange={() => {}}+ />,+ );+ expect(screen.getByRole('switch')).toBeInTheDocument();++ rerender(+ <Switch+ label="Medium switch"+ value={false}+ size="md"+ onChange={() => {}}+ />,+ );+ expect(screen.getByRole('switch')).toBeInTheDocument();+ });+it('calls onChange with new checked state when clicked', async () => {const user = userEvent.setup();const handleChange = vi.fn();diff --git a/packages/core/src/Switch/Switch.tsx b/packages/core/src/Switch/Switch.tsxindex efe15f1883..462d2a7b40 100644--- a/packages/core/src/Switch/Switch.tsx+++ b/packages/core/src/Switch/Switch.tsx@@ -47,17 +47,75 @@ import type {SizeValue} from '../utils/types';import {themeProps} from '../utils/themeProps';import {VisuallyHidden} from '../VisuallyHidden';-// Fixed dimensions: 40px width, 24px height, 16px thumb (off), 20px thumb (on)-const SWITCH_WIDTH = 40;-const SWITCH_HEIGHT = 24;-const THUMB_SIZE_OFF = 16;-const THUMB_SIZE_ON = 20;-const TRACK_PADDING = 4;-// Padding between thumb right edge and track inner edge when on-const ON_RIGHT_PADDING = 2;-// Travel distance for on state: positions thumb with ON_RIGHT_PADDING from right edge-const THUMB_TRAVEL_ON =- SWITCH_WIDTH - TRACK_PADDING - THUMB_SIZE_ON - ON_RIGHT_PADDING;+const wrapperSizeStyles = stylex.create({+ sm: {+ width: 34,+ height: 20,+ },+ md: {+ width: 40,+ height: 24,+ },+});++const inputSizeStyles = stylex.create({+ sm: {+ width: 34,+ height: 20,+ },+ md: {+ width: 40,+ height: 24,+ },+});++const trackSizeStyles = stylex.create({+ sm: {+ width: 34,+ height: 20,+ padding: 3,+ },+ md: {+ width: 40,+ height: 24,+ padding: 4,+ },+});++const thumbOffSizeStyles = stylex.create({+ sm: {+ width: 12,+ height: 12,+ transform: 'translateX(0)',+ },+ md: {+ width: 16,+ height: 16,+ transform: 'translateX(0)',+ },+});++const thumbOnSizeStyles = stylex.create({+ sm: {+ width: 16,+ height: 16,+ transform: 'translateX(13px)',+ },+ md: {+ width: 20,+ height: 20,+ transform: 'translateX(14px)',+ },+});++const labelWrapperSizeStyles = stylex.create({+ sm: {+ minHeight: 20,+ },+ md: {+ minHeight: 24,+ },+});const styles = stylex.create({container: {@@ -77,8 +135,6 @@ const styles = stylex.create({display: 'flex',alignItems: 'center',flexShrink: 0,- width: SWITCH_WIDTH,- height: SWITCH_HEIGHT,isolation: 'isolate',},input: {@@ -88,8 +144,6 @@ const styles = stylex.create({opacity: 0,cursor: 'pointer',zIndex: 1,- width: SWITCH_WIDTH,- height: SWITCH_HEIGHT,},inputDisabled: {cursor: 'not-allowed',@@ -100,9 +154,6 @@ const styles = stylex.create({track: {display: 'flex',alignItems: 'center',- width: SWITCH_WIDTH,- height: SWITCH_HEIGHT,- padding: TRACK_PADDING,borderRadius: radiusVars['--radius-full'],transitionProperty: 'background-color',transitionDuration: {@@ -159,22 +210,11 @@ const styles = stylex.create({},transitionTimingFunction: easeVars['--ease-standard'],},- thumbOff: {- width: THUMB_SIZE_OFF,- height: THUMB_SIZE_OFF,- transform: 'translateX(0)',- },- thumbOn: {- width: THUMB_SIZE_ON,- height: THUMB_SIZE_ON,- transform: `translateX(${THUMB_TRAVEL_ON}px)`,- },labelWrapper: {display: 'flex',flexDirection: 'column',gap: spacingVars['--spacing-0-5'],justifyContent: 'center',- minHeight: SWITCH_HEIGHT,},description: {fontFamily: typographyVars['--font-family-body'],@@ -313,6 +353,13 @@ export interface SwitchProps extends Omit<BaseProps, 'onChange'> {* When set with a message, displays a colored message box below the switch.*/status?: InputStatus;+ /**+ * Size variant controlling track and thumb dimensions.+ * - 'sm': 34x20px (matches sm checkbox/radio vertical rhythm)+ * - 'md': 40x24px (default, matches md checkbox/radio vertical rhythm)+ * @default 'md'+ */+ size?: 'sm' | 'md';}// Dynamic field width (number -> px, string used as-is).@@ -358,6 +405,7 @@ export function Switch({labelPosition = 'end',labelSpacing = 'hug',status,+ size = 'md',width,xstyle,className,@@ -410,7 +458,7 @@ export function Switch({describedByParts.length > 0 ? describedByParts.join(' ') : undefined;const switchElement = (- <div {...stylex.props(styles.switchWrapper)}>+ <div {...stylex.props(styles.switchWrapper, wrapperSizeStyles[size])}><inputref={mergeRefs(ref, disabledMessageTooltip.positionRef)}id={id}@@ -447,6 +495,7 @@ export function Switch({aria-busy={isBusy || undefined}{...stylex.props(styles.input,+ inputSizeStyles[size],isDisabled && styles.inputDisabled,isBusy && styles.inputBusy,)}@@ -457,9 +506,11 @@ export function Switch({themeProps('switch', {checked: isOn ? 'checked' : null,disabled: isDisabled ? 'disabled' : null,+ size,}),stylex.props(styles.track,+ trackSizeStyles[size],isOn ? styles.trackOn : styles.trackOff,!isDisabled && styles.trackFocus,isDisabled && styles.trackDisabled,@@ -468,8 +519,14 @@ export function Switch({)}><div{...mergeProps(- themeProps('switch-thumb', {checked: isOn ? 'checked' : null}),- stylex.props(styles.thumb, isOn ? styles.thumbOn : styles.thumbOff),+ themeProps('switch-thumb', {+ checked: isOn ? 'checked' : null,+ size,+ }),+ stylex.props(+ styles.thumb,+ isOn ? thumbOnSizeStyles[size] : thumbOffSizeStyles[size],+ ),)}>{isBusy && <Spinner size="sm" />}</div>@@ -479,7 +536,7 @@ export function Switch({);const labelElement = (- <div {...stylex.props(styles.labelWrapper)}>+ <div {...stylex.props(styles.labelWrapper, labelWrapperSizeStyles[size])}><FieldLabellabel={label}inputID={id}