Liquid glass controls
A control-centre panel in liquid glass: a segmented switch with a sliding lens, sliders and toggles whose thumbs swell into clear lenses as you hold them, and glass buttons that catch the light.
"use client";
import {
useRef,
useState,
type CSSProperties,
type KeyboardEvent,
type PointerEvent,
type ReactNode,
} from "react";
import { Glass, GlassScene } from "./LiquidGlass";
import "./glass-controls.css";
/** Turns pointer speed into a stretch: the faster you move, the longer the lens. */
function stretch(el: HTMLElement | null, v: number) {
if (!el) return;
const k = Math.min(0.4, Math.abs(v) * 0.22);
el.style.setProperty("--sx", String(1 + k));
el.style.setProperty("--sy", String(1 - k * 0.4));
}
/* ---------- Toggle ---------- */
export function GlassToggle({
label,
defaultOn = false,
}: {
label: string;
defaultOn?: boolean;
}) {
const [on, setOn] = useState(defaultOn);
return (
<label className="gu-gc__toggle-row">
<span>{label}</span>
<button
type="button"
role="switch"
aria-checked={on}
aria-label={label}
className="gu-gc__toggle"
onClick={() => setOn((v) => !v)}
>
{/* On press the thumb turns into a clear lens and stretches while it travels. */}
<Glass
radius={999}
depth={18}
edge={9}
tint={0}
className="gu-gc__knob"
data-hop={on ? "a" : "b"}
aria-hidden="true"
/>
</button>
</label>
);
}
/* ---------- Slider ---------- */
export function GlassSlider({
label,
defaultValue = 60,
icon,
}: {
label: string;
defaultValue?: number;
icon?: "sun" | "sound";
}) {
const [value, setValue] = useState(defaultValue);
const track = useRef<HTMLDivElement>(null);
const knob = useRef<HTMLElement | null>(null);
const last = useRef<{ x: number; t: number } | null>(null);
const at = (x: number) => {
const b = track.current!.getBoundingClientRect();
return Math.round(Math.max(0, Math.min(1, (x - b.left) / b.width)) * 100);
};
const down = (e: PointerEvent<HTMLDivElement>) => {
e.currentTarget.setPointerCapture(e.pointerId);
e.currentTarget.dataset.held = "true";
last.current = { x: e.clientX, t: e.timeStamp };
setValue(at(e.clientX));
};
const move = (e: PointerEvent<HTMLDivElement>) => {
const l = last.current;
if (!l) return;
stretch(knob.current, (e.clientX - l.x) / Math.max(1, e.timeStamp - l.t));
last.current = { x: e.clientX, t: e.timeStamp };
setValue(at(e.clientX));
};
const up = (e: PointerEvent<HTMLDivElement>) => {
last.current = null;
e.currentTarget.dataset.held = "false";
stretch(knob.current, 0);
};
const keys = (e: KeyboardEvent) => {
const step = {
ArrowRight: 5,
ArrowUp: 5,
ArrowLeft: -5,
ArrowDown: -5,
PageUp: 20,
PageDown: -20,
}[e.key];
if (step) {
e.preventDefault();
setValue((v) => Math.max(0, Math.min(100, v + step)));
} else if (e.key === "Home" || e.key === "End") {
e.preventDefault();
setValue(e.key === "Home" ? 0 : 100);
}
};
return (
<div className="gu-gc__slider-wrap">
<span className="gu-gc__slider-label">
{label}
<output>{value}%</output>
</span>
<div
ref={track}
className="gu-gc__slider"
role="slider"
tabIndex={0}
aria-label={label}
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={value}
aria-valuetext={`${value}%`}
onPointerDown={down}
onPointerMove={move}
onPointerUp={up}
onPointerCancel={up}
onKeyDown={keys}
style={{ "--v": value / 100 } as CSSProperties}
>
<span className="gu-gc__fill" aria-hidden="true">
{icon && (
<svg
viewBox="0 0 24 24"
className="gu-gc__slider-icon"
style={{ "--v": value / 100 } as CSSProperties}
>
{icon === "sun" ? (
<>
<circle cx="12" cy="12" r="4" />
<path d="M12 2.5v2M12 19.5v2M2.5 12h2M19.5 12h2M5.3 5.3l1.4 1.4M17.3 17.3l1.4 1.4M5.3 18.7l1.4-1.4M17.3 6.7l1.4-1.4" />
</>
) : (
<path d="M4 9.5h3.5L12 5.5v13l-4.5-4H4ZM15.5 9a4 4 0 0 1 0 6M18 6.5a7.5 7.5 0 0 1 0 11" />
)}
</svg>
)}
</span>
<span className="gu-gc__ticks" aria-hidden="true">
{Array.from({ length: 11 }, (_, i) => (
<i key={i} data-lit={i * 10 <= value} />
))}
</span>
<Glass
radius={999}
depth={20}
edge={10}
tint={0}
className="gu-gc__thumb"
glassRef={(n) => (knob.current = n)}
aria-hidden="true"
/>
</div>
</div>
);
}
/* ---------- Segmented control ---------- */
export function GlassSegmented({
label,
options,
defaultIndex = 0,
}: {
label: string;
options: string[];
defaultIndex?: number;
}) {
const [i, setI] = useState(defaultIndex);
const [from, setFrom] = useState(defaultIndex);
const [hop, setHop] = useState(0);
const box = useRef<HTMLDivElement>(null);
const pick = (to: number) => {
if (to === i) return;
setFrom(i);
setI(to);
setHop((h) => h + 1);
};
const keys = (e: KeyboardEvent) => {
const k = { ArrowRight: 1, ArrowDown: 1, ArrowLeft: -1, ArrowUp: -1 }[
e.key
];
if (!k) return;
e.preventDefault();
const to = (i + k + options.length) % options.length;
pick(to);
box.current
?.querySelectorAll<HTMLButtonElement>("[role=radio]")
[to]?.focus();
};
return (
<div
ref={box}
className="gu-gc__seg"
role="radiogroup"
aria-label={label}
onKeyDown={keys}
style={{ "--n": options.length, "--i": i } as CSSProperties}
>
<Glass
radius={999}
depth={18}
edge={10}
tint={0.18}
className="gu-gc__seg-lens"
data-hop={hop === 0 ? undefined : hop % 2}
style={{ "--st": Math.abs(i - from) > 1 ? 0.5 : 0.3 } as CSSProperties}
aria-hidden="true"
/>
{options.map((o, j) => (
<button
key={o}
type="button"
role="radio"
aria-checked={j === i}
tabIndex={j === i ? 0 : -1}
onClick={() => pick(j)}
>
{o}
</button>
))}
</div>
);
}
/* ---------- Button ---------- */
export function GlassButton({
children,
tone = "light",
onClick,
}: {
children: ReactNode;
tone?: "light" | "dark";
onClick?: () => void;
}) {
const [rip, setRip] = useState<{ x: number; y: number; id: number } | null>(
null,
);
return (
<Glass
as="button"
type="button"
radius={999}
depth={24}
edge={12}
tone={tone}
className="gu-gc__btn"
onPointerDown={(e: PointerEvent<HTMLElement>) => {
const b = e.currentTarget.getBoundingClientRect();
setRip({
x: e.clientX - b.left,
y: e.clientY - b.top,
id: e.timeStamp,
});
}}
onClick={onClick}
>
{rip && (
<span
key={rip.id}
className="gu-gc__ripple"
style={{ left: rip.x, top: rip.y }}
aria-hidden="true"
/>
)}
<span className="gu-gc__btn-text">{children}</span>
</Glass>
);
}
/**
* GlassControls: a control-centre panel of liquid glass controls over moving colour.
* - Toggle: the thumb turns into a clear lens while pressed and stretches as it travels.
* - Slider: the thumb lifts into a lens that stretches with your speed; lit ticks follow the value.
* - Segmented switch: a glass lens slides between options with a liquid wobble.
* - Buttons: the rim catches the light under the pointer and a soft ripple spreads from your press.
*/
export function GlassControls() {
const [saved, setSaved] = useState(false);
return (
<GlassScene className="gu-gc" words={["Bright", "Calm", "Focus", "Glow"]}>
<div className="gu-gc__grid">
<Glass
radius={30}
tone="dark"
tint={0.02}
className="gu-gc__tile gu-gc__tile--wide"
>
<GlassSegmented
label="Mode"
options={["Day", "Dusk", "Night"]}
defaultIndex={1}
/>
</Glass>
<Glass radius={30} tone="dark" tint={0.02} className="gu-gc__tile">
<GlassSlider label="Brightness" icon="sun" defaultValue={70} />
<GlassSlider label="Volume" icon="sound" defaultValue={35} />
</Glass>
<Glass radius={30} tone="dark" tint={0.02} className="gu-gc__tile">
<GlassToggle label="Wi-Fi" defaultOn />
<GlassToggle label="Focus" />
<GlassToggle label="Night light" defaultOn />
</Glass>
<div className="gu-gc__actions">
<GlassButton onClick={() => setSaved((v) => !v)}>
<span key={String(saved)} className="gu-gc__swap">
{saved ? "Saved" : "Save scene"}
</span>
</GlassButton>
<GlassButton tone="dark">Reset</GlassButton>
</div>
</div>
</GlassScene>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Liquid glass controls
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A "control centre" panel built from liquid glass, shown over a colourful moving backdrop: a segmented switch (Day / Dusk / Night), two sliders (brightness and volume), three toggles and two glass buttons. Every thumb behaves like a drop of liquid glass: it swells into a clear lens while you hold it and stretches in the direction it moves.
## Layout and look
- Backdrop (drawn in code): near-black #0e0e10, four big blurred colour blobs (#ff5a1f, #2b35ff, #ff3d8b, #d7ff3a) drifting over 15–26s, three rows of huge bold words sliding sideways in overlay blend mode, and a faint 28px grid. Radius 28px, padding 16–32px, max width 620px.
- A 2-column grid (1 column under 520px) of glass tiles, radius 30px, gap 14px. The segmented switch spans the full width; then a tile with two sliders and a tile with three toggle rows; the buttons sit centred below.
- White text, system sans-serif, 600 weight labels at 13–15px.
## The glass
Same recipe as all GiftUI glass: rgba(255,255,255,0.1) fill, backdrop-filter blur + saturate(1.9) + brightness(1.06), real rim refraction through an SVG feDisplacementMap filter in Chromium (map generated per size with a ResizeObserver), a 1.2px rim highlight that follows the pointer (--lx/--ly), a top sheen and soft shadows. Without Chromium: blur(14px) only.
## Behavior and motion
1. Tiles rise in on load (18px, 0.96 scale, blur 8px → 0), 80ms apart.
2. Segmented: a glass lens slides to the chosen option (600ms spring) and stretches on the way (scale 1.3 × 0.88 at 30%, 1.5 when skipping one, then 0.95 × 1.05). Two identical keyframes alternate so it replays.
3. Slider: 40px track with a white fill (min 40px) holding an icon that rotates and grows with the value, 11 ticks that get taller when lit. The 52px white thumb, when held, becomes almost clear glass and scales 1.35 × 1.5, and stretches with pointer speed (up to +0.4) while you drag; it springs back on release.
4. Toggle: 64 × 30 track, green #34c759 when on. The thumb springs across (500ms) with a squash keyframe (1.45 × 1.1); while pressed it swells into a clear lens (1.5 × 1.45).
5. Buttons: lift 2px on hover, press to 0.95, a soft white ripple spreads from the press point; the label swaps with a blur-in ("Save scene" → "Saved").
## Accessibility
- Segmented is a radiogroup with arrow keys and roving tabIndex. Sliders are role="slider" with arrows (±5), Page Up/Down (±20), Home/End and aria-valuetext. Toggles are role="switch" buttons with aria-checked inside a label. Visible white focus rings.
- prefers-reduced-motion: no stretching, wobble, rise-in or drifting. prefers-reduced-transparency: solid surfaces.
## Props
GlassControls has none; the parts are exported separately: GlassSegmented { label, options, defaultIndex }, GlassSlider { label, defaultValue, icon: "sun" | "sound" }, GlassToggle { label, defaultOn }, GlassButton { children, tone, onClick }.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### GlassControls.tsx
```tsx
"use client";
import {
useRef,
useState,
type CSSProperties,
type KeyboardEvent,
type PointerEvent,
type ReactNode,
} from "react";
import { Glass, GlassScene } from "./LiquidGlass";
import "./glass-controls.css";
/** Turns pointer speed into a stretch: the faster you move, the longer the lens. */
function stretch(el: HTMLElement | null, v: number) {
if (!el) return;
const k = Math.min(0.4, Math.abs(v) * 0.22);
el.style.setProperty("--sx", String(1 + k));
el.style.setProperty("--sy", String(1 - k * 0.4));
}
/* ---------- Toggle ---------- */
export function GlassToggle({
label,
defaultOn = false,
}: {
label: string;
defaultOn?: boolean;
}) {
const [on, setOn] = useState(defaultOn);
return (
<label className="gu-gc__toggle-row">
<span>{label}</span>
<button
type="button"
role="switch"
aria-checked={on}
aria-label={label}
className="gu-gc__toggle"
onClick={() => setOn((v) => !v)}
>
{/* On press the thumb turns into a clear lens and stretches while it travels. */}
<Glass
radius={999}
depth={18}
edge={9}
tint={0}
className="gu-gc__knob"
data-hop={on ? "a" : "b"}
aria-hidden="true"
/>
</button>
</label>
);
}
/* ---------- Slider ---------- */
export function GlassSlider({
label,
defaultValue = 60,
icon,
}: {
label: string;
defaultValue?: number;
icon?: "sun" | "sound";
}) {
const [value, setValue] = useState(defaultValue);
const track = useRef<HTMLDivElement>(null);
const knob = useRef<HTMLElement | null>(null);
const last = useRef<{ x: number; t: number } | null>(null);
const at = (x: number) => {
const b = track.current!.getBoundingClientRect();
return Math.round(Math.max(0, Math.min(1, (x - b.left) / b.width)) * 100);
};
const down = (e: PointerEvent<HTMLDivElement>) => {
e.currentTarget.setPointerCapture(e.pointerId);
e.currentTarget.dataset.held = "true";
last.current = { x: e.clientX, t: e.timeStamp };
setValue(at(e.clientX));
};
const move = (e: PointerEvent<HTMLDivElement>) => {
const l = last.current;
if (!l) return;
stretch(knob.current, (e.clientX - l.x) / Math.max(1, e.timeStamp - l.t));
last.current = { x: e.clientX, t: e.timeStamp };
setValue(at(e.clientX));
};
const up = (e: PointerEvent<HTMLDivElement>) => {
last.current = null;
e.currentTarget.dataset.held = "false";
stretch(knob.current, 0);
};
const keys = (e: KeyboardEvent) => {
const step = {
ArrowRight: 5,
ArrowUp: 5,
ArrowLeft: -5,
ArrowDown: -5,
PageUp: 20,
PageDown: -20,
}[e.key];
if (step) {
e.preventDefault();
setValue((v) => Math.max(0, Math.min(100, v + step)));
} else if (e.key === "Home" || e.key === "End") {
e.preventDefault();
setValue(e.key === "Home" ? 0 : 100);
}
};
return (
<div className="gu-gc__slider-wrap">
<span className="gu-gc__slider-label">
{label}
<output>{value}%</output>
</span>
<div
ref={track}
className="gu-gc__slider"
role="slider"
tabIndex={0}
aria-label={label}
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={value}
aria-valuetext={`${value}%`}
onPointerDown={down}
onPointerMove={move}
onPointerUp={up}
onPointerCancel={up}
onKeyDown={keys}
style={{ "--v": value / 100 } as CSSProperties}
>
<span className="gu-gc__fill" aria-hidden="true">
{icon && (
<svg
viewBox="0 0 24 24"
className="gu-gc__slider-icon"
style={{ "--v": value / 100 } as CSSProperties}
>
{icon === "sun" ? (
<>
<circle cx="12" cy="12" r="4" />
<path d="M12 2.5v2M12 19.5v2M2.5 12h2M19.5 12h2M5.3 5.3l1.4 1.4M17.3 17.3l1.4 1.4M5.3 18.7l1.4-1.4M17.3 6.7l1.4-1.4" />
</>
) : (
<path d="M4 9.5h3.5L12 5.5v13l-4.5-4H4ZM15.5 9a4 4 0 0 1 0 6M18 6.5a7.5 7.5 0 0 1 0 11" />
)}
</svg>
)}
</span>
<span className="gu-gc__ticks" aria-hidden="true">
{Array.from({ length: 11 }, (_, i) => (
<i key={i} data-lit={i * 10 <= value} />
))}
</span>
<Glass
radius={999}
depth={20}
edge={10}
tint={0}
className="gu-gc__thumb"
glassRef={(n) => (knob.current = n)}
aria-hidden="true"
/>
</div>
</div>
);
}
/* ---------- Segmented control ---------- */
export function GlassSegmented({
label,
options,
defaultIndex = 0,
}: {
label: string;
options: string[];
defaultIndex?: number;
}) {
const [i, setI] = useState(defaultIndex);
const [from, setFrom] = useState(defaultIndex);
const [hop, setHop] = useState(0);
const box = useRef<HTMLDivElement>(null);
const pick = (to: number) => {
if (to === i) return;
setFrom(i);
setI(to);
setHop((h) => h + 1);
};
const keys = (e: KeyboardEvent) => {
const k = { ArrowRight: 1, ArrowDown: 1, ArrowLeft: -1, ArrowUp: -1 }[
e.key
];
if (!k) return;
e.preventDefault();
const to = (i + k + options.length) % options.length;
pick(to);
box.current
?.querySelectorAll<HTMLButtonElement>("[role=radio]")
[to]?.focus();
};
return (
<div
ref={box}
className="gu-gc__seg"
role="radiogroup"
aria-label={label}
onKeyDown={keys}
style={{ "--n": options.length, "--i": i } as CSSProperties}
>
<Glass
radius={999}
depth={18}
edge={10}
tint={0.18}
className="gu-gc__seg-lens"
data-hop={hop === 0 ? undefined : hop % 2}
style={{ "--st": Math.abs(i - from) > 1 ? 0.5 : 0.3 } as CSSProperties}
aria-hidden="true"
/>
{options.map((o, j) => (
<button
key={o}
type="button"
role="radio"
aria-checked={j === i}
tabIndex={j === i ? 0 : -1}
onClick={() => pick(j)}
>
{o}
</button>
))}
</div>
);
}
/* ---------- Button ---------- */
export function GlassButton({
children,
tone = "light",
onClick,
}: {
children: ReactNode;
tone?: "light" | "dark";
onClick?: () => void;
}) {
const [rip, setRip] = useState<{ x: number; y: number; id: number } | null>(
null,
);
return (
<Glass
as="button"
type="button"
radius={999}
depth={24}
edge={12}
tone={tone}
className="gu-gc__btn"
onPointerDown={(e: PointerEvent<HTMLElement>) => {
const b = e.currentTarget.getBoundingClientRect();
setRip({
x: e.clientX - b.left,
y: e.clientY - b.top,
id: e.timeStamp,
});
}}
onClick={onClick}
>
{rip && (
<span
key={rip.id}
className="gu-gc__ripple"
style={{ left: rip.x, top: rip.y }}
aria-hidden="true"
/>
)}
<span className="gu-gc__btn-text">{children}</span>
</Glass>
);
}
/**
* GlassControls: a control-centre panel of liquid glass controls over moving colour.
* - Toggle: the thumb turns into a clear lens while pressed and stretches as it travels.
* - Slider: the thumb lifts into a lens that stretches with your speed; lit ticks follow the value.
* - Segmented switch: a glass lens slides between options with a liquid wobble.
* - Buttons: the rim catches the light under the pointer and a soft ripple spreads from your press.
*/
export function GlassControls() {
const [saved, setSaved] = useState(false);
return (
<GlassScene className="gu-gc" words={["Bright", "Calm", "Focus", "Glow"]}>
<div className="gu-gc__grid">
<Glass
radius={30}
tone="dark"
tint={0.02}
className="gu-gc__tile gu-gc__tile--wide"
>
<GlassSegmented
label="Mode"
options={["Day", "Dusk", "Night"]}
defaultIndex={1}
/>
</Glass>
<Glass radius={30} tone="dark" tint={0.02} className="gu-gc__tile">
<GlassSlider label="Brightness" icon="sun" defaultValue={70} />
<GlassSlider label="Volume" icon="sound" defaultValue={35} />
</Glass>
<Glass radius={30} tone="dark" tint={0.02} className="gu-gc__tile">
<GlassToggle label="Wi-Fi" defaultOn />
<GlassToggle label="Focus" />
<GlassToggle label="Night light" defaultOn />
</Glass>
<div className="gu-gc__actions">
<GlassButton onClick={() => setSaved((v) => !v)}>
<span key={String(saved)} className="gu-gc__swap">
{saved ? "Saved" : "Save scene"}
</span>
</GlassButton>
<GlassButton tone="dark">Reset</GlassButton>
</div>
</div>
</GlassScene>
);
}
```
### glass-controls.css
```css
.gu-gc {
--spring: cubic-bezier(0.34, 1.56, 0.64, 1);
--ease: cubic-bezier(0.16, 1, 0.3, 1);
--accent: #ff5a1f;
width: min(100%, 620px);
padding: clamp(16px, 4vw, 32px);
font-family:
ui-sans-serif,
system-ui,
-apple-system,
"Segoe UI",
sans-serif;
}
.gu-gc__grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 14px;
}
@media (max-width: 520px) {
.gu-gc__grid {
grid-template-columns: 1fr;
}
}
.gu-gc__tile {
display: grid;
align-content: center;
gap: 16px;
padding: 18px;
animation: gu-gc-in 0.9s var(--ease) backwards;
}
.gu-gc__tile:nth-child(2) {
animation-delay: 0.08s;
}
.gu-gc__tile:nth-child(3) {
animation-delay: 0.16s;
}
.gu-gc__tile--wide {
grid-column: 1 / -1;
padding: 8px;
}
@keyframes gu-gc-in {
from {
opacity: 0;
translate: 0 18px;
scale: 0.96;
filter: blur(8px);
}
}
/* ---------- Segmented ---------- */
.gu-gc__seg {
position: relative;
display: grid;
grid-template-columns: repeat(var(--n), 1fr);
height: 52px;
}
.gu-gc__seg button {
position: relative;
z-index: 1;
border: 0;
background: none;
border-radius: 999px;
font-weight: 600;
font-size: 15px;
line-height: 1;
font-family: inherit;
color: rgb(255 255 255 / 0.75);
cursor: pointer;
transition:
color 0.3s,
scale 0.3s var(--spring);
}
.gu-gc__seg button[aria-checked="true"] {
color: #fff;
}
.gu-gc__seg button:active {
scale: 0.94;
}
.gu-gc__seg button:focus-visible {
outline: 2px solid #fff;
outline-offset: -3px;
}
.gu-gc__seg-lens {
position: absolute;
inset: 0 auto 0 0;
width: calc(100% / var(--n));
translate: calc(var(--i) * 100%) 0;
transition: translate 0.6s var(--spring);
pointer-events: none;
}
.gu-gc__seg-lens[data-hop="0"] {
animation: gu-gc-stretch-a 0.6s var(--ease);
}
.gu-gc__seg-lens[data-hop="1"] {
animation: gu-gc-stretch-b 0.6s var(--ease);
}
@keyframes gu-gc-stretch-a {
30% {
scale: calc(1 + var(--st)) calc(1 - var(--st) * 0.4);
}
62% {
scale: 0.95 1.05;
}
}
@keyframes gu-gc-stretch-b {
30% {
scale: calc(1 + var(--st)) calc(1 - var(--st) * 0.4);
}
62% {
scale: 0.95 1.05;
}
}
/* ---------- Slider ---------- */
.gu-gc__slider-wrap {
display: grid;
gap: 8px;
}
.gu-gc__slider-label {
display: flex;
justify-content: space-between;
font-size: 13px;
font-weight: 600;
color: rgb(255 255 255 / 0.85);
}
.gu-gc__slider-label output {
font-variant-numeric: tabular-nums;
color: #fff;
}
.gu-gc__slider {
--v: 0.5;
--sx: 1;
--sy: 1;
position: relative;
height: 40px;
border-radius: 999px;
background: rgb(0 0 0 / 0.28);
box-shadow:
inset 0 1px 3px rgb(0 0 0 / 0.35),
inset 0 -1px 0 rgb(255 255 255 / 0.12);
cursor: pointer;
touch-action: none;
outline: none;
}
.gu-gc__slider:focus-visible {
box-shadow: 0 0 0 2px #fff;
}
.gu-gc__fill {
position: absolute;
inset: 0 auto 0 0;
width: max(40px, calc(var(--v) * 100%));
border-radius: inherit;
background: linear-gradient(90deg, rgb(255 255 255 / 0.65), #fff);
display: flex;
align-items: center;
padding-left: 11px;
transition: width 0.25s var(--ease);
}
.gu-gc__slider[data-held="true"] .gu-gc__fill {
transition-duration: 0.05s;
}
.gu-gc__slider-icon {
width: 18px;
height: 18px;
fill: none;
stroke: #0e0e10;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
rotate: calc(var(--v) * 90deg);
scale: calc(0.85 + var(--v) * 0.25);
transition:
rotate 0.3s var(--ease),
scale 0.3s var(--ease);
}
.gu-gc__ticks {
position: absolute;
inset: 0 22px;
display: flex;
justify-content: space-between;
align-items: center;
pointer-events: none;
}
.gu-gc__ticks i {
width: 2px;
height: 8px;
border-radius: 2px;
background: rgb(255 255 255 / 0.35);
transition:
background 0.3s,
height 0.3s var(--spring);
}
.gu-gc__ticks i[data-lit="true"] {
background: rgb(14 14 16 / 0.25);
height: 12px;
}
/* Thumb: a white pill that lifts into a clear lens while you hold it. */
.gu-gc__thumb {
position: absolute;
top: 3px;
bottom: 3px;
width: 52px;
left: clamp(3px, calc(var(--v) * 100% - 26px), calc(100% - 55px));
background: #fff !important;
scale: var(--sx) var(--sy);
transition:
left 0.25s var(--ease),
scale 0.45s var(--spring),
background-color 0.3s,
translate 0.4s var(--spring);
pointer-events: none;
}
.gu-gc__slider[data-held="true"] .gu-gc__thumb {
background: rgb(255 255 255 / 0.04) !important;
translate: 0 0;
scale: calc(var(--sx) * 1.35) calc(var(--sy) * 1.5);
transition:
left 0.05s linear,
scale 0.3s var(--spring),
background-color 0.25s;
}
/* ---------- Toggle ---------- */
.gu-gc__toggle-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
font-size: 15px;
font-weight: 600;
cursor: pointer;
}
.gu-gc__toggle {
position: relative;
flex: none;
width: 64px;
height: 30px;
padding: 0;
border: 0;
border-radius: 999px;
background: rgb(0 0 0 / 0.3);
box-shadow: inset 0 1px 3px rgb(0 0 0 / 0.35);
cursor: pointer;
transition: background-color 0.35s var(--ease);
}
.gu-gc__toggle[aria-checked="true"] {
background: #34c759;
}
.gu-gc__toggle:focus-visible {
outline: 2px solid #fff;
outline-offset: 3px;
}
.gu-gc__knob {
position: absolute;
top: 2px;
left: 2px;
width: 38px;
height: 26px;
background: #fff !important;
translate: 0 0;
transition:
translate 0.5s var(--spring),
scale 0.4s var(--spring),
background-color 0.25s;
}
.gu-gc__toggle[aria-checked="true"] .gu-gc__knob {
translate: 22px 0;
}
.gu-gc__knob[data-hop="a"],
.gu-gc__knob[data-hop="b"] {
animation: gu-gc-knob-a 0.5s var(--ease);
}
.gu-gc__knob[data-hop="b"] {
animation-name: gu-gc-knob-b;
}
@keyframes gu-gc-knob-a {
35% {
scale: 1.45 1.1;
}
}
@keyframes gu-gc-knob-b {
35% {
scale: 1.45 1.1;
}
}
/* Pressed: the thumb swells into a clear lens over the track. */
.gu-gc__toggle:active .gu-gc__knob {
background: rgb(255 255 255 / 0.05) !important;
scale: 1.5 1.45;
}
/* ---------- Buttons ---------- */
.gu-gc__actions {
grid-column: 1 / -1;
display: flex;
gap: 12px;
justify-content: center;
animation: gu-gc-in 0.9s 0.24s var(--ease) backwards;
}
.gu-gc__btn {
overflow: hidden;
min-height: 52px;
padding: 0 26px;
border: 0;
font-weight: 700;
font-size: 16px;
line-height: 1;
font-family: inherit;
color: #fff;
cursor: pointer;
transition:
scale 0.4s var(--spring),
translate 0.4s var(--spring);
}
.gu-gc__btn:hover {
translate: 0 -2px;
}
.gu-gc__btn:active {
scale: 0.95;
translate: 0 0;
}
.gu-gc__btn:focus-visible {
outline: 2px solid #fff;
outline-offset: 3px;
}
.gu-gc__btn-text {
position: relative;
display: inline-block;
}
.gu-gc__swap {
display: inline-block;
animation: gu-gc-swap 0.5s var(--spring);
}
@keyframes gu-gc-swap {
from {
opacity: 0;
translate: 0 10px;
filter: blur(4px);
}
}
.gu-gc__ripple {
position: absolute;
z-index: -1;
width: 260px;
aspect-ratio: 1;
margin: -130px 0 0 -130px;
border-radius: 50%;
background: radial-gradient(circle, rgb(255 255 255 / 0.5), transparent 65%);
animation: gu-gc-ripple 0.8s var(--ease) forwards;
pointer-events: none;
}
@keyframes gu-gc-ripple {
from {
scale: 0;
opacity: 1;
}
to {
scale: 1;
opacity: 0;
}
}
@media (prefers-reduced-motion: reduce) {
.gu-gc *,
.gu-gc *::before,
.gu-gc *::after {
animation-duration: 0.01ms !important;
animation-delay: 0ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
```
### LiquidGlass.tsx
```tsx
"use client";
import {
useEffect,
useId,
useRef,
useState,
useSyncExternalStore,
type CSSProperties,
type ElementType,
type HTMLAttributes,
type PointerEvent,
type ReactNode,
} from "react";
import "./liquid-glass.css";
/*
* Liquid glass core. Copy this file and liquid-glass.css next to any glass piece.
* Real refraction (the background bending at the rim) uses an SVG filter inside backdrop-filter,
* which only Chromium browsers draw today. Everywhere else the glass keeps the same frost, rim light
* and shadows, so nothing ever looks broken.
*/
const never = () => () => {};
export function useCanRefract() {
return useSyncExternalStore(
never,
() =>
"userAgentData" in navigator &&
!window.matchMedia("(prefers-reduced-transparency: reduce)").matches,
() => false,
);
}
/**
* Displacement map for a rounded rectangle: neutral grey in the middle (no bending) and red/green
* ramps at the rim, so the background near the edge is pulled inward, like light through thick glass.
*/
function lensMap(w: number, h: number, r: number, edge: number) {
const e = Math.max(2, Math.min(edge, w / 2 - 1, h / 2 - 1));
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}" viewBox="0 0 ${w} ${h}"><defs><linearGradient id="x" x2="1"><stop offset="0" stop-color="#f00"/><stop offset="1" stop-color="#000"/></linearGradient><linearGradient id="y" x2="0" y2="1"><stop offset="0" stop-color="#0f0"/><stop offset="1" stop-color="#000"/></linearGradient><filter id="b" x="-50%" y="-50%" width="200%" height="200%"><feGaussianBlur stdDeviation="${(e / 2).toFixed(1)}"/></filter></defs><rect width="${w}" height="${h}" fill="#000"/><rect width="${w}" height="${h}" fill="url(#x)"/><rect width="${w}" height="${h}" fill="url(#y)" style="mix-blend-mode:screen"/><rect x="${e}" y="${e}" width="${w - 2 * e}" height="${h - 2 * e}" rx="${Math.max(0, r - e)}" fill="rgb(128,128,0)" filter="url(#b)"/></svg>`;
return `data:image/svg+xml;utf8,${encodeURIComponent(svg)}`;
}
export type GlassProps = Omit<HTMLAttributes<HTMLElement>, "ref"> & {
as?: ElementType;
/** Corner radius in px (use a big number for a pill). */
radius?: number;
/** How far the rim bends the background, in px. */
depth?: number;
/** Width of the bending rim, in px. */
edge?: number;
/** Frost (blur) in px. */
blur?: number;
/** Milkiness of the glass, 0 to 1. */
tint?: number;
/** "light" is clear glass, "dark" is smoked glass. */
tone?: "light" | "dark";
/** Callback ref to the glass element. */
glassRef?: (node: HTMLElement | null) => void;
children?: ReactNode;
[data: `data-${string}`]: unknown;
type?: string;
disabled?: boolean;
};
/**
* Glass: one sheet of liquid glass.
* - Bends what is behind it at the rim (Chromium), frosts and saturates it everywhere.
* - The rim catches the light nearest the pointer, like a real edge turning toward a lamp.
*/
export function Glass({
as: Tag = "div",
radius = 24,
depth = 30,
edge = 14,
blur = 4,
tint = 0.1,
tone = "light",
className = "",
style,
children,
glassRef,
onPointerMove,
...rest
}: GlassProps) {
const id = `gu-lens-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`;
const el = useRef<HTMLElement | null>(null);
const [size, setSize] = useState<[number, number]>([0, 0]);
const refract = useCanRefract();
useEffect(() => {
const node = el.current;
if (!node || !refract) return;
const ro = new ResizeObserver(() => {
// offsetWidth ignores transforms, so a stretching lens keeps one stable map.
const w = node.offsetWidth;
const h = node.offsetHeight;
setSize((s) => (s[0] === w && s[1] === h ? s : [w, h]));
});
ro.observe(node);
return () => ro.disconnect();
}, [refract]);
const [w, h] = size;
const r = Math.min(radius, w / 2, h / 2);
const lens = refract && w > 4 && h > 4;
const frost = `blur(${blur}px) saturate(1.9) brightness(1.06)`;
const setRefs = (node: HTMLElement | null) => {
el.current = node;
glassRef?.(node);
};
const light = (e: PointerEvent<HTMLElement>) => {
const b = e.currentTarget.getBoundingClientRect();
e.currentTarget.style.setProperty(
"--lx",
`${((e.clientX - b.left) / b.width) * 100}%`,
);
e.currentTarget.style.setProperty(
"--ly",
`${((e.clientY - b.top) / b.height) * 100}%`,
);
onPointerMove?.(e);
};
return (
<Tag
ref={setRefs}
className={`gu-glass ${className}`}
data-tone={tone}
onPointerMove={light}
style={
{
"--r": `${radius}px`,
"--tint": tint,
...(lens
? {
backdropFilter: `url(#${id}) ${frost}`,
WebkitBackdropFilter: frost,
}
: null),
...style,
} as CSSProperties
}
{...rest}
>
{lens && (
<svg
className="gu-glass__defs"
width="0"
height="0"
aria-hidden="true"
focusable="false"
>
<filter
id={id}
x="0"
y="0"
width={w}
height={h}
filterUnits="userSpaceOnUse"
colorInterpolationFilters="sRGB"
>
<feImage
href={lensMap(w, h, r, edge)}
x="0"
y="0"
width={w}
height={h}
result="map"
preserveAspectRatio="none"
/>
<feDisplacementMap
in="SourceGraphic"
in2="map"
scale={depth}
xChannelSelector="R"
yChannelSelector="G"
/>
</filter>
</svg>
)}
{children}
</Tag>
);
}
/**
* GlassScene: a colourful, slowly moving backdrop drawn in code, so the glass has something to bend.
* Swap it for your own photo or page; the glass works over anything.
*/
export function GlassScene({
children,
className = "",
words = ["Liquid", "Glass", "Light", "Bend"],
style,
}: {
children?: ReactNode;
className?: string;
words?: string[];
style?: CSSProperties;
}) {
return (
<div className={`gu-scene ${className}`} style={style}>
<div className="gu-scene__bg" aria-hidden="true">
<span className="gu-scene__blob" data-b="1" />
<span className="gu-scene__blob" data-b="2" />
<span className="gu-scene__blob" data-b="3" />
<span className="gu-scene__blob" data-b="4" />
<div className="gu-scene__words">
{[0, 1, 2].map((row) => (
<div key={row} className="gu-scene__row" data-row={row}>
{[...words, ...words, ...words].map((w, i) => (
<span key={i}>{w}</span>
))}
</div>
))}
</div>
<span className="gu-scene__grid" />
</div>
<div className="gu-scene__fg">{children}</div>
</div>
);
}
```
### liquid-glass.css
```css
/* ---------- Liquid glass ---------- */
.gu-glass {
--r: 24px;
--tint: 0.1;
--lx: 30%;
--ly: 0%;
position: relative;
isolation: isolate;
border-radius: var(--r);
color: #fff;
background: rgb(255 255 255 / var(--tint));
-webkit-backdrop-filter: blur(14px) saturate(1.9) brightness(1.06);
backdrop-filter: blur(14px) saturate(1.9) brightness(1.06);
box-shadow:
inset 0 1px 0.5px rgb(255 255 255 / 0.6),
inset 0 -1px 1px rgb(255 255 255 / 0.14),
inset 0 0 18px rgb(255 255 255 / 0.08),
0 16px 36px -14px rgb(0 0 0 / 0.45),
0 3px 8px -3px rgb(0 0 0 / 0.22);
}
.gu-glass[data-tone="dark"] {
background: rgb(16 16 20 / calc(var(--tint) + 0.26));
box-shadow:
inset 0 1px 0.5px rgb(255 255 255 / 0.26),
inset 0 0 0 0.5px rgb(255 255 255 / 0.12),
0 16px 36px -14px rgb(0 0 0 / 0.55);
}
/* The rim: brightest where the pointer is, plus a fixed highlight top-left and a reflection bottom-right. */
.gu-glass::before {
content: "";
position: absolute;
inset: 0;
z-index: -1;
padding: 1.2px;
border-radius: inherit;
background:
radial-gradient(
160px 110px at var(--lx) var(--ly),
rgb(255 255 255 / 0.95),
transparent 70%
),
linear-gradient(
135deg,
rgb(255 255 255 / 0.7),
rgb(255 255 255 / 0.06) 35%,
rgb(255 255 255 / 0.02) 65%,
rgb(255 255 255 / 0.45)
);
-webkit-mask:
linear-gradient(#000 0 0) content-box,
linear-gradient(#000 0 0);
-webkit-mask-composite: xor;
mask:
linear-gradient(#000 0 0) content-box exclude,
linear-gradient(#000 0 0);
transition: opacity 0.4s;
pointer-events: none;
}
/* A curved-surface sheen across the top. */
.gu-glass::after {
content: "";
position: absolute;
inset: 0;
z-index: -1;
border-radius: inherit;
background:
radial-gradient(
130% 80% at 50% -30%,
rgb(255 255 255 / 0.3),
transparent 58%
),
radial-gradient(
60% 40% at var(--lx) 120%,
rgb(255 255 255 / 0.12),
transparent 70%
);
pointer-events: none;
}
.gu-glass__defs {
position: absolute;
width: 0;
height: 0;
overflow: hidden;
pointer-events: none;
}
/* ---------- Scene (the colourful backdrop) ---------- */
.gu-scene {
position: relative;
isolation: isolate;
overflow: hidden;
border-radius: 28px;
background: #0e0e10;
color: #fff;
}
.gu-scene__bg {
position: absolute;
inset: 0;
z-index: 0;
overflow: hidden;
}
.gu-scene__blob {
position: absolute;
width: 60%;
aspect-ratio: 1;
border-radius: 50%;
filter: blur(36px);
opacity: 0.95;
animation: gu-scene-drift 18s ease-in-out infinite alternate;
}
.gu-scene__blob[data-b="1"] {
background: #ff5a1f;
left: -12%;
top: -18%;
}
.gu-scene__blob[data-b="2"] {
background: #2b35ff;
right: -16%;
top: 4%;
animation-duration: 22s;
animation-delay: -6s;
}
.gu-scene__blob[data-b="3"] {
background: #ff3d8b;
left: 18%;
bottom: -30%;
animation-duration: 26s;
animation-delay: -12s;
}
.gu-scene__blob[data-b="4"] {
background: #d7ff3a;
width: 26%;
right: 14%;
bottom: 6%;
animation-duration: 15s;
animation-delay: -3s;
opacity: 0.85;
}
@keyframes gu-scene-drift {
0% {
translate: 0 0;
scale: 1;
}
50% {
translate: 14% 10%;
scale: 1.18;
}
100% {
translate: -8% 16%;
scale: 0.92;
}
}
/* Big rows of type give the glass sharp edges to bend. */
.gu-scene__words {
position: absolute;
inset: -10% -20%;
display: grid;
align-content: center;
gap: 0.1em;
rotate: -8deg;
font:
800 clamp(48px, 9vw, 112px) / 0.95 ui-sans-serif,
system-ui,
sans-serif;
letter-spacing: -0.04em;
color: rgb(255 255 255 / 0.9);
mix-blend-mode: overlay;
}
.gu-scene__row {
display: flex;
gap: 0.45em;
white-space: nowrap;
animation: gu-scene-slide 40s linear infinite;
}
.gu-scene__row[data-row="1"] {
animation-direction: reverse;
animation-duration: 52s;
color: rgb(14 14 16 / 0.55);
}
.gu-scene__row[data-row="2"] {
animation-duration: 34s;
}
@keyframes gu-scene-slide {
to {
translate: -33.333% 0;
}
}
.gu-scene__grid {
position: absolute;
inset: 0;
background-image:
linear-gradient(rgb(255 255 255 / 0.12) 1px, transparent 1px),
linear-gradient(90deg, rgb(255 255 255 / 0.12) 1px, transparent 1px);
background-size: 28px 28px;
-webkit-mask: radial-gradient(80% 80% at 50% 50%, #000, transparent);
mask: radial-gradient(80% 80% at 50% 50%, #000, transparent);
}
.gu-scene__fg {
position: relative;
z-index: 1;
height: 100%;
}
@media (prefers-reduced-motion: reduce) {
.gu-scene__blob,
.gu-scene__row {
animation: none;
}
}
@media (prefers-reduced-transparency: reduce) {
.gu-glass {
background: rgb(244 244 248 / 0.94) !important;
color: #0e0e10;
-webkit-backdrop-filter: none !important;
backdrop-filter: none !important;
}
.gu-glass[data-tone="dark"] {
background: rgb(28 28 32 / 0.96) !important;
color: #fff;
}
}
```
You might also want
Next piece: Liquid glass tab bar appBefore and after slider
Components, beginner
Snap carousel with progress
Components, intermediate
Cookie consent banner
Components, intermediate