@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.mjs CHANGED
@@ -206,12 +206,17 @@ function usePress(ref, options = {}) {
206
206
  window.addEventListener("pointercancel", up);
207
207
  };
208
208
  const blur = () => up();
209
+ const hold = (event) => {
210
+ if (pointer && event.cancelable) event.preventDefault();
211
+ };
212
+ el2.addEventListener("touchmove", hold, { passive: false });
209
213
  el2.addEventListener("pointerdown", down);
210
214
  window.addEventListener("blur", blur);
211
215
  return () => {
212
216
  up();
213
217
  springs.forEach((s) => s.stop());
214
218
  el2.removeEventListener("pointerdown", down);
219
+ el2.removeEventListener("touchmove", hold);
215
220
  window.removeEventListener("blur", blur);
216
221
  el2.style.removeProperty("scale");
217
222
  el2.style.removeProperty("translate");
@@ -238,6 +243,7 @@ function clone(source) {
238
243
  pairs.push({ original, copy, css: original.style.cssText, cls: original.className.toString() });
239
244
  });
240
245
  root.querySelectorAll(`[${SELF}], .lg-mirror, [data-lg-mirror-skip], input, textarea, select`).forEach((n) => n.remove());
246
+ root.removeAttribute("data-mirror");
241
247
  root.setAttribute("inert", "");
242
248
  root.setAttribute("aria-hidden", "true");
243
249
  return { root, pairs };
@@ -250,6 +256,7 @@ var alpha = (color) => {
250
256
  };
251
257
  function backdropChain(glass, source) {
252
258
  const chain = [];
259
+ if (glass.hasAttribute("data-lg-mirror-bare")) return chain;
253
260
  for (let node = glass.parentElement; node; node = node.parentElement) {
254
261
  if (node === source || source.contains(node)) continue;
255
262
  const cs = getComputedStyle(node);
@@ -474,14 +481,13 @@ function bake(width, height, radius, shape, pad = 0, simple = false) {
474
481
  const ch = Math.max(1, Math.ceil(H * density));
475
482
  const lens = shape.kind === "lens";
476
483
  const shift = bevelProfile(shape.bezel, shape.thickness, shape.ior);
477
- const magnify = lens ? 1 - 1 / Math.max(1, shape.zoom) : 0;
484
+ const magnify = lens ? 1 - 1 / Math.max(0.5, shape.zoom) : 0;
478
485
  const direction = lens ? 1 : -1;
479
486
  const dx = new Float32Array(cw * ch);
480
487
  const dy = new Float32Array(cw * ch);
481
488
  const rimA = new Float32Array(cw * ch);
482
489
  const rimHue = new Float32Array(cw * ch);
483
490
  const inside = new Float32Array(cw * ch);
484
- const depth = new Float32Array(cw * ch);
485
491
  let max = 1e-4;
486
492
  const hw = width / 2;
487
493
  const hh = height / 2;
@@ -521,15 +527,13 @@ function bake(width, height, radius, shape, pad = 0, simple = false) {
521
527
  const m = Math.max(Math.abs(vx), Math.abs(vy));
522
528
  if (m > max) max = m;
523
529
  inside[i] = clamp(d / px1 + 0.5, 0, 1);
524
- const t = clamp(d / (shape.bezel * 0.9), 0, 1);
525
- depth[i] = t * t * (3 - 2 * t);
526
530
  if (d >= -px1) {
527
531
  const facing = -(nx + ny) * Math.SQRT1_2;
528
532
  const light = 0.22 + 0.78 * Math.abs(facing) ** 2.2 * (facing > 0 ? 1 : 0.62);
529
533
  const line = Math.exp(-(((d - 0.55) / 0.55) ** 2));
530
- const inner2 = Math.exp(-((d / (shape.bezel * 0.55)) ** 2)) * 0.14;
534
+ const inner = Math.exp(-((d / (shape.bezel * 0.55)) ** 2)) * 0.14;
531
535
  const edgeAA = clamp(d / px1 + 1, 0, 1);
532
- rimA[i] = clamp((line * (lens ? 0.95 : 0.7) + inner2) * light * edgeAA, 0, 1);
536
+ rimA[i] = clamp((line * (lens ? 0.95 : 0.7) + inner) * light * edgeAA, 0, 1);
533
537
  rimHue[i] = Math.atan2(ny, nx);
534
538
  }
535
539
  }
@@ -594,9 +598,6 @@ function bake(width, height, radius, shape, pad = 0, simple = false) {
594
598
  }
595
599
  ctx.putImageData(shapeMask, 0, 0);
596
600
  const mask = canvas.toDataURL();
597
- for (let i = 0; i < cw * ch; i++) shapeMask.data[i * 4 + 3] = Math.round(depth[i] * 255);
598
- ctx.putImageData(shapeMask, 0, 0);
599
- const inner = canvas.toDataURL();
600
601
  const id = `lg-refract-${++counter}`;
601
602
  const box = { x: 0, y: 0, width: W, height: H };
602
603
  const filter = el("filter", {
@@ -666,7 +667,7 @@ function bake(width, height, radius, shape, pad = 0, simple = false) {
666
667
  el("feComposite", { in: "bent", in2: "shape", operator: "in" })
667
668
  );
668
669
  container().append(filter);
669
- return { filter: `url(#${id})`, rim: `url("${rim}")`, inner: `url("${inner}")` };
670
+ return { filter: `url(#${id})`, rim: `url("${rim}")`, inner: `url("${mask}")` };
670
671
  }
671
672
  function resolve(shape, width, height) {
672
673
  const kind = shape.kind ?? "surface";
@@ -856,9 +857,9 @@ var GlassCard = React2.forwardRef(
856
857
  "div",
857
858
  {
858
859
  ref,
859
- className: ["lg-glass", "lg-card", `lg-card--${variant}`, className].filter(Boolean).join(" "),
860
+ className: ["lg-glass", "lg-glass--host", "lg-card", `lg-card--${variant}`, className].filter(Boolean).join(" "),
860
861
  "data-liquid-glass": "",
861
- style: { padding, borderRadius: radius, ...tint ? { "--lg-card-tint": tint } : {}, ...style },
862
+ style: { padding, borderRadius: radius, ...tint ? { "--lg-host-tint": tint } : {}, ...style },
862
863
  ...rest,
863
864
  children
864
865
  }
@@ -1064,8 +1065,8 @@ var GlassSheet = ({ open, onClose, intensity = 5, tint, children, "aria-label":
1064
1065
  "aria-modal": "true",
1065
1066
  "aria-label": ariaLabel,
1066
1067
  tabIndex: -1,
1067
- className: "lg-glass lg-sheet",
1068
- style: tint ? { "--lg-card-tint": tint } : void 0,
1068
+ className: "lg-glass lg-glass--host lg-sheet",
1069
+ style: tint ? { "--lg-host-tint": tint } : void 0,
1069
1070
  children: [
1070
1071
  /* @__PURE__ */ jsx4("div", { className: "lg-sheet__grabber-zone", "aria-hidden": "true", children: /* @__PURE__ */ jsx4("span", { className: "lg-sheet__grabber" }) }),
1071
1072
  /* @__PURE__ */ jsx4("div", { className: "lg-sheet__content", children })
@@ -1096,11 +1097,13 @@ function useLensTrack(refs, options) {
1096
1097
  const tint = refs.tint.current;
1097
1098
  const row = refs.row.current;
1098
1099
  const glass = refs.glass.current;
1099
- if (!bar || !lens || !tint || !row || !glass) return;
1100
+ const select = refs.select.current;
1101
+ if (!bar || !lens || !tint || !row || !glass || !select) return;
1100
1102
  let slot = 0;
1101
1103
  let height = 0;
1102
1104
  let pad = 0;
1103
- let barHeight = 0;
1105
+ let W = 0;
1106
+ let H = 0;
1104
1107
  const state = { x: 0, lift: 0, swell: 0, squash: 0 };
1105
1108
  let current = latest.current.index;
1106
1109
  let gesture = null;
@@ -1109,19 +1112,32 @@ function useLensTrack(refs, options) {
1109
1112
  const style = getComputedStyle(bar);
1110
1113
  pad = parseFloat(style.paddingLeft) || 0;
1111
1114
  slot = (bar.clientWidth - pad - (parseFloat(style.paddingRight) || 0)) / latest.current.count;
1112
- height = lens.offsetHeight;
1113
- barHeight = bar.offsetHeight;
1115
+ height = bar.clientHeight - (parseFloat(style.paddingTop) || 0) - (parseFloat(style.paddingBottom) || 0);
1116
+ const { overhang, spread } = latest.current;
1117
+ W = Math.round(slot * (1 + spread));
1118
+ H = Math.round(bar.offsetHeight + overhang);
1119
+ for (const el2 of [lens, tint]) {
1120
+ el2.style.width = `${W}px`;
1121
+ el2.style.height = `${H}px`;
1122
+ el2.style.left = `${pad + (slot - W) / 2}px`;
1123
+ el2.style.top = `${pad + (height - H) / 2}px`;
1124
+ }
1125
+ select.style.width = `${slot}px`;
1126
+ row.style.width = `${slot * latest.current.count}px`;
1127
+ row.style.height = `${height}px`;
1114
1128
  };
1115
1129
  const paint = () => {
1116
- const { x: x2, lift: lift2, swell: swell2, squash } = state;
1117
- const { overhang, spread } = latest.current;
1118
- const liftH = barHeight + overhang - height;
1119
- const sx = 1 + spread * lift2 + squash;
1120
- const sy = 1 + liftH / height * lift2 - squash * 0.6;
1121
- const transform = `translateX(${x2}px) scale(${sx}, ${sy})`;
1130
+ const { x: x2, swell: swell2, squash } = state;
1131
+ const lift2 = clamp(state.lift, 0, 1.08);
1132
+ if (!W || !H) return;
1133
+ const kx = (slot + (W - slot) * lift2) / W * (1 + squash);
1134
+ const ky = (height + (H - height) * lift2) / H * (1 - squash * 0.6);
1135
+ const transform = `translateX(${x2}px) scale(${kx}, ${ky})`;
1122
1136
  lens.style.transform = transform;
1123
1137
  tint.style.transform = transform;
1124
- row.style.transform = `scale(${1 / sx}, ${1 / sy}) translate(${(sx - 1) * slot / 2 - x2}px, ${(sy - 1) * height / 2}px)`;
1138
+ row.style.transform = `translate(${W / 2 + (-slot / 2 - x2) / kx}px, ${H / 2 - height / (2 * ky)}px) scale(${1 / kx}, ${1 / ky})`;
1139
+ select.style.transform = `translateX(${x2}px)`;
1140
+ lens.style.opacity = String(clamp(lift2 * 4, 0, 1));
1125
1141
  const grow = 1 + latest.current.swell / Math.max(bar.offsetWidth, 1) * swell2;
1126
1142
  bar.style.scale = `${grow}`;
1127
1143
  lens.style.setProperty("--lg-lift", String(clamp(lift2, 0, 1)));
@@ -1130,7 +1146,7 @@ function useLensTrack(refs, options) {
1130
1146
  };
1131
1147
  const x = createSpring(current * slot, SPRINGS.snap, (value, velocity) => {
1132
1148
  state.x = value;
1133
- state.squash = clamp(Math.abs(velocity) / 4200, 0, 0.16);
1149
+ state.squash = clamp(Math.abs(velocity) / 6e3, 0, 0.08);
1134
1150
  paint();
1135
1151
  });
1136
1152
  const lift = createSpring(0, SPRINGS.lift, (value) => {
@@ -1251,7 +1267,7 @@ function useLensTrack(refs, options) {
1251
1267
  bar.querySelector(`[data-lg-item="${next}"]`)?.focus();
1252
1268
  };
1253
1269
  const onTouchMove = (event) => {
1254
- if (gesture?.moved && event.cancelable) event.preventDefault();
1270
+ if (gesture && event.cancelable) event.preventDefault();
1255
1271
  };
1256
1272
  const observer = new ResizeObserver(() => {
1257
1273
  measure();
@@ -1307,18 +1323,13 @@ function LensTrack({
1307
1323
  const lens = useRef6(null);
1308
1324
  const tint = useRef6(null);
1309
1325
  const row = useRef6(null);
1326
+ const select = useRef6(null);
1310
1327
  const strength = Math.max(1, Math.min(10, intensity)) / 5;
1311
1328
  useRefraction(glass, { kind: "surface", strength });
1312
- useRefraction(
1313
- lens,
1314
- { kind: "lens", strength, zoom: variant === "tabbar" ? 1.14 : 1.1 },
1315
- true,
1316
- bar,
1317
- () => Boolean(bar.current?.hasAttribute("data-lifted"))
1318
- );
1329
+ useRefraction(lens, { kind: "lens", strength, zoom: variant === "tabbar" ? 1.14 : 1.1 });
1319
1330
  const tabbar = variant === "tabbar";
1320
1331
  useLensTrack(
1321
- { bar, glass, lens, tint, row },
1332
+ { bar, glass, lens, tint, row, select },
1322
1333
  {
1323
1334
  count: items.length,
1324
1335
  index,
@@ -1339,6 +1350,7 @@ function LensTrack({
1339
1350
  style: { ...style, "--lg-count": items.length, "--lg-index": index },
1340
1351
  children: [
1341
1352
  /* @__PURE__ */ jsx5("span", { ref: glass, className: "lg-glass lg-track__glass", "aria-hidden": "true" }),
1353
+ /* @__PURE__ */ jsx5("span", { ref: select, className: "lg-track__select", "aria-hidden": "true" }),
1342
1354
  items.map((item, i) => /* @__PURE__ */ jsx5(
1343
1355
  "button",
1344
1356
  {
@@ -1354,7 +1366,7 @@ function LensTrack({
1354
1366
  i
1355
1367
  )),
1356
1368
  /* @__PURE__ */ jsx5("span", { ref: tint, className: "lg-track__tint", "aria-hidden": "true", children: /* @__PURE__ */ jsx5("span", { ref: row, className: "lg-track__row", children: items.map((item, i) => /* @__PURE__ */ jsx5("span", { className: "lg-track__copy", children: item }, i)) }) }),
1357
- /* @__PURE__ */ jsx5("span", { ref: lens, className: "lg-glass lg-track__lens", "aria-hidden": "true", children: /* @__PURE__ */ jsx5("span", { className: "lg-track__pill" }) })
1369
+ /* @__PURE__ */ jsx5("span", { ref: lens, className: "lg-glass lg-track__lens", "aria-hidden": "true" })
1358
1370
  ]
1359
1371
  }
1360
1372
  );
@@ -1455,10 +1467,12 @@ var GlassSlider = React8.forwardRef(
1455
1467
  const blockRef = useRef7(null);
1456
1468
  useImperativeHandle4(forwardedRef, () => inputRef.current);
1457
1469
  const strength = Math.max(1, Math.min(10, intensity)) / 5;
1470
+ const [mirrored, setMirrored] = React8.useState(false);
1471
+ useEffect5(() => setMirrored(!canRefractBackdrop()), []);
1458
1472
  const progress = max > min ? clamp((Number(value) - min) / (max - min), 0, 1) : 0;
1459
1473
  useRefraction(
1460
1474
  thumbRef,
1461
- { kind: "lens", zoom: 1.95, strength, dispersion: 0.6 },
1475
+ { kind: "lens", zoom: 1.95, bezel: 7, thickness: 16, strength, dispersion: 0.6 },
1462
1476
  showThumb,
1463
1477
  rootRef,
1464
1478
  () => parseFloat(rootRef.current?.style.getPropertyValue("--lg-lift") || "0") > 0.01
@@ -1628,11 +1642,21 @@ var GlassSlider = React8.forwardRef(
1628
1642
  }
1629
1643
  );
1630
1644
  }
1631
- return /* @__PURE__ */ jsxs7("div", { ref: rootRef, className: ["lg-slider", className].filter(Boolean).join(" "), "data-liquid-glass": "", style: vars, children: [
1632
- /* @__PURE__ */ jsx8("span", { className: "lg-slider__rail", "aria-hidden": "true", children: /* @__PURE__ */ jsx8("span", { className: "lg-slider__fill" }) }),
1633
- /* @__PURE__ */ jsx8("span", { className: "lg-slider__thumb-slot", "aria-hidden": "true", children: /* @__PURE__ */ jsx8("span", { ref: thumbRef, className: "lg-glass lg-slider__thumb", children: /* @__PURE__ */ jsx8("span", { ref: faceRef, className: "lg-slider__face" }) }) }),
1634
- input
1635
- ] });
1645
+ return /* @__PURE__ */ jsxs7(
1646
+ "div",
1647
+ {
1648
+ ref: rootRef,
1649
+ className: ["lg-slider", className].filter(Boolean).join(" "),
1650
+ "data-liquid-glass": "",
1651
+ "data-mirror": mirrored ? "" : void 0,
1652
+ style: vars,
1653
+ children: [
1654
+ /* @__PURE__ */ jsx8("span", { className: "lg-slider__rail", "aria-hidden": "true", children: /* @__PURE__ */ jsx8("span", { className: "lg-slider__fill" }) }),
1655
+ /* @__PURE__ */ jsx8("span", { className: "lg-slider__thumb-slot", "aria-hidden": "true", children: /* @__PURE__ */ jsx8("span", { ref: thumbRef, className: "lg-glass lg-slider__thumb", "data-lg-mirror-bare": "", children: /* @__PURE__ */ jsx8("span", { ref: faceRef, className: "lg-slider__face" }) }) }),
1656
+ input
1657
+ ]
1658
+ }
1659
+ );
1636
1660
  }
1637
1661
  );
1638
1662
  GlassSlider.displayName = "GlassSlider";
@@ -1640,6 +1664,7 @@ GlassSlider.displayName = "GlassSlider";
1640
1664
  // src/components/GlassSwitch/GlassSwitch.tsx
1641
1665
  import React9, { useEffect as useEffect6, useImperativeHandle as useImperativeHandle5, useRef as useRef8, useState as useState3 } from "react";
1642
1666
  import { jsx as jsx9, jsxs as jsxs8 } from "react/jsx-runtime";
1667
+ var SETTLE = springFrom(0.36, 0.68);
1643
1668
  var REST = 0.655;
1644
1669
  var TRAVEL = 22;
1645
1670
  var GlassSwitch = React9.forwardRef(
@@ -1665,7 +1690,7 @@ var GlassSwitch = React9.forwardRef(
1665
1690
  const sync = useRef8(null);
1666
1691
  useRefraction(
1667
1692
  thumbRef,
1668
- { kind: "lens", zoom: 1.45, strength: Math.max(1, Math.min(10, intensity)) / 5 },
1693
+ { kind: "lens", zoom: 1, dispersion: 0.5, strength: Math.max(1, Math.min(10, intensity)) / 5 },
1669
1694
  true,
1670
1695
  ref,
1671
1696
  () => parseFloat(ref.current?.style.getPropertyValue("--lg-lift") || "0") > 0.01
@@ -1687,7 +1712,7 @@ var GlassSwitch = React9.forwardRef(
1687
1712
  };
1688
1713
  const position = createSpring(v.x, SPRINGS.snap, (value, velocity) => {
1689
1714
  v.x = value;
1690
- v.stretch = clamp(Math.abs(velocity) / 40, 0, 0.14);
1715
+ v.stretch = clamp(Math.abs(velocity) / 140, 0, 0.05);
1691
1716
  paint();
1692
1717
  });
1693
1718
  const lift = createSpring(0, SPRINGS.lift, (value) => {
@@ -1710,11 +1735,12 @@ var GlassSwitch = React9.forwardRef(
1710
1735
  const drop = (delay) => {
1711
1736
  clearTimeout(dropTimer);
1712
1737
  dropTimer = window.setTimeout(() => {
1713
- lift.tune(SPRINGS.settle);
1738
+ lift.tune(SETTLE);
1714
1739
  lift.set(0);
1715
1740
  }, delay);
1716
1741
  };
1717
1742
  const down = (event) => {
1743
+ el2.removeAttribute("data-keyboard");
1718
1744
  if (!event.isPrimary || event.button !== 0 || latest.current.disabled) return;
1719
1745
  clearTimeout(dropTimer);
1720
1746
  gesture = { id: event.pointerId, x: event.clientX, from: latest.current.checked ? 1 : 0, moved: false, at: performance.now() };
@@ -1800,6 +1826,10 @@ var GlassSwitch = React9.forwardRef(
1800
1826
  "data-liquid-glass": "",
1801
1827
  style: tint ? { "--lg-switch-on": tint, ...style } : style,
1802
1828
  ...rest,
1829
+ onKeyDown: (event) => {
1830
+ event.currentTarget.setAttribute("data-keyboard", "");
1831
+ rest.onKeyDown?.(event);
1832
+ },
1803
1833
  children: [
1804
1834
  /* @__PURE__ */ jsx9("span", { ref: fillRef, className: "lg-switch__fill", "aria-hidden": "true" }),
1805
1835
  /* @__PURE__ */ jsx9("span", { ref: thumbRef, className: "lg-glass lg-switch__thumb", "aria-hidden": "true", children: /* @__PURE__ */ jsx9("span", { ref: faceRef, className: "lg-switch__face" }) })