fix(ui): drop the switch label tab stop with no focus ring (#3297)

This commit is contained in:
Hampus authored and GitHub committed 2026-10-08 17:42:11 +02:00
1 parent b8f3dbddbf
commit b82bbaa2dd
2 files changed
+6 -17

No files matched your search

@@ -22,9 +22,6 @@
flex-direction: column; flex-direction: column;
align-items: flex-start; align-items: flex-start;
gap: 0.25rem; gap: 0.25rem;
border: 0;
background: transparent;
padding: 0;
text-align: left; text-align: left;
} }
@@ -29,6 +29,7 @@ export const Switch = observer(
const baseId = useId(); const baseId = useId();
const labelId = useMemo(() => `${baseId}-switch-label`, [baseId]); const labelId = useMemo(() => `${baseId}-switch-label`, [baseId]);
const descriptionId = useMemo(() => `${baseId}-switch-description`, [baseId]); const descriptionId = useMemo(() => `${baseId}-switch-description`, [baseId]);
const switchId = useMemo(() => `${baseId}-switch`, [baseId]);
const hasLabel = useMemo( const hasLabel = useMemo(
() => label !== undefined && label !== null && !(typeof label === 'string' && label.trim().length === 0), () => label !== undefined && label !== null && !(typeof label === 'string' && label.trim().length === 0),
[label], [label],
@@ -50,11 +51,6 @@ export const Switch = observer(
}, },
[disabled, onChange], [disabled, onChange],
); );
const handleLabelToggle = useCallback(() => {
if (disabled) return;
onChange(!value);
rootRef.current?.focus();
}, [disabled, onChange, value]);
return ( return (
<div <div
ref={forwardedRef} ref={forwardedRef}
@@ -62,15 +58,10 @@ export const Switch = observer(
data-flx="ui.form.switch.container" data-flx="ui.form.switch.container"
> >
{(hasLabel || hasDescription) && ( {(hasLabel || hasDescription) && (
<button <label
type="button" htmlFor={switchId}
className={clsx(styles.labelContainer, !disabled && styles.clickable)} className={clsx(styles.labelContainer, !disabled && styles.clickable)}
disabled={disabled} data-flx="ui.form.switch.label-container"
onClick={handleLabelToggle}
aria-pressed={value}
aria-labelledby={hasLabel ? labelId : undefined}
aria-describedby={hasDescription ? descriptionId : undefined}
data-flx="ui.form.switch.label-container.label-toggle"
> >
{hasLabel && ( {hasLabel && (
<span <span
@@ -86,11 +77,12 @@ export const Switch = observer(
{description} {description}
</span> </span>
)} )}
</button> </label>
)} )}
<FocusRing focusTarget={rootRef} ringTarget={rootRef} offset={-2} data-flx="ui.form.switch.focus-ring"> <FocusRing focusTarget={rootRef} ringTarget={rootRef} offset={-2} data-flx="ui.form.switch.focus-ring">
<SwitchPrimitive.Root <SwitchPrimitive.Root
ref={rootRef} ref={rootRef}
id={switchId}
checked={value} checked={value}
onCheckedChange={valueChange} onCheckedChange={valueChange}
disabled={disabled} disabled={disabled}