Liquid toggles
Five liquid switches: a liquid fill, a blob, a drop, a wave and mercury. 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-liquidDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motionCopy the source
components/ui/toggles-liquid.tsx"use client"
import * as React from "react"
import { motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Liquid toggle family: 5 liquid switches. role="switch", keyboard accessible. Color
comes ONLY from tokens. The thumb slides with the framer-motion `layout` and
switches instantly under reduced-motion; the morph/flow stops 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
}
/* Liquid: dolgu toggle sirasinda sivi gibi akar. */
export function LiquidToggle(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], "overflow-hidden rounded-full bg-input", on ? "justify-end" : "justify-start", props.className)}
{...stripToggleProps(props)}
>
<motion.span
aria-hidden
className="pointer-events-none absolute inset-0 rounded-full bg-primary"
initial={false}
animate={{ clipPath: on ? "circle(140% at 100% 50%)" : "circle(0% at 100% 50%)" }}
transition={spring}
/>
<motion.span layout transition={spring} className={cn(thumbSize[size], "relative z-10 rounded-full bg-background shadow-sm")} />
</button>
)
}
/* Blob: morph eden blob basparmak. */
export function BlobToggle(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-success" : "justify-start bg-input", props.className)}
{...stripToggleProps(props)}
>
<motion.span
layout
className={cn(thumbSize[size], "bg-background shadow-sm")}
animate={
reduce
? { borderRadius: "50%" }
: { borderRadius: on ? ["50%", "60% 40% 55% 45%", "50%"] : ["50%", "45% 55% 40% 60%", "50%"] }
}
transition={{ layout: spring, borderRadius: reduce ? { duration: 0 } : { duration: 0.7, ease: "easeInOut" } }}
style={{ borderRadius: "50%" }}
/>
</button>
)
}
/* Drop: damla seklinde basparmak. */
export function DropToggle(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}
animate={reduce ? { rotate: 0 } : { rotate: on ? 135 : -45 }}
className={cn(thumbSize[size], "bg-background shadow-sm")}
style={{ borderRadius: "50% 50% 50% 0" }}
/>
</button>
)
}
/* Wave: acikken izi bir dalga doldurur. */
export function WaveToggle(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], "overflow-hidden rounded-full bg-input", on ? "justify-end" : "justify-start", props.className)}
{...stripToggleProps(props)}
>
<motion.span
aria-hidden
className="pointer-events-none absolute inset-y-0 left-0 bg-info"
initial={false}
animate={{ width: on ? "100%" : "0%" }}
transition={spring}
>
<motion.span
className="absolute top-0 right-0 h-full w-2 translate-x-1/2 rounded-full bg-info"
animate={reduce || !on ? { y: 0 } : { y: ["-12%", "12%", "-12%"] }}
transition={reduce || !on ? { duration: 0 } : { duration: 1.2, repeat: Infinity, ease: "easeInOut" }}
/>
</motion.span>
<motion.span layout transition={spring} className={cn(thumbSize[size], "relative z-10 rounded-full bg-background shadow-sm")} />
</button>
)
}
/* Mercury: metalik sivi basparmak, kayarken uzar. */
export function MercuryToggle(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-primary" : "justify-start bg-input", props.className)}
{...stripToggleProps(props)}
>
<motion.span
layout
transition={spring}
animate={reduce ? { scaleX: 1 } : { scaleX: [1, 1.5, 1] }}
className={cn(thumbSize[size], "rounded-full")}
style={{
background: "linear-gradient(160deg, color-mix(in oklab, var(--background) 92%, transparent), color-mix(in oklab, var(--foreground) 22%, transparent))",
boxShadow: "inset 0 1px 1px 0 color-mix(in oklab, var(--background) 90%, transparent), 0 1px 2px 0 color-mix(in oklab, var(--foreground) 30%, 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.
Liquid
The fill flows like liquid as it toggles.
import { LiquidToggle } from "@/components/ui/toggles-liquid"
<LiquidToggle defaultChecked={true} />Blob
A morphing blob thumb.
import { BlobToggle } from "@/components/ui/toggles-liquid"
<BlobToggle defaultChecked={true} />Drop
A droplet thumb.
import { DropToggle } from "@/components/ui/toggles-liquid"
<DropToggle defaultChecked={true} />Wave
A wave fills the track when on.
import { WaveToggle } from "@/components/ui/toggles-liquid"
<WaveToggle defaultChecked={true} />Mercury
A metallic liquid thumb that stretches as it moves.
import { MercuryToggle } from "@/components/ui/toggles-liquid"
<MercuryToggle defaultChecked={true} />ai2 Liquid toggles: 5 styled variations on the token system
The ai2 Liquid toggles are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around liquid-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 morphs the blob and flows the fill as the thumb slides. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the morph and flow stop and the thumb jumps.
What is in the ai2 Liquid toggles?
5 exports in one file: Liquid, Blob, Drop, Wave and Mercury. 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 morphs the blob and flows the fill as the thumb slides.
- Reduced-motion aware: Under prefers-reduced-motion, the morph and flow stop 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 Liquid 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.