Themed toggles
Five icon switches: day/night, check/x, on/off, eye and lock. 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-themedDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/toggles-themed.tsx"use client"
import * as React from "react"
import { Check, Eye, EyeOff, Lock, LockOpen, Moon, Sun, X } from "lucide-react"
import { motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Themed toggle family: 5 meaningful switches (theme/state). role="switch", keyboard
accessible. Color comes ONLY from tokens. There is a lucide icon on the thumb; it
slides with the framer-motion `layout` and switches instantly under
reduced-motion. Used uncontrolled/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
}
/* DayNight: sun/moon; the track color changes with the day/night token. */
export function DayNightToggle(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-info", 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>
)
}
/* CheckX: acikken tik, kapaliyken carpi. */
export function CheckXToggle(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],
"flex items-center justify-center rounded-full bg-background [&_svg]:size-2/3",
on ? "text-success" : "text-muted-foreground"
)}
>
{on ? <Check /> : <X />}
</motion.span>
</button>
)
}
/* OnOff: iz icinde ON/OFF metni, basparmak solda/sagda. */
export function OnOffToggle(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-[68px] p-1 text-[10px] font-bold tracking-widest",
"rounded-md transition-colors",
on ? "justify-end bg-primary text-primary-foreground" : "justify-start bg-input text-muted-foreground",
props.className
)}
{...stripToggleProps(props)}
>
<span className={cn("absolute inset-y-0 flex items-center", on ? "left-2.5" : "right-2.5")}>{on ? "ON" : "OFF"}</span>
<motion.span layout transition={spring} className="relative size-5 rounded-[3px] bg-background shadow-sm" />
</button>
)
}
/* Eye: gorunurluk anahtari (goz / goz-kapali). */
export function EyeToggle(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 ? <Eye /> : <EyeOff />}
</motion.span>
</button>
)
}
/* Lock: kilit / kilit-acik durum anahtari. */
export function LockToggle(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],
"flex items-center justify-center rounded-full bg-background [&_svg]:size-2/3",
on ? "text-success" : "text-muted-foreground"
)}
>
{on ? <Lock /> : <LockOpen />}
</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.
Day / Night
A sun and moon with a shifting track.
import { DayNightToggle } from "@/components/ui/toggles-themed"
<DayNightToggle defaultChecked={true} />Check / X
A check when on, an x when off.
import { CheckXToggle } from "@/components/ui/toggles-themed"
<CheckXToggle defaultChecked={true} />On / Off
An ON and OFF text layout.
import { OnOffToggle } from "@/components/ui/toggles-themed"
<OnOffToggle defaultChecked={true} />Eye
An eye and eye-off visibility switch.
import { EyeToggle } from "@/components/ui/toggles-themed"
<EyeToggle defaultChecked={true} />Lock
A lock and unlock switch.
import { LockToggle } from "@/components/ui/toggles-themed"
<LockToggle defaultChecked={true} />ai2 Themed toggles: 5 styled variations on the token system
The ai2 Themed toggles are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around icon-driven 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 slides the thumb and its icon 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 Themed toggles?
5 exports in one file: Day/Night, Check/X, On/Off, Eye and Lock. 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 slides the thumb and its icon 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 Themed 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.