liquidglass-ts 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 liquidglass-ts contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,28 @@
1
- # Temporary Holding Version
1
+ [![Browser checks](https://github.com/bradleyseanf/liquidglass-ts/actions/workflows/browsers.yml/badge.svg?branch=dev)](https://github.com/bradleyseanf/liquidglass-ts/actions/workflows/browsers.yml) [![npm](https://img.shields.io/npm/v/liquidglass-ts)](https://www.npmjs.com/package/liquidglass-ts) [![MIT license](https://img.shields.io/badge/license-MIT-blue)](LICENSE)
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ # liquidglass-ts
4
+
5
+ [![A glass lens refracting the liquidglass-ts wordmark](https://raw.githubusercontent.com/bradleyseanf/liquidglass-ts/main/assets/wordmark.png)](https://bradleyseanf.github.io/liquidglass-ts/)
6
+
7
+ A drop-in, TypeScript-only take on glassmorphism, aiming for a 1:1 recreation of Apple’s Liquid Glass. Live lenses bend text and pictures as you drag. Controls expand and spring back when held. Zero runtime dependencies. No WebGL.
8
+
9
+ **[Try the playground](https://bradleyseanf.github.io/liquidglass-ts/)** · Install with `npm install liquidglass-ts`.
10
+
11
+ ```ts
12
+ import "liquidglass-ts/controls.css";
13
+ import { createGlassSwitch } from "liquidglass-ts/controls";
14
+
15
+ const wifi = createGlassSwitch("Wi-Fi", true, enabled => console.log(enabled));
16
+ document.body.append(wifi.element);
17
+ // Call wifi.destroy() when removing the control.
18
+ ```
19
+
20
+ | Asset | API |
21
+ | --- | --- |
22
+ | Lens over HTML text or pictures | `attachGlass(overlay, material, radius, backdrop)` |
23
+ | Lens over a canvas you draw | `new LiquidGlass().draw(…)` |
24
+ | Tab bar, slider, switch | `createTabBar`, `createGlassSlider`, `createGlassSwitch` from `liquidglass-ts/controls` |
25
+
26
+ ![Tab bar, slider and held glass switch in the playground](https://raw.githubusercontent.com/bradleyseanf/liquidglass-ts/main/assets/controls.png)
27
+
28
+ For HTML lenses, position the overlay above a separate backdrop element and pass both to `attachGlass`. For canvas lenses, redraw after the source or lens moves.
@@ -0,0 +1,23 @@
1
+ import { type Material } from "./glass.js";
2
+ export interface GlassTab {
3
+ label: string;
4
+ icon: SVGElement;
5
+ }
6
+ export declare function createTabBar(tabs: GlassTab[], onChange: (index: number) => void): {
7
+ setMaterial(m: Partial<Material>): void;
8
+ element: HTMLElement;
9
+ setBackdrop(source?: HTMLElement): void;
10
+ destroy(): void;
11
+ };
12
+ export declare function createGlassSlider(label: string, value: number, onChange: (v: number) => void): {
13
+ element: HTMLElement;
14
+ setBackdrop(source?: HTMLElement): void;
15
+ setMaterial(m: Partial<Material>): void;
16
+ destroy(): void;
17
+ };
18
+ export declare function createGlassSwitch(label: string, initial: boolean, onChange: (checked: boolean) => void): {
19
+ element: HTMLElement;
20
+ setBackdrop(source?: HTMLElement): void;
21
+ setMaterial(m: Partial<Material>): void;
22
+ destroy(): void;
23
+ };
@@ -0,0 +1,271 @@
1
+ import { glassMotion } from "./motion.js";
2
+ import { tabOptics } from "./tab-optics.js";
3
+ import { attachGlass } from "./glass.js";
4
+ const unit = (n) => Math.min(1, Math.max(0, n));
5
+ /** Captures a single pointer. Canceled gestures restore the committed value. */
6
+ function track(el, preview, commit, cancel) {
7
+ let pointer = null;
8
+ const events = new AbortController(), opts = { signal: events.signal };
9
+ const at = (e) => {
10
+ const r = el.getBoundingClientRect();
11
+ return unit((e.clientX - r.left) / r.width);
12
+ };
13
+ el.addEventListener("pointerdown", (e) => {
14
+ if (pointer !== null || e.button !== 0)
15
+ return;
16
+ pointer = e.pointerId;
17
+ el.setPointerCapture(pointer);
18
+ el.dataset.held = "";
19
+ preview(at(e));
20
+ }, opts);
21
+ el.addEventListener("pointermove", (e) => {
22
+ if (pointer === e.pointerId)
23
+ preview(at(e));
24
+ }, opts);
25
+ el.addEventListener("pointerup", (e) => {
26
+ if (pointer !== e.pointerId)
27
+ return;
28
+ pointer = null;
29
+ delete el.dataset.held;
30
+ commit(at(e));
31
+ }, opts);
32
+ el.addEventListener("lostpointercapture", () => {
33
+ if (pointer !== null) {
34
+ pointer = null;
35
+ delete el.dataset.held;
36
+ cancel();
37
+ }
38
+ }, opts);
39
+ return () => events.abort();
40
+ }
41
+ function part(parent, klass) {
42
+ const node = document.createElement("span");
43
+ node.className = klass;
44
+ node.setAttribute("aria-hidden", "true");
45
+ parent.append(node);
46
+ return node;
47
+ }
48
+ // The backing and the lens respond to the same spring. Only the visual layers
49
+ // move, keeping hit targets, values and pointer capture stable.
50
+ function moveLayers(surface, lens, x, y) {
51
+ surface.style.translate = x * 0.14 + "px " + y * 0.2 + "px";
52
+ lens.style.translate = x + "px " + y + "px";
53
+ }
54
+ function binding(el, parts, lenses, cleanup, backdrop) {
55
+ return {
56
+ element: el,
57
+ setBackdrop(source) {
58
+ backdrop?.setBackdrop(source);
59
+ },
60
+ setMaterial(m) {
61
+ lenses.forEach((l) => l.setMaterial(m));
62
+ },
63
+ destroy() {
64
+ cleanup();
65
+ lenses.forEach((l) => l.destroy());
66
+ parts.forEach((p) => p.remove());
67
+ el.remove();
68
+ },
69
+ };
70
+ }
71
+ export function createTabBar(tabs, onChange) {
72
+ if (!tabs.length)
73
+ throw new Error("Provide at least one tab.");
74
+ const el = document.createElement("nav");
75
+ el.className = "lg-tabs";
76
+ el.setAttribute("aria-label", "Sections");
77
+ el.style.setProperty("--count", String(tabs.length));
78
+ const surface = part(el, "lg-surface"), thumb = part(el, "lg-selection");
79
+ const items = document.createElement("div");
80
+ items.className = "lg-items";
81
+ const viewport = document.createElement("div");
82
+ viewport.className = "lg-tab-viewport";
83
+ viewport.append(items);
84
+ el.append(viewport);
85
+ const buttons = tabs.map(({ label, icon }, index) => {
86
+ const b = document.createElement("button");
87
+ b.type = "button";
88
+ b.append(icon, document.createTextNode(label));
89
+ items.append(b);
90
+ b.onclick = (e) => {
91
+ if (e.detail === 0)
92
+ select(index);
93
+ };
94
+ return b;
95
+ });
96
+ const bar = attachGlass(surface);
97
+ const selection = tabOptics(items, thumb);
98
+ let material = {};
99
+ let index = 0;
100
+ function clearLens() {
101
+ selection.clear();
102
+ viewport.style.clipPath = "";
103
+ }
104
+ function cutAperture() {
105
+ // The clear lens supplies the pixels inside this hole; the original glyphs
106
+ // remain visible outside it. Both rectangles use viewport coordinates.
107
+ const base = viewport.getBoundingClientRect(), r = thumb.getBoundingClientRect();
108
+ const x = r.left - base.left, y = r.top - base.top, right = x + r.width, bottom = y + r.height;
109
+ const a = Math.min(r.width, r.height) / 2;
110
+ viewport.style.clipPath =
111
+ 'path(evenodd,"M0 0H' +
112
+ base.width +
113
+ "V" +
114
+ base.height +
115
+ "H0Z M" +
116
+ (x + a) +
117
+ " " +
118
+ y +
119
+ "H" +
120
+ (right - a) +
121
+ "A" +
122
+ a +
123
+ " " +
124
+ a +
125
+ " 0 0 1 " +
126
+ right +
127
+ " " +
128
+ (y + a) +
129
+ "V" +
130
+ (bottom - a) +
131
+ "A" +
132
+ a +
133
+ " " +
134
+ a +
135
+ " 0 0 1 " +
136
+ (right - a) +
137
+ " " +
138
+ bottom +
139
+ "H" +
140
+ (x + a) +
141
+ "A" +
142
+ a +
143
+ " " +
144
+ a +
145
+ " 0 0 1 " +
146
+ x +
147
+ " " +
148
+ (bottom - a) +
149
+ "V" +
150
+ (y + a) +
151
+ "A" +
152
+ a +
153
+ " " +
154
+ a +
155
+ " 0 0 1 " +
156
+ (x + a) +
157
+ " " +
158
+ y +
159
+ 'Z")';
160
+ }
161
+ const stopNavMotion = glassMotion(el, 8, (x, y) => {
162
+ thumb.style.translate = x || y ? x + "px " + y + "px" : "";
163
+ if (el.hasAttribute("data-held")) {
164
+ cutAperture();
165
+ selection.draw(material);
166
+ }
167
+ });
168
+ function position(n) {
169
+ thumb.style.setProperty("--tab", String(n));
170
+ if (el.hasAttribute("data-held")) {
171
+ selection.draw(material);
172
+ cutAperture();
173
+ }
174
+ else {
175
+ clearLens();
176
+ }
177
+ }
178
+ function select(i) {
179
+ index = i;
180
+ position(i);
181
+ buttons.forEach((b, j) => b.setAttribute("aria-current", i === j ? "page" : "false"));
182
+ onChange(i);
183
+ }
184
+ const stop = track(el, (f) => position(Math.max(0, Math.min(tabs.length - 1, f * tabs.length - 0.5))), (f) => select(Math.min(tabs.length - 1, Math.floor(f * tabs.length))), () => position(index));
185
+ select(0);
186
+ const bindingResult = binding(el, [], [bar], () => {
187
+ stopNavMotion();
188
+ clearLens();
189
+ stop();
190
+ }, bar);
191
+ return {
192
+ ...bindingResult,
193
+ setMaterial(m) {
194
+ material = { ...material, ...m };
195
+ bar.setMaterial(m);
196
+ if (el.hasAttribute("data-held"))
197
+ selection.draw(material);
198
+ },
199
+ };
200
+ }
201
+ export function createGlassSlider(label, value, onChange) {
202
+ const el = document.createElement("div");
203
+ el.className = "lg-slider";
204
+ const rail = part(el, "lg-rail"), thumb = part(el, "lg-knob");
205
+ const input = document.createElement("input");
206
+ input.type = "range";
207
+ input.min = "0";
208
+ input.max = "100";
209
+ input.value = String(Math.round(unit(value) * 100));
210
+ input.setAttribute("aria-label", label);
211
+ el.append(input);
212
+ function sync() {
213
+ el.style.setProperty("--level", input.value + "%");
214
+ el.style.setProperty("--fraction", String(input.valueAsNumber / 100));
215
+ onChange(input.valueAsNumber / 100);
216
+ }
217
+ input.oninput = sync;
218
+ input.onpointerdown = () => (el.dataset.held = "");
219
+ input.onpointerup =
220
+ input.onpointercancel =
221
+ input.onlostpointercapture =
222
+ () => {
223
+ delete el.dataset.held;
224
+ };
225
+ sync();
226
+ const lens = attachGlass(thumb, {}, Infinity, rail, "track");
227
+ const stopMotion = glassMotion(el, 5, (x, y) => moveLayers(rail, thumb, x, y));
228
+ return binding(el, [rail, thumb], [lens], stopMotion);
229
+ }
230
+ export function createGlassSwitch(label, initial, onChange) {
231
+ const el = document.createElement("button");
232
+ el.type = "button";
233
+ el.className = "lg-switch";
234
+ el.setAttribute("role", "switch");
235
+ el.setAttribute("aria-label", label);
236
+ const rail = part(el, "lg-switch-rail");
237
+ const thumb = part(el, "lg-switch-thumb");
238
+ let checked = initial, start = 0;
239
+ function set(v) {
240
+ checked = v;
241
+ el.setAttribute("aria-checked", String(v));
242
+ thumb.style.setProperty("--toggle", v ? "1" : "0");
243
+ onChange(v);
244
+ }
245
+ let first = true;
246
+ const stop = track(el, (f) => {
247
+ if (first) {
248
+ start = f;
249
+ first = false;
250
+ }
251
+ if (Math.abs(f - start) > 0.1)
252
+ thumb.style.setProperty("--toggle", String(unit((f - 0.24) / 0.52)));
253
+ }, (f) => {
254
+ first = true;
255
+ set(Math.abs(f - start) < 0.12 ? !checked : f >= 0.5);
256
+ }, () => {
257
+ first = true;
258
+ set(checked);
259
+ });
260
+ el.onclick = (e) => {
261
+ if (e.detail === 0)
262
+ set(!checked);
263
+ };
264
+ set(initial);
265
+ const lens = attachGlass(thumb, {}, Infinity, rail, "track");
266
+ const stopMotion = glassMotion(el, 4, (x, y) => moveLayers(rail, thumb, x, y));
267
+ return binding(el, [], [lens], () => {
268
+ stopMotion();
269
+ stop();
270
+ });
271
+ }
@@ -0,0 +1,32 @@
1
+ /** Coordinates use the same logical pixel space as the displayed source. */
2
+ export interface Lens {
3
+ x: number;
4
+ y: number;
5
+ width: number;
6
+ height: number;
7
+ radius: number;
8
+ }
9
+ export interface Material {
10
+ refraction: number;
11
+ depth: number;
12
+ highlight: number;
13
+ }
14
+ export declare const defaultMaterial: Material;
15
+ /** Draw one lens above a live, origin-clean source. Call draw after content or position changes. */
16
+ export declare class LiquidGlass {
17
+ readonly canvas: HTMLCanvasElement;
18
+ private readonly patch;
19
+ private readonly reader;
20
+ private readonly ctx;
21
+ private key;
22
+ private map;
23
+ private output?;
24
+ draw(source: HTMLCanvasElement, logicalWidth: number, logicalHeight: number, lens: Lens, options?: Partial<Material>, profile?: "lens" | "tab"): void;
25
+ }
26
+ /** Live DOM backdrop mode for browsers that composite SVG backdrop filters.
27
+ * Mount a dedicated overlay element and position it using ordinary CSS. */
28
+ export declare function attachGlass(element: HTMLElement, initial?: Partial<Material>, radius?: number, source?: HTMLElement, profile?: "lens" | "track"): {
29
+ setBackdrop: (next?: HTMLElement) => void;
30
+ setMaterial(changes: Partial<Material>): void;
31
+ destroy(): void;
32
+ };
@@ -0,0 +1,216 @@
1
+ import { mirrorBackdrop } from "./html-backdrop.js";
2
+ export const defaultMaterial = {
3
+ refraction: 0.72,
4
+ depth: 0.65,
5
+ highlight: 0.8,
6
+ };
7
+ const clamp = (v, hi) => Math.max(0, Math.min(hi, v));
8
+ function geometry(lens, options) {
9
+ const w = Math.max(1, Math.round(lens.width)), h = Math.max(1, Math.round(lens.height));
10
+ const radius = clamp(lens.radius, Math.min(w, h) / 2);
11
+ const bend = clamp(options.refraction ?? defaultMaterial.refraction, 1);
12
+ const depth = clamp(options.depth ?? defaultMaterial.depth, 1);
13
+ const light = clamp(options.highlight ?? defaultMaterial.highlight, 1);
14
+ return { w, h, radius, bend, depth, light };
15
+ }
16
+ function displacement({ w, h, radius, bend, depth, light }, track = false, tab = false) {
17
+ const map = new Float32Array(w * h * 3);
18
+ const bevel = Math.min(w, h) * (0.055 + 0.19 * depth);
19
+ for (let y = 0; y < h; y++)
20
+ for (let x = 0; x < w; x++) {
21
+ const dx = x + 0.5 - w / 2, dy = y + 0.5 - h / 2;
22
+ const qx = Math.abs(dx) - w / 2 + radius, qy = Math.abs(dy) - h / 2 + radius;
23
+ const a = Math.max(qx, 0), b = Math.max(qy, 0), len = Math.hypot(a, b);
24
+ const inside = radius - len - Math.min(Math.max(qx, qy), 0);
25
+ const nx = Math.sign(dx) * (len ? a / len : Number(qx >= qy));
26
+ const ny = Math.sign(dy) * (len ? b / len : Number(qy > qx));
27
+ const rim = clamp(1 - inside / bevel, 1);
28
+ const offset = bend * bevel * 1.6 * (1 - Math.sqrt(Math.max(0, 1 - rim * rim)));
29
+ const i = (y * w + x) * 3;
30
+ // A pressed thumb has a broad, nearly flat face. Its raised bevel
31
+ // compresses the track and turns the image back at grazing angles.
32
+ const zoom = (track ? -0.42 : 0.32) * bend * depth;
33
+ const deflection = track ? offset * 0.52 : offset;
34
+ map[i] = clamp(x - dx * zoom - nx * deflection, w - 1);
35
+ map[i + 1] = clamp(y - dy * zoom - ny * deflection, h - 1);
36
+ if (tab) {
37
+ const t = dx / (w / 2);
38
+ // A smooth shoulder gathers neighboring glyphs toward the center.
39
+ // Its derivative stays positive, so lettering never smears into a strip.
40
+ map[i] = clamp(x +
41
+ (0.68 * bend * depth * (w / 2) * Math.sin(Math.PI * t)) / Math.PI -
42
+ nx * offset * 0.22, w - 1);
43
+ map[i + 1] = clamp(y - dy * 0.18 * bend * depth - ny * offset * 0.34, h - 1);
44
+ }
45
+ // Grazing light concentrates on two narrow, opposing arcs.
46
+ const incidence = -nx * 0.55 - ny * 0.84;
47
+ const edge = Math.exp(-Math.max(inside, 0) / 1.25);
48
+ map[i + 2] =
49
+ light *
50
+ edge *
51
+ (0.56 * Math.max(0, incidence) ** 2 +
52
+ 0.28 * Math.max(0, -incidence) ** 5 -
53
+ 0.16);
54
+ }
55
+ return map;
56
+ }
57
+ /** Draw one lens above a live, origin-clean source. Call draw after content or position changes. */
58
+ export class LiquidGlass {
59
+ canvas = document.createElement("canvas");
60
+ patch = document.createElement("canvas");
61
+ reader = this.patch.getContext("2d", {
62
+ willReadFrequently: true,
63
+ });
64
+ ctx = this.canvas.getContext("2d");
65
+ key = "";
66
+ map = new Float32Array();
67
+ output;
68
+ draw(source, logicalWidth, logicalHeight, lens, options = {}, profile = "lens") {
69
+ if (logicalWidth <= 0 ||
70
+ logicalHeight <= 0 ||
71
+ !source.width ||
72
+ !source.height)
73
+ return;
74
+ const shape = geometry(lens, options);
75
+ const { w, h, radius, bend, depth, light } = shape;
76
+ const key = [w, h, radius, bend, depth, light, profile].join(",");
77
+ if (this.key !== key) {
78
+ this.key = key;
79
+ this.canvas.width = this.patch.width = w;
80
+ this.canvas.height = this.patch.height = h;
81
+ this.map = displacement(shape, false, profile === "tab");
82
+ this.output = this.ctx.createImageData(w, h);
83
+ }
84
+ const sx = source.width / logicalWidth, sy = source.height / logicalHeight;
85
+ this.reader.clearRect(0, 0, w, h);
86
+ this.reader.drawImage(source, lens.x * sx, lens.y * sy, lens.width * sx, lens.height * sy, 0, 0, w, h);
87
+ const input = this.reader.getImageData(0, 0, w, h).data, output = this.output, pixels = output.data;
88
+ for (let p = 0; p < w * h; p++) {
89
+ const u = this.map[p * 3], v = this.map[p * 3 + 1], tint = this.map[p * 3 + 2];
90
+ const x = Math.floor(u), y = Math.floor(v), fx = u - x, fy = v - y;
91
+ const tl = (y * w + x) * 4, tr = tl + (x < w - 1 ? 4 : 0), step = y < h - 1 ? w * 4 : 0;
92
+ for (let c = 0; c < 4; c++) {
93
+ let i = tl + c, j = tr + c;
94
+ // Chromatic separation is subpixel in the face and strongest at the bevel.
95
+ let blend = fx;
96
+ if (profile === "tab" && (c === 0 || c === 2)) {
97
+ const shift = Math.max(-2, Math.min(2, (u - (p % w)) * 0.09)) *
98
+ (c === 0 ? 1 : -1);
99
+ const shifted = clamp(u + shift, w - 1), column = Math.floor(shifted);
100
+ i = (y * w + column) * 4 + c;
101
+ j = i + (column < w - 1 ? 4 : 0);
102
+ blend = shifted - column;
103
+ }
104
+ const top = input[i] + (input[j] - input[i]) * blend;
105
+ const bottom = input[i + step] + (input[j + step] - input[i + step]) * blend;
106
+ const sample = top + (bottom - top) * fy;
107
+ pixels[p * 4 + c] =
108
+ c === 3 ? sample : sample + (tint > 0 ? 255 - sample : sample) * tint;
109
+ }
110
+ }
111
+ this.ctx.putImageData(output, 0, 0);
112
+ Object.assign(this.canvas.style, {
113
+ width: lens.width + "px",
114
+ height: lens.height + "px",
115
+ borderRadius: radius + "px",
116
+ position: "absolute",
117
+ left: lens.x + "px",
118
+ top: lens.y + "px",
119
+ boxShadow: "0 2px 5px #10182020",
120
+ });
121
+ }
122
+ }
123
+ /** Live DOM backdrop mode for browsers that composite SVG backdrop filters.
124
+ * Mount a dedicated overlay element and position it using ordinary CSS. */
125
+ export function attachGlass(element, initial = {}, radius = Infinity, source, profile = "lens") {
126
+ const ns = "http://www.w3.org/2000/svg";
127
+ const svg = document.createElementNS(ns, "svg"), filter = document.createElementNS(ns, "filter");
128
+ const image = document.createElementNS(ns, "feImage"), warp = document.createElementNS(ns, "feDisplacementMap");
129
+ const id = "glass-" +
130
+ Array.from(crypto.getRandomValues(new Uint32Array(3)), (n) => n.toString(36)).join("-");
131
+ svg.setAttribute("aria-hidden", "true");
132
+ svg.style.cssText = "position:absolute;width:0;height:0;pointer-events:none";
133
+ filter.id = id;
134
+ filter.setAttribute("filterUnits", "userSpaceOnUse");
135
+ filter.setAttribute("color-interpolation-filters", "sRGB");
136
+ for (const node of [filter, image]) {
137
+ node.setAttribute("x", "0");
138
+ node.setAttribute("y", "0");
139
+ }
140
+ image.setAttribute("result", "offsets");
141
+ image.setAttribute("preserveAspectRatio", "none");
142
+ warp.setAttribute("in", "SourceGraphic");
143
+ warp.setAttribute("in2", "offsets");
144
+ warp.setAttribute("xChannelSelector", "R");
145
+ warp.setAttribute("yChannelSelector", "G");
146
+ filter.append(image, warp);
147
+ svg.append(filter);
148
+ document.body.append(svg);
149
+ const props = ["backdrop-filter", "border-radius", "box-shadow"];
150
+ const previous = props.map((p) => element.style.getPropertyValue(p));
151
+ let stopMirror, activeSource;
152
+ function setBackdrop(next) {
153
+ if (activeSource === next)
154
+ return;
155
+ stopMirror?.();
156
+ stopMirror = undefined;
157
+ activeSource = next;
158
+ if (next &&
159
+ (navigator.vendor !== "Google Inc." ||
160
+ !CSS.supports("backdrop-filter", "url(#" + id + ")")))
161
+ stopMirror = mirrorBackdrop(element, next, id);
162
+ element.style.backdropFilter = stopMirror ? "none" : "url(#" + id + ")";
163
+ }
164
+ setBackdrop(source);
165
+ let options = { ...initial };
166
+ let lastKey = "";
167
+ function update() {
168
+ const w = element.offsetWidth, h = element.offsetHeight;
169
+ if (!w || !h)
170
+ return;
171
+ const shape = geometry({ x: 0, y: 0, width: w, height: h, radius }, options);
172
+ const key = [w, h, shape.radius, shape.bend, shape.depth, shape.light].join(",");
173
+ if (lastKey === key)
174
+ return;
175
+ lastKey = key;
176
+ const map = displacement(shape, profile === "track"), scratch = document.createElement("canvas");
177
+ scratch.width = w;
178
+ scratch.height = h;
179
+ const c = scratch.getContext("2d"), pixels = c.createImageData(w, h);
180
+ let peak = 1;
181
+ for (let i = 0; i < w * h; i++)
182
+ peak = Math.max(peak, Math.abs(map[i * 3] - (i % w)), Math.abs(map[i * 3 + 1] - Math.floor(i / w)));
183
+ for (let i = 0; i < w * h; i++) {
184
+ pixels.data[i * 4] = 127.5 + ((map[i * 3] - (i % w)) * 127.5) / peak;
185
+ pixels.data[i * 4 + 1] =
186
+ 127.5 + ((map[i * 3 + 1] - Math.floor(i / w)) * 127.5) / peak;
187
+ pixels.data[i * 4 + 3] = 255;
188
+ }
189
+ c.putImageData(pixels, 0, 0);
190
+ for (const node of [filter, image]) {
191
+ node.setAttribute("width", String(w));
192
+ node.setAttribute("height", String(h));
193
+ }
194
+ image.setAttribute("href", scratch.toDataURL());
195
+ warp.setAttribute("scale", String(2 * peak));
196
+ element.style.backdropFilter = stopMirror ? "none" : "url(#" + id + ")";
197
+ element.style.borderRadius = shape.radius + "px";
198
+ element.style.boxShadow = `inset .5px 1px 1px rgba(255,255,255,${shape.light * 0.62}), inset -.6px -.8px .8px rgba(16,30,42,${shape.light * 0.3}), var(--lg-shadow,0 2px 5px #10182019)`;
199
+ }
200
+ const observer = new ResizeObserver(update);
201
+ observer.observe(element);
202
+ update();
203
+ return {
204
+ setBackdrop,
205
+ setMaterial(changes) {
206
+ options = { ...options, ...changes };
207
+ update();
208
+ },
209
+ destroy() {
210
+ observer.disconnect();
211
+ stopMirror?.();
212
+ svg.remove();
213
+ props.forEach((p, i) => element.style.setProperty(p, previous[i]));
214
+ },
215
+ };
216
+ }
@@ -0,0 +1,3 @@
1
+ /** Local fallback for engines that reject URL filters on the backdrop. An explicitly
2
+ * registered, small image/text subtree is mirrored behind a normal SVG filter. */
3
+ export declare function mirrorBackdrop(host: HTMLElement, source: HTMLElement, filterId: string): () => void;
@@ -0,0 +1,149 @@
1
+ /** Local fallback for engines that reject URL filters on the backdrop. An explicitly
2
+ * registered, small image/text subtree is mirrored behind a normal SVG filter. */
3
+ export function mirrorBackdrop(host, source, filterId) {
4
+ if (source.contains(host) || host.contains(source))
5
+ throw new Error("The backdrop and lens must be separate layers.");
6
+ const clip = document.createElement("span"), layer = document.createElement("span"), veil = document.createElement("span");
7
+ const isolation = host.style.isolation;
8
+ host.style.isolation = "isolate";
9
+ clip.inert = true;
10
+ clip.setAttribute("aria-hidden", "true");
11
+ clip.style.cssText =
12
+ "position:absolute;inset:0;overflow:hidden;border-radius:inherit;pointer-events:none;z-index:-1";
13
+ layer.style.cssText = "position:absolute;inset:0;pointer-events:none;";
14
+ layer.style.filter = "url(#" + filterId + ")";
15
+ veil.style.cssText = "position:absolute;inset:0;border-radius:inherit";
16
+ clip.append(layer, veil);
17
+ host.prepend(clip);
18
+ let copy, dirty = true, frame = 0, visible = false;
19
+ function snapshot(original) {
20
+ // Mirror passive visuals. Interactive controls, embedded documents and scripts
21
+ // are deliberately outside the text/photo source contract.
22
+ if (!/^(DIV|SECTION|ARTICLE|MAIN|HEADER|FOOTER|P|H[1-6]|A|EM|I|B|STRONG|SPAN|SMALL|BR|BUTTON|FIGURE|FIGCAPTION|CANVAS|IMG)$/.test(original.tagName))
23
+ return null;
24
+ const clone = document.createElement(original.tagName === "IMG"
25
+ ? "img"
26
+ : original.tagName === "CANVAS"
27
+ ? "canvas"
28
+ : "div");
29
+ const css = getComputedStyle(original);
30
+ for (const property of css)
31
+ clone.style.setProperty(property, css.getPropertyValue(property));
32
+ if (original instanceof HTMLImageElement)
33
+ clone.src = original.currentSrc || original.src;
34
+ else
35
+ for (const child of original.childNodes) {
36
+ if (child.nodeType === Node.TEXT_NODE)
37
+ clone.append(child.textContent ?? "");
38
+ else if (child instanceof SVGSVGElement) {
39
+ const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
40
+ svg.setAttribute("viewBox", child.getAttribute("viewBox") ?? "0 0 24 24");
41
+ for (const prop of ["width", "height", "fill", "color", "display"])
42
+ svg.style.setProperty(prop, getComputedStyle(child).getPropertyValue(prop));
43
+ child.querySelectorAll("path").forEach((p) => {
44
+ const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
45
+ path.setAttribute("d", p.getAttribute("d") ?? "");
46
+ for (const prop of [
47
+ "fill",
48
+ "fill-rule",
49
+ "stroke",
50
+ "stroke-width",
51
+ "stroke-linejoin",
52
+ "stroke-linecap",
53
+ ])
54
+ path.style.setProperty(prop, getComputedStyle(p).getPropertyValue(prop));
55
+ svg.append(path);
56
+ });
57
+ clone.append(svg);
58
+ }
59
+ else if (child instanceof Element) {
60
+ const node = snapshot(child);
61
+ if (node)
62
+ clone.append(node);
63
+ }
64
+ }
65
+ return clone;
66
+ }
67
+ function paint() {
68
+ frame = 0;
69
+ if (!visible)
70
+ return;
71
+ if (dirty) {
72
+ copy = snapshot(source) ?? undefined;
73
+ if (copy) {
74
+ copy.style.position = "absolute";
75
+ copy.style.margin = "0";
76
+ copy.style.inset = "auto";
77
+ copy.style.translate = "none";
78
+ }
79
+ layer.replaceChildren(...(copy ? [copy] : []));
80
+ dirty = false;
81
+ }
82
+ if (copy) {
83
+ const a = source.getBoundingClientRect(), b = host.getBoundingClientRect();
84
+ // Geometry is sampled while visible so CSS transforms, dragging and scroll
85
+ // stay aligned without a full DOM snapshot each frame.
86
+ const sx = host.offsetWidth / b.width, sy = host.offsetHeight / b.height;
87
+ copy.style.transformOrigin = "0 0";
88
+ copy.style.transform = "scale(" + sx + "," + sy + ")";
89
+ copy.style.left = (a.left - b.left) * sx + "px";
90
+ copy.style.top = (a.top - b.top) * sy + "px";
91
+ if (copy instanceof HTMLCanvasElement &&
92
+ source instanceof HTMLCanvasElement &&
93
+ a.width &&
94
+ a.height &&
95
+ b.width &&
96
+ b.height) {
97
+ const w = Math.max(1, Math.round(b.width)), h = Math.max(1, Math.round(b.height));
98
+ if (copy.width !== w)
99
+ copy.width = w;
100
+ if (copy.height !== h)
101
+ copy.height = h;
102
+ Object.assign(copy.style, {
103
+ left: "0",
104
+ top: "0",
105
+ width: w + "px",
106
+ height: h + "px",
107
+ });
108
+ const ctx = copy.getContext("2d");
109
+ if (ctx && source.width && source.height) {
110
+ ctx.clearRect(0, 0, w, h);
111
+ ctx.drawImage(source, ((b.x - a.x) * source.width) / a.width, ((b.y - a.y) * source.height) / a.height, (b.width * source.width) / a.width, (b.height * source.height) / a.height, 0, 0, w, h);
112
+ }
113
+ }
114
+ veil.style.backgroundColor = getComputedStyle(host).backgroundColor;
115
+ }
116
+ frame = requestAnimationFrame(paint);
117
+ }
118
+ const show = new IntersectionObserver((entries) => {
119
+ visible = entries[0].isIntersecting;
120
+ if (visible && !frame)
121
+ frame = requestAnimationFrame(paint);
122
+ });
123
+ show.observe(host);
124
+ const mark = () => {
125
+ dirty = true;
126
+ };
127
+ const mutations = new MutationObserver(mark);
128
+ mutations.observe(source, {
129
+ subtree: true,
130
+ childList: true,
131
+ characterData: true,
132
+ attributes: true,
133
+ });
134
+ if (source.parentElement)
135
+ mutations.observe(source.parentElement, { attributes: true });
136
+ const resize = new ResizeObserver(mark);
137
+ resize.observe(source);
138
+ source.addEventListener("load", mark, true);
139
+ return () => {
140
+ visible = false;
141
+ cancelAnimationFrame(frame);
142
+ show.disconnect();
143
+ resize.disconnect();
144
+ mutations.disconnect();
145
+ source.removeEventListener("load", mark, true);
146
+ clip.remove();
147
+ host.style.isolation = isolation;
148
+ };
149
+ }
@@ -0,0 +1 @@
1
+ export declare function glassMotion(target: HTMLElement, travel: number, paint: (x: number, y: number) => void): () => void;
@@ -0,0 +1,95 @@
1
+ // A bounded spring lifts the lens during a horizontal drag and trails lateral
2
+ // acceleration. Positions are CSS pixels and velocities are pixels per second.
3
+ export function glassMotion(target, travel, paint) {
4
+ const listeners = new AbortController(), options = { signal: listeners.signal, passive: true };
5
+ const reduced = matchMedia("(prefers-reduced-motion: reduce)");
6
+ const bound = (n, limit) => Math.min(limit, Math.max(-limit, n));
7
+ let pointer = null, anchorX = 0, dragging = false, lastX = 0, lift = 0;
8
+ let x = 0, y = 0, vx = 0, vy = 0, frame = 0, lastTime = 0;
9
+ function reset() {
10
+ cancelAnimationFrame(frame);
11
+ frame = lastTime = 0;
12
+ x = y = vx = vy = lift = 0;
13
+ pointer = null;
14
+ paint(0, 0);
15
+ }
16
+ function step(now) {
17
+ frame = 0;
18
+ if (reduced.matches) {
19
+ reset();
20
+ return;
21
+ }
22
+ let dt = Math.min(0.04, (now - lastTime) / 1000);
23
+ lastTime = now;
24
+ while (dt > 0) {
25
+ const h = Math.min(dt, 1 / 120);
26
+ vx += (-480 * x - 22 * vx) * h;
27
+ vy += (440 * (lift - y) - 14 * vy) * h;
28
+ x += vx * h;
29
+ y += vy * h;
30
+ dt -= h;
31
+ }
32
+ if (Math.abs(x) + Math.abs(y - lift) < 0.025 &&
33
+ Math.abs(vx) + Math.abs(vy) < 0.6) {
34
+ x = vx = vy = 0;
35
+ y = lift;
36
+ paint(0, y);
37
+ lastTime = 0;
38
+ return;
39
+ }
40
+ paint(x, y);
41
+ frame = requestAnimationFrame(step);
42
+ }
43
+ function wake() {
44
+ if (!frame) {
45
+ lastTime = performance.now();
46
+ frame = requestAnimationFrame(step);
47
+ }
48
+ }
49
+ target.addEventListener("pointerdown", (e) => {
50
+ if (pointer !== null || e.button !== 0 || reduced.matches)
51
+ return;
52
+ pointer = e.pointerId;
53
+ lastX = e.clientX;
54
+ anchorX = lastX;
55
+ dragging = false;
56
+ lift = -travel * 0.18;
57
+ vy -= travel * 10;
58
+ wake();
59
+ }, options);
60
+ target.addEventListener("pointermove", (e) => {
61
+ if (e.pointerId !== pointer)
62
+ return;
63
+ if (!dragging && Math.abs(e.clientX - anchorX) < 4)
64
+ return;
65
+ dragging = true;
66
+ const dx = bound(e.clientX - lastX, travel * 3);
67
+ lastX = e.clientX;
68
+ vx = bound(vx - dx * 6, travel * 19);
69
+ // Couple lateral acceleration into a damped vertical rebound. A reversal
70
+ // changes the impulse immediately, even within a single tab or short rail.
71
+ vy = bound(vy + dx * 3, travel * 16);
72
+ wake();
73
+ }, options);
74
+ function release(e) {
75
+ if (e.pointerId !== pointer)
76
+ return;
77
+ pointer = null;
78
+ lift = 0;
79
+ wake();
80
+ }
81
+ for (const type of [
82
+ "pointerup",
83
+ "pointercancel",
84
+ "lostpointercapture",
85
+ ])
86
+ target.addEventListener(type, release, options);
87
+ window.addEventListener("pointerup", release, options);
88
+ window.addEventListener("pointercancel", release, options);
89
+ window.addEventListener("blur", reset, { signal: listeners.signal });
90
+ reduced.addEventListener("change", reset, { signal: listeners.signal });
91
+ return () => {
92
+ listeners.abort();
93
+ reset();
94
+ };
95
+ }
@@ -0,0 +1,5 @@
1
+ import { type Material } from "./glass.js";
2
+ export declare function tabOptics(items: HTMLElement, thumb: HTMLElement): {
3
+ draw(options: Partial<Material>): void;
4
+ clear(): void;
5
+ };
@@ -0,0 +1,88 @@
1
+ import { LiquidGlass } from "./glass.js";
2
+ // The moving lens samples one aligned image of the tab foreground. This keeps
3
+ // neighboring glyphs out of the resting pill and avoids nested backdrop filters.
4
+ export function tabOptics(items, thumb) {
5
+ const source = document.createElement("canvas"), ctx = source.getContext("2d"), lens = new LiquidGlass();
6
+ const icons = new Map();
7
+ let pending = 0, material = {};
8
+ function render() {
9
+ pending = 0;
10
+ if (!thumb.isConnected || !thumb.parentElement?.hasAttribute("data-held"))
11
+ return;
12
+ const base = items.getBoundingClientRect(), r = thumb.getBoundingClientRect(), scale = Math.min(devicePixelRatio, 2), pad = r.height;
13
+ const w = Math.ceil((base.width + 2 * pad) * scale), h = Math.ceil((base.height + 2 * pad) * scale);
14
+ if (source.width !== w || source.height !== h) {
15
+ source.width = w;
16
+ source.height = h;
17
+ }
18
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
19
+ ctx.clearRect(0, 0, w, h);
20
+ ctx.setTransform(scale, 0, 0, scale, pad * scale, pad * scale);
21
+ for (const button of items.querySelectorAll("button")) {
22
+ const svg = button.querySelector("svg"), label = button.lastChild;
23
+ if (!svg || !label)
24
+ continue;
25
+ const style = getComputedStyle(button), box = svg.getBoundingClientRect();
26
+ let icon = icons.get(svg);
27
+ if (!icon || icon.key !== style.color) {
28
+ const clone = svg.cloneNode(true);
29
+ clone.setAttribute("width", String(box.width * scale));
30
+ clone.setAttribute("height", String(box.height * scale));
31
+ clone.setAttribute("color", style.color);
32
+ clone.setAttribute("fill", style.color);
33
+ const image = new Image();
34
+ image.onload = schedule;
35
+ image.src =
36
+ "data:image/svg+xml;charset=utf-8," +
37
+ encodeURIComponent(new XMLSerializer().serializeToString(clone));
38
+ icon = { key: style.color, image };
39
+ icons.set(svg, icon);
40
+ }
41
+ if (icon.image.complete && icon.image.naturalWidth)
42
+ ctx.drawImage(icon.image, box.x - base.x, box.y - base.y, box.width, box.height);
43
+ const range = document.createRange();
44
+ range.selectNodeContents(label);
45
+ const text = range.getBoundingClientRect();
46
+ ctx.font =
47
+ style.fontWeight + " " + style.fontSize + " " + style.fontFamily;
48
+ ctx.fillStyle = style.color;
49
+ ctx.textBaseline = "middle";
50
+ ctx.textAlign = "center";
51
+ ctx.fillText(label.textContent ?? "", text.x - base.x + text.width / 2, text.y - base.y + text.height / 2);
52
+ }
53
+ lens.draw(source, w, h, {
54
+ x: (r.x - base.x + pad) * scale,
55
+ y: (r.y - base.y + pad) * scale,
56
+ width: r.width * scale,
57
+ height: r.height * scale,
58
+ radius: (r.height * scale) / 2,
59
+ }, material, "tab");
60
+ Object.assign(lens.canvas.style, {
61
+ left: "0",
62
+ top: "0",
63
+ width: "100%",
64
+ height: "100%",
65
+ borderRadius: "inherit",
66
+ boxShadow: "none",
67
+ pointerEvents: "none",
68
+ });
69
+ if (!lens.canvas.parentElement)
70
+ thumb.append(lens.canvas);
71
+ }
72
+ function schedule() {
73
+ if (!pending)
74
+ pending = requestAnimationFrame(render);
75
+ }
76
+ return {
77
+ draw(options) {
78
+ material = options;
79
+ cancelAnimationFrame(pending);
80
+ render();
81
+ },
82
+ clear() {
83
+ cancelAnimationFrame(pending);
84
+ pending = 0;
85
+ lens.canvas.remove();
86
+ },
87
+ };
88
+ }
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
2
  "name": "liquidglass-ts",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": {
7
+ "types": "./dist/src/glass.d.ts",
8
+ "import": "./dist/src/glass.js"
9
+ },
10
+ "./controls": {
11
+ "types": "./dist/src/controls.d.ts",
12
+ "import": "./dist/src/controls.js"
13
+ },
14
+ "./controls.css": "./src/controls.css"
15
+ },
16
+ "files": [
17
+ "dist/src",
18
+ "src/controls.css"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsc",
22
+ "dev": "npm run build:pages && node scripts/serve-pages.mjs",
23
+ "prepack": "npm run build",
24
+ "test": "npm run build && node tests/browsers.mjs",
25
+ "build:pages": "npm run build && node scripts/build-pages.mjs",
26
+ "bench": "npm run build && node tests/performance.mjs"
27
+ },
28
+ "devDependencies": {
29
+ "playwright-core": "^1.65.1",
30
+ "pngjs": "^7.0.0",
31
+ "typescript": "^5.9.3"
32
+ },
33
+ "description": "Live TypeScript glass refraction for web content, sliders, switches and tab bars.",
34
+ "license": "MIT",
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "git+https://github.com/bradleyseanf/liquidglass-ts.git"
38
+ },
39
+ "homepage": "https://bradleyseanf.github.io/liquidglass-ts/",
40
+ "bugs": {
41
+ "url": "https://github.com/bradleyseanf/liquidglass-ts/issues"
42
+ },
43
+ "keywords": [
44
+ "liquid-glass",
45
+ "glassmorphism",
46
+ "typescript",
47
+ "refraction"
48
+ ],
49
+ "publishConfig": {
50
+ "access": "public"
51
+ }
52
+ }
@@ -0,0 +1,217 @@
1
+ .lg-tabs,
2
+ .lg-slider,
3
+ .lg-switch {
4
+ box-sizing: border-box;
5
+ font-family: -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
6
+ touch-action: none;
7
+ -webkit-tap-highlight-color: transparent;
8
+ user-select: none;
9
+ -webkit-user-select: none;
10
+ -webkit-touch-callout: none;
11
+ }
12
+ .lg-tabs {
13
+ position: relative;
14
+ display: flex;
15
+ padding: 4px;
16
+ height: 63px;
17
+ width: min(340px, 100%);
18
+ isolation: isolate;
19
+ }
20
+ .lg-surface,
21
+ .lg-selection,
22
+ .lg-knob,
23
+ .lg-switch-thumb {
24
+ position: absolute;
25
+ pointer-events: none;
26
+ border-radius: 999px;
27
+ }
28
+ .lg-surface {
29
+ inset: 0;
30
+ background: rgba(246, 248, 250, 0.72);
31
+ }
32
+ .lg-selection {
33
+ top: 5px;
34
+ bottom: 5px;
35
+ left: calc(4px + (100% - 8px) / var(--count) * (var(--tab) + 0.5));
36
+ width: calc((100% - 8px) / var(--count));
37
+ background: rgba(112, 118, 127, 0.16);
38
+ transform: translateX(-50%);
39
+ transition:
40
+ left 0.32s cubic-bezier(0.19, 1.22, 0.38, 1),
41
+ background 0.16s;
42
+ }
43
+ .lg-tabs[data-held] .lg-selection {
44
+ top: -5%;
45
+ bottom: -5%;
46
+ transition: background 0.13s;
47
+ background: #ffffff12;
48
+ z-index: 2;
49
+ width: calc((100% - 8px) / var(--count) * var(--lg-held-width, 1.14));
50
+ box-shadow:
51
+ inset 0 1px 1px #ffffff60,
52
+ inset 0 -1px 1px #0a182d88,
53
+ 0 1px 4px #00152330;
54
+ }
55
+ .lg-tabs button {
56
+ position: relative;
57
+ flex: 1;
58
+ min-width: 0;
59
+ background: transparent;
60
+ border: 0;
61
+ display: flex;
62
+ gap: 2px;
63
+ align-items: center;
64
+ justify-content: center;
65
+ flex-direction: column;
66
+ font:
67
+ 600 10px/1.1 -apple-system,
68
+ system-ui,
69
+ sans-serif;
70
+ color: #19191b;
71
+ cursor: pointer;
72
+ }
73
+ .lg-tabs button[aria-current="page"] {
74
+ color: #007aff;
75
+ }
76
+ .lg-tabs svg {
77
+ width: 24px;
78
+ height: 24px;
79
+ fill: currentColor;
80
+ }
81
+ .lg-slider {
82
+ --knob: 38px;
83
+ position: relative;
84
+ height: 46px;
85
+ width: 100%;
86
+ color: #007aff;
87
+ }
88
+ .lg-rail {
89
+ position: absolute;
90
+ top: 50%;
91
+ left: calc(var(--knob) / 2);
92
+ right: calc(var(--knob) / 2);
93
+ height: 6px;
94
+ transform: translateY(-50%);
95
+ border-radius: 8px;
96
+ background: linear-gradient(
97
+ to right,
98
+ currentColor var(--level),
99
+ #78788042 var(--level)
100
+ );
101
+ }
102
+ .lg-knob {
103
+ width: var(--knob);
104
+ height: 24px;
105
+ top: 50%;
106
+ left: calc((100% - var(--knob)) * var(--fraction, 0.5));
107
+ transform: translateY(-50%);
108
+ background: #fff;
109
+ transition:
110
+ width 0.15s,
111
+ height 0.15s,
112
+ margin 0.15s,
113
+ background 0.14s;
114
+ }
115
+ .lg-slider input {
116
+ position: absolute;
117
+ inset: 0;
118
+ width: 100%;
119
+ height: 100%;
120
+ margin: 0;
121
+ opacity: 0;
122
+ cursor: grab;
123
+ appearance: none;
124
+ background: transparent;
125
+ }
126
+ .lg-slider input::-webkit-slider-thumb {
127
+ appearance: none;
128
+ width: var(--knob);
129
+ height: 30px;
130
+ }
131
+ .lg-slider input::-moz-range-thumb {
132
+ width: var(--knob);
133
+ height: 30px;
134
+ }
135
+ .lg-slider[data-held] .lg-knob {
136
+ background: rgba(255, 255, 255, 0.08);
137
+ width: 58px;
138
+ height: 38px;
139
+ margin-left: -10px;
140
+ }
141
+ .lg-slider:focus-within,
142
+ .lg-tabs button:focus-visible,
143
+ .lg-switch:focus-visible {
144
+ outline: 2px solid #007aff;
145
+ outline-offset: 2px;
146
+ border-radius: 30px;
147
+ }
148
+ .lg-switch {
149
+ position: relative;
150
+ width: 64px;
151
+ height: 28px;
152
+ flex-shrink: 0;
153
+ border: 0;
154
+ border-radius: 18px;
155
+ background: transparent;
156
+ overflow: visible;
157
+ cursor: pointer;
158
+ }
159
+ .lg-switch-rail {
160
+ position: absolute;
161
+ inset: 0;
162
+ border-radius: inherit;
163
+ background: #78788059;
164
+ transition: background 0.18s;
165
+ }
166
+ .lg-switch[aria-checked="true"] .lg-switch-rail {
167
+ background: #34c759;
168
+ }
169
+ .lg-switch-thumb {
170
+ width: 36px;
171
+ height: 24px;
172
+ left: 2px;
173
+ top: 50%;
174
+ background: white;
175
+ transform: translateY(-50%) translateX(calc(var(--toggle) * 24px));
176
+ transition:
177
+ width 0.15s,
178
+ height 0.15s,
179
+ margin 0.15s,
180
+ background 0.14s,
181
+ transform 0.22s cubic-bezier(0.2, 1.15, 0.34, 1);
182
+ }
183
+ /* Resize the aperture itself so its captured track stays aligned. */
184
+ .lg-switch[data-held] .lg-switch-thumb {
185
+ width: 58px;
186
+ height: 38px;
187
+ margin-left: -11px;
188
+ background: rgba(255, 255, 255, 0.08);
189
+ transition:
190
+ width 0.15s,
191
+ height 0.15s,
192
+ margin 0.15s,
193
+ background 0.14s;
194
+ }
195
+ .lg-switch[data-held],
196
+ .lg-slider[data-held] {
197
+ --lg-shadow: 0 4px 7px rgba(25, 45, 60, 0.16);
198
+ }
199
+ .lg-items {
200
+ display: flex;
201
+ position: relative;
202
+ width: 100%;
203
+ height: 100%;
204
+ }
205
+ .lg-tab-viewport {
206
+ position: relative;
207
+ width: 100%;
208
+ height: 100%;
209
+ }
210
+ @media (prefers-reduced-motion: reduce) {
211
+ .lg-tabs .lg-selection,
212
+ .lg-slider .lg-knob,
213
+ .lg-switch .lg-switch-thumb,
214
+ .lg-switch .lg-switch-rail {
215
+ transition: none;
216
+ }
217
+ }