Glass Slider
Liquid Glass slider: the thumb becomes refracting glass while dragged, stretches with velocity and springs back to solid.
- Liquid Glass
- Automotive
- 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-slider.json- Byte-identical to the preview
- No Tailwind or config needed
- SSR-safe · keyboard accessible
- Respects reduced motion & transparency
components/liquid-glass/glass-slider.tsx
1/**2 * GlassSlider, a slider whose thumb turns into Liquid Glass while dragged.3 *4 * At rest the thumb is a compact solid capsule. Grab it and it swells into5 * clear glass that refracts the fill and track underneath, stretches along6 * its velocity (area-preserving) and springs back to solid on release.7 *8 * Props: value, onChange(value), min (0), max (100), step (1), label9 * (accessible name) or aria-labelledby, valueText(value) for10 * aria-valuetext, accent (fill colour or CSS gradient), tone ("light" |11 * "dark"), ticks (number of tick marks), disabled, className.12 * Accessibility: native <input type="range"> drives everything, Arrow keys,13 * PageUp/PageDown, Home/End and screen readers work as usual.14 * Reduced motion: no swell or stretch, thumb moves instantly.15 * Browser support: refraction in Chromium; frosted glass elsewhere.16 */17import { useEffect, useRef, useState } from "react";18import type { CSSProperties } from "react";19import { GlassScope, LiquidGlass, createVelocityTracker, useGlassEnv, useSpring } from "./liquid-glass";20 21export interface GlassSliderProps {22 value: number;23 onChange: (value: number) => void;24 min?: number;25 max?: number;26 step?: number;27 label?: string;28 "aria-labelledby"?: string;29 valueText?: (v: number) => string;30 /** Fill colour or gradient. */31 accent?: string;32 tone?: "light" | "dark";33 ticks?: number;34 disabled?: boolean;35 className?: string;36 id?: string;37}38 39const KW = 38; // thumb width at rest40const KH = 24;41 42export function GlassSlider({43 value,44 onChange,45 min = 0,46 max = 100,47 step = 1,48 label,49 valueText,50 accent = "#0A84FF",51 tone = "light",52 ticks = 0,53 disabled,54 className,55 id,56 ...aria57}: GlassSliderProps) {58 const env = useGlassEnv();59 const root = useRef<HTMLDivElement | null>(null);60 const thumb = useRef<HTMLElement | null>(null);61 const input = useRef<HTMLInputElement | null>(null);62 const [active, setActive] = useState(false);63 const pct = (value - min) / (max - min || 1);64 65 const width = useRef(0);66 const paint = () => {67 const t = thumb.current;68 const r = root.current;69 if (!t || !r) return;70 const lift = sp.value;71 const v = env.reducedMotion ? 0 : px.velocity;72 const st = Math.min(0.32, Math.abs(v) * 0.0005);73 const s = 1 + lift * 0.55;74 const x = px.value * (width.current - KW);75 t.style.transform = `translateX(${x.toFixed(2)}px) scale(${(s * (1 + st)).toFixed(4)}, ${(s / (1 + st * 0.7)).toFixed(4)})`;76 t.style.setProperty("--fd-gsl-solid", Math.max(0, 1 - lift * 1.3).toFixed(3));77 r.style.setProperty("--fd-gsl-x", `${(x + KW / 2).toFixed(2)}px`);78 };79 const px = useSpring(pct, paint, { stiffness: 520, damping: 34, precision: 0.0002 });80 const sp = useSpring(0, paint, { stiffness: 520, damping: 30 });81 82 // measure + follow external value changes83 useEffect(() => {84 const r = root.current;85 if (!r) return;86 const measure = () => {87 width.current = r.clientWidth;88 paint();89 };90 measure();91 const ro = typeof ResizeObserver !== "undefined" ? new ResizeObserver(measure) : null;92 ro?.observe(r);93 return () => ro?.disconnect();94 // eslint-disable-next-line react-hooks/exhaustive-deps95 }, []);96 const dragging = useRef(false);97 useEffect(() => {98 if (dragging.current) return;99 if (env.reducedMotion) px.set(pct);100 else px.to(pct);101 }, [pct, env.reducedMotion, px]);102 103 // latest props for the long-lived pointer handlers104 const live = useRef({ value, min, max, step, disabled, onChange });105 live.current = { value, min, max, step, disabled, onChange };106 107 // pointer: drag anywhere on the track; thumb follows the finger continuously, value snaps to step108 useEffect(() => {109 const r = root.current;110 if (!r) return;111 const vt = createVelocityTracker();112 let pid = -1;113 let grabOffset = 0;114 const toPct = (clientX: number) => {115 const b = r.getBoundingClientRect();116 return Math.min(1, Math.max(0, (clientX - b.left - grabOffset - KW / 2) / (b.width - KW)));117 };118 const emit = (p: number) => {119 const { value, min, max, step, onChange } = live.current;120 const raw = min + p * (max - min);121 const snapped = Math.min(max, Math.max(min, Math.round((raw - min) / step) * step + min));122 if (snapped !== value) onChange(Number(snapped.toFixed(6)));123 };124 const down = (e: PointerEvent) => {125 if (live.current.disabled || e.button !== 0) return;126 e.preventDefault();127 pid = e.pointerId;128 r.setPointerCapture(pid);129 r.dataset.pointer = "1";130 input.current?.focus({ preventScroll: true });131 const b = r.getBoundingClientRect();132 const thumbX = b.left + px.value * (b.width - KW) + KW / 2;133 // grabbing the thumb keeps the offset; tapping the track jumps there134 grabOffset = Math.abs(e.clientX - thumbX) < KW ? e.clientX - thumbX : 0;135 dragging.current = true;136 setActive(true);137 vt.reset();138 vt.push(e.clientX, 0);139 if (env.reducedMotion) sp.set(1);140 else sp.to(1);141 const p = toPct(e.clientX);142 px.stiffness = 700;143 px.damping = 40;144 if (env.reducedMotion) px.set(p);145 else px.to(p);146 emit(p);147 };148 const move = (e: PointerEvent) => {149 if (e.pointerId !== pid) return;150 vt.push(e.clientX, 0);151 const p = toPct(e.clientX);152 if (env.reducedMotion) px.set(p);153 else px.to(p);154 emit(p);155 };156 const up = (e: PointerEvent) => {157 if (e.pointerId !== pid) return;158 pid = -1;159 dragging.current = false;160 setActive(false);161 px.stiffness = 520;162 px.damping = 34;163 if (env.reducedMotion) sp.set(0);164 else sp.to(0);165 };166 r.addEventListener("pointerdown", down);167 r.addEventListener("pointermove", move);168 r.addEventListener("pointerup", up);169 r.addEventListener("pointercancel", up);170 return () => {171 r.removeEventListener("pointerdown", down);172 r.removeEventListener("pointermove", move);173 r.removeEventListener("pointerup", up);174 r.removeEventListener("pointercancel", up);175 };176 }, [env.reducedMotion, px, sp]);177 178 // after a drag ends, glide to the snapped position179 useEffect(() => {180 if (!active && !dragging.current) px.to(pct);181 }, [active, pct, px]);182 183 return (184 <GlassScope>185 <div186 ref={root}187 className={["fd-gsl", `fd-gsl--${tone}`, active ? "is-active" : "", disabled ? "is-disabled" : "", className ?? ""].filter(Boolean).join(" ")}188 style={{ "--fd-gsl-accent": accent, "--fd-gsl-kw": `${KW}px`, "--fd-gsl-kh": `${KH}px`, "--fd-gsl-x": `calc(${pct} * (100% - ${KW}px) + ${KW / 2}px)` } as CSSProperties}189 >190 <style>{css}</style>191 <span className="fd-gsl-track" aria-hidden="true">192 <span className="fd-gsl-fill" />193 {ticks > 1 && (194 <span className="fd-gsl-ticks">195 {Array.from({ length: ticks }, (_, i) => (196 <i key={i} />197 ))}198 </span>199 )}200 </span>201 <LiquidGlass ref={thumb} as="span" className="fd-gsl-thumb" variant="clear" radius="capsule" refraction={1.3} dispersion={0.1} shadow={false} aria-hidden="true">202 <span className="fd-gsl-solid" />203 </LiquidGlass>204 <input205 ref={input}206 id={id}207 className="fd-gsl-input"208 type="range"209 min={min}210 max={max}211 step={step}212 value={value}213 disabled={disabled}214 aria-label={label}215 aria-valuetext={valueText?.(value)}216 onChange={(e) => onChange(Number(e.target.value))}217 onKeyDown={() => root.current?.removeAttribute("data-pointer")}218 {...aria}219 />220 </div>221 </GlassScope>222 );223}224 225const css = `226.fd-gsl,.fd-gsl *,.fd-gsl *::before,.fd-gsl *::after{box-sizing:border-box}227.fd-gsl{--fd-gsl-track:rgba(120,120,128,0.2);position:relative;display:block;width:100%;height:36px;margin:0;padding:0;touch-action:none;cursor:pointer;user-select:none;-webkit-user-select:none;-webkit-tap-highlight-color:transparent}228.fd-gsl--dark{--fd-gsl-track:rgba(255,255,255,0.18)}229.fd-gsl.is-disabled{opacity:0.45;cursor:default}230.fd-gsl-track{position:absolute;left:0;right:0;top:50%;height:6px;margin:-3px 0 0;padding:0;border-radius:999px;background:var(--fd-gsl-track);overflow:hidden;display:block}231.fd-gsl-fill{position:absolute;left:0;top:0;bottom:0;width:var(--fd-gsl-x);display:block;margin:0;padding:0;border-radius:inherit;background:var(--fd-gsl-accent)}232.fd-gsl-ticks{position:absolute;inset:0;display:flex;justify-content:space-between;align-items:center;margin:0;padding:0 calc(var(--fd-gsl-kw) / 2 - 1px)}233.fd-gsl-ticks i{display:block;width:2px;height:2px;margin:0;border-radius:2px;background:rgba(255,255,255,0.7)}234.fd-gsl-thumb{position:absolute !important;left:0;top:50%;display:block;width:var(--fd-gsl-kw);height:var(--fd-gsl-kh);margin:calc(var(--fd-gsl-kh) / -2) 0 0;padding:0;pointer-events:none;will-change:transform;235 --fd-lg-shadow:0 3px 8px rgba(0,0,0,0.16),0 1px 1px rgba(0,0,0,0.12)}236.fd-gsl.is-active .fd-gsl-thumb{--fd-lg-shadow:0 10px 24px -8px rgba(0,0,0,0.3)}237.fd-gsl-solid{position:absolute;inset:0;display:block;margin:0;padding:0;border-radius:inherit;background:#FFFFFF;opacity:var(--fd-gsl-solid,1)}238.fd-gsl-thumb .fd-lg-spec{opacity:calc(1 - var(--fd-gsl-solid,1) * 0.85)}239.fd-gsl-thumb .fd-lg-fill{background:rgba(255,255,255,0.04)}240.fd-gsl-input{position:absolute;inset:0;width:100%;height:100%;margin:0;padding:0;border:0;opacity:0;pointer-events:none;-webkit-appearance:none;appearance:none;background:none}241.fd-gsl:not([data-pointer]):has(.fd-gsl-input:focus-visible) .fd-gsl-thumb{outline:2px solid color-mix(in srgb,#0A84FF 70%,transparent);outline-offset:3px}242@media (prefers-contrast: more){.fd-gsl-track{box-shadow:inset 0 0 0 1px rgba(0,0,0,0.6)}.fd-gsl-solid{opacity:1 !important}}243@media (prefers-reduced-transparency: reduce){.fd-gsl-solid{opacity:1 !important}}244`;245 246/* ───────────────────────── demo ───────────────────────── */247 248export function Demo() {249 const [v, setV] = useState(64);250 return (251 <div className="fd-gsld">252 <style>{demoCss}</style>253 <div className="fd-gsld-box">254 <GlassSlider value={v} onChange={setV} label="Brightness" valueText={(n) => `${n} percent`} />255 </div>256 </div>257 );258}259 260const F = `ui-sans-serif,-apple-system,BlinkMacSystemFont,"SF Pro Text","Inter","Segoe UI",Roboto,sans-serif`;261 262const demoCss = `263.fd-gsld,.fd-gsld *,.fd-gsld *::before,.fd-gsld *::after{box-sizing:border-box}264.fd-gsld{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;265 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}266.fd-gsld-box{width:min(100%,320px);margin:0;padding:0}267`;More Liquid Glass. Same file contract.
Questions about Glass Slider
Run npx shadcn@latest add https://freedesignmd.com/r/glass-slider.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 slider: the thumb becomes refracting glass while dragged, stretches with velocity and springs back to solid. 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.


