Skip to the content

Controls and surfaces

switch

An on/off switch.

Records

When to use it

A setting that takes effect as soon as it is flipped. For a choice that is submitted with a form use checkbox; for a pressed/unpressed toolbar button use toggle.

See it working

The demonstration the browser checks drive, with the keyboard as well as the pointer. More of it is on the Records page.

Compare table

PlanStarterTeamStudio
Basics
Price a monthBilled yearly€12Best€29€59
Projects320No limitBest
ExportYesYesYes
Support
Help by emailWithin 2 daysWithin 2 daysWithin 2 days
Team
Members11050

Fact rows

  • DietVegetarian
    You said this on 3 May
  • Home airport (editing is controlled)Lisbon
    From your booking in June

Editing the airport: no

Consent sheet

Decision: nothing yet

Consent panel

Share your trip with the guide?

The guide sees it until the day after the trip.

  • Your first name
    Required
  • So that the route can be adapted.

Switched on: none. Decision: nothing yet

Language switcher

Chosen: en

Price breakdown and countdown

2 × day ticket€46.00 each
€92.00
Service fee
€6.00
Early booking
-€10.00
Audio guide
Included
TotalTaxes included
€88.00

The price is held for no time yet. Ended 0 times.

Undo line

  • Market walk
  • Lunch
  • Pottery class

Timeline

  1. Done:

    Order placed

  2. Done:

    Packed

    Two parcels.
  3. Now:

    On its way

    With the carrier since this morning.
  4. Upcoming:

    Delivered

    Expected on 15 March.

Add it

npx shadcn@latest add @rivier-ui/switch

The registry is named once in components.json: see Installation.

Example

Import it from @/components/ui/switch.

<Label><Switch defaultChecked /> Weekly summary</Label>
<Switch aria-label="Dark mode" checked={dark} onCheckedChange={setDark} />

What it needs

A client component: it uses state, effects or browser behaviour. Packages: radix-ui.

It imports

It is used by

Source

The whole file. Once copied it is yours: the registry item holds the same text.

src/components/ui/switch.tsx
"use client";
/**
 * @name switch
 * @category ui
 * @description An on/off switch.
 * @when A setting that takes effect as soon as it is flipped. For a choice that is submitted with a form use checkbox; for a pressed/unpressed toolbar button use toggle.
 * @example
 * <Label><Switch defaultChecked /> Weekly summary</Label>
 * <Switch aria-label="Dark mode" checked={dark} onCheckedChange={setDark} />
 */
import { Switch as SwitchPrimitive } from "radix-ui";
import type { ComponentProps } from "react";
import { cn } from "@/lib/cn";

/** The track is 44 by 24px; an invisible margin makes the touch target 40px tall. */
export function Switch({ className, ...props }: ComponentProps<typeof SwitchPrimitive.Root>) {
  return (
    <SwitchPrimitive.Root
      data-slot="switch"
      className={cn(
        "peer relative inline-flex h-6 w-11 shrink-0 items-center rounded-full border border-transparent bg-input transition-[background-color,border-color,opacity] duration-instant ease-out after:absolute after:-inset-x-px after:-inset-y-2 disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive data-[state=checked]:bg-primary motion-reduce:transition-none",
        className,
      )}
      {...props}
    >
      <SwitchPrimitive.Thumb
        data-slot="switch-thumb"
        className="pointer-events-none block size-5 translate-x-px rounded-full bg-background transition-transform duration-base ease-out data-[state=checked]:translate-x-[calc(100%+1px)] data-[state=checked]:bg-primary-foreground motion-reduce:transition-none rtl:-translate-x-px rtl:data-[state=checked]:-translate-x-[calc(100%+1px)]"
      />
    </SwitchPrimitive.Root>
  );
}