@zakisheriff/liquid-glass 4.0.1 → 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);
@@ -670,6 +670,18 @@ function setGlassQuality(quality) {
670
670
  // src/engine/refraction.ts
671
671
  var SVG_NS = "http://www.w3.org/2000/svg";
672
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
+ }
673
685
  var defs = null;
674
686
  var counter = 0;
675
687
  function el(name, attrs) {
@@ -719,6 +731,10 @@ function bevelProfile(bezel, thickness, ior) {
719
731
  return a + (b - a) * (f - i);
720
732
  };
721
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
+ }
722
738
  function bake(width, height, radius, shape, pad = 0, simple = false, fringe = false, box = false) {
723
739
  const W = width + pad * 2;
724
740
  const H = height + pad * 2;
@@ -736,6 +752,8 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
736
752
  const rimHue = new Float32Array(cw * ch);
737
753
  const inside = new Float32Array(cw * ch);
738
754
  const zone = new Float32Array(cw * ch);
755
+ const outer = new Int32Array(cw * ch);
756
+ let edges = 0;
739
757
  let max = 1e-4;
740
758
  let maxX = 1e-4;
741
759
  let maxY = 1e-4;
@@ -763,6 +781,7 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
763
781
  inside[y * cw + x] = 1;
764
782
  continue;
765
783
  }
784
+ outer[edges++] = y * cw + x;
766
785
  let nx;
767
786
  let ny;
768
787
  if (len > 0) {
@@ -806,12 +825,14 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
806
825
  const canvas = document.createElement("canvas");
807
826
  canvas.width = cw;
808
827
  canvas.height = ch;
809
- const ctx = canvas.getContext("2d");
828
+ const ctx = canvas.getContext("2d", { willReadFrequently: true });
810
829
  const scale = box ? 2 * Math.max(maxX / W, maxY / H) : max * 2;
811
830
  const spanX = box ? scale * W / 2 : max;
812
831
  const spanY = box ? scale * H / 2 : max;
813
832
  const field = ctx.createImageData(cw, ch);
814
- 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];
815
836
  const o = i * 4;
816
837
  field.data[o] = Math.round(127.5 + dx[i] / spanX * 127.5);
817
838
  field.data[o + 1] = Math.round(127.5 + dy[i] / spanY * 127.5);
@@ -819,9 +840,9 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
819
840
  field.data[o + 3] = 255;
820
841
  }
821
842
  ctx.putImageData(field, 0, 0);
822
- const map = canvas.toDataURL();
843
+ const map = encode(canvas);
823
844
  const reach = Math.max(W, H) / 2;
824
- let zoomMap = "";
845
+ let zoomMap = Promise.resolve("");
825
846
  if (magnify && !simple) {
826
847
  const ramp = ctx.createImageData(cw, ch);
827
848
  for (let y = 0; y < ch; y++) {
@@ -837,10 +858,12 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
837
858
  }
838
859
  }
839
860
  ctx.putImageData(ramp, 0, 0);
840
- zoomMap = canvas.toDataURL();
861
+ zoomMap = encode(canvas);
841
862
  }
842
863
  const shine = ctx.createImageData(cw, ch);
843
- 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];
844
867
  const o = i * 4;
845
868
  let r = 255;
846
869
  let g = 255;
@@ -858,26 +881,35 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
858
881
  shine.data[o + 3] = Math.round(rimA[i] * 255);
859
882
  }
860
883
  ctx.putImageData(shine, 0, 0);
861
- const rim = canvas.toDataURL();
884
+ const rim = encode(canvas);
862
885
  const shapeMask = ctx.createImageData(cw, ch);
863
- 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];
864
889
  const o = i * 4;
865
890
  shapeMask.data[o] = shapeMask.data[o + 1] = shapeMask.data[o + 2] = 255;
866
891
  shapeMask.data[o + 3] = Math.round(inside[i] * 255);
867
892
  }
868
893
  ctx.putImageData(shapeMask, 0, 0);
869
- const mask = canvas.toDataURL();
870
- let smooth = "";
894
+ const mask = encode(canvas);
895
+ let smooth = Promise.resolve("");
871
896
  if (lens && !simple) {
872
897
  const soft = ctx.createImageData(cw, ch);
873
- 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];
874
901
  const o = i * 4;
875
902
  soft.data[o] = soft.data[o + 1] = soft.data[o + 2] = 255;
876
903
  soft.data[o + 3] = Math.round(zone[i] * 255);
877
904
  }
878
905
  ctx.putImageData(soft, 0, 0);
879
- smooth = canvas.toDataURL();
906
+ smooth = encode(canvas);
880
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) {
881
913
  const id = `lg-refract-${++counter}`;
882
914
  const area = box ? { x: 0, y: 0, width: 1, height: 1 } : { x: 0, y: 0, width: W, height: H };
883
915
  const filter = el(
@@ -1024,9 +1056,24 @@ function glassFor(width, height, radius, shape = {}, pad = 0, simple = false, fr
1024
1056
  var soon = [];
1025
1057
  var later = [];
1026
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
+ }
1027
1070
  function pump() {
1028
1071
  pumping = true;
1029
1072
  const run = (deadline) => {
1073
+ if (held) {
1074
+ pumping = false;
1075
+ return;
1076
+ }
1030
1077
  const end = performance.now() + 8;
1031
1078
  const left = () => deadline ? deadline.timeRemaining() > 4 : performance.now() < end;
1032
1079
  let ran = 0;
@@ -1082,32 +1129,51 @@ function useRefraction(ref, shape = {}, enabled = true, mirror, mirrorActive) {
1082
1129
  const viewport = element.querySelector(":scope > .lg-mirror > .lg-mirror__viewport");
1083
1130
  if (viewport && copyFilter) viewport.style.filter = copyFilter;
1084
1131
  };
1132
+ let fits = 0;
1133
+ let gone = false;
1085
1134
  const fit = () => {
1086
1135
  const width = element.offsetWidth;
1087
1136
  const height = element.offsetHeight;
1088
1137
  if (!width || !height) return;
1138
+ const run = ++fits;
1089
1139
  const radius = cornerRadius(element, width, height);
1090
- const baked = glassFor(width, height, radius, shape);
1091
- filter = baked.filter;
1092
- if (!live && mirror?.current) {
1140
+ const mirrored = !live && !!mirror?.current;
1141
+ if (mirrored) {
1093
1142
  pad = Math.ceil(Math.min(width, height) * 0.45 + 6);
1094
1143
  ({ zoom, zoomX } = resolve(shape, width, height));
1095
1144
  if (tears) {
1096
1145
  zoom = Math.min(zoom, 1);
1097
1146
  zoomX = Math.min(zoomX, 1);
1098
1147
  }
1099
- element.style.setProperty("--lg-inner-mask", baked.inner);
1100
- if (bendCopy) {
1101
- copyFilter = glassFor(width, height, radius, { ...shape, zoom: 1, zoomX: 1 }, pad, false, true, !chromium).filter;
1102
- applyCopyFilter();
1103
- }
1104
- }
1105
- if (live || bendCopy && chromium && !shape.copyOnly) {
1106
- const backdrop = live ? baked.filter : glassFor(width, height, radius, { ...shape, zoom: 1, zoomX: 1, strength: (shape.strength ?? 1) * 0.45 }).filter;
1107
- element.style.setProperty("--lg-refraction", backdrop);
1108
- element.setAttribute("data-lg-refract", shape.kind ?? "surface");
1109
1148
  }
1110
- 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
+ });
1111
1177
  };
1112
1178
  let seen = typeof IntersectionObserver !== "function";
1113
1179
  const request = () => {
@@ -1136,6 +1202,7 @@ function useRefraction(ref, shape = {}, enabled = true, mirror, mirrorActive) {
1136
1202
  applyCopyFilter();
1137
1203
  const ungovern = governRefraction(element);
1138
1204
  return () => {
1205
+ gone = true;
1139
1206
  cancel?.();
1140
1207
  unmirror?.();
1141
1208
  ungovern();
@@ -1414,11 +1481,20 @@ var GlassSheet = ({ open, onClose, intensity = 5, tint, container: container2, d
1414
1481
  if (wanted.length && sheet.hasAttribute("data-raised") !== raised) sheet.toggleAttribute("data-raised", raised);
1415
1482
  };
1416
1483
  let closing = false;
1417
- 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) => {
1418
1491
  paint(y);
1419
1492
  if (closing && y >= hidden - 1) setMounted(false);
1493
+ if (release && Math.abs(y - position.target) < 1 && Math.abs(velocity) < 20) landed();
1420
1494
  });
1421
1495
  const go = (y, feel) => {
1496
+ release ?? (release = holdBakes());
1497
+ sheet.setAttribute("data-lg-moving", "");
1422
1498
  position.tune(feel);
1423
1499
  position.set(y);
1424
1500
  };
@@ -1442,6 +1518,8 @@ var GlassSheet = ({ open, onClose, intensity = 5, tint, container: container2, d
1442
1518
  if (raised && !target.closest(".lg-sheet__grabber-zone") && sheet.scrollTop > 0) return;
1443
1519
  if (target.closest('input, textarea, button, a, [role="slider"], [role="switch"]')) return;
1444
1520
  position.stop();
1521
+ landed();
1522
+ sheet.setAttribute("data-lg-moving", "");
1445
1523
  gesture = { id: event.pointerId, y: event.clientY, from: position.value, t: event.timeStamp, v: 0, last: position.value };
1446
1524
  sheet.setPointerCapture(event.pointerId);
1447
1525
  };
@@ -1492,6 +1570,7 @@ var GlassSheet = ({ open, onClose, intensity = 5, tint, container: container2, d
1492
1570
  sheet.focus({ preventScroll: true });
1493
1571
  return () => {
1494
1572
  position.stop();
1573
+ landed();
1495
1574
  api.current = null;
1496
1575
  observer.disconnect();
1497
1576
  sheet.removeEventListener("pointerdown", down);
@@ -1654,7 +1733,7 @@ var GlassMenu = ({
1654
1733
  (0, import_react7.useEffect)(() => {
1655
1734
  if (!mounted) return;
1656
1735
  const panel = surfaceRef.current;
1657
- let held = null;
1736
+ let held2 = null;
1658
1737
  let hot = null;
1659
1738
  const itemAt = (x, y) => document.elementFromPoint(x, y)?.closest(".lg-menu__item:not(:disabled)") ?? null;
1660
1739
  const light = (next) => {
@@ -1668,16 +1747,16 @@ var GlassMenu = ({
1668
1747
  };
1669
1748
  const down = (event) => {
1670
1749
  if (!event.isPrimary || event.button !== 0) return;
1671
- held = event.pointerId;
1750
+ held2 = event.pointerId;
1672
1751
  hot = null;
1673
1752
  light(itemAt(event.clientX, event.clientY));
1674
1753
  };
1675
1754
  const move = (event) => {
1676
- if (event.pointerId === held) light(itemAt(event.clientX, event.clientY));
1755
+ if (event.pointerId === held2) light(itemAt(event.clientX, event.clientY));
1677
1756
  };
1678
1757
  const up = (event) => {
1679
- if (event.pointerId !== held) return;
1680
- held = null;
1758
+ if (event.pointerId !== held2) return;
1759
+ held2 = null;
1681
1760
  const target = hot;
1682
1761
  light(null);
1683
1762
  const index = target ? Number(target.dataset.index) : -1;
@@ -1690,12 +1769,12 @@ var GlassMenu = ({
1690
1769
  }
1691
1770
  };
1692
1771
  const cancel = (event) => {
1693
- if (event.pointerId !== held) return;
1694
- held = null;
1772
+ if (event.pointerId !== held2) return;
1773
+ held2 = null;
1695
1774
  light(null);
1696
1775
  };
1697
1776
  const hold = (event) => {
1698
- if (held !== null && event.cancelable) event.preventDefault();
1777
+ if (held2 !== null && event.cancelable) event.preventDefault();
1699
1778
  };
1700
1779
  panel.addEventListener("pointerdown", down);
1701
1780
  window.addEventListener("pointermove", move);