Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/clerk-js/src/ui/customizables/elementDescriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ export const APPEARANCE_KEYS = containsAllElementsConfigKeys([
'segmentedControlButton',

'switchRoot',
'switchIndicator',
'switchThumb',
'switchLabel',

'alert',
'alertIcon',
Expand Down
61 changes: 32 additions & 29 deletions packages/clerk-js/src/ui/elements/Switch.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef, useId, useState } from 'react';
import React, { forwardRef, useState } from 'react';

import type { LocalizationKey } from '../customizables';
import { descriptors, Flex, Text } from '../customizables';
Expand All @@ -14,44 +14,49 @@ interface SwitchProps {
}

export const Switch = forwardRef<HTMLDivElement, SwitchProps>(
({ checked: controlledChecked, defaultChecked, onChange, disabled = false, 'aria-label': ariaLabel, label }, ref) => {
({ checked: controlledChecked, defaultChecked, onChange, disabled = false, label }, ref) => {
const [internalChecked, setInternalChecked] = useState(!!defaultChecked);
const isControlled = controlledChecked !== undefined;
const checked = isControlled ? controlledChecked : internalChecked;
const labelId = useId();

const handleToggle = (_: React.MouseEvent | React.KeyboardEvent) => {
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if (disabled) return;
if (!isControlled) {
setInternalChecked(!checked);
}
onChange?.(!checked);
};

const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === ' ' || e.key === 'Enter') {
e.preventDefault();
handleToggle(e);
setInternalChecked(e.target.checked);
}
onChange?.(e.target.checked);
};

return (
<Flex
ref={ref}
elementDescriptor={descriptors.switchRoot}
direction='row'
align='center'
as='div'
as='label'
sx={t => ({
width: 'fit-content',
'&:has(input:focus-visible) > span': {
...common.focusRingStyles(t),
},
})}
>
<Flex
as='div'
ref={ref}
tabIndex={disabled ? -1 : 0}
{/* The order of the elements is important here for the focus ring to work. The input is visually hidden, so the focus ring is applied to the span. */}
<input
type='checkbox'
role='switch'
aria-checked={checked}
aria-label={ariaLabel}
aria-labelledby={label && !ariaLabel ? labelId : undefined}
elementDescriptor={descriptors.switchRoot}
onClick={handleToggle}
onKeyDown={handleKeyDown}
disabled={disabled}
defaultChecked={defaultChecked}
checked={checked}
onChange={handleChange}
style={{
...common.visuallyHidden(),
}}
/>
<Flex
elementDescriptor={descriptors.switchIndicator}
as='span'
data-checked={checked}
sx={t => ({
width: t.sizes.$6,
height: t.sizes.$4,
Expand All @@ -65,7 +70,6 @@ export const Switch = forwardRef<HTMLDivElement, SwitchProps>(
outline: 'none',
boxSizing: 'border-box',
boxShadow: '0px 0px 0px 1px rgba(0, 0, 0, 0.06) inset',
...common.focusRing(t),
})}
>
<Flex
Expand All @@ -87,15 +91,14 @@ export const Switch = forwardRef<HTMLDivElement, SwitchProps>(
</Flex>
{label && (
<Text
id={labelId}
variant='caption'
colorScheme='secondary'
localizationKey={label}
sx={{
marginLeft: 8,
sx={t => ({
paddingInlineStart: t.sizes.$2,
cursor: disabled ? 'not-allowed' : 'pointer',
userSelect: 'none',
}}
})}
/>
)}
</Flex>
Expand Down
19 changes: 13 additions & 6 deletions packages/clerk-js/src/ui/styledSystem/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,21 @@ const borderColor = (t: InternalTheme, props?: any) => {
} as const;
};

const focusRingStyles = (t: InternalTheme) => {
return {
'&::-moz-focus-inner': { border: '0' },
WebkitTapHighlightColor: 'transparent',
boxShadow: t.shadows.$focusRing.replace('{{color}}', t.colors.$neutralAlpha200),
transitionProperty: t.transitionProperty.$common,
transitionTimingFunction: t.transitionTiming.$common,
transitionDuration: t.transitionDuration.$focusRing,
} as const;
};

const focusRing = (t: InternalTheme) => {
return {
'&:focus': {
'&::-moz-focus-inner': { border: '0' },
WebkitTapHighlightColor: 'transparent',
boxShadow: t.shadows.$focusRing.replace('{{color}}', t.colors.$neutralAlpha200),
transitionProperty: t.transitionProperty.$common,
transitionTimingFunction: t.transitionTiming.$common,
transitionDuration: t.transitionDuration.$focusRing,
...focusRingStyles(t),
},
} as const;
};
Expand Down Expand Up @@ -198,6 +204,7 @@ const visuallyHidden = () =>
export const common = {
textVariants,
borderVariants,
focusRingStyles,
focusRing,
disabled,
borderColor,
Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/appearance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ export type ElementsConfig = {
segmentedControlButton: WithOptions;

switchRoot: WithOptions;
switchIndicator: WithOptions;
switchThumb: WithOptions;
switchLabel: WithOptions;

avatarBox: WithOptions;
avatarImage: WithOptions;
Expand Down