@zakisheriff/liquid-glass 0.3.12 → 0.3.14

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/README.md CHANGED
@@ -26,7 +26,7 @@
26
26
  <br />
27
27
  <br />
28
28
 
29
- <a href="https://liquidglass.theatom.lk/docs#demo"><img src="https://raw.githubusercontent.com/zakisheriff/Liquid-Glass/main/.github/demo-cover.webp" alt="Liquid Glass playground: buttons, sliders, switches, tab bar, menu and sheet. Click to watch the demo video." width="100%" /></a>
29
+ <a href="https://liquidglass.theatom.lk/docs#demo"><img src="https://raw.githubusercontent.com/zakisheriff/Liquid-Glass/main/.github/demo-cover-2.webp" alt="Liquid Glass playground: buttons, sliders, switches, tab bar, menu and sheet. Click to watch the demo video." width="100%" /></a>
30
30
 
31
31
  **[▶ Click to watch the demo video](https://liquidglass.theatom.lk/docs#demo)**
32
32
 
@@ -533,6 +533,8 @@ About **31 KB of JavaScript and 5.5 KB of CSS gzipped**, with **no runtime depen
533
533
  - Refraction maps are computed once per glass size, cached and shared, then drawn by the browser. Nothing is recalculated while you scroll.
534
534
  - Springs run on one shared animation frame loop and stop when they settle.
535
535
  - Lens copies only copy and lay out content while a control is pressed, and stop entirely off screen.
536
+ - Refraction pauses while the page scrolls and for glass off screen; blur and the rim carry the look in motion.
537
+ - On slower devices resting surfaces keep blur and rim only, and lenses still bend while held. The quality is picked from the device and the first scrolls; `setGlassQuality('high' | 'low' | 'auto')` pins it.
536
538
  - The demo page, with 99 glass elements, holds about 11 MB of JavaScript memory and renders at 60 fps in Chrome.
537
539
 
538
540
  The GPU cost is in what moves *under* the glass. Keep large refracting panels over static or slow content, prefer a few large glass surfaces over many small overlapping ones, and lower `intensity` (or use `GlassCard variant="clear"`) for low-end devices.
@@ -554,12 +556,12 @@ function Droplet() {
554
556
  }
555
557
  ```
556
558
 
557
- `createSpring`, `SPRINGS` and `haptic()` are exported too.
559
+ Lenses take `stretch: 1` for the iOS rim, where light under the edge smears out to it. `createSpring`, `SPRINGS`, `haptic()` and `setGlassQuality()` are exported too.
558
560
 
559
561
  ## Browser support
560
562
 
561
563
  - **Chrome / Edge / Arc:** live refraction of anything behind the glass through `backdrop-filter: url(#svg)`. Tab and segmented lenses refract a tinted live copy of the bar, so everything inside the lens takes the active colour.
562
- - **Safari:** lenses (switch, slider, tab bar, segmented) show a magnified live copy of the content beneath them. Bars, buttons and cards over arbitrary page content get frosting, rims and motion.
564
+ - **Safari:** lenses (switch, slider, tab bar, segmented) refract a live copy of the content beneath them, bending and stretching it into the rim. Bars, buttons and cards over arbitrary page content get frosting, rims and motion.
563
565
  - **Firefox:** uses the same path as Safari; not yet tested.
564
566
  - Respects `prefers-reduced-motion` and `prefers-reduced-transparency`.
565
567
 
package/dist/index.d.mts CHANGED
@@ -272,7 +272,12 @@ interface GlassShape {
272
272
  */
273
273
  copy?: boolean | 'refract';
274
274
  }
275
- /** Only Chromium runs SVG filters inside `backdrop-filter`. */
275
+ /**
276
+ * Only Chromium runs SVG filters inside `backdrop-filter`, and only desktop
277
+ * GPUs carry it: it re-runs over the live backdrop every frame, which holds
278
+ * even flagship Android phones near 30fps. Android takes the path iPhones do
279
+ * (lenses refract a live copy, resting glass keeps blur and rim).
280
+ */
276
281
  declare function canRefractBackdrop(): boolean;
277
282
  /**
278
283
  * Bakes refraction for `ref`'s current box (untransformed) and keeps it fresh
package/dist/index.d.ts CHANGED
@@ -272,7 +272,12 @@ interface GlassShape {
272
272
  */
273
273
  copy?: boolean | 'refract';
274
274
  }
275
- /** Only Chromium runs SVG filters inside `backdrop-filter`. */
275
+ /**
276
+ * Only Chromium runs SVG filters inside `backdrop-filter`, and only desktop
277
+ * GPUs carry it: it re-runs over the live backdrop every frame, which holds
278
+ * even flagship Android phones near 30fps. Android takes the path iPhones do
279
+ * (lenses refract a live copy, resting glass keeps blur and rim).
280
+ */
276
281
  declare function canRefractBackdrop(): boolean;
277
282
  /**
278
283
  * Bakes refraction for `ref`'s current box (untransformed) and keeps it fresh
package/dist/index.js CHANGED
@@ -680,7 +680,7 @@ function container() {
680
680
  function canRefractBackdrop() {
681
681
  if (typeof navigator === "undefined") return false;
682
682
  const ua = navigator.userAgent;
683
- return /Chrom(e|ium)\/|Edg\//.test(ua) && !/CriOS|FxiOS|EdgiOS/.test(ua);
683
+ return /Chrom(e|ium)\/|Edg\//.test(ua) && !/CriOS|FxiOS|EdgiOS|Android/.test(ua);
684
684
  }
685
685
  function bevelProfile(bezel, thickness, ior) {
686
686
  const N = 256;
@@ -733,6 +733,7 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
733
733
  const t = clamp((Math.abs(py) - (edge - 2)) / 3, 0, 1);
734
734
  return 1 - t * t * (3 - 2 * t);
735
735
  };
736
+ const flat = simple && magnify ? Infinity : Math.max(shape.bezel * 1.7, 5);
736
737
  for (let y = 0; y < ch; y++) {
737
738
  for (let x = 0; x < cw; x++) {
738
739
  const px = (x + 0.5) * W / cw - W / 2;
@@ -743,6 +744,10 @@ function bake(width, height, radius, shape, pad = 0, simple = false, fringe = fa
743
744
  const oy = Math.max(qy, 0);
744
745
  const len = Math.hypot(ox, oy);
745
746
  const d = radius - (len + Math.min(Math.max(qx, qy), 0));
747
+ if (d > flat) {
748
+ inside[y * cw + x] = 1;
749
+ continue;
750
+ }
746
751
  let nx;
747
752
  let ny;
748
753
  if (len > 0) {
@@ -1000,6 +1005,34 @@ function glassFor(width, height, radius, shape = {}, pad = 0, simple = false, fr
1000
1005
  }
1001
1006
  return baked;
1002
1007
  }
1008
+ var soon = [];
1009
+ var later = [];
1010
+ var pumping = false;
1011
+ function pump() {
1012
+ pumping = true;
1013
+ const run = (deadline) => {
1014
+ const end = performance.now() + 8;
1015
+ const left = () => deadline ? deadline.timeRemaining() > 4 : performance.now() < end;
1016
+ let ran = 0;
1017
+ while ((soon.length || later.length) && (ran === 0 || left())) {
1018
+ (soon.shift() ?? later.shift())();
1019
+ ran++;
1020
+ }
1021
+ if (soon.length || later.length) idle(run);
1022
+ else pumping = false;
1023
+ };
1024
+ idle(run);
1025
+ }
1026
+ function bakeSoon(job, urgent = true) {
1027
+ (urgent ? soon : later).push(job);
1028
+ if (!pumping) pump();
1029
+ return () => {
1030
+ for (const list of [soon, later]) {
1031
+ const at = list.indexOf(job);
1032
+ if (at >= 0) list.splice(at, 1);
1033
+ }
1034
+ };
1035
+ }
1003
1036
  function idle(callback) {
1004
1037
  if (typeof requestIdleCallback === "function") {
1005
1038
  const handle2 = requestIdleCallback(callback, { timeout: 120 });
@@ -1055,15 +1088,28 @@ function useRefraction(ref, shape = {}, enabled = true, mirror, mirrorActive) {
1055
1088
  }
1056
1089
  element.style.setProperty("--lg-rim", baked.rim);
1057
1090
  };
1091
+ let seen = typeof IntersectionObserver !== "function";
1092
+ const request = () => {
1093
+ cancel?.();
1094
+ cancel = bakeSoon(fit, seen);
1095
+ };
1096
+ const near = typeof IntersectionObserver === "function" ? new IntersectionObserver(
1097
+ (entries) => {
1098
+ if (!entries.some((entry) => entry.isIntersecting)) return;
1099
+ seen = true;
1100
+ near?.disconnect();
1101
+ request();
1102
+ },
1103
+ { rootMargin: "100% 0px" }
1104
+ ) : null;
1105
+ near?.observe(element);
1058
1106
  const observer = new ResizeObserver(() => {
1059
1107
  cancel?.();
1060
- const timer = setTimeout(() => {
1061
- cancel = idle(fit);
1062
- }, 140);
1108
+ const timer = setTimeout(request, 140);
1063
1109
  cancel = () => clearTimeout(timer);
1064
1110
  });
1065
1111
  observer.observe(element);
1066
- fit();
1112
+ request();
1067
1113
  const source = mirror?.current;
1068
1114
  const unmirror = !live && source ? mirrorBackdrop(element, source, () => pad, () => [zoomX, zoom], mirrorActive) : void 0;
1069
1115
  applyCopyFilter();
@@ -1072,6 +1118,7 @@ function useRefraction(ref, shape = {}, enabled = true, mirror, mirrorActive) {
1072
1118
  cancel?.();
1073
1119
  unmirror?.();
1074
1120
  ungovern();
1121
+ near?.disconnect();
1075
1122
  observer.disconnect();
1076
1123
  element.style.removeProperty("--lg-refraction");
1077
1124
  element.style.removeProperty("--lg-rim");
@@ -1709,6 +1756,8 @@ var import_react9 = require("react");
1709
1756
  // src/engine/lensTrack.ts
1710
1757
  var import_react8 = require("react");
1711
1758
  var FOLLOW2 = springFrom(0.2, 0.82);
1759
+ var PULL_REACH = 0.08;
1760
+ var PULL_GIVE = 0.15;
1712
1761
  function useLensTrack(refs, options) {
1713
1762
  const latest = (0, import_react8.useRef)(options);
1714
1763
  latest.current = options;
@@ -1781,15 +1830,18 @@ function useLensTrack(refs, options) {
1781
1830
  if (!W || !H) return;
1782
1831
  const kx = (slot + (W - slot) * lift2) / W * (1 + squash);
1783
1832
  const ky = (height + (H - height) * lift2) / H * (1 - squash * 0.6);
1784
- const grow = 1 + latest.current.swell / Math.max(bar.offsetWidth, 1) * swell2;
1785
1833
  const pull2 = state.pull;
1786
- const ey = Math.abs(pull2) / Math.max(bar.offsetHeight, 1);
1834
+ const h = Math.max(bar.offsetHeight, 1);
1835
+ const reach = Math.abs(pull2);
1836
+ const taken = clamp(reach / (h * PULL_REACH), 0, 1);
1837
+ const grow = 1 + latest.current.swell / Math.max(bar.offsetWidth, 1) * swell2 * (1 - taken * 0.85);
1838
+ const lean = Math.sign(pull2) * (reach / 2 + h * 0.03 * taken);
1787
1839
  const transform = `translateX(${x2}px) scale(${kx}, ${ky})`;
1788
1840
  lens.style.transform = transform;
1789
1841
  select.style.transform = `translateX(${x2}px)`;
1790
1842
  lens.style.opacity = String(clamp(lift2 * 4, 0, 1));
1791
- shell.style.scale = `${grow * (1 - ey * 0.15)} ${grow * (1 + ey * 0.5)}`;
1792
- shell.style.translate = `0px ${pull2 / 2}px`;
1843
+ shell.style.scale = `${grow * (1 - taken * 0.04)} ${grow + reach / h}`;
1844
+ shell.style.translate = `0px ${lean}px`;
1793
1845
  lens.style.setProperty("--lg-lift", String(clamp(lift2, 0, 1)));
1794
1846
  bar.style.setProperty("--lg-lift", String(clamp(lift2, 0, 1)));
1795
1847
  for (const glyph of glyphs) {
@@ -1864,8 +1916,10 @@ function useLensTrack(refs, options) {
1864
1916
  paint();
1865
1917
  });
1866
1918
  const pull = (dy) => {
1919
+ if (!latest.current.pull) return;
1920
+ const toLayout = gesture ? bar.offsetHeight / Math.max(1, gesture.box.height) : 1;
1867
1921
  pullY.tune(FOLLOW2);
1868
- pullY.set(rubberband(dy, Math.min(10, bar.offsetHeight * 0.2), 0.5));
1922
+ pullY.set(rubberband(dy * toLayout, bar.offsetHeight * PULL_REACH, PULL_GIVE));
1869
1923
  };
1870
1924
  const letGo = () => {
1871
1925
  pullY.tune(SPRINGS.release);
@@ -2105,7 +2159,9 @@ function LensTrack({
2105
2159
  spread: tabbar ? 0.3 : 0.16,
2106
2160
  contentZoom: zoom,
2107
2161
  copied: true,
2108
- swell: tabbar ? 8 : 14
2162
+ swell: tabbar ? 12 : 16,
2163
+ // The tab bar holds its place; only the segmented control gives.
2164
+ pull: !tabbar
2109
2165
  }
2110
2166
  );
2111
2167
  return /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)(