@zakisheriff/liquid-glass 4.0.0 → 4.0.2

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
@@ -206,21 +206,21 @@ var clamp = (value, min, max) => Math.min(max, Math.max(min, value));
206
206
 
207
207
  // src/engine/selection.ts
208
208
  function guardSelection(el2) {
209
- let held = false;
209
+ let held2 = false;
210
210
  const block = (event) => {
211
- if (held) event.preventDefault();
211
+ if (held2) event.preventDefault();
212
212
  };
213
213
  const clear = () => {
214
214
  const selection = window.getSelection?.();
215
215
  if (selection && !selection.isCollapsed) selection.removeAllRanges();
216
216
  };
217
217
  const down = () => {
218
- held = true;
218
+ held2 = true;
219
219
  clear();
220
220
  };
221
221
  const up = () => {
222
- if (!held) return;
223
- held = false;
222
+ if (!held2) return;
223
+ held2 = false;
224
224
  clear();
225
225
  };
226
226
  el2.addEventListener("pointerdown", down, true);
@@ -312,6 +312,9 @@ function usePress(ref, options = {}) {
312
312
  const hold = (event) => {
313
313
  if (pointer && event.cancelable) event.preventDefault();
314
314
  };
315
+ const noDrag = (event) => event.preventDefault();
316
+ el2.setAttribute("data-lg-press", "");
317
+ el2.addEventListener("dragstart", noDrag);
315
318
  const unguard = guardSelection(el2);
316
319
  el2.addEventListener("touchmove", hold, { passive: false });
317
320
  el2.addEventListener("pointerdown", down);
@@ -322,6 +325,8 @@ function usePress(ref, options = {}) {
322
325
  unguard();
323
326
  el2.removeEventListener("pointerdown", down);
324
327
  el2.removeEventListener("touchmove", hold);
328
+ el2.removeEventListener("dragstart", noDrag);
329
+ el2.removeAttribute("data-lg-press");
325
330
  window.removeEventListener("blur", blur);
326
331
  el2.style.removeProperty("scale");
327
332
  el2.style.removeProperty("translate");
@@ -374,6 +379,7 @@ function backdropChain(glass, source) {
374
379
  return chain;
375
380
  }
376
381
  var stateOf = (el2) => Array.from(el2.attributes).map((a) => a.name).filter((name) => name.startsWith("data-") && name !== "data-mirror" && !name.startsWith("data-lg-mirror")).sort().join(" ");
382
+ var paints = (name) => !!name && name !== "style" && name !== "class" && name !== "tabindex" && !name.startsWith("data-") && !name.startsWith("aria-");
377
383
  var BACKGROUND = ["background-color", "background-image", "background-size", "background-position", "background-repeat", "border-radius"];
378
384
  var PLACEMENT = /* @__PURE__ */ new Set(["position", "left", "top", "margin", "margin-top", "margin-right", "margin-bottom", "margin-left", "width", "height", "max-width", "max-height", "min-width", "min-height", "scale", "translate", "transform-origin", "transform"]);
379
385
  function syncStyle(original, copy, root2) {
@@ -542,7 +548,7 @@ function mirrorBackdrop(glass, source, padding, zoom, active2 = () => true) {
542
548
  const mutations = new MutationObserver((records) => {
543
549
  if (records.every((r) => layer.contains(r.target) || r.target === layer || r.target === glass)) return;
544
550
  wake2();
545
- const structural = records.some((r) => r.type !== "attributes" && !layer.contains(r.target));
551
+ const structural = records.some((r) => !layer.contains(r.target) && (r.type !== "attributes" || paints(r.attributeName)));
546
552
  if (structural && !pending) pending = requestAnimationFrame(recopy);
547
553
  else if (!structural && !stopped && visible && !document.hidden && active2()) {
548
554
  applyZoom();
@@ -550,7 +556,7 @@ function mirrorBackdrop(glass, source, padding, zoom, active2 = () => true) {
550
556
  align();
551
557
  }
552
558
  });
553
- mutations.observe(source, { childList: true, characterData: true, attributes: true, attributeFilter: ["style", "class"], subtree: true });
559
+ mutations.observe(source, { childList: true, characterData: true, attributes: true, subtree: true });
554
560
  const io = typeof IntersectionObserver === "function" ? new IntersectionObserver((entries) => {
555
561
  visible = entries.some((e) => e.isIntersecting);
556
562
  wake2();
@@ -664,6 +670,18 @@ function setGlassQuality(quality) {
664
670
  // src/engine/refraction.ts
665
671
  var SVG_NS = "http://www.w3.org/2000/svg";
666
672
  var cache = /* @__PURE__ */ new Map();
673
+ function encode(canvas) {
674
+ return new Promise((done) => {
675
+ if (typeof canvas.toBlob !== "function" || typeof FileReader !== "function") return done(canvas.toDataURL());
676
+ canvas.toBlob((blob) => {
677
+ if (!blob) return done(canvas.toDataURL());
678
+ const reader = new FileReader();
679
+ reader.onload = () => done(reader.result);
680
+ reader.onerror = () => done(canvas.toDataURL());
681
+ reader.readAsDataURL(blob);
682
+ });
683
+ });
684
+ }
667
685
  var defs = null;
668
686
  var counter = 0;
669
687
  function el(name, attrs) {
@@ -713,6 +731,10 @@ function bevelProfile(bezel, thickness, ior) {
713
731
  return a + (b - a) * (f - i);
714
732
  };
715
733
  }
734
+ function fillPixels(image, r, g, b, a) {
735
+ const word = new Uint32Array(new Uint8ClampedArray([r, g, b, a]).buffer)[0];
736
+ new Uint32Array(image.data.buffer, image.data.byteOffset, image.data.length / 4).fill(word);
737
+ }
716
738
  function bake(width, height, radius, shape, pad = 0, simple = false, fringe = false, box = false) {
717
739
  const W = width + pad * 2;
718
740
  const H = height + pad * 2;
@@ -730,6 +752,8 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
730
752
  const rimHue = new Float32Array(cw * ch);
731
753
  const inside = new Float32Array(cw * ch);
732
754
  const zone = new Float32Array(cw * ch);
755
+ const outer = new Int32Array(cw * ch);
756
+ let edges = 0;
733
757
  let max = 1e-4;
734
758
  let maxX = 1e-4;
735
759
  let maxY = 1e-4;
@@ -757,6 +781,7 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
757
781
  inside[y * cw + x] = 1;
758
782
  continue;
759
783
  }
784
+ outer[edges++] = y * cw + x;
760
785
  let nx;
761
786
  let ny;
762
787
  if (len > 0) {
@@ -800,12 +825,14 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
800
825
  const canvas = document.createElement("canvas");
801
826
  canvas.width = cw;
802
827
  canvas.height = ch;
803
- const ctx = canvas.getContext("2d");
828
+ const ctx = canvas.getContext("2d", { willReadFrequently: true });
804
829
  const scale = box ? 2 * Math.max(maxX / W, maxY / H) : max * 2;
805
830
  const spanX = box ? scale * W / 2 : max;
806
831
  const spanY = box ? scale * H / 2 : max;
807
832
  const field = ctx.createImageData(cw, ch);
808
- for (let i = 0; i < cw * ch; i++) {
833
+ fillPixels(field, 128, 128, 128, 255);
834
+ for (let e = 0; e < edges; e++) {
835
+ const i = outer[e];
809
836
  const o = i * 4;
810
837
  field.data[o] = Math.round(127.5 + dx[i] / spanX * 127.5);
811
838
  field.data[o + 1] = Math.round(127.5 + dy[i] / spanY * 127.5);
@@ -813,9 +840,9 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
813
840
  field.data[o + 3] = 255;
814
841
  }
815
842
  ctx.putImageData(field, 0, 0);
816
- const map = canvas.toDataURL();
843
+ const map = encode(canvas);
817
844
  const reach = Math.max(W, H) / 2;
818
- let zoomMap = "";
845
+ let zoomMap = Promise.resolve("");
819
846
  if (magnify && !simple) {
820
847
  const ramp = ctx.createImageData(cw, ch);
821
848
  for (let y = 0; y < ch; y++) {
@@ -831,10 +858,12 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
831
858
  }
832
859
  }
833
860
  ctx.putImageData(ramp, 0, 0);
834
- zoomMap = canvas.toDataURL();
861
+ zoomMap = encode(canvas);
835
862
  }
836
863
  const shine = ctx.createImageData(cw, ch);
837
- for (let i = 0; i < cw * ch; i++) {
864
+ fillPixels(shine, 255, 255, 255, 0);
865
+ for (let e = 0; e < edges; e++) {
866
+ const i = outer[e];
838
867
  const o = i * 4;
839
868
  let r = 255;
840
869
  let g = 255;
@@ -852,26 +881,35 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
852
881
  shine.data[o + 3] = Math.round(rimA[i] * 255);
853
882
  }
854
883
  ctx.putImageData(shine, 0, 0);
855
- const rim = canvas.toDataURL();
884
+ const rim = encode(canvas);
856
885
  const shapeMask = ctx.createImageData(cw, ch);
857
- for (let i = 0; i < cw * ch; i++) {
886
+ fillPixels(shapeMask, 255, 255, 255, 255);
887
+ for (let e = 0; e < edges; e++) {
888
+ const i = outer[e];
858
889
  const o = i * 4;
859
890
  shapeMask.data[o] = shapeMask.data[o + 1] = shapeMask.data[o + 2] = 255;
860
891
  shapeMask.data[o + 3] = Math.round(inside[i] * 255);
861
892
  }
862
893
  ctx.putImageData(shapeMask, 0, 0);
863
- const mask = canvas.toDataURL();
864
- let smooth = "";
894
+ const mask = encode(canvas);
895
+ let smooth = Promise.resolve("");
865
896
  if (lens && !simple) {
866
897
  const soft = ctx.createImageData(cw, ch);
867
- for (let i = 0; i < cw * ch; i++) {
898
+ fillPixels(soft, 255, 255, 255, 0);
899
+ for (let e = 0; e < edges; e++) {
900
+ const i = outer[e];
868
901
  const o = i * 4;
869
902
  soft.data[o] = soft.data[o + 1] = soft.data[o + 2] = 255;
870
903
  soft.data[o + 3] = Math.round(zone[i] * 255);
871
904
  }
872
905
  ctx.putImageData(soft, 0, 0);
873
- smooth = canvas.toDataURL();
906
+ smooth = encode(canvas);
874
907
  }
908
+ return Promise.all([map, zoomMap, rim, mask, smooth]).then(
909
+ ([map2, zoomMap2, rim2, mask2, smooth2]) => assemble(W, H, box, magnify, reach, scale, shape, simple, fringe, map2, zoomMap2, rim2, mask2, smooth2)
910
+ );
911
+ }
912
+ function assemble(W, H, box, magnify, reach, scale, shape, simple, fringe, map, zoomMap, rim, mask, smooth) {
875
913
  const id = `lg-refract-${++counter}`;
876
914
  const area = box ? { x: 0, y: 0, width: 1, height: 1 } : { x: 0, y: 0, width: W, height: H };
877
915
  const filter = el(
@@ -1018,9 +1056,24 @@ function glassFor(width, height, radius, shape = {}, pad = 0, simple = false, fr
1018
1056
  var soon = [];
1019
1057
  var later = [];
1020
1058
  var pumping = false;
1059
+ var held = 0;
1060
+ function holdBakes() {
1061
+ held++;
1062
+ let released = false;
1063
+ return () => {
1064
+ if (released) return;
1065
+ released = true;
1066
+ held--;
1067
+ if (!held && !pumping && (soon.length || later.length)) pump();
1068
+ };
1069
+ }
1021
1070
  function pump() {
1022
1071
  pumping = true;
1023
1072
  const run = (deadline) => {
1073
+ if (held) {
1074
+ pumping = false;
1075
+ return;
1076
+ }
1024
1077
  const end = performance.now() + 8;
1025
1078
  const left = () => deadline ? deadline.timeRemaining() > 4 : performance.now() < end;
1026
1079
  let ran = 0;
@@ -1076,32 +1129,51 @@ function useRefraction(ref, shape = {}, enabled = true, mirror, mirrorActive) {
1076
1129
  const viewport = element.querySelector(":scope > .lg-mirror > .lg-mirror__viewport");
1077
1130
  if (viewport && copyFilter) viewport.style.filter = copyFilter;
1078
1131
  };
1132
+ let fits = 0;
1133
+ let gone = false;
1079
1134
  const fit = () => {
1080
1135
  const width = element.offsetWidth;
1081
1136
  const height = element.offsetHeight;
1082
1137
  if (!width || !height) return;
1138
+ const run = ++fits;
1083
1139
  const radius = cornerRadius(element, width, height);
1084
- const baked = glassFor(width, height, radius, shape);
1085
- filter = baked.filter;
1086
- if (!live && mirror?.current) {
1140
+ const mirrored = !live && !!mirror?.current;
1141
+ if (mirrored) {
1087
1142
  pad = Math.ceil(Math.min(width, height) * 0.45 + 6);
1088
1143
  ({ zoom, zoomX } = resolve(shape, width, height));
1089
1144
  if (tears) {
1090
1145
  zoom = Math.min(zoom, 1);
1091
1146
  zoomX = Math.min(zoomX, 1);
1092
1147
  }
1093
- element.style.setProperty("--lg-inner-mask", baked.inner);
1094
- if (bendCopy) {
1095
- copyFilter = glassFor(width, height, radius, { ...shape, zoom: 1, zoomX: 1 }, pad, false, true, !chromium).filter;
1096
- applyCopyFilter();
1097
- }
1098
- }
1099
- if (live || bendCopy && chromium && !shape.copyOnly) {
1100
- const backdrop = live ? baked.filter : glassFor(width, height, radius, { ...shape, zoom: 1, zoomX: 1, strength: (shape.strength ?? 1) * 0.45 }).filter;
1101
- element.style.setProperty("--lg-refraction", backdrop);
1102
- element.setAttribute("data-lg-refract", shape.kind ?? "surface");
1103
1148
  }
1104
- element.style.setProperty("--lg-rim", baked.rim);
1149
+ const refractBackdrop = live || bendCopy && chromium && !shape.copyOnly;
1150
+ const none = Promise.resolve(null);
1151
+ Promise.all([
1152
+ glassFor(width, height, radius, shape),
1153
+ // The copy is magnified by CSS; the filter only bends the rim and
1154
+ // fringes what it bends with colour.
1155
+ mirrored && bendCopy ? glassFor(width, height, radius, { ...shape, zoom: 1, zoomX: 1 }, pad, false, true, !chromium) : none,
1156
+ // Behind a bent copy the backdrop only carries the bar's glass and
1157
+ // edges: the copy already magnifies the content, so the backdrop isn't
1158
+ // zoomed (that pushed the bar's edge into a ring inside the lens) and
1159
+ // its rim bends gently, curling the bar's edge into thin arcs.
1160
+ refractBackdrop && !live ? glassFor(width, height, radius, { ...shape, zoom: 1, zoomX: 1, strength: (shape.strength ?? 1) * 0.45 }) : none
1161
+ ]).then(([baked, copy, behind]) => {
1162
+ if (run !== fits || gone) return;
1163
+ filter = baked.filter;
1164
+ if (mirrored) {
1165
+ element.style.setProperty("--lg-inner-mask", baked.inner);
1166
+ if (copy) {
1167
+ copyFilter = copy.filter;
1168
+ applyCopyFilter();
1169
+ }
1170
+ }
1171
+ if (refractBackdrop) {
1172
+ element.style.setProperty("--lg-refraction", live ? baked.filter : behind.filter);
1173
+ element.setAttribute("data-lg-refract", shape.kind ?? "surface");
1174
+ }
1175
+ element.style.setProperty("--lg-rim", baked.rim);
1176
+ });
1105
1177
  };
1106
1178
  let seen = typeof IntersectionObserver !== "function";
1107
1179
  const request = () => {
@@ -1130,6 +1202,7 @@ function useRefraction(ref, shape = {}, enabled = true, mirror, mirrorActive) {
1130
1202
  applyCopyFilter();
1131
1203
  const ungovern = governRefraction(element);
1132
1204
  return () => {
1205
+ gone = true;
1133
1206
  cancel?.();
1134
1207
  unmirror?.();
1135
1208
  ungovern();
@@ -1408,11 +1481,20 @@ var GlassSheet = ({ open, onClose, intensity = 5, tint, container: container2, d
1408
1481
  if (wanted.length && sheet.hasAttribute("data-raised") !== raised) sheet.toggleAttribute("data-raised", raised);
1409
1482
  };
1410
1483
  let closing = false;
1411
- const position = createSpring(hidden, OPEN, (y) => {
1484
+ let release = null;
1485
+ const landed = () => {
1486
+ release?.();
1487
+ release = null;
1488
+ sheet.removeAttribute("data-lg-moving");
1489
+ };
1490
+ const position = createSpring(hidden, OPEN, (y, velocity) => {
1412
1491
  paint(y);
1413
1492
  if (closing && y >= hidden - 1) setMounted(false);
1493
+ if (release && Math.abs(y - position.target) < 1 && Math.abs(velocity) < 20) landed();
1414
1494
  });
1415
1495
  const go = (y, feel) => {
1496
+ release ?? (release = holdBakes());
1497
+ sheet.setAttribute("data-lg-moving", "");
1416
1498
  position.tune(feel);
1417
1499
  position.set(y);
1418
1500
  };
@@ -1436,6 +1518,8 @@ var GlassSheet = ({ open, onClose, intensity = 5, tint, container: container2, d
1436
1518
  if (raised && !target.closest(".lg-sheet__grabber-zone") && sheet.scrollTop > 0) return;
1437
1519
  if (target.closest('input, textarea, button, a, [role="slider"], [role="switch"]')) return;
1438
1520
  position.stop();
1521
+ landed();
1522
+ sheet.setAttribute("data-lg-moving", "");
1439
1523
  gesture = { id: event.pointerId, y: event.clientY, from: position.value, t: event.timeStamp, v: 0, last: position.value };
1440
1524
  sheet.setPointerCapture(event.pointerId);
1441
1525
  };
@@ -1486,6 +1570,7 @@ var GlassSheet = ({ open, onClose, intensity = 5, tint, container: container2, d
1486
1570
  sheet.focus({ preventScroll: true });
1487
1571
  return () => {
1488
1572
  position.stop();
1573
+ landed();
1489
1574
  api.current = null;
1490
1575
  observer.disconnect();
1491
1576
  sheet.removeEventListener("pointerdown", down);
@@ -1648,7 +1733,7 @@ var GlassMenu = ({
1648
1733
  (0, import_react7.useEffect)(() => {
1649
1734
  if (!mounted) return;
1650
1735
  const panel = surfaceRef.current;
1651
- let held = null;
1736
+ let held2 = null;
1652
1737
  let hot = null;
1653
1738
  const itemAt = (x, y) => document.elementFromPoint(x, y)?.closest(".lg-menu__item:not(:disabled)") ?? null;
1654
1739
  const light = (next) => {
@@ -1662,16 +1747,16 @@ var GlassMenu = ({
1662
1747
  };
1663
1748
  const down = (event) => {
1664
1749
  if (!event.isPrimary || event.button !== 0) return;
1665
- held = event.pointerId;
1750
+ held2 = event.pointerId;
1666
1751
  hot = null;
1667
1752
  light(itemAt(event.clientX, event.clientY));
1668
1753
  };
1669
1754
  const move = (event) => {
1670
- if (event.pointerId === held) light(itemAt(event.clientX, event.clientY));
1755
+ if (event.pointerId === held2) light(itemAt(event.clientX, event.clientY));
1671
1756
  };
1672
1757
  const up = (event) => {
1673
- if (event.pointerId !== held) return;
1674
- held = null;
1758
+ if (event.pointerId !== held2) return;
1759
+ held2 = null;
1675
1760
  const target = hot;
1676
1761
  light(null);
1677
1762
  const index = target ? Number(target.dataset.index) : -1;
@@ -1684,12 +1769,12 @@ var GlassMenu = ({
1684
1769
  }
1685
1770
  };
1686
1771
  const cancel = (event) => {
1687
- if (event.pointerId !== held) return;
1688
- held = null;
1772
+ if (event.pointerId !== held2) return;
1773
+ held2 = null;
1689
1774
  light(null);
1690
1775
  };
1691
1776
  const hold = (event) => {
1692
- if (held !== null && event.cancelable) event.preventDefault();
1777
+ if (held2 !== null && event.cancelable) event.preventDefault();
1693
1778
  };
1694
1779
  panel.addEventListener("pointerdown", down);
1695
1780
  window.addEventListener("pointermove", move);
@@ -1825,9 +1910,10 @@ function useLensTrack(refs, options) {
1825
1910
  select.style.width = `${slot}px`;
1826
1911
  select.style.left = `${pad}px`;
1827
1912
  glyphs = buttons.flatMap(
1828
- (button) => Array.from(button.querySelectorAll(".lg-nav__label, .lg-nav__icon > .lg-symbol")).map((node) => {
1829
- const label2 = node.classList.contains("lg-nav__label");
1830
- const host = label2 ? node : node.parentElement;
1913
+ (button) => Array.from(button.querySelectorAll(".lg-nav__label, .lg-nav__icon")).map((box) => {
1914
+ const label2 = box.classList.contains("lg-nav__label");
1915
+ const node = label2 ? box : box.querySelector(":scope > .lg-symbol") ?? box;
1916
+ const host = box;
1831
1917
  const n = node.getBoundingClientRect();
1832
1918
  const h = host.getBoundingClientRect();
1833
1919
  return {
@@ -2246,7 +2332,10 @@ var GlassNav = ({
2246
2332
  onChange,
2247
2333
  labels: items.map((item) => item.label),
2248
2334
  items: items.map((item, i) => /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)(import_react10.default.Fragment, { children: [
2249
- item.icon != null && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: "lg-nav__icon", children: item.icon }),
2335
+ item.icon != null && (item.activeIcon != null ? /* @__PURE__ */ (0, import_jsx_runtime8.jsxs)("span", { className: "lg-nav__icons", children: [
2336
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: "lg-nav__icon lg-nav__icon--idle", children: item.icon }),
2337
+ /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: "lg-nav__icon lg-nav__icon--active", children: item.activeIcon })
2338
+ ] }) : /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: "lg-nav__icon", children: item.icon })),
2250
2339
  !iconsOnly && /* @__PURE__ */ (0, import_jsx_runtime8.jsx)("span", { className: "lg-nav__label", children: item.label })
2251
2340
  ] }, i))
2252
2341
  }
@@ -2608,6 +2697,7 @@ GlassSlider.displayName = "GlassSlider";
2608
2697
  var import_react13 = __toESM(require("react"));
2609
2698
  var import_jsx_runtime11 = require("react/jsx-runtime");
2610
2699
  var SETTLE = springFrom(0.36, 0.68);
2700
+ var TINT = springFrom(0.5, 1);
2611
2701
  var REST = 0.655;
2612
2702
  var TRAVEL = 22;
2613
2703
  var GlassSwitch = import_react13.default.forwardRef(
@@ -2668,7 +2758,7 @@ var GlassSwitch = import_react13.default.forwardRef(
2668
2758
  v.glass = lift.target ? value : Math.min(v.glass, value);
2669
2759
  paint();
2670
2760
  });
2671
- const color = createSpring(checked ? 1 : 0, SPRINGS.fade, (value) => {
2761
+ const color = createSpring(checked ? 1 : 0, TINT, (value) => {
2672
2762
  fill.style.opacity = String(clamp(value, 0, 1));
2673
2763
  });
2674
2764
  fill.style.opacity = checked ? "1" : "0";