GooseUI

Components

Switch

New

Switch is a native on/off control. The knob moves on a spring curve and stretches while pressed, like on iOS. It is a checkbox with role="switch", so it works in forms without JavaScript and submits its value by name.

Browser Support

appearance

Customizes the native checkbox appearance while retaining its browser behavior.

Your browser:
linear() easing

Spring easing for the knob. Browsers without it fall back to cubic-bezier.

Your browser:
:has()

Focus ring around the switch.

Your browser:

Preview

Installation

npx shadcn@latest add @gooseui/switch
pnpm dlx shadcn@latest add @gooseui/switch
yarn dlx shadcn@latest add @gooseui/switch
bunx --bun shadcn@latest add @gooseui/switch

Usage

import { Switch } from "@/components/ui/switch"
import { Label } from "@/components/ui/label"

<Switch id="airplane" name="airplane" defaultChecked />
<Label htmlFor="airplane">Airplane mode</Label>

Examples

Sizes

<Switch size="sm" />
<Switch />
<Switch size="lg" />

With a label

<Switch id="airplane" />
<Label htmlFor="airplane">Airplane mode</Label>

Disabled

<Switch disabled />
<Switch disabled defaultChecked />

Toast on change

<Switch
  aria-label="Notifications"
  onChange={(e) =>
    customToast.success(
      `Notifications: ${e.currentTarget.checked ? "on" : "off"}`,
    )
  }
/>

In a form

<form>
  <Switch name="notifications" value="yes" defaultChecked />
  <Switch name="sound" />
  <Button type="submit">Save</Button>
</form>
// FormData: { notifications: "yes" }

Settings list

<li className="flex items-center justify-between">
  <Label htmlFor="sound">Sound</Label>
  <Switch id="sound" defaultChecked />
</li>

Props

PropTypeDefaultDescription
size"sm" | "default" | "lg""default"Control size.
checked / defaultCheckedboolean-Controlled or initial state.
disabledboolean-Disables the switch.
namestring-Field name in the form.
valuestring-Value submitted when on. Defaults to "on".
classNamestring-Classes for the wrapper element.
...propsInputHTMLAttributes<HTMLInputElement>-Other native input attributes, including onChange and aria attributes.