Skip to content
Drivn
6 min read

Drivn vs shadcn/ui — Switch Compared

Drivn ships a ~40 line zero-runtime Switch built on a native button; shadcn/ui wraps @radix-ui/react-switch. Here is the practical difference.

Choosing between the Drivn and shadcn/ui Switch comes down to one question: should the toggle be a button you can read top to bottom, or a wrapper around a Radix primitive? Both draw the same thing on screen, a rounded track with a sliding thumb, and both expect the checked value to live in your own component. What differs is everything underneath.

The Drivn Switch is one client file of about forty lines, and the track is the element itself: a native <button type="button" role="switch"> with aria-checked={checked}, filled bg-primary when on and bg-border when off. A bg-primary-foreground thumb slides from translate-x-[3px] to translate-x-[25px], and a focus-visible:ring-[3px] ring marks keyboard focus. The file imports React and cn() and nothing else, so there is no Radix package or class-variance-authority to audit, upgrade, or tree-shake.

shadcn/ui wraps @radix-ui/react-switch. That adds defaultChecked for uncontrolled use, data-state attributes to style against, and a hidden checkbox input that joins native form submissions when the switch sits inside a <form>. Those features are real, and so is the extra package in your lockfile.

The sections below walk through the source, the checked + onChange contract, how each version is labelled and announced, and how to decide which one belongs in your project. The Drivn CLI installs the file in one command.

Side-by-side comparison

FeatureDrivnshadcn/ui
Install commandnpx drivn add switchnpx shadcn@latest add switch
Underlying implementationNative button with role="switch"@radix-ui/react-switch primitives
Runtime UI dependenciescn() utility only — zero npm UI packages@radix-ui/react-switch + peers
Component lines~40 lines, single fileShort wrapper file + the @radix-ui/react-switch package
Public API shape<Switch checked onChange /><Switch checked onCheckedChange />
Controlled statechecked + onChange in callerchecked + onCheckedChange in caller
Uncontrolled stateNot built-in — caller owns useStatedefaultChecked supported via Radix
ARIA patternrole="switch" + aria-checked on buttonFull WAI-ARIA Switch pattern via Radix
Keyboard behaviourBrowser default — Space/Enter on buttonSpace/Enter on the Radix Root button
Focus ringfocus-visible:ring-[3px] ring-ring/50focus-visible ring classes in the wrapper
Form integrationPair with hidden input in callerHidden checkbox input rendered inside a <form>
Thumb animationCSS translate-x 200msCSS translate-x 200ms via data-state
Disabled stateStandard button disabled attributeRadix data-disabled + standard attribute
Client componentYes — 'use client'Yes — 'use client'
LicenseMITMIT

Forty lines of button vs a Radix wrapper

The Drivn Switch file is short enough to read in one breath. It declares a styles object with two entries — base for the pill track and its focus ring, thumb for the bg-primary-foreground circle — then renders a <button type="button" role="switch" aria-checked={checked}> whose track colour swaps between bg-primary and bg-border and whose thumb translates between translate-x-[3px] and translate-x-[25px] based on checked. The toggle logic is one line: onClick={() => onChange?.(!checked)}. That is the whole component.

The shadcn/ui Switch is a thin wrapper around @radix-ui/react-switch. The wrapper file is roughly the same length as the Drivn file, but it imports SwitchPrimitive.Root and SwitchPrimitive.Thumb from a separate package that ships its own state machine, its own data attributes (data-state="checked", data-disabled), its own focus management, and its own form integration hooks. You get more behaviour for free, at the cost of a runtime dependency you cannot remove without rewriting the component.

1// packages/drivn/src/registry/components/switch.ts — verbatim
2const styles = {
3 base: cn(
4 'relative w-12 h-[26px] rounded-full outline-none',
5 'transition-colors duration-200 overflow-hidden',
6 'focus-visible:ring-[3px] focus-visible:ring-ring/50'
7 ),
8 thumb: cn(
9 'absolute left-0 top-[3px] w-5 h-5',
10 'bg-primary-foreground rounded-full shadow-md',
11 'transition-transform duration-200'
12 ),
13}

Controlled state shape — checked + onChange

Both libraries expect controlled state from the caller. Drivn names the callback onChange and types it as (checked: boolean) => void — note the Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'onChange'> on the props type so the native button onChange does not collide. shadcn/ui (via Radix) names the callback onCheckedChange, which is the Radix convention to disambiguate from native form onChange events. Same idea, different name.

Drivn does not ship an uncontrolled mode — there is no defaultChecked prop, no internal useState. If you want uncontrolled state you wrap the Switch in your own component and own the state there. Radix-backed shadcn/ui supports both controlled (checked + onCheckedChange) and uncontrolled (defaultChecked) out of the box because Radix maintains the internal state machine for you. For most forms this difference is invisible — the value already lives in form state next to your Input and Textarea fields, so the Switch is controlled at the form level anyway. For one-off toggles, uncontrolled mode is a small ergonomic win.

1"use client"
2
3import * as React from "react"
4import { Switch } from "@/components/ui/switch"
5
6export default function NotificationToggle() {
7 const [enabled, setEnabled] = React.useState(false)
8
9 return (
10 <Switch
11 checked={enabled}
12 onChange={setEnabled}
13 />
14 )
15}

Accessibility — the button does the work, you add the name

The Drivn Switch gets most of its accessibility from the element it renders. A native <button> is in the tab order by default and fires its click handler on Enter and Space, so the component only adds role="switch" and aria-checked={checked} for screen readers to announce "switch, on" or "switch, off". The styles.base entry pairs outline-none with focus-visible:ring-[3px] focus-visible:ring-ring/50, which keeps mouse clicks clean while keyboard focus gets a visible ring in the theme ring token.

What no switch can supply on its own is a name. Wrap it in a Label with visible text, point a label htmlFor at the switch id, or pass aria-label directly, since ...props spreads straight onto the button. The shadcn/ui Radix Switch renders the same role="switch" and aria-checked pair on its own button and needs a label just as much, so the screen reader output for a labelled toggle is the same in both libraries. Drivn gets there without a primitive package between you and the DOM.

1"use client"
2
3import * as React from "react"
4import { Label } from "@/components/ui/label"
5import { Switch } from "@/components/ui/switch"
6
7export default function EmailToggle() {
8 const [enabled, setEnabled] = React.useState(true)
9
10 return (
11 <Label className="flex items-center gap-3">
12 <Switch checked={enabled} onChange={setEnabled} />
13 Email notifications
14 </Label>
15 )
16}

When the shadcn + Radix path still wins

Three situations push you toward the Radix-backed shadcn Switch even though Drivn ships fewer dependencies. The first is a project already wired through @radix-ui/react-form or another Radix form layer — the Switch primitive integrates with that form context cleanly, broadcasts its state via data-state, and participates in the form submission lifecycle without extra glue. Pairing the Drivn Switch with the same form layer means adding a hidden <input type="checkbox"> next to the button and syncing it manually.

The second is a design system mandate of WAI-ARIA exhaustiveness — every component audited against the full pattern, every keystroke documented, every screen reader output specified. Radix is engineered for that bar; the Drivn Switch is engineered to cover the practical 95% with a third of the surface area. The third is an existing shadcn project — switching one component to Drivn means running two registries side by side, two CLIs in the build, and two @/components/ui conventions. Stay on shadcn unless you are migrating the whole surface. For a fresh start where bundle size, registry simplicity, and dot-notation parity with the rest of the Drivn library matter more than Radix-grade accessibility, the Drivn Switch is the cleaner pick.

1# Drivn — one CLI, one registry
2npx drivn add switch
3
4# shadcn/ui — wraps a Radix package
5npx shadcn@latest add switch
6# also installs @radix-ui/react-switch
Get started

Install Drivn in one command

Copy the source into your project and own every line. Zero runtime dependencies, pure React + Tailwind.

npx drivn@latest create

Requires Node 18+. Works with npm, pnpm, and yarn.

Enjoying Drivn?
Star the repo on GitHub to follow new component releases.
Star →

Frequently asked questions

No. The Drivn Switch is a single file at packages/drivn/src/registry/components/switch.ts that imports React and the local cn() utility from @/utils/cn — nothing else. There is no @radix-ui/react-switch dependency, no class-variance-authority, no headless toggle library. The component is a native HTML button with role="switch" and aria-checked, plus a thumb span that translates via CSS. The whole file is roughly forty lines and ships as TypeScript source via the Drivn CLI.

Drivn calls it onChange and types it as (checked: boolean) => void. To prevent a collision with the native button onChange event, the SwitchProps interface uses Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "onChange"> before adding the custom signature. shadcn/ui follows the Radix convention and uses onCheckedChange to disambiguate from form onChange events. When moving a shadcn codebase onto Drivn, rename each onCheckedChange to onChange and the rest of the call site stays the same.

No. The Drivn Switch is controlled-only — the caller owns the boolean state via useState (or a form library) and passes it down through the checked prop. There is no defaultChecked, no internal state machine, no useUncontrolledState hook. If you need uncontrolled behaviour, wrap the Switch in a small component that owns the state and exposes its own API. The shadcn/ui Switch inherits Radix uncontrolled mode via defaultChecked out of the box.

The thumb is an absolutely positioned span sized w-5 h-5 with bg-primary-foreground, rounded-full, and shadow-md. Its base class adds transition-transform duration-200. When the checked prop is true, the cn() call appends translate-x-[25px], moving the thumb 25 pixels to the right. When false, it appends translate-x-[3px], settling the thumb on the left edge of the 48-pixel-wide track. The track itself animates between bg-primary and bg-border via transition-colors duration-200. No framer-motion, no animation library — pure Tailwind utilities.

For most production surfaces, yes. The component renders a native button with role="switch" and aria-checked, which is the WAI-ARIA Switch pattern baseline. Screen readers announce the on/off state correctly, Tab reaches the control, and Space and Enter activate it because those are native button keystrokes. The Drivn Switch does not match the exhaustive per-keystroke contract that Radix documents — for a design system requiring that level of audit, the shadcn/ui Switch wrapping @radix-ui/react-switch is the safer pick.

The Switch renders a button, not a form input, so a form submit does not include its value automatically. Three patterns: pair it with react-hook-form using Controller to bridge the checked + onChange API into the form state, drop a hidden <input type="checkbox" name="..." value={checked.toString()} /> next to the Switch and sync the two values, or call the form library setValue from the onChange callback directly. For most Drivn projects the react-hook-form Controller pattern is the cleanest because it keeps the Switch component itself unmodified.