@zakisheriff/liquid-glass 0.3.6 → 0.3.8

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
@@ -43,7 +43,7 @@ function loop(time) {
43
43
  frame = 0;
44
44
  const dt = Math.min(0.064, Math.max(0, (time - last) / 1e3));
45
45
  last = time;
46
- for (const s of active) {
46
+ for (const s of [...active]) {
47
47
  step(s, dt);
48
48
  if (Math.abs(s.value - s.target) < 1e-4 && Math.abs(s.velocity) < 1e-3) {
49
49
  s.value = s.target;
@@ -52,7 +52,7 @@ function loop(time) {
52
52
  }
53
53
  s.onUpdate(s.value, s.velocity);
54
54
  }
55
- if (active.size) frame = requestAnimationFrame(loop);
55
+ if (active.size && !frame) frame = requestAnimationFrame(loop);
56
56
  }
57
57
  function wake() {
58
58
  if (frame) return;
@@ -125,8 +125,21 @@ var SPRINGS = {
125
125
  /** Lens deflating back into a pill: visibly wobbly. */
126
126
  settle: springFrom(0.45, 0.46),
127
127
  /** Colour cross-fades. */
128
- fade: springFrom(0.3, 1)
128
+ fade: springFrom(0.3, 1),
129
+ /** A dragged lens's stretch: loose, so it wobbles when the drag stops. */
130
+ jelly: springFrom(0.3, 0.3)
129
131
  };
132
+ function createJelly(onUpdate, max, speed) {
133
+ const spring = createSpring(0, SPRINGS.jelly, (value) => onUpdate(value));
134
+ return {
135
+ feed(velocity) {
136
+ spring.set(max * Math.tanh(Math.abs(velocity) / speed));
137
+ },
138
+ stop() {
139
+ spring.stop();
140
+ }
141
+ };
142
+ }
130
143
  function rubberband(offset, dimension, constant = 0.55) {
131
144
  const sign = Math.sign(offset);
132
145
  const x = Math.abs(offset);
@@ -447,14 +460,16 @@ function mirrorBackdrop(glass, source, padding, zoom, active2 = () => true) {
447
460
  shown = on;
448
461
  layer.style.visibility = on ? "" : "hidden";
449
462
  }
450
- if (on) {
451
- applyZoom();
452
- sync();
453
- align();
454
- }
463
+ if (!on) return;
464
+ applyZoom();
465
+ sync();
466
+ align();
455
467
  frame2 = requestAnimationFrame(tick);
456
468
  };
457
- frame2 = requestAnimationFrame(tick);
469
+ const wake2 = () => {
470
+ if (!frame2 && !stopped) frame2 = requestAnimationFrame(tick);
471
+ };
472
+ wake2();
458
473
  let pending = 0;
459
474
  const recopy = () => {
460
475
  pending = 0;
@@ -469,6 +484,7 @@ function mirrorBackdrop(glass, source, padding, zoom, active2 = () => true) {
469
484
  };
470
485
  const mutations = new MutationObserver((records) => {
471
486
  if (records.every((r) => layer.contains(r.target) || r.target === layer || r.target === glass)) return;
487
+ wake2();
472
488
  const structural = records.some((r) => r.type !== "attributes" && !layer.contains(r.target));
473
489
  if (structural && !pending) pending = requestAnimationFrame(recopy);
474
490
  else if (!structural && !stopped && visible && !document.hidden && active2()) {
@@ -480,14 +496,17 @@ function mirrorBackdrop(glass, source, padding, zoom, active2 = () => true) {
480
496
  mutations.observe(source, { childList: true, characterData: true, attributes: true, attributeFilter: ["style", "class"], subtree: true });
481
497
  const io = typeof IntersectionObserver === "function" ? new IntersectionObserver((entries) => {
482
498
  visible = entries.some((e) => e.isIntersecting);
499
+ wake2();
483
500
  }) : null;
484
501
  io?.observe(glass);
502
+ document.addEventListener("visibilitychange", wake2);
485
503
  return () => {
486
504
  stopped = true;
487
505
  cancelAnimationFrame(frame2);
488
506
  cancelAnimationFrame(pending);
489
507
  mutations.disconnect();
490
508
  io?.disconnect();
509
+ document.removeEventListener("visibilitychange", wake2);
491
510
  layer.remove();
492
511
  glass.removeAttribute(SELF);
493
512
  };
@@ -539,7 +558,7 @@ function bevelProfile(bezel, thickness, ior) {
539
558
  return a + (b - a) * (f - i);
540
559
  };
541
560
  }
542
- function bake(width, height, radius, shape, pad = 0, simple = false) {
561
+ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = false) {
543
562
  const W = width + pad * 2;
544
563
  const H = height + pad * 2;
545
564
  let density = Math.min(typeof devicePixelRatio === "number" ? devicePixelRatio : 1, 2);
@@ -713,7 +732,31 @@ function bake(width, height, radius, shape, pad = 0, simple = false) {
713
732
  source = "magnified";
714
733
  }
715
734
  const scale = max * 2;
716
- if (shape.dispersion > 0 && !simple) {
735
+ if (shape.dispersion > 0 && !simple && fringe) {
736
+ const spread = 0.3 * shape.dispersion;
737
+ for (const [name, factor] of [["r", 1 + spread], ["g", 1], ["b", 1 - spread]]) {
738
+ filter.append(
739
+ el("feDisplacementMap", {
740
+ in: source,
741
+ in2: "field",
742
+ scale: scale * factor,
743
+ xChannelSelector: "R",
744
+ yChannelSelector: "G",
745
+ result: `${name}-ray`
746
+ })
747
+ );
748
+ }
749
+ filter.append(
750
+ el("feComposite", { in: "r-ray", in2: "g-ray", operator: "out", result: "r-edge" }),
751
+ el("feColorMatrix", { in: "r-edge", type: "matrix", values: "0.2 0 0 0 0.62 0 0.2 0 0 0.16 0 0 0.2 0 0.8 0 0 0 1 0", result: "warm" }),
752
+ el("feComposite", { in: "b-ray", in2: "g-ray", operator: "out", result: "b-edge" }),
753
+ el("feColorMatrix", { in: "b-edge", type: "matrix", values: "0.2 0 0 0 0.1 0 0.2 0 0 0.78 0 0 0.2 0 0.62 0 0 0 1 0", result: "cool" }),
754
+ el("feMerge", {})
755
+ );
756
+ const merge = filter.lastElementChild;
757
+ for (const name of ["warm", "cool", "g-ray"]) merge.append(el("feMergeNode", { in: name }));
758
+ merge.setAttribute("result", "bent");
759
+ } else if (shape.dispersion > 0 && !simple) {
717
760
  const spread = 0.16 * shape.dispersion;
718
761
  const channels = [
719
762
  ["r", 1 + spread, "1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0"],
@@ -784,15 +827,15 @@ function resolve(shape, width, height) {
784
827
  copy: shape.copy ?? false
785
828
  };
786
829
  }
787
- function glassFor(width, height, radius, shape = {}, pad = 0, simple = false) {
830
+ function glassFor(width, height, radius, shape = {}, pad = 0, simple = false, fringe = false) {
788
831
  const w = Math.round(width);
789
832
  const h = Math.round(height);
790
833
  const r = Math.min(w / 2, h / 2, Math.max(0, radius));
791
834
  const full = resolve(shape, w, h);
792
- const key = [w, h, Math.round(r * 2) / 2, full.kind, full.bezel.toFixed(1), full.thickness.toFixed(1), full.ior, full.zoom, full.dispersion, full.strength, full.inset, pad, simple].join("|");
835
+ const key = [w, h, Math.round(r * 2) / 2, full.kind, full.bezel.toFixed(1), full.thickness.toFixed(1), full.ior, full.zoom, full.dispersion, full.strength, full.inset, pad, simple, fringe].join("|");
793
836
  let baked = cache.get(key);
794
837
  if (!baked) {
795
- baked = bake(w, h, r, full, pad, simple);
838
+ baked = bake(w, h, r, full, pad, simple, fringe);
796
839
  cache.set(key, baked);
797
840
  }
798
841
  return baked;
@@ -840,12 +883,13 @@ function useRefraction(ref, shape = {}, enabled = true, mirror, mirrorActive) {
840
883
  ({ zoom, zoomX } = resolve(shape, width, height));
841
884
  element.style.setProperty("--lg-inner-mask", baked.inner);
842
885
  if (bendCopy) {
843
- copyFilter = glassFor(width, height, radius, { ...shape, zoom: 1, zoomX: 1, dispersion: 0 }, pad).filter;
886
+ copyFilter = glassFor(width, height, radius, { ...shape, zoom: 1, zoomX: 1 }, pad, false, true).filter;
844
887
  applyCopyFilter();
845
888
  }
846
889
  }
847
- if (live) {
848
- element.style.setProperty("--lg-filter", baked.filter);
890
+ if (live || bendCopy) {
891
+ const backdrop = live ? baked.filter : glassFor(width, height, radius, { ...shape, zoom: 1, zoomX: 1, strength: (shape.strength ?? 1) * 0.45 }).filter;
892
+ element.style.setProperty("--lg-filter", backdrop);
849
893
  element.setAttribute("data-lg-refract", "");
850
894
  }
851
895
  element.style.setProperty("--lg-rim", baked.rim);
@@ -1626,9 +1670,13 @@ function useLensTrack(refs, options) {
1626
1670
  }
1627
1671
  if (!gesture && lift2 < 0.02) delete bar.dataset.lifted;
1628
1672
  };
1673
+ const jelly = createJelly((value) => {
1674
+ state.squash = value;
1675
+ paint();
1676
+ }, 0.13, 1600);
1629
1677
  const x = createSpring(current * slot, SPRINGS.snap, (value, velocity) => {
1630
1678
  state.x = value;
1631
- state.squash = clamp(Math.abs(velocity) / 6e3, 0, 0.08);
1679
+ jelly.feed(velocity);
1632
1680
  paint();
1633
1681
  });
1634
1682
  const lift = createSpring(0, SPRINGS.lift, (value) => {
@@ -1803,7 +1851,7 @@ function useLensTrack(refs, options) {
1803
1851
  return () => {
1804
1852
  clearTimeout(dropTimer);
1805
1853
  api.current = null;
1806
- [x, lift, swell].forEach((s) => s.stop());
1854
+ [x, lift, swell, jelly].forEach((s) => s.stop());
1807
1855
  unguard();
1808
1856
  observer.disconnect();
1809
1857
  bar.removeEventListener("pointerdown", onDown);
@@ -1853,7 +1901,7 @@ function LensTrack({
1853
1901
  const overhang = tabbar ? 14 : 12;
1854
1902
  useRefraction(
1855
1903
  lens,
1856
- { kind: "lens", strength: 0.85, zoom, dispersion: 0.25, inset: overhang / 2, copy: "refract" },
1904
+ { kind: "lens", strength: 1.15, zoom, dispersion: 1, copy: "refract" },
1857
1905
  true,
1858
1906
  bar,
1859
1907
  () => bar.current?.hasAttribute("data-lifted") ?? false
@@ -2316,7 +2364,7 @@ var GlassSwitch = React10.forwardRef(
2316
2364
  const sync = useRef9(null);
2317
2365
  useRefraction(
2318
2366
  thumbRef,
2319
- { kind: "lens", zoom: 0.72, zoomX: 1, dispersion: 0.5, copy: true, strength: Math.max(1, Math.min(10, intensity)) / 5 },
2367
+ { kind: "lens", zoom: 0.72, zoomX: 1, dispersion: 1, copy: "refract", strength: Math.max(1, Math.min(10, intensity)) / 5 },
2320
2368
  true,
2321
2369
  ref,
2322
2370
  () => parseFloat(ref.current?.style.getPropertyValue("--lg-lift") || "0") > 0.01
@@ -2337,9 +2385,13 @@ var GlassSwitch = React10.forwardRef(
2337
2385
  el2.style.setProperty("--lg-lift", String(l));
2338
2386
  el2.toggleAttribute("data-lifted", l > 0.01);
2339
2387
  };
2388
+ const jelly = createJelly((value) => {
2389
+ v.stretch = value;
2390
+ paint();
2391
+ }, 0.14, 5);
2340
2392
  const position = createSpring(v.x, SPRINGS.snap, (value, velocity) => {
2341
2393
  v.x = value;
2342
- v.stretch = clamp(Math.abs(velocity) / 140, 0, 0.05);
2394
+ jelly.feed(velocity);
2343
2395
  paint();
2344
2396
  });
2345
2397
  const lift = createSpring(0, SPRINGS.lift, (value) => {
@@ -2356,6 +2408,7 @@ var GlassSwitch = React10.forwardRef(
2356
2408
  position.set(value ? 1 : 0);
2357
2409
  color.set(value ? 1 : 0);
2358
2410
  };
2411
+ const flips = (from, raw) => from ? raw > 0.3 : raw > 0.7;
2359
2412
  let gesture = null;
2360
2413
  let dropTimer = 0;
2361
2414
  let ignoreClick = false;
@@ -2388,7 +2441,7 @@ var GlassSwitch = React10.forwardRef(
2388
2441
  const over = raw < 0 ? rubberband(raw * TRAVEL, TRAVEL, 0.6) / TRAVEL : raw > 1 ? 1 + rubberband((raw - 1) * TRAVEL, TRAVEL, 0.6) / TRAVEL : raw;
2389
2442
  position.tune(SPRINGS.drag);
2390
2443
  position.set(over);
2391
- const on = raw > 0.5;
2444
+ const on = flips(gesture.from, raw);
2392
2445
  if (color.target === 1 !== on) haptic();
2393
2446
  color.set(on ? 1 : 0);
2394
2447
  };
@@ -2397,7 +2450,7 @@ var GlassSwitch = React10.forwardRef(
2397
2450
  const { from, x, moved, at } = gesture;
2398
2451
  gesture = null;
2399
2452
  ignoreClick = true;
2400
- const next = moved ? from + (event.clientX - x) / TRAVEL > 0.5 : !latest.current.checked;
2453
+ const next = moved ? flips(from, from + (event.clientX - x) / TRAVEL) : !latest.current.checked;
2401
2454
  position.tune(SPRINGS.snap);
2402
2455
  position.set(next ? 1 : 0);
2403
2456
  color.set(next ? 1 : 0);
@@ -2431,7 +2484,7 @@ var GlassSwitch = React10.forwardRef(
2431
2484
  el2.addEventListener("click", click);
2432
2485
  return () => {
2433
2486
  clearTimeout(dropTimer);
2434
- [position, lift, color].forEach((s) => s.stop());
2487
+ [position, lift, color, jelly].forEach((s) => s.stop());
2435
2488
  unguard();
2436
2489
  el2.removeEventListener("pointerdown", down);
2437
2490
  el2.removeEventListener("pointermove", move);