An open source design system that's fully customizable and agent ready
TypeScript
fix(Switch): hardening — swarm findings (#768)
Joeye33cbeb96a12
diff --git a/.claude/internet-mode-used_DO_NOT_REMOVE_MANUALLY_SECURITY_RISK b/.claude/internet-mode-used_DO_NOT_REMOVE_MANUALLY_SECURITY_RISKdeleted file mode 100644index f61efc832b..0000000000--- a/.claude/internet-mode-used_DO_NOT_REMOVE_MANUALLY_SECURITY_RISK+++ /dev/null@@ -1,3 +0,0 @@-This directory has been used with Claude Code's internet mode.-Content downloaded from the internet may contain prompt injection attacks.-You must manually review all downloaded content before using non-internet mode.diff --git a/packages/core/src/Switch/Switch.doc.mjs b/packages/core/src/Switch/Switch.doc.mjsindex 35c6a16283..eaef44d004 100644--- a/packages/core/src/Switch/Switch.doc.mjs+++ b/packages/core/src/Switch/Switch.doc.mjs@@ -209,7 +209,7 @@ export const docs = {'labelPosition="start" with labelSpacing="spread" produces a settings panel style layout','Follows the same patterns as XDSCheckboxInput for structural consistency','Interaction is blocked during busy state (loading or pending async action) to prevent double-toggling',- 'Track and thumb transitions respect prefers-reduced-motion (0.01s duration when reduced motion preferred)',+ 'Track and thumb transitions respect prefers-reduced-motion (0s duration when reduced motion preferred)',],};diff --git a/packages/core/src/Switch/XDSSwitch.test.tsx b/packages/core/src/Switch/XDSSwitch.test.tsxindex cca32b8b19..06d5f60a0a 100644--- a/packages/core/src/Switch/XDSSwitch.test.tsx+++ b/packages/core/src/Switch/XDSSwitch.test.tsx@@ -236,4 +236,101 @@ describe('XDSSwitch', () => {);expect(screen.getByRole('switch')).toBeInTheDocument();});++ it('sets aria-busy on input when loading', () => {+ render(+ <XDSSwitch+ label="Enable notifications"+ value={false}+ isLoading+ onChange={() => {}}+ />,+ );+ expect(screen.getByRole('switch')).toHaveAttribute('aria-busy', 'true');+ });++ it('renders status message when status prop is provided', () => {+ render(+ <XDSSwitch+ label="Enable notifications"+ value={false}+ onChange={() => {}}+ status={{type: 'error', message: 'Failed to save setting'}}+ />,+ );+ expect(screen.getByText('Failed to save setting')).toBeInTheDocument();+ });++ it('sets aria-invalid when status type is error', () => {+ render(+ <XDSSwitch+ label="Enable notifications"+ value={false}+ onChange={() => {}}+ status={{type: 'error', message: 'Error message'}}+ />,+ );+ expect(screen.getByRole('switch')).toHaveAttribute('aria-invalid', 'true');+ });++ it('does not set aria-invalid when status type is not error', () => {+ render(+ <XDSSwitch+ label="Enable notifications"+ value={false}+ onChange={() => {}}+ status={{type: 'warning', message: 'Warning message'}}+ />,+ );+ expect(screen.getByRole('switch')).not.toHaveAttribute('aria-invalid');+ });++ it('associates status message with switch via aria-describedby', () => {+ render(+ <XDSSwitch+ label="Enable notifications"+ value={false}+ onChange={() => {}}+ status={{type: 'error', message: 'Error message'}}+ />,+ );+ const switchEl = screen.getByRole('switch');+ const describedBy = switchEl.getAttribute('aria-describedby');+ expect(describedBy).toBeTruthy();+ });++ it('calls onFocus and onBlur callbacks', async () => {+ const user = userEvent.setup();+ const handleFocus = vi.fn();+ const handleBlur = vi.fn();+ render(+ <XDSSwitch+ label="Enable notifications"+ value={false}+ onChange={() => {}}+ onFocus={handleFocus}+ onBlur={handleBlur}+ />,+ );++ const switchEl = screen.getByRole('switch');+ await user.click(switchEl);+ expect(handleFocus).toHaveBeenCalled();++ await user.tab();+ expect(handleBlur).toHaveBeenCalled();+ });++ it('sets required attribute when isRequired is true', () => {+ render(+ <XDSSwitch+ label="Enable notifications"+ value={false}+ onChange={() => {}}+ isRequired+ />,+ );+ expect(screen.getByRole('switch')).toBeRequired();+ });+});diff --git a/packages/core/src/Switch/XDSSwitch.tsx b/packages/core/src/Switch/XDSSwitch.tsxindex bb9582efa0..431a192ff7 100644--- a/packages/core/src/Switch/XDSSwitch.tsx+++ b/packages/core/src/Switch/XDSSwitch.tsx@@ -54,7 +54,7 @@ const THUMB_TRAVEL_ON =const styles = stylex.create({container: {display: 'flex',- alignItems: 'flex-start',+ alignItems: 'center',gap: spacingVars['--spacing-2'],},containerSpread: {@@ -85,6 +85,9 @@ const styles = stylex.create({inputDisabled: {cursor: 'not-allowed',},+ inputBusy: {+ pointerEvents: 'none',+ },track: {display: 'flex',alignItems: 'center',@@ -161,13 +164,25 @@ const styles = stylex.create({display: 'flex',flexDirection: 'column',gap: spacingVars['--spacing-0-5'],- marginTop: 3,+ justifyContent: 'center',+ minHeight: SWITCH_HEIGHT,},description: {fontFamily: typographyVars['--font-body'],fontSize: typeScaleVars['--text-supporting-size'],color: colorVars['--color-text-secondary'],},+ srOnly: {+ position: 'absolute',+ width: 1,+ height: 1,+ padding: 0,+ margin: -1,+ overflow: 'hidden',+ clip: 'rect(0, 0, 0, 0)',+ whiteSpace: 'nowrap',+ borderWidth: 0,+ },});export type XDSSwitchLabelPosition = 'start' | 'end';@@ -347,7 +362,11 @@ export function XDSSwitch({aria-describedby={ariaDescribedBy}aria-invalid={status?.type === 'error' ? true : undefined}aria-busy={isBusy || undefined}- {...stylex.props(styles.input, isDisabled && styles.inputDisabled)}+ {...stylex.props(+ styles.input,+ isDisabled && styles.inputDisabled,+ isBusy && styles.inputBusy,+ )}/><divaria-hidden="true"@@ -372,6 +391,11 @@ export function XDSSwitch({{isBusy && <XDSSpinner size="sm" />}</div></div>+ {isBusy && (+ <span {...stylex.props(styles.srOnly)} role="status">+ Loading+ </span>+ )}</div>);@@ -398,7 +422,10 @@ export function XDSSwitch({return (<div{...mergeProps(- xdsClassName('switch-field', {labelPosition, labelSpacing}),+ xdsClassName('switch-field', {+ labelPosition: labelPosition !== 'end' ? labelPosition : undefined,+ labelSpacing: labelSpacing !== 'default' ? labelSpacing : undefined,+ }),stylex.props(xstyle),className,style,