Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/switch-label-position.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@launchpad-ui/components': minor
---

Add `labelPosition` to `Switch`. Pass `labelPosition="start"` to render a visible label before the track instead of after it, while keeping the label inside the switch so it stays clickable and remains the accessible name. Defaults to `"end"`, matching current behavior.
10 changes: 10 additions & 0 deletions packages/components/__tests__/Switch.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ describe('Switch', () => {
expect(screen.queryByText('On')).not.toBeInTheDocument();
});

it('renders a visible label after the track by default', () => {
render(<Switch>Dark mode</Switch>);
expect(screen.getByRole('switch', { name: /Dark mode/ }).closest('label')?.className).not.toContain('labelStart');
});

it('renders a visible label before the track when labelPosition is start', () => {
render(<Switch labelPosition="start">Dark mode</Switch>);
expect(screen.getByRole('switch', { name: /Dark mode/ }).closest('label')?.className).toContain('labelStart');
});

it('renders with primary variant', () => {
render(<Switch variant="primary" defaultSelected />);
expect(screen.getByRole('switch')).toBeVisible();
Expand Down
11 changes: 9 additions & 2 deletions packages/components/src/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ const switchStyles = cva(styles.switch, {
compact: {
true: styles.compact,
},
labelPosition: {
start: styles.labelStart,
end: '',
},
},
defaultVariants: {
variant: 'default',
labelPosition: 'end',
},
});

Expand All @@ -38,19 +43,21 @@ const SwitchContext = createContext<ContextValue<SwitchProps, HTMLLabelElement>>
* A switch allows a user to turn a setting on or off.
*
* Provide an accessible name via `children` (visible label) or `aria-label` (visually hidden label).
* A visible label renders after the track by default; pass `labelPosition="start"` to render it
* before the track while keeping it part of the switch's label and click target.
*
* https://react-spectrum.adobe.com/react-aria/Switch.html
*/
const Switch = ({ ref, ...props }: SwitchProps) => {
const [mergedProps, mergedRef] = useLPContextProps(props, ref, SwitchContext);
const { switchLabels, variant } = mergedProps;
const { switchLabels, variant, labelPosition } = mergedProps;
const hideLabels = switchLabels === false ? true : undefined;
return (
<AriaSwitch
{...mergedProps}
ref={mergedRef}
className={composeRenderProps(mergedProps.className, (className, renderProps) =>
switchStyles({ ...renderProps, variant, compact: hideLabels, className }),
switchStyles({ ...renderProps, variant, labelPosition, compact: hideLabels, className }),
)}
>
{composeRenderProps(mergedProps.children, (children, { isSelected }) => (
Expand Down
6 changes: 6 additions & 0 deletions packages/components/src/styles/Switch.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
isolation: isolate;
}

.switch.labelStart {
& .track {
order: 1;
}
}

.track {
position: relative;
display: flex;
Expand Down
12 changes: 12 additions & 0 deletions packages/components/stories/Switch.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const meta: Meta<typeof Switch> = {
control: 'select',
options: ['default', 'primary'],
},
labelPosition: {
control: 'inline-radio',
options: ['start', 'end'],
},
},
parameters: {
figma: {
Expand Down Expand Up @@ -54,3 +58,11 @@ export const PrimaryHideLabels: Story = {
export const WithChildren: Story = {
args: { children: 'Dark mode' },
};

export const LabelStart: Story = {
args: { children: 'Dark mode', labelPosition: 'start' },
};

export const LabelStartHideLabels: Story = {
args: { children: 'Dark mode', labelPosition: 'start', switchLabels: false },
};
Loading