Glass Toggle
Liquid Glass toggle switch in the iOS 26 style: the knob swells into clear glass while pressed or dragged, then settles.
- Liquid Glass
- Smart home
- React only · 0 deps
- 2 files
Browser support · Real refraction (SVG displacement inside backdrop-filter) in Chromium browsers. Safari and Firefox fall back to a frosted blur with the same rim and highlights.
Install. One command, or copy the file.
$
npx shadcn@latest add https://freedesignmd.com/r/glass-toggle.json- Byte-identical to the preview
- No Tailwind or config needed
- SSR-safe · keyboard accessible
- Respects reduced motion & transparency
components/liquid-glass/glass-toggle.tsx
1/**2 * GlassToggle, an iOS 26 switch whose knob turns into Liquid Glass while3 * you touch it.4 *5 * At rest the knob is a solid white capsule. On press it swells, the white6 * drains away and it becomes clear glass refracting the track beneath it; you7 * can drag it across, and on release it springs to its side and turns solid8 * again (HIG: transient interactive elements may become glass).9 *10 * Props: checked, onChange(checked), disabled, label (accessible name, or11 * use aria-labelledby), accent (on colour, default #34C759), size ("m" | "l"),12 * className, id. Controlled.13 * Accessibility: <button role="switch" aria-checked>; Space/Enter toggle;14 * focus ring. Reduced motion: no swell/spring, instant snap.15 * Browser support: refraction in Chromium; frosted glass elsewhere.16 */17import { useEffect, useRef, useState } from "react";18import type { CSSProperties } from "react";19import { GlassScope, LiquidGlass, useGlassEnv, useSpring } from "./liquid-glass";20 21export interface GlassToggleProps {22 checked: boolean;23 onChange: (checked: boolean) => void;24 disabled?: boolean;25 label?: string;26 "aria-labelledby"?: string;27 "aria-describedby"?: string;28 accent?: string;29 size?: "m" | "l";30 id?: string;31 className?: string;32}33 34const DIM = { m: { w: 64, h: 28, kw: 39, kh: 24, pad: 2 }, l: { w: 80, h: 36, kw: 50, kh: 31, pad: 2.5 } };35 36export function GlassToggle({ checked, onChange, disabled, label, accent = "#34C759", size = "m", id, className, ...aria }: GlassToggleProps) {37 const env = useGlassEnv();38 const root = useRef<HTMLButtonElement | null>(null);39 const knob = useRef<HTMLElement | null>(null);40 const d = DIM[size];41 const travel = d.w - d.kw - d.pad * 2;42 const [active, setActive] = useState(false);43 44 const paint = () => {45 const k = knob.current;46 if (!k) return;47 const lift = sp.value; // 0 solid → 1 glass48 const sx = 1 + lift * 0.42 + Math.min(0.18, Math.abs(kx.velocity) * 0.0006);49 const sy = 1 + lift * 0.42 - Math.min(0.08, Math.abs(kx.velocity) * 0.0003);50 k.style.transform = `translateX(${kx.value.toFixed(2)}px) scale(${sx.toFixed(4)}, ${sy.toFixed(4)})`;51 k.style.setProperty("--fd-gtg-solid", String(Math.max(0, 1 - lift * 1.25).toFixed(3)));52 root.current?.style.setProperty("--fd-gtg-p", String((kx.value / travel).toFixed(4)));53 };54 const kx = useSpring(checked ? travel : 0, paint, { stiffness: 420, damping: 26 });55 const sp = useSpring(0, paint, { stiffness: 520, damping: 30 });56 57 // follow `checked` from outside58 const firstRun = useRef(true);59 useEffect(() => {60 if (firstRun.current || env.reducedMotion) {61 firstRun.current = false;62 kx.set(checked ? travel : 0);63 return;64 }65 if (!drag.current.active) kx.to(checked ? travel : 0);66 }, [checked, travel, env.reducedMotion, kx]);67 68 const drag = useRef({ active: false, moved: false, x0: 0, k0: 0, pid: -1 });69 70 useEffect(() => {71 const el = root.current;72 if (!el) return;73 const down = (e: PointerEvent) => {74 if (disabled || e.button !== 0) return;75 const g = drag.current;76 g.active = true;77 g.moved = false;78 g.x0 = e.clientX;79 g.k0 = kx.value;80 g.pid = e.pointerId;81 el.setPointerCapture(e.pointerId);82 setActive(true);83 if (env.reducedMotion) sp.set(1);84 else sp.to(1);85 };86 const move = (e: PointerEvent) => {87 const g = drag.current;88 if (!g.active || e.pointerId !== g.pid) return;89 const dx = e.clientX - g.x0;90 if (Math.abs(dx) > 3) g.moved = true;91 if (!g.moved) return;92 let x = g.k0 + dx;93 if (x < 0) x *= 0.25;94 if (x > travel) x = travel + (x - travel) * 0.25;95 kx.stiffness = 1000;96 kx.damping = 55;97 kx.to(x);98 };99 const up = (e: PointerEvent) => {100 const g = drag.current;101 if (!g.active || e.pointerId !== g.pid) return;102 g.active = false;103 setActive(false);104 kx.stiffness = 420;105 kx.damping = 26;106 const next = g.moved ? kx.value > travel / 2 : !checked;107 if (env.reducedMotion) {108 sp.set(0);109 kx.set(next ? travel : 0);110 } else {111 sp.to(0);112 kx.to(next ? travel : 0);113 }114 if (next !== checked) onChange(next);115 };116 el.addEventListener("pointerdown", down);117 el.addEventListener("pointermove", move);118 el.addEventListener("pointerup", up);119 el.addEventListener("pointercancel", up);120 return () => {121 el.removeEventListener("pointerdown", down);122 el.removeEventListener("pointermove", move);123 el.removeEventListener("pointerup", up);124 el.removeEventListener("pointercancel", up);125 };126 }, [checked, disabled, onChange, travel, env.reducedMotion, kx, sp]);127 128 return (129 <GlassScope>130 <button131 ref={root}132 id={id}133 type="button"134 role="switch"135 aria-checked={checked}136 aria-label={label}137 disabled={disabled}138 className={["fd-gtg", `fd-gtg--${size}`, active ? "is-active" : "", className ?? ""].filter(Boolean).join(" ")}139 style={{ "--fd-gtg-accent": accent, "--fd-gtg-w": `${d.w}px`, "--fd-gtg-h": `${d.h}px`, "--fd-gtg-kw": `${d.kw}px`, "--fd-gtg-kh": `${d.kh}px`, "--fd-gtg-pad": `${d.pad}px`, "--fd-gtg-p": checked ? 1 : 0 } as CSSProperties}140 onClick={(e) => {141 // pointer toggles are handled on pointerup; this is keyboard / AT activation142 if (e.detail === 0 && !disabled) onChange(!checked);143 }}144 {...aria}145 >146 <style>{css}</style>147 <span className="fd-gtg-track" aria-hidden="true">148 <span className="fd-gtg-on" />149 </span>150 <LiquidGlass ref={knob} as="span" className="fd-gtg-knob" variant="clear" radius="capsule" refraction={1.25} dispersion={0.1} shadow={false} aria-hidden="true">151 <span className="fd-gtg-solid" />152 </LiquidGlass>153 </button>154 </GlassScope>155 );156}157 158const css = `159.fd-gtg,.fd-gtg *,.fd-gtg *::before,.fd-gtg *::after{box-sizing:border-box}160.fd-gtg{position:relative;display:inline-block;flex:none;width:var(--fd-gtg-w);height:var(--fd-gtg-h);margin:0;padding:0;border:0;border-radius:999px;background:none;color:inherit;font:inherit;vertical-align:middle;161 cursor:pointer;touch-action:none;-webkit-appearance:none;appearance:none;-webkit-tap-highlight-color:transparent;outline:none}162.fd-gtg:focus-visible{outline:2px solid color-mix(in srgb,#0A84FF 70%,transparent);outline-offset:2px}163.fd-gtg:disabled{cursor:default;opacity:0.45}164.fd-gtg-track{position:absolute;inset:0;display:block;margin:0;padding:0;border-radius:inherit;overflow:hidden;background:rgba(120,120,128,0.16);box-shadow:inset 0 0 0 0.5px rgba(0,0,0,0.04)}165.fd-gtg-on{position:absolute;inset:0;display:block;margin:0;padding:0;border-radius:inherit;background:var(--fd-gtg-accent);opacity:var(--fd-gtg-p);transform-origin:0 50%}166.fd-gtg-knob{position:absolute !important;left:var(--fd-gtg-pad);top:var(--fd-gtg-pad);display:block;width:var(--fd-gtg-kw);height:var(--fd-gtg-kh);margin:0;padding:0;will-change:transform;167 --fd-lg-shadow:0 3px 8px rgba(0,0,0,0.15),0 1px 1px rgba(0,0,0,0.12),0 3px 1px rgba(0,0,0,0.04)}168.fd-gtg.is-active .fd-gtg-knob{--fd-lg-shadow:0 8px 22px -6px rgba(0,0,0,0.28),0 1px 2px rgba(0,0,0,0.08)}169.fd-gtg-solid{position:absolute;inset:0;display:block;margin:0;padding:0;border-radius:inherit;background:#FFFFFF;opacity:var(--fd-gtg-solid,1)}170.fd-gtg-knob .fd-lg-spec{opacity:calc(1 - var(--fd-gtg-solid,1) * 0.85)}171.fd-gtg-knob .fd-lg-fill{background:rgba(255,255,255,0.04)}172@media (prefers-contrast: more){.fd-gtg-track{box-shadow:inset 0 0 0 1px rgba(0,0,0,0.6)}.fd-gtg-solid{opacity:1 !important}}173@media (prefers-reduced-transparency: reduce){.fd-gtg-solid{opacity:1 !important}}174`;175 176/* ───────────────────────── demo ───────────────────────── */177 178export function Demo() {179 const [a, setA] = useState(true);180 const [b, setB] = useState(false);181 const [c, setC] = useState(true);182 return (183 <div className="fd-gtgd">184 <style>{demoCss}</style>185 <div className="fd-gtgd-row">186 <GlassToggle size="l" checked={a} onChange={setA} label="Wi-Fi" />187 <GlassToggle size="l" checked={b} onChange={setB} label="Bluetooth" />188 <GlassToggle size="l" checked={c} onChange={setC} label="Focus" accent="#0A84FF" />189 </div>190 </div>191 );192}193 194const F = `ui-sans-serif,-apple-system,BlinkMacSystemFont,"SF Pro Text","Inter","Segoe UI",Roboto,sans-serif`;195 196const demoCss = `197.fd-gtgd,.fd-gtgd *,.fd-gtgd *::before,.fd-gtgd *::after{box-sizing:border-box}198.fd-gtgd{display:flex;align-items:center;justify-content:center;min-height:100vh;width:100%;margin:0;padding:32px 20px;overflow:hidden;color:#1d1d1f;font:400 15px/1.4 ${F};letter-spacing:normal;199 background:radial-gradient(46% 62% at 28% 32%,#ffcfa6 0%,transparent 70%),radial-gradient(44% 58% at 74% 68%,#b7c3ff 0%,transparent 70%),radial-gradient(30% 40% at 62% 22%,#ffc2dc 0%,transparent 70%),#f1eeea}200.fd-gtgd-row{display:flex;flex-wrap:wrap;align-items:center;justify-content:center;gap:28px;margin:0;padding:0}201`;More Liquid Glass. Same file contract.
Questions about Glass Toggle
Run npx shadcn@latest add https://freedesignmd.com/r/glass-toggle.json, or open the page and copy the files into your project. It has zero dependencies besides React. What you copy is the exact file that renders the live demo.
Liquid Glass toggle switch in the iOS 26 style: the knob swells into clear glass while pressed or dragged, then settles. It is a free, MIT-licensed React liquid glass component from the freedesignmd library.
Yes. It is free and MIT-licensed, use, modify and ship it in personal and commercial projects without attribution.
Real refraction (SVG displacement inside backdrop-filter) in Chromium browsers. Safari and Firefox fall back to a frosted blur with the same rim and highlights.
Yes. It is a plain React component with its own styles, so it runs in Next.js, Vite, Remix and Lovable projects, and it needs no Tailwind setup. It has zero dependencies besides React.


