3D toggles
Five depth switches: raised, pill press, tactile, dome and lever. 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-3dDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motionCopy the source
components/ui/toggles-3d.tsx"use client"
import * as React from "react"
import { motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* 3D toggle family: 5 depth switches. role="switch", keyboard accessible. Color
comes ONLY from tokens; the shadow/highlight uses token color-mix. The thumb
slides with the framer-motion `layout`, and switches instantly under
reduced-motion. 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
}
/* Raised: 3D kabartma govdeli basparmak, token alt golge. */
export function RaisedToggle(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")}
style={{ boxShadow: "0 2px 3px 0 color-mix(in oklab, var(--foreground) 30%, transparent), inset 0 1px 0 0 color-mix(in oklab, var(--background) 90%, transparent)" }}
/>
</button>
)
}
/* PillPress: iz basik/oyuk gorunur (inset golge). */
export function PillPressToggle(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)}
style={{ boxShadow: "inset 0 2px 4px 0 color-mix(in oklab, var(--foreground) 28%, transparent)" }}
{...stripToggleProps(props)}
>
<motion.span
layout
transition={spring}
className={cn(thumbSize[size], "rounded-full bg-background")}
style={{ boxShadow: "0 1px 2px 0 color-mix(in oklab, var(--foreground) 25%, transparent)" }}
/>
</button>
)
}
/* Tactile: tiklandiginda basparmak asagi bastirilir (y ofset). */
export function TactileToggle(props: ToggleProps) {
const { on, toggle, spring } = useToggle(props)
const reduce = useReducedMotion()
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}
whileTap={reduce ? undefined : { y: 1, scale: 0.92 }}
className={cn(thumbSize[size], "rounded-full bg-background")}
style={{ boxShadow: "0 2px 3px 0 color-mix(in oklab, var(--foreground) 30%, transparent)" }}
/>
</button>
)
}
/* Dome: kubbeli basparmak, ust highlight parlamasi. */
export function DomeToggle(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], "relative overflow-hidden rounded-full bg-background")}
style={{ boxShadow: "0 2px 4px 0 color-mix(in oklab, var(--foreground) 32%, transparent)" }}
>
<span
aria-hidden
className="absolute inset-x-1 top-0.5 h-1/3 rounded-full"
style={{ background: "color-mix(in oklab, var(--background) 85%, transparent)" }}
/>
</motion.span>
</button>
)
}
/* SwitchLever: kol/rocker gorunumu, acik/kapaliya gore egilir. */
export function SwitchLeverToggle(props: ToggleProps) {
const { on, toggle, spring } = useToggle(props)
const reduce = useReducedMotion()
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-success" : "justify-start bg-input", props.className)}
style={{ boxShadow: "inset 0 1px 3px 0 color-mix(in oklab, var(--foreground) 25%, transparent)" }}
{...stripToggleProps(props)}
>
<motion.span
layout
transition={spring}
animate={reduce ? undefined : { rotate: on ? 8 : -8 }}
className={cn(thumbSize[size], "rounded-[3px] bg-background")}
style={{ boxShadow: "0 2px 3px 0 color-mix(in oklab, var(--foreground) 30%, transparent), inset 0 1px 0 0 color-mix(in oklab, var(--background) 90%, transparent)" }}
/>
</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.
Raised
A raised thumb with a token shadow.
import { RaisedToggle } from "@/components/ui/toggles-3d"
<RaisedToggle defaultChecked={true} />Pill press
The track looks pressed and inset.
import { PillPressToggle } from "@/components/ui/toggles-3d"
<PillPressToggle defaultChecked={true} />Tactile
The thumb presses down on click.
import { TactileToggle } from "@/components/ui/toggles-3d"
<TactileToggle defaultChecked={true} />Dome
A domed thumb with a highlight.
import { DomeToggle } from "@/components/ui/toggles-3d"
<DomeToggle defaultChecked={true} />Lever
A rocker lever that tilts.
import { SwitchLeverToggle } from "@/components/ui/toggles-3d"
<SwitchLeverToggle defaultChecked={true} />ai2 3D toggles: 5 styled variations on the token system
The ai2 3D toggles are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around depth-styled 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 presses the thumb and slides it 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 press and slide are disabled and the thumb jumps.
What is in the ai2 3D toggles?
5 exports in one file: Raised, Pill press, Tactile, Dome and Lever. 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 presses the thumb and slides it across the track.
- Reduced-motion aware: Under prefers-reduced-motion, the press and slide are disabled and the thumb jumps.
- 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 3D 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.