Toggles
Five switch treatments: an iOS pill, a square, a theme-style icon, a glowing thumb and a labeled ON/OFF. Each is sized, token-driven and keyboard accessible with role switch.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/toggles-styledDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/toggles-styled.tsx"use client"
import * as React from "react"
import { Check, Moon, Sun, X } from "lucide-react"
import { motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Toggle family: 5 decorative switches. role="switch", keyboard accessible. Color
comes ONLY from tokens. Size = the track scale. The thumb slides with the
framer-motion `layout`; it switches instantly under reduced-motion. Used
uncontrolled (defaultChecked) or controlled. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
const trackSize: Record<StyledSize, string> = {
sm: "h-5 w-9 p-0.5",
md: "h-6 w-11 p-0.5",
lg: "h-7 w-[52px] p-1",
xl: "h-8 w-[60px] p-1",
}
const thumbSize: Record<StyledSize, string> = {
sm: "size-4",
md: "size-5",
lg: "size-5",
xl: "size-6",
}
const trackBase =
"relative inline-flex shrink-0 cursor-pointer items-center outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50"
interface ToggleProps extends Omit<React.ComponentProps<"button">, "onChange"> {
size?: StyledSize
checked?: boolean
defaultChecked?: boolean
onCheckedChange?: (checked: boolean) => void
}
function useToggle(props: ToggleProps) {
const { checked, defaultChecked, onCheckedChange } = props
const reduce = useReducedMotion()
const [internal, setInternal] = React.useState(defaultChecked ?? false)
const on = checked ?? internal
const toggle = () => {
if (checked === undefined) setInternal((v) => !v)
onCheckedChange?.(!on)
}
const spring = reduce ? { duration: 0 } : ({ type: "spring", stiffness: 500, damping: 30 } as const)
return { on, toggle, spring }
}
function stripToggleProps(props: ToggleProps) {
const { checked, defaultChecked, onCheckedChange, size, className, children, ...rest } = props
return rest
}
/* iOS: klasik pill + yuvarlak basparmak. */
export function IOSToggle(props: ToggleProps) {
const { on, toggle, spring } = useToggle(props)
const size = props.size ?? "md"
return (
<button
type="button"
role="switch"
aria-checked={on}
data-slot="styled-toggle"
onClick={toggle}
className={cn(trackBase, trackSize[size], "rounded-full transition-colors", on ? "justify-end bg-primary" : "justify-start bg-input", props.className)}
{...stripToggleProps(props)}
>
<motion.span layout transition={spring} className={cn(thumbSize[size], "rounded-full bg-background shadow-sm")} />
</button>
)
}
/* Square: koseli iz + kare basparmak. */
export function SquareToggle(props: ToggleProps) {
const { on, toggle, spring } = useToggle(props)
const size = props.size ?? "md"
return (
<button
type="button"
role="switch"
aria-checked={on}
data-slot="styled-toggle"
onClick={toggle}
className={cn(trackBase, trackSize[size], "rounded-md transition-colors", on ? "justify-end bg-primary" : "justify-start bg-input", props.className)}
{...stripToggleProps(props)}
>
<motion.span layout transition={spring} className={cn(thumbSize[size], "rounded-[3px] bg-background shadow-sm")} />
</button>
)
}
/* Icon: basparmakta gunes/ay ikonu (tema anahtari hissi). */
export function IconToggle(props: ToggleProps) {
const { on, toggle, spring } = useToggle(props)
const size = props.size ?? "md"
return (
<button
type="button"
role="switch"
aria-checked={on}
data-slot="styled-toggle"
onClick={toggle}
className={cn(trackBase, trackSize[size], "rounded-full transition-colors", on ? "justify-end bg-info" : "justify-start bg-input", props.className)}
{...stripToggleProps(props)}
>
<motion.span
layout
transition={spring}
className={cn(thumbSize[size], "flex items-center justify-center rounded-full bg-background text-foreground [&_svg]:size-2/3")}
>
{on ? <Moon /> : <Sun />}
</motion.span>
</button>
)
}
/* Glow: acikken basparmak token isima yayar. */
export function GlowToggle(props: ToggleProps) {
const { on, toggle, spring } = useToggle(props)
const size = props.size ?? "md"
return (
<button
type="button"
role="switch"
aria-checked={on}
data-slot="styled-toggle"
onClick={toggle}
className={cn(trackBase, trackSize[size], "rounded-full transition-colors", on ? "justify-end bg-success" : "justify-start bg-input", props.className)}
{...stripToggleProps(props)}
>
<motion.span
layout
transition={spring}
className={cn(thumbSize[size], "rounded-full bg-background", on && "shadow-[0_0_10px_1px_var(--success)]")}
/>
</button>
)
}
/* Labeled: iz icinde ON/OFF + isaret ikonu. */
export function LabeledToggle(props: ToggleProps) {
const { on, toggle, spring } = useToggle(props)
const size = props.size ?? "lg"
return (
<button
type="button"
role="switch"
aria-checked={on}
data-slot="styled-toggle"
onClick={toggle}
className={cn(
trackBase,
"h-7 w-16 p-1 text-[10px] font-semibold lg:text-xs",
"rounded-full transition-colors",
on ? "justify-end bg-primary text-primary-foreground" : "justify-start bg-input text-muted-foreground",
props.className
)}
{...stripToggleProps(props)}
>
{on && <span className="absolute left-2 tracking-wide">ON</span>}
{!on && <span className="absolute right-2 tracking-wide">OFF</span>}
<motion.span
layout
transition={spring}
className="relative flex size-5 items-center justify-center rounded-full bg-background text-foreground [&_svg]:size-3"
>
{on ? <Check /> : <X />}
</motion.span>
</button>
)
}Manual installs skip the @ai2/tokens theme, so add the token CSS from the theming guide or the tone colors will be missing.
Variations
5 takes on the same idea. Each is its own export, and every one accepts a size prop (sm, md, lg, xl) aligned to the base Button scale.
iOS
A classic pill track with a round thumb.
import { IOSToggle } from "@/components/ui/toggles-styled"
<IOSToggle defaultChecked />Square
A squared track and thumb.
import { SquareToggle } from "@/components/ui/toggles-styled"
<SquareToggle defaultChecked />Icon
A sun and moon ride in the thumb, like a theme switch.
import { IconToggle } from "@/components/ui/toggles-styled"
<IconToggle defaultChecked />Glow
The thumb glows with a token when on.
import { GlowToggle } from "@/components/ui/toggles-styled"
<GlowToggle defaultChecked />Labeled
An ON/OFF label with a check mark in the thumb.
import { LabeledToggle } from "@/components/ui/toggles-styled"
<LabeledToggle defaultChecked />ai2 Toggles: 5 styled variations on the token system
The ai2 Toggles are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around on and off switches. They are free and MIT licensed, and every color comes from a semantic token, so they theme with the rest of ai2 in light and dark.
Motion runs on framer-motion: framer-motion layout slides the thumb across the track. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the thumb jumps instantly with no slide.
What is in the ai2 Toggles?
5 exports in one file: iOS, Square, Icon, Glow and Labeled. Each renders a native button and takes a size prop (sm, md, lg, xl) aligned to the base Button. They are separate from the base Button on purpose: the base keeps its clean variant, tone and size axes, while the styled layer carries the effects.
You own the file. Copy the one category file and you have all 5 variations, with no runtime dependency on ai2 itself.
Why use it
- On-system by construction: Every color resolves to an ai2 semantic token, so the buttons follow your theme in light and dark with no extra work.
- Effect without the sprawl: The decorations live in a dedicated styled file, so the base Button keeps its clean, predictable API.
- Accessible and honest: Each renders a real button element, keeps a visible focus ring, and respects prefers-reduced-motion.
Features
- Token-driven color: No hardcoded hex or oklch; the look recolors with your theme tokens.
- framer-motion: framer-motion layout slides the thumb across the track.
- Reduced-motion aware: Under prefers-reduced-motion, the thumb jumps instantly with no slide.
- Size aligned to the base: Every variation takes sm, md, lg and xl matching the base Button height scale, so styled and base buttons line up in a row.
Production tips
- Use it for emphasis, not everywhere: Styled buttons draw the eye. Reserve them for the one action you want people to take on a screen, and use the base Button for the rest.
- Keep labels as verbs: The decoration adds weight, so a clear action label keeps the button scannable.
- Pick one variation per surface: The variations share a family; using two different ones in the same view competes for attention.
Works with the rest of ai2
The Toggles sit alongside the base Button and the rest of the @ai2 registry. They share the same token file, so a styled action next to a base button or a badge stays visually consistent in both modes.