Files
anthony lio 3b12d2e167 feat(ui): switch component (#53379)
## Problem

* the shared switch in `packages/ui` uses the radix primitive
* the thumb does not react to hover or press, and only the track color
shows the state
* a click on a switch label toggles the switch, but the label keeps the
default cursor

## Solution

* replaces the radix switch with base ui, and keeps a native `<button>`,
so labels and `role="switch"` work as before
* changes the thumb to white and the resting track one step darker, so
the off state shows on light pages
* replaces the hover color change with a thumb stretch that grows more
on press, so the thumb reacts to the pointer
* adds a pointer cursor on labels next to an enabled switch, because a
click on the label toggles it
* removes the `PointerEvent` stand-in from the studio test setup,
because it blocks base ui switch clicks in jsdom
* changes the install step in the switch docs to `@base-ui/react`, so
the manual install matches the component

<!-- linear:table-colwidths:345,344 -->
<table>
<thead>
<tr>
<th></th>
<th>preview</th>
</tr>
</thead>
<tbody>
<tr>
<td>light</td>
<td>


https://github.com/user-attachments/assets/a7c33dda-3487-43b8-881b-344e28891650

</td>
</tr>
<tr>
<td>dark</td>
<td>


https://github.com/user-attachments/assets/9718057b-f983-4099-8f7e-82521001b625

</td>
</tr>
</tbody>
</table>

## Preview links

| Site | Live | Preview | Search for |
| -- | -- | -- | -- |
| Design system |
[/design-system/docs/components/switch](<https://supabase.com/design-system/docs/components/switch>)
|
[/design-system/docs/components/switch](<https://design-system-git-feat-switch-base-ui-supabase.vercel.app/design-system/docs/components/switch>)
| Airplane Mode |
| Studio |
[/dashboard/project/\_/storage/files](<https://supabase.com/dashboard/project/_/storage/files>)
|
[/dashboard/project/\_/storage/files](<https://studio-git-feat-switch-base-ui-supabase.vercel.app/dashboard/project/_/storage/files>)
| Public bucket |

## Review instructions

1. open
[/design-system/docs/components/switch](<https://supabase.com/design-system/docs/components/switch>)
and
[/design-system/docs/components/switch](<https://design-system-git-feat-switch-base-ui-supabase.vercel.app/design-system/docs/components/switch>)
side by side
2. hover and click the "airplane mode" label, and make sure that the
cursor is a pointer and the switch toggles
3. change the theme to light and to dark, and make sure that the thumb
stays white in the on and off states
4. scroll to the form example, and make sure that the disabled "security
emails" switch does not toggle or stretch
5. open
[/dashboard/project/\_/storage/files](<https://studio-git-feat-switch-base-ui-supabase.vercel.app/dashboard/project/_/storage/files>),
click "new bucket" and turn on "public bucket", and make sure that the
"public buckets are not protected" warning shows

## Checklist

Check all before review:

- [X] I have read
[CONTRIBUTING.md](<https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md>)
- [X] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](<https://github.com/supabase/supabase/tree/master/apps/docs/style-guide>)
2026-11-19 21:32:09 +00:00

87 lines
1.7 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: Switch
description: A control for toggling between unchecked and checked.
component: true
links:
doc: https://base-ui.com/react/components/switch
api: https://base-ui.com/react/components/switch#api-reference
source:
shadcn: true
---
<ComponentPreview name="switch-demo" peekCode wide />
## Installation
<Tabs defaultValue="cli">
<TabsList>
<TabsTrigger value="cli">CLI</TabsTrigger>
<TabsTrigger value="manual">Manual</TabsTrigger>
</TabsList>
<TabsContent value="cli">
```bash
npx shadcn-ui@latest add switch
```
</TabsContent>
<TabsContent value="manual">
<Steps>
<Step>Install the following dependencies:</Step>
```bash
npm install @base-ui/react
```
<Step>Copy and paste the following code into your project.</Step>
<ComponentSource name="switch" />
<Step>Update the import paths to match your project setup.</Step>
</Steps>
</TabsContent>
</Tabs>
## Usage
```tsx
import { Switch } from '@/components/ui/switch'
```
```tsx
<Switch />
```
### Relative positioning
When using Switch in custom flex layouts (especially with height constraints like `h-full`), you may need to add `relative` positioning to the parent container to ensure proper rendering of focus rings, form validation messages, and overflow.
```tsx
<div className="relative">
<FormField
control={form.control}
name="enabled"
render={({ field }) => (
<FormControl>
<Switch checked={field.value} onCheckedChange={field.onChange} />
</FormControl>
)}
/>
</div>
```
You don’t need to add `relative` manually when using FormItemLayout as it provides the necessary positioning context automatically.
## Examples
### Form
<ComponentPreview name="switch-form" />