@zakisheriff/liquid-glass 0.2.1 → 0.2.3

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/dist/index.js CHANGED
@@ -258,12 +258,17 @@ function usePress(ref, options = {}) {
258
258
  window.addEventListener("pointercancel", up);
259
259
  };
260
260
  const blur = () => up();
261
+ const hold = (event) => {
262
+ if (pointer && event.cancelable) event.preventDefault();
263
+ };
264
+ el2.addEventListener("touchmove", hold, { passive: false });
261
265
  el2.addEventListener("pointerdown", down);
262
266
  window.addEventListener("blur", blur);
263
267
  return () => {
264
268
  up();
265
269
  springs.forEach((s) => s.stop());
266
270
  el2.removeEventListener("pointerdown", down);
271
+ el2.removeEventListener("touchmove", hold);
267
272
  window.removeEventListener("blur", blur);
268
273
  el2.style.removeProperty("scale");
269
274
  el2.style.removeProperty("translate");
@@ -290,6 +295,7 @@ function clone(source) {
290
295
  pairs.push({ original, copy, css: original.style.cssText, cls: original.className.toString() });
291
296
  });
292
297
  root.querySelectorAll(`[${SELF}], .lg-mirror, [data-lg-mirror-skip], input, textarea, select`).forEach((n) => n.remove());
298
+ root.removeAttribute("data-mirror");
293
299
  root.setAttribute("inert", "");
294
300
  root.setAttribute("aria-hidden", "true");
295
301
  return { root, pairs };
@@ -302,6 +308,7 @@ var alpha = (color) => {
302
308
  };
303
309
  function backdropChain(glass, source) {
304
310
  const chain = [];
311
+ if (glass.hasAttribute("data-lg-mirror-bare")) return chain;
305
312
  for (let node = glass.parentElement; node; node = node.parentElement) {
306
313
  if (node === source || source.contains(node)) continue;
307
314
  const cs = getComputedStyle(node);
@@ -526,14 +533,13 @@ function bake(width, height, radius, shape, pad = 0, simple = false) {
526
533
  const ch = Math.max(1, Math.ceil(H * density));
527
534
  const lens = shape.kind === "lens";
528
535
  const shift = bevelProfile(shape.bezel, shape.thickness, shape.ior);
529
- const magnify = lens ? 1 - 1 / Math.max(1, shape.zoom) : 0;
536
+ const magnify = lens ? 1 - 1 / Math.max(0.5, shape.zoom) : 0;
530
537
  const direction = lens ? 1 : -1;
531
538
  const dx = new Float32Array(cw * ch);
532
539
  const dy = new Float32Array(cw * ch);
533
540
  const rimA = new Float32Array(cw * ch);
534
541
  const rimHue = new Float32Array(cw * ch);
535
542
  const inside = new Float32Array(cw * ch);
536
- const depth = new Float32Array(cw * ch);
537
543
  let max = 1e-4;
538
544
  const hw = width / 2;
539
545
  const hh = height / 2;
@@ -573,15 +579,13 @@ function bake(width, height, radius, shape, pad = 0, simple = false) {
573
579
  const m = Math.max(Math.abs(vx), Math.abs(vy));
574
580
  if (m > max) max = m;
575
581
  inside[i] = clamp(d / px1 + 0.5, 0, 1);
576
- const t = clamp(d / (shape.bezel * 0.9), 0, 1);
577
- depth[i] = t * t * (3 - 2 * t);
578
582
  if (d >= -px1) {
579
583
  const facing = -(nx + ny) * Math.SQRT1_2;
580
584
  const light = 0.22 + 0.78 * Math.abs(facing) ** 2.2 * (facing > 0 ? 1 : 0.62);
581
585
  const line = Math.exp(-(((d - 0.55) / 0.55) ** 2));
582
- const inner2 = Math.exp(-((d / (shape.bezel * 0.55)) ** 2)) * 0.14;
586
+ const inner = Math.exp(-((d / (shape.bezel * 0.55)) ** 2)) * 0.14;
583
587
  const edgeAA = clamp(d / px1 + 1, 0, 1);
584
- rimA[i] = clamp((line * (lens ? 0.95 : 0.7) + inner2) * light * edgeAA, 0, 1);
588
+ rimA[i] = clamp((line * (lens ? 0.95 : 0.7) + inner) * light * edgeAA, 0, 1);
585
589
  rimHue[i] = Math.atan2(ny, nx);
586
590
  }
587
591
  }
@@ -646,9 +650,6 @@ function bake(width, height, radius, shape, pad = 0, simple = false) {
646
650
  }
647
651
  ctx.putImageData(shapeMask, 0, 0);
648
652
  const mask = canvas.toDataURL();
649
- for (let i = 0; i < cw * ch; i++) shapeMask.data[i * 4 + 3] = Math.round(depth[i] * 255);
650
- ctx.putImageData(shapeMask, 0, 0);
651
- const inner = canvas.toDataURL();
652
653
  const id = `lg-refract-${++counter}`;
653
654
  const box = { x: 0, y: 0, width: W, height: H };
654
655
  const filter = el("filter", {
@@ -718,7 +719,7 @@ function bake(width, height, radius, shape, pad = 0, simple = false) {
718
719
  el("feComposite", { in: "bent", in2: "shape", operator: "in" })
719
720
  );
720
721
  container().append(filter);
721
- return { filter: `url(#${id})`, rim: `url("${rim}")`, inner: `url("${inner}")` };
722
+ return { filter: `url(#${id})`, rim: `url("${rim}")`, inner: `url("${mask}")` };
722
723
  }
723
724
  function resolve(shape, width, height) {
724
725
  const kind = shape.kind ?? "surface";
@@ -908,9 +909,9 @@ var GlassCard = import_react4.default.forwardRef(
908
909
  "div",
909
910
  {
910
911
  ref,
911
- className: ["lg-glass", "lg-card", `lg-card--${variant}`, className].filter(Boolean).join(" "),
912
+ className: ["lg-glass", "lg-glass--host", "lg-card", `lg-card--${variant}`, className].filter(Boolean).join(" "),
912
913
  "data-liquid-glass": "",
913
- style: { padding, borderRadius: radius, ...tint ? { "--lg-card-tint": tint } : {}, ...style },
914
+ style: { padding, borderRadius: radius, ...tint ? { "--lg-host-tint": tint } : {}, ...style },
914
915
  ...rest,
915
916
  children
916
917
  }
@@ -1116,8 +1117,8 @@ var GlassSheet = ({ open, onClose, intensity = 5, tint, children, "aria-label":
1116
1117
  "aria-modal": "true",
1117
1118
  "aria-label": ariaLabel,
1118
1119
  tabIndex: -1,
1119
- className: "lg-glass lg-sheet",
1120
- style: tint ? { "--lg-card-tint": tint } : void 0,
1120
+ className: "lg-glass lg-glass--host lg-sheet",
1121
+ style: tint ? { "--lg-host-tint": tint } : void 0,
1121
1122
  children: [
1122
1123
  /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "lg-sheet__grabber-zone", "aria-hidden": "true", children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { className: "lg-sheet__grabber" }) }),
1123
1124
  /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "lg-sheet__content", children })
@@ -1148,11 +1149,13 @@ function useLensTrack(refs, options) {
1148
1149
  const tint = refs.tint.current;
1149
1150
  const row = refs.row.current;
1150
1151
  const glass = refs.glass.current;
1151
- if (!bar || !lens || !tint || !row || !glass) return;
1152
+ const select = refs.select.current;
1153
+ if (!bar || !lens || !tint || !row || !glass || !select) return;
1152
1154
  let slot = 0;
1153
1155
  let height = 0;
1154
1156
  let pad = 0;
1155
- let barHeight = 0;
1157
+ let W = 0;
1158
+ let H = 0;
1156
1159
  const state = { x: 0, lift: 0, swell: 0, squash: 0 };
1157
1160
  let current = latest.current.index;
1158
1161
  let gesture = null;
@@ -1161,19 +1164,32 @@ function useLensTrack(refs, options) {
1161
1164
  const style = getComputedStyle(bar);
1162
1165
  pad = parseFloat(style.paddingLeft) || 0;
1163
1166
  slot = (bar.clientWidth - pad - (parseFloat(style.paddingRight) || 0)) / latest.current.count;
1164
- height = lens.offsetHeight;
1165
- barHeight = bar.offsetHeight;
1167
+ height = bar.clientHeight - (parseFloat(style.paddingTop) || 0) - (parseFloat(style.paddingBottom) || 0);
1168
+ const { overhang, spread } = latest.current;
1169
+ W = Math.round(slot * (1 + spread));
1170
+ H = Math.round(bar.offsetHeight + overhang);
1171
+ for (const el2 of [lens, tint]) {
1172
+ el2.style.width = `${W}px`;
1173
+ el2.style.height = `${H}px`;
1174
+ el2.style.left = `${pad + (slot - W) / 2}px`;
1175
+ el2.style.top = `${pad + (height - H) / 2}px`;
1176
+ }
1177
+ select.style.width = `${slot}px`;
1178
+ row.style.width = `${slot * latest.current.count}px`;
1179
+ row.style.height = `${height}px`;
1166
1180
  };
1167
1181
  const paint = () => {
1168
- const { x: x2, lift: lift2, swell: swell2, squash } = state;
1169
- const { overhang, spread } = latest.current;
1170
- const liftH = barHeight + overhang - height;
1171
- const sx = 1 + spread * lift2 + squash;
1172
- const sy = 1 + liftH / height * lift2 - squash * 0.6;
1173
- const transform = `translateX(${x2}px) scale(${sx}, ${sy})`;
1182
+ const { x: x2, swell: swell2, squash } = state;
1183
+ const lift2 = clamp(state.lift, 0, 1.08);
1184
+ if (!W || !H) return;
1185
+ const kx = (slot + (W - slot) * lift2) / W * (1 + squash);
1186
+ const ky = (height + (H - height) * lift2) / H * (1 - squash * 0.6);
1187
+ const transform = `translateX(${x2}px) scale(${kx}, ${ky})`;
1174
1188
  lens.style.transform = transform;
1175
1189
  tint.style.transform = transform;
1176
- row.style.transform = `scale(${1 / sx}, ${1 / sy}) translate(${(sx - 1) * slot / 2 - x2}px, ${(sy - 1) * height / 2}px)`;
1190
+ row.style.transform = `translate(${W / 2 + (-slot / 2 - x2) / kx}px, ${H / 2 - height / (2 * ky)}px) scale(${1 / kx}, ${1 / ky})`;
1191
+ select.style.transform = `translateX(${x2}px)`;
1192
+ lens.style.opacity = String(clamp(lift2 * 4, 0, 1));
1177
1193
  const grow = 1 + latest.current.swell / Math.max(bar.offsetWidth, 1) * swell2;
1178
1194
  bar.style.scale = `${grow}`;
1179
1195
  lens.style.setProperty("--lg-lift", String(clamp(lift2, 0, 1)));
@@ -1182,7 +1198,7 @@ function useLensTrack(refs, options) {
1182
1198
  };
1183
1199
  const x = createSpring(current * slot, SPRINGS.snap, (value, velocity) => {
1184
1200
  state.x = value;
1185
- state.squash = clamp(Math.abs(velocity) / 4200, 0, 0.16);
1201
+ state.squash = clamp(Math.abs(velocity) / 6e3, 0, 0.08);
1186
1202
  paint();
1187
1203
  });
1188
1204
  const lift = createSpring(0, SPRINGS.lift, (value) => {
@@ -1303,7 +1319,7 @@ function useLensTrack(refs, options) {
1303
1319
  bar.querySelector(`[data-lg-item="${next}"]`)?.focus();
1304
1320
  };
1305
1321
  const onTouchMove = (event) => {
1306
- if (gesture?.moved && event.cancelable) event.preventDefault();
1322
+ if (gesture && event.cancelable) event.preventDefault();
1307
1323
  };
1308
1324
  const observer = new ResizeObserver(() => {
1309
1325
  measure();
@@ -1359,18 +1375,13 @@ function LensTrack({
1359
1375
  const lens = (0, import_react8.useRef)(null);
1360
1376
  const tint = (0, import_react8.useRef)(null);
1361
1377
  const row = (0, import_react8.useRef)(null);
1378
+ const select = (0, import_react8.useRef)(null);
1362
1379
  const strength = Math.max(1, Math.min(10, intensity)) / 5;
1363
1380
  useRefraction(glass, { kind: "surface", strength });
1364
- useRefraction(
1365
- lens,
1366
- { kind: "lens", strength, zoom: variant === "tabbar" ? 1.14 : 1.1 },
1367
- true,
1368
- bar,
1369
- () => Boolean(bar.current?.hasAttribute("data-lifted"))
1370
- );
1381
+ useRefraction(lens, { kind: "lens", strength, zoom: variant === "tabbar" ? 1.14 : 1.1 });
1371
1382
  const tabbar = variant === "tabbar";
1372
1383
  useLensTrack(
1373
- { bar, glass, lens, tint, row },
1384
+ { bar, glass, lens, tint, row, select },
1374
1385
  {
1375
1386
  count: items.length,
1376
1387
  index,
@@ -1391,6 +1402,7 @@ function LensTrack({
1391
1402
  style: { ...style, "--lg-count": items.length, "--lg-index": index },
1392
1403
  children: [
1393
1404
  /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { ref: glass, className: "lg-glass lg-track__glass", "aria-hidden": "true" }),
1405
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { ref: select, className: "lg-track__select", "aria-hidden": "true" }),
1394
1406
  items.map((item, i) => /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
1395
1407
  "button",
1396
1408
  {
@@ -1406,7 +1418,7 @@ function LensTrack({
1406
1418
  i
1407
1419
  )),
1408
1420
  /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { ref: tint, className: "lg-track__tint", "aria-hidden": "true", children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { ref: row, className: "lg-track__row", children: items.map((item, i) => /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { className: "lg-track__copy", children: item }, i)) }) }),
1409
- /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { ref: lens, className: "lg-glass lg-track__lens", "aria-hidden": "true", children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { className: "lg-track__pill" }) })
1421
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { ref: lens, className: "lg-glass lg-track__lens", "aria-hidden": "true" })
1410
1422
  ]
1411
1423
  }
1412
1424
  );
@@ -1507,10 +1519,12 @@ var GlassSlider = import_react11.default.forwardRef(
1507
1519
  const blockRef = (0, import_react11.useRef)(null);
1508
1520
  (0, import_react11.useImperativeHandle)(forwardedRef, () => inputRef.current);
1509
1521
  const strength = Math.max(1, Math.min(10, intensity)) / 5;
1522
+ const [mirrored, setMirrored] = import_react11.default.useState(false);
1523
+ (0, import_react11.useEffect)(() => setMirrored(!canRefractBackdrop()), []);
1510
1524
  const progress = max > min ? clamp((Number(value) - min) / (max - min), 0, 1) : 0;
1511
1525
  useRefraction(
1512
1526
  thumbRef,
1513
- { kind: "lens", zoom: 1.95, strength, dispersion: 0.6 },
1527
+ { kind: "lens", zoom: 1.95, bezel: 7, thickness: 16, strength, dispersion: 0.6 },
1514
1528
  showThumb,
1515
1529
  rootRef,
1516
1530
  () => parseFloat(rootRef.current?.style.getPropertyValue("--lg-lift") || "0") > 0.01
@@ -1680,11 +1694,21 @@ var GlassSlider = import_react11.default.forwardRef(
1680
1694
  }
1681
1695
  );
1682
1696
  }
1683
- return /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("div", { ref: rootRef, className: ["lg-slider", className].filter(Boolean).join(" "), "data-liquid-glass": "", style: vars, children: [
1684
- /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: "lg-slider__rail", "aria-hidden": "true", children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: "lg-slider__fill" }) }),
1685
- /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: "lg-slider__thumb-slot", "aria-hidden": "true", children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { ref: thumbRef, className: "lg-glass lg-slider__thumb", children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { ref: faceRef, className: "lg-slider__face" }) }) }),
1686
- input
1687
- ] });
1697
+ return /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(
1698
+ "div",
1699
+ {
1700
+ ref: rootRef,
1701
+ className: ["lg-slider", className].filter(Boolean).join(" "),
1702
+ "data-liquid-glass": "",
1703
+ "data-mirror": mirrored ? "" : void 0,
1704
+ style: vars,
1705
+ children: [
1706
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: "lg-slider__rail", "aria-hidden": "true", children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: "lg-slider__fill" }) }),
1707
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: "lg-slider__thumb-slot", "aria-hidden": "true", children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { ref: thumbRef, className: "lg-glass lg-slider__thumb", "data-lg-mirror-bare": "", children: /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { ref: faceRef, className: "lg-slider__face" }) }) }),
1708
+ input
1709
+ ]
1710
+ }
1711
+ );
1688
1712
  }
1689
1713
  );
1690
1714
  GlassSlider.displayName = "GlassSlider";
@@ -1692,6 +1716,7 @@ GlassSlider.displayName = "GlassSlider";
1692
1716
  // src/components/GlassSwitch/GlassSwitch.tsx
1693
1717
  var import_react12 = __toESM(require("react"));
1694
1718
  var import_jsx_runtime9 = require("react/jsx-runtime");
1719
+ var SETTLE = springFrom(0.36, 0.68);
1695
1720
  var REST = 0.655;
1696
1721
  var TRAVEL = 22;
1697
1722
  var GlassSwitch = import_react12.default.forwardRef(
@@ -1717,7 +1742,7 @@ var GlassSwitch = import_react12.default.forwardRef(
1717
1742
  const sync = (0, import_react12.useRef)(null);
1718
1743
  useRefraction(
1719
1744
  thumbRef,
1720
- { kind: "lens", zoom: 1.45, strength: Math.max(1, Math.min(10, intensity)) / 5 },
1745
+ { kind: "lens", zoom: 1, dispersion: 0.5, strength: Math.max(1, Math.min(10, intensity)) / 5 },
1721
1746
  true,
1722
1747
  ref,
1723
1748
  () => parseFloat(ref.current?.style.getPropertyValue("--lg-lift") || "0") > 0.01
@@ -1739,7 +1764,7 @@ var GlassSwitch = import_react12.default.forwardRef(
1739
1764
  };
1740
1765
  const position = createSpring(v.x, SPRINGS.snap, (value, velocity) => {
1741
1766
  v.x = value;
1742
- v.stretch = clamp(Math.abs(velocity) / 40, 0, 0.14);
1767
+ v.stretch = clamp(Math.abs(velocity) / 140, 0, 0.05);
1743
1768
  paint();
1744
1769
  });
1745
1770
  const lift = createSpring(0, SPRINGS.lift, (value) => {
@@ -1762,11 +1787,12 @@ var GlassSwitch = import_react12.default.forwardRef(
1762
1787
  const drop = (delay) => {
1763
1788
  clearTimeout(dropTimer);
1764
1789
  dropTimer = window.setTimeout(() => {
1765
- lift.tune(SPRINGS.settle);
1790
+ lift.tune(SETTLE);
1766
1791
  lift.set(0);
1767
1792
  }, delay);
1768
1793
  };
1769
1794
  const down = (event) => {
1795
+ el2.removeAttribute("data-keyboard");
1770
1796
  if (!event.isPrimary || event.button !== 0 || latest.current.disabled) return;
1771
1797
  clearTimeout(dropTimer);
1772
1798
  gesture = { id: event.pointerId, x: event.clientX, from: latest.current.checked ? 1 : 0, moved: false, at: performance.now() };
@@ -1852,6 +1878,10 @@ var GlassSwitch = import_react12.default.forwardRef(
1852
1878
  "data-liquid-glass": "",
1853
1879
  style: tint ? { "--lg-switch-on": tint, ...style } : style,
1854
1880
  ...rest,
1881
+ onKeyDown: (event) => {
1882
+ event.currentTarget.setAttribute("data-keyboard", "");
1883
+ rest.onKeyDown?.(event);
1884
+ },
1855
1885
  children: [
1856
1886
  /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("span", { ref: fillRef, className: "lg-switch__fill", "aria-hidden": "true" }),
1857
1887
  /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("span", { ref: thumbRef, className: "lg-glass lg-switch__thumb", "aria-hidden": "true", children: /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("span", { ref: faceRef, className: "lg-switch__face" }) })