@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.mjs CHANGED
@@ -152,21 +152,21 @@ var clamp = (value, min, max) => Math.min(max, Math.max(min, value));
152
152
 
153
153
  // src/engine/selection.ts
154
154
  function guardSelection(el2) {
155
- let held = false;
155
+ let held2 = false;
156
156
  const block = (event) => {
157
- if (held) event.preventDefault();
157
+ if (held2) event.preventDefault();
158
158
  };
159
159
  const clear = () => {
160
160
  const selection = window.getSelection?.();
161
161
  if (selection && !selection.isCollapsed) selection.removeAllRanges();
162
162
  };
163
163
  const down = () => {
164
- held = true;
164
+ held2 = true;
165
165
  clear();
166
166
  };
167
167
  const up = () => {
168
- if (!held) return;
169
- held = false;
168
+ if (!held2) return;
169
+ held2 = false;
170
170
  clear();
171
171
  };
172
172
  el2.addEventListener("pointerdown", down, true);
@@ -258,6 +258,9 @@ function usePress(ref, options = {}) {
258
258
  const hold = (event) => {
259
259
  if (pointer && event.cancelable) event.preventDefault();
260
260
  };
261
+ const noDrag = (event) => event.preventDefault();
262
+ el2.setAttribute("data-lg-press", "");
263
+ el2.addEventListener("dragstart", noDrag);
261
264
  const unguard = guardSelection(el2);
262
265
  el2.addEventListener("touchmove", hold, { passive: false });
263
266
  el2.addEventListener("pointerdown", down);
@@ -268,6 +271,8 @@ function usePress(ref, options = {}) {
268
271
  unguard();
269
272
  el2.removeEventListener("pointerdown", down);
270
273
  el2.removeEventListener("touchmove", hold);
274
+ el2.removeEventListener("dragstart", noDrag);
275
+ el2.removeAttribute("data-lg-press");
271
276
  window.removeEventListener("blur", blur);
272
277
  el2.style.removeProperty("scale");
273
278
  el2.style.removeProperty("translate");
@@ -320,6 +325,7 @@ function backdropChain(glass, source) {
320
325
  return chain;
321
326
  }
322
327
  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(" ");
328
+ var paints = (name) => !!name && name !== "style" && name !== "class" && name !== "tabindex" && !name.startsWith("data-") && !name.startsWith("aria-");
323
329
  var BACKGROUND = ["background-color", "background-image", "background-size", "background-position", "background-repeat", "border-radius"];
324
330
  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"]);
325
331
  function syncStyle(original, copy, root2) {
@@ -488,7 +494,7 @@ function mirrorBackdrop(glass, source, padding, zoom, active2 = () => true) {
488
494
  const mutations = new MutationObserver((records) => {
489
495
  if (records.every((r) => layer.contains(r.target) || r.target === layer || r.target === glass)) return;
490
496
  wake2();
491
- const structural = records.some((r) => r.type !== "attributes" && !layer.contains(r.target));
497
+ const structural = records.some((r) => !layer.contains(r.target) && (r.type !== "attributes" || paints(r.attributeName)));
492
498
  if (structural && !pending) pending = requestAnimationFrame(recopy);
493
499
  else if (!structural && !stopped && visible && !document.hidden && active2()) {
494
500
  applyZoom();
@@ -496,7 +502,7 @@ function mirrorBackdrop(glass, source, padding, zoom, active2 = () => true) {
496
502
  align();
497
503
  }
498
504
  });
499
- mutations.observe(source, { childList: true, characterData: true, attributes: true, attributeFilter: ["style", "class"], subtree: true });
505
+ mutations.observe(source, { childList: true, characterData: true, attributes: true, subtree: true });
500
506
  const io = typeof IntersectionObserver === "function" ? new IntersectionObserver((entries) => {
501
507
  visible = entries.some((e) => e.isIntersecting);
502
508
  wake2();
@@ -610,6 +616,18 @@ function setGlassQuality(quality) {
610
616
  // src/engine/refraction.ts
611
617
  var SVG_NS = "http://www.w3.org/2000/svg";
612
618
  var cache = /* @__PURE__ */ new Map();
619
+ function encode(canvas) {
620
+ return new Promise((done) => {
621
+ if (typeof canvas.toBlob !== "function" || typeof FileReader !== "function") return done(canvas.toDataURL());
622
+ canvas.toBlob((blob) => {
623
+ if (!blob) return done(canvas.toDataURL());
624
+ const reader = new FileReader();
625
+ reader.onload = () => done(reader.result);
626
+ reader.onerror = () => done(canvas.toDataURL());
627
+ reader.readAsDataURL(blob);
628
+ });
629
+ });
630
+ }
613
631
  var defs = null;
614
632
  var counter = 0;
615
633
  function el(name, attrs) {
@@ -659,6 +677,10 @@ function bevelProfile(bezel, thickness, ior) {
659
677
  return a + (b - a) * (f - i);
660
678
  };
661
679
  }
680
+ function fillPixels(image, r, g, b, a) {
681
+ const word = new Uint32Array(new Uint8ClampedArray([r, g, b, a]).buffer)[0];
682
+ new Uint32Array(image.data.buffer, image.data.byteOffset, image.data.length / 4).fill(word);
683
+ }
662
684
  function bake(width, height, radius, shape, pad = 0, simple = false, fringe = false, box = false) {
663
685
  const W = width + pad * 2;
664
686
  const H = height + pad * 2;
@@ -676,6 +698,8 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
676
698
  const rimHue = new Float32Array(cw * ch);
677
699
  const inside = new Float32Array(cw * ch);
678
700
  const zone = new Float32Array(cw * ch);
701
+ const outer = new Int32Array(cw * ch);
702
+ let edges = 0;
679
703
  let max = 1e-4;
680
704
  let maxX = 1e-4;
681
705
  let maxY = 1e-4;
@@ -703,6 +727,7 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
703
727
  inside[y * cw + x] = 1;
704
728
  continue;
705
729
  }
730
+ outer[edges++] = y * cw + x;
706
731
  let nx;
707
732
  let ny;
708
733
  if (len > 0) {
@@ -746,12 +771,14 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
746
771
  const canvas = document.createElement("canvas");
747
772
  canvas.width = cw;
748
773
  canvas.height = ch;
749
- const ctx = canvas.getContext("2d");
774
+ const ctx = canvas.getContext("2d", { willReadFrequently: true });
750
775
  const scale = box ? 2 * Math.max(maxX / W, maxY / H) : max * 2;
751
776
  const spanX = box ? scale * W / 2 : max;
752
777
  const spanY = box ? scale * H / 2 : max;
753
778
  const field = ctx.createImageData(cw, ch);
754
- for (let i = 0; i < cw * ch; i++) {
779
+ fillPixels(field, 128, 128, 128, 255);
780
+ for (let e = 0; e < edges; e++) {
781
+ const i = outer[e];
755
782
  const o = i * 4;
756
783
  field.data[o] = Math.round(127.5 + dx[i] / spanX * 127.5);
757
784
  field.data[o + 1] = Math.round(127.5 + dy[i] / spanY * 127.5);
@@ -759,9 +786,9 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
759
786
  field.data[o + 3] = 255;
760
787
  }
761
788
  ctx.putImageData(field, 0, 0);
762
- const map = canvas.toDataURL();
789
+ const map = encode(canvas);
763
790
  const reach = Math.max(W, H) / 2;
764
- let zoomMap = "";
791
+ let zoomMap = Promise.resolve("");
765
792
  if (magnify && !simple) {
766
793
  const ramp = ctx.createImageData(cw, ch);
767
794
  for (let y = 0; y < ch; y++) {
@@ -777,10 +804,12 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
777
804
  }
778
805
  }
779
806
  ctx.putImageData(ramp, 0, 0);
780
- zoomMap = canvas.toDataURL();
807
+ zoomMap = encode(canvas);
781
808
  }
782
809
  const shine = ctx.createImageData(cw, ch);
783
- for (let i = 0; i < cw * ch; i++) {
810
+ fillPixels(shine, 255, 255, 255, 0);
811
+ for (let e = 0; e < edges; e++) {
812
+ const i = outer[e];
784
813
  const o = i * 4;
785
814
  let r = 255;
786
815
  let g = 255;
@@ -798,26 +827,35 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
798
827
  shine.data[o + 3] = Math.round(rimA[i] * 255);
799
828
  }
800
829
  ctx.putImageData(shine, 0, 0);
801
- const rim = canvas.toDataURL();
830
+ const rim = encode(canvas);
802
831
  const shapeMask = ctx.createImageData(cw, ch);
803
- for (let i = 0; i < cw * ch; i++) {
832
+ fillPixels(shapeMask, 255, 255, 255, 255);
833
+ for (let e = 0; e < edges; e++) {
834
+ const i = outer[e];
804
835
  const o = i * 4;
805
836
  shapeMask.data[o] = shapeMask.data[o + 1] = shapeMask.data[o + 2] = 255;
806
837
  shapeMask.data[o + 3] = Math.round(inside[i] * 255);
807
838
  }
808
839
  ctx.putImageData(shapeMask, 0, 0);
809
- const mask = canvas.toDataURL();
810
- let smooth = "";
840
+ const mask = encode(canvas);
841
+ let smooth = Promise.resolve("");
811
842
  if (lens && !simple) {
812
843
  const soft = ctx.createImageData(cw, ch);
813
- for (let i = 0; i < cw * ch; i++) {
844
+ fillPixels(soft, 255, 255, 255, 0);
845
+ for (let e = 0; e < edges; e++) {
846
+ const i = outer[e];
814
847
  const o = i * 4;
815
848
  soft.data[o] = soft.data[o + 1] = soft.data[o + 2] = 255;
816
849
  soft.data[o + 3] = Math.round(zone[i] * 255);
817
850
  }
818
851
  ctx.putImageData(soft, 0, 0);
819
- smooth = canvas.toDataURL();
852
+ smooth = encode(canvas);
820
853
  }
854
+ return Promise.all([map, zoomMap, rim, mask, smooth]).then(
855
+ ([map2, zoomMap2, rim2, mask2, smooth2]) => assemble(W, H, box, magnify, reach, scale, shape, simple, fringe, map2, zoomMap2, rim2, mask2, smooth2)
856
+ );
857
+ }
858
+ function assemble(W, H, box, magnify, reach, scale, shape, simple, fringe, map, zoomMap, rim, mask, smooth) {
821
859
  const id = `lg-refract-${++counter}`;
822
860
  const area = box ? { x: 0, y: 0, width: 1, height: 1 } : { x: 0, y: 0, width: W, height: H };
823
861
  const filter = el(
@@ -964,9 +1002,24 @@ function glassFor(width, height, radius, shape = {}, pad = 0, simple = false, fr
964
1002
  var soon = [];
965
1003
  var later = [];
966
1004
  var pumping = false;
1005
+ var held = 0;
1006
+ function holdBakes() {
1007
+ held++;
1008
+ let released = false;
1009
+ return () => {
1010
+ if (released) return;
1011
+ released = true;
1012
+ held--;
1013
+ if (!held && !pumping && (soon.length || later.length)) pump();
1014
+ };
1015
+ }
967
1016
  function pump() {
968
1017
  pumping = true;
969
1018
  const run = (deadline) => {
1019
+ if (held) {
1020
+ pumping = false;
1021
+ return;
1022
+ }
970
1023
  const end = performance.now() + 8;
971
1024
  const left = () => deadline ? deadline.timeRemaining() > 4 : performance.now() < end;
972
1025
  let ran = 0;
@@ -1022,32 +1075,51 @@ function useRefraction(ref, shape = {}, enabled = true, mirror, mirrorActive) {
1022
1075
  const viewport = element.querySelector(":scope > .lg-mirror > .lg-mirror__viewport");
1023
1076
  if (viewport && copyFilter) viewport.style.filter = copyFilter;
1024
1077
  };
1078
+ let fits = 0;
1079
+ let gone = false;
1025
1080
  const fit = () => {
1026
1081
  const width = element.offsetWidth;
1027
1082
  const height = element.offsetHeight;
1028
1083
  if (!width || !height) return;
1084
+ const run = ++fits;
1029
1085
  const radius = cornerRadius(element, width, height);
1030
- const baked = glassFor(width, height, radius, shape);
1031
- filter = baked.filter;
1032
- if (!live && mirror?.current) {
1086
+ const mirrored = !live && !!mirror?.current;
1087
+ if (mirrored) {
1033
1088
  pad = Math.ceil(Math.min(width, height) * 0.45 + 6);
1034
1089
  ({ zoom, zoomX } = resolve(shape, width, height));
1035
1090
  if (tears) {
1036
1091
  zoom = Math.min(zoom, 1);
1037
1092
  zoomX = Math.min(zoomX, 1);
1038
1093
  }
1039
- element.style.setProperty("--lg-inner-mask", baked.inner);
1040
- if (bendCopy) {
1041
- copyFilter = glassFor(width, height, radius, { ...shape, zoom: 1, zoomX: 1 }, pad, false, true, !chromium).filter;
1042
- applyCopyFilter();
1043
- }
1044
- }
1045
- if (live || bendCopy && chromium && !shape.copyOnly) {
1046
- const backdrop = live ? baked.filter : glassFor(width, height, radius, { ...shape, zoom: 1, zoomX: 1, strength: (shape.strength ?? 1) * 0.45 }).filter;
1047
- element.style.setProperty("--lg-refraction", backdrop);
1048
- element.setAttribute("data-lg-refract", shape.kind ?? "surface");
1049
1094
  }
1050
- element.style.setProperty("--lg-rim", baked.rim);
1095
+ const refractBackdrop = live || bendCopy && chromium && !shape.copyOnly;
1096
+ const none = Promise.resolve(null);
1097
+ Promise.all([
1098
+ glassFor(width, height, radius, shape),
1099
+ // The copy is magnified by CSS; the filter only bends the rim and
1100
+ // fringes what it bends with colour.
1101
+ mirrored && bendCopy ? glassFor(width, height, radius, { ...shape, zoom: 1, zoomX: 1 }, pad, false, true, !chromium) : none,
1102
+ // Behind a bent copy the backdrop only carries the bar's glass and
1103
+ // edges: the copy already magnifies the content, so the backdrop isn't
1104
+ // zoomed (that pushed the bar's edge into a ring inside the lens) and
1105
+ // its rim bends gently, curling the bar's edge into thin arcs.
1106
+ refractBackdrop && !live ? glassFor(width, height, radius, { ...shape, zoom: 1, zoomX: 1, strength: (shape.strength ?? 1) * 0.45 }) : none
1107
+ ]).then(([baked, copy, behind]) => {
1108
+ if (run !== fits || gone) return;
1109
+ filter = baked.filter;
1110
+ if (mirrored) {
1111
+ element.style.setProperty("--lg-inner-mask", baked.inner);
1112
+ if (copy) {
1113
+ copyFilter = copy.filter;
1114
+ applyCopyFilter();
1115
+ }
1116
+ }
1117
+ if (refractBackdrop) {
1118
+ element.style.setProperty("--lg-refraction", live ? baked.filter : behind.filter);
1119
+ element.setAttribute("data-lg-refract", shape.kind ?? "surface");
1120
+ }
1121
+ element.style.setProperty("--lg-rim", baked.rim);
1122
+ });
1051
1123
  };
1052
1124
  let seen = typeof IntersectionObserver !== "function";
1053
1125
  const request = () => {
@@ -1076,6 +1148,7 @@ function useRefraction(ref, shape = {}, enabled = true, mirror, mirrorActive) {
1076
1148
  applyCopyFilter();
1077
1149
  const ungovern = governRefraction(element);
1078
1150
  return () => {
1151
+ gone = true;
1079
1152
  cancel?.();
1080
1153
  unmirror?.();
1081
1154
  ungovern();
@@ -1354,11 +1427,20 @@ var GlassSheet = ({ open, onClose, intensity = 5, tint, container: container2, d
1354
1427
  if (wanted.length && sheet.hasAttribute("data-raised") !== raised) sheet.toggleAttribute("data-raised", raised);
1355
1428
  };
1356
1429
  let closing = false;
1357
- const position = createSpring(hidden, OPEN, (y) => {
1430
+ let release = null;
1431
+ const landed = () => {
1432
+ release?.();
1433
+ release = null;
1434
+ sheet.removeAttribute("data-lg-moving");
1435
+ };
1436
+ const position = createSpring(hidden, OPEN, (y, velocity) => {
1358
1437
  paint(y);
1359
1438
  if (closing && y >= hidden - 1) setMounted(false);
1439
+ if (release && Math.abs(y - position.target) < 1 && Math.abs(velocity) < 20) landed();
1360
1440
  });
1361
1441
  const go = (y, feel) => {
1442
+ release ?? (release = holdBakes());
1443
+ sheet.setAttribute("data-lg-moving", "");
1362
1444
  position.tune(feel);
1363
1445
  position.set(y);
1364
1446
  };
@@ -1382,6 +1464,8 @@ var GlassSheet = ({ open, onClose, intensity = 5, tint, container: container2, d
1382
1464
  if (raised && !target.closest(".lg-sheet__grabber-zone") && sheet.scrollTop > 0) return;
1383
1465
  if (target.closest('input, textarea, button, a, [role="slider"], [role="switch"]')) return;
1384
1466
  position.stop();
1467
+ landed();
1468
+ sheet.setAttribute("data-lg-moving", "");
1385
1469
  gesture = { id: event.pointerId, y: event.clientY, from: position.value, t: event.timeStamp, v: 0, last: position.value };
1386
1470
  sheet.setPointerCapture(event.pointerId);
1387
1471
  };
@@ -1432,6 +1516,7 @@ var GlassSheet = ({ open, onClose, intensity = 5, tint, container: container2, d
1432
1516
  sheet.focus({ preventScroll: true });
1433
1517
  return () => {
1434
1518
  position.stop();
1519
+ landed();
1435
1520
  api.current = null;
1436
1521
  observer.disconnect();
1437
1522
  sheet.removeEventListener("pointerdown", down);
@@ -1594,7 +1679,7 @@ var GlassMenu = ({
1594
1679
  useEffect4(() => {
1595
1680
  if (!mounted) return;
1596
1681
  const panel = surfaceRef.current;
1597
- let held = null;
1682
+ let held2 = null;
1598
1683
  let hot = null;
1599
1684
  const itemAt = (x, y) => document.elementFromPoint(x, y)?.closest(".lg-menu__item:not(:disabled)") ?? null;
1600
1685
  const light = (next) => {
@@ -1608,16 +1693,16 @@ var GlassMenu = ({
1608
1693
  };
1609
1694
  const down = (event) => {
1610
1695
  if (!event.isPrimary || event.button !== 0) return;
1611
- held = event.pointerId;
1696
+ held2 = event.pointerId;
1612
1697
  hot = null;
1613
1698
  light(itemAt(event.clientX, event.clientY));
1614
1699
  };
1615
1700
  const move = (event) => {
1616
- if (event.pointerId === held) light(itemAt(event.clientX, event.clientY));
1701
+ if (event.pointerId === held2) light(itemAt(event.clientX, event.clientY));
1617
1702
  };
1618
1703
  const up = (event) => {
1619
- if (event.pointerId !== held) return;
1620
- held = null;
1704
+ if (event.pointerId !== held2) return;
1705
+ held2 = null;
1621
1706
  const target = hot;
1622
1707
  light(null);
1623
1708
  const index = target ? Number(target.dataset.index) : -1;
@@ -1630,12 +1715,12 @@ var GlassMenu = ({
1630
1715
  }
1631
1716
  };
1632
1717
  const cancel = (event) => {
1633
- if (event.pointerId !== held) return;
1634
- held = null;
1718
+ if (event.pointerId !== held2) return;
1719
+ held2 = null;
1635
1720
  light(null);
1636
1721
  };
1637
1722
  const hold = (event) => {
1638
- if (held !== null && event.cancelable) event.preventDefault();
1723
+ if (held2 !== null && event.cancelable) event.preventDefault();
1639
1724
  };
1640
1725
  panel.addEventListener("pointerdown", down);
1641
1726
  window.addEventListener("pointermove", move);
@@ -1771,9 +1856,10 @@ function useLensTrack(refs, options) {
1771
1856
  select.style.width = `${slot}px`;
1772
1857
  select.style.left = `${pad}px`;
1773
1858
  glyphs = buttons.flatMap(
1774
- (button) => Array.from(button.querySelectorAll(".lg-nav__label, .lg-nav__icon > .lg-symbol")).map((node) => {
1775
- const label2 = node.classList.contains("lg-nav__label");
1776
- const host = label2 ? node : node.parentElement;
1859
+ (button) => Array.from(button.querySelectorAll(".lg-nav__label, .lg-nav__icon")).map((box) => {
1860
+ const label2 = box.classList.contains("lg-nav__label");
1861
+ const node = label2 ? box : box.querySelector(":scope > .lg-symbol") ?? box;
1862
+ const host = box;
1777
1863
  const n = node.getBoundingClientRect();
1778
1864
  const h = host.getBoundingClientRect();
1779
1865
  return {
@@ -2192,7 +2278,10 @@ var GlassNav = ({
2192
2278
  onChange,
2193
2279
  labels: items.map((item) => item.label),
2194
2280
  items: items.map((item, i) => /* @__PURE__ */ jsxs6(React7.Fragment, { children: [
2195
- item.icon != null && /* @__PURE__ */ jsx8("span", { className: "lg-nav__icon", children: item.icon }),
2281
+ item.icon != null && (item.activeIcon != null ? /* @__PURE__ */ jsxs6("span", { className: "lg-nav__icons", children: [
2282
+ /* @__PURE__ */ jsx8("span", { className: "lg-nav__icon lg-nav__icon--idle", children: item.icon }),
2283
+ /* @__PURE__ */ jsx8("span", { className: "lg-nav__icon lg-nav__icon--active", children: item.activeIcon })
2284
+ ] }) : /* @__PURE__ */ jsx8("span", { className: "lg-nav__icon", children: item.icon })),
2196
2285
  !iconsOnly && /* @__PURE__ */ jsx8("span", { className: "lg-nav__label", children: item.label })
2197
2286
  ] }, i))
2198
2287
  }
@@ -2554,6 +2643,7 @@ GlassSlider.displayName = "GlassSlider";
2554
2643
  import React10, { useEffect as useEffect7, useImperativeHandle as useImperativeHandle5, useRef as useRef9, useState as useState4 } from "react";
2555
2644
  import { jsx as jsx11, jsxs as jsxs9 } from "react/jsx-runtime";
2556
2645
  var SETTLE = springFrom(0.36, 0.68);
2646
+ var TINT = springFrom(0.5, 1);
2557
2647
  var REST = 0.655;
2558
2648
  var TRAVEL = 22;
2559
2649
  var GlassSwitch = React10.forwardRef(
@@ -2614,7 +2704,7 @@ var GlassSwitch = React10.forwardRef(
2614
2704
  v.glass = lift.target ? value : Math.min(v.glass, value);
2615
2705
  paint();
2616
2706
  });
2617
- const color = createSpring(checked ? 1 : 0, SPRINGS.fade, (value) => {
2707
+ const color = createSpring(checked ? 1 : 0, TINT, (value) => {
2618
2708
  fill.style.opacity = String(clamp(value, 0, 1));
2619
2709
  });
2620
2710
  fill.style.opacity = checked ? "1" : "0";