Animated Toggle Switch for React
An animated React toggle switch, an iOS switch in Tailwind, whose thumb springs across a glass track with Motion and fills with the accent color when on.
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/switch.jsonStudio lighting
Switch
Using it in a form? See the react-hook-form and zod recipe.
With label and description
import { useState } from "react";
import { Switch } from "@/components/sheenui/switch/switch";
// Package build (after the first npm release, or a packed tarball): import { Switch } from "@sheenui/theme";
export function Example() {
const [on, setOn] = useState(true);
return (
<Switch
checked={on}
onCheckedChange={setOn}
label="Email notifications"
description="Get a summary every Monday."
/>
);
}Files to copy
5 folders · 10 files · 4 packages
- From
theme/src/ components/switch/hooks/{use-direction.ts, use-latest.ts, use-media-query.ts, use-preferences.ts}motion/tokens.tslib/{cn.ts, field-control-shell.tsx, form.ts, motion-props.ts, state-light.tsx}styles/
- shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/switch.jsonRegistry item: switch. The styles need the Tailwind v4 glass UI setup.- Packages
pnpm add @base-ui/react clsx motion tailwind-merge
A settings list
One piece of state for a group of switches, each with its own name.
import { useState } from "react";
import { Switch } from "@/components/sheenui/switch/switch";
// Package build (after the first npm release, or a packed tarball): import { Switch } from "@sheenui/theme";
const channels = [
{ key: "email", label: "Email", description: "Receipts and weekly summaries." },
{ key: "push", label: "Push notifications", description: "Mentions and replies." },
{ key: "sms", label: "Text messages", description: "Security alerts only." },
] as const;
export function Example() {
const [enabled, setEnabled] = useState({ email: true, push: true, sms: false });
return (
<div className="flex flex-col gap-5">
{channels.map(({ key, label, description }) => (
<Switch
key={key}
label={label}
description={description}
checked={enabled[key]}
onCheckedChange={(checked) => setEnabled((prev) => ({ ...prev, [key]: checked }))}
/>
))}
</div>
);
}Props
Keyboard
Accessibility
- role="switch" with aria-checked; the label and description share one <label>.
- On and off differ by thumb position as well as color; forced colors outline the track.
- The touch hit area is 44 px; under reduced motion the thumb jumps instead of sliding.
When to use Switch
- Use Switch as an animated switch in React for settings that apply the moment they change, such as dark mode, push notifications or a public profile toggle in an account page. Use it as a Framer Motion switch: the thumb moves with Motion, the library formerly called Framer Motion, on the shared SheenUI spring.
- Use label and description together for a preferences row, so the text and the helper line sit in one label element and clicking the words flips the switch.
- Use the error prop when a toggle can fail to save, for example a server rejection, so the message shows under the control and is announced to screen readers.
- Skip it when the choice is only applied after a form is submitted. Use Checkbox for that, and use RadioGroup or SegmentedControl when the choice is between named options.
- Pair it with GlassCard for a settings panel, Toast to confirm that a change was saved, and Field or the settings-page block for grouped preference rows.
Frequently asked questions
Related pages
Guides that cover it
67 components pages in all: browse them on All components.
By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .