@zakisheriff/liquid-glass 0.2.0 → 0.2.1

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
@@ -256,12 +256,12 @@ function backdropChain(glass, source) {
256
256
  const image = cs.backgroundImage !== "none";
257
257
  const a = alpha(cs.backgroundColor);
258
258
  if (image || a > 0) chain.unshift(node);
259
- if (image || a >= 1) break;
259
+ if (image || a >= 1 || node.classList.contains("lg-glass")) break;
260
260
  }
261
261
  return chain;
262
262
  }
263
263
  var BACKGROUND = ["background-color", "background-image", "background-size", "background-position", "background-repeat", "border-radius"];
264
- function mirrorBackdrop(glass, source, filter, padding) {
264
+ function mirrorBackdrop(glass, source, padding, zoom, active2 = () => true) {
265
265
  const layer = document.createElement("div");
266
266
  layer.className = "lg-mirror";
267
267
  layer.setAttribute("aria-hidden", "true");
@@ -270,6 +270,9 @@ function mirrorBackdrop(glass, source, filter, padding) {
270
270
  const viewport = document.createElement("div");
271
271
  viewport.className = "lg-mirror__viewport";
272
272
  layer.append(viewport);
273
+ const zoomer = document.createElement("div");
274
+ zoomer.className = "lg-mirror__zoom";
275
+ viewport.append(zoomer);
273
276
  let copy = clone(source);
274
277
  let under = [];
275
278
  const buildUnder = () => {
@@ -284,14 +287,15 @@ function mirrorBackdrop(glass, source, filter, padding) {
284
287
  div.style.transformOrigin = "0 0";
285
288
  return { original, copy: div, placed: "" };
286
289
  });
287
- viewport.prepend(...under.map((u) => u.copy));
290
+ zoomer.prepend(...under.map((u) => u.copy));
288
291
  };
289
- viewport.append(copy.root);
292
+ zoomer.append(copy.root);
290
293
  buildUnder();
291
294
  let stopped = false;
292
295
  let frame2 = 0;
293
296
  let visible = true;
294
297
  let placed = "";
298
+ let shown = true;
295
299
  let pad = 0;
296
300
  const place = (from, g, sgx, sgy) => {
297
301
  const sw = from.offsetWidth;
@@ -362,15 +366,21 @@ function mirrorBackdrop(glass, source, filter, padding) {
362
366
  }
363
367
  }
364
368
  };
365
- const applyFilter = () => {
366
- const value = filter();
367
- if (layer.style.filter !== value) layer.style.filter = value;
369
+ let zoomed = "";
370
+ const applyZoom = () => {
371
+ const value = `scale(${zoom()})`;
372
+ if (value !== zoomed) zoomer.style.transform = zoomed = value;
368
373
  };
369
374
  const tick = () => {
370
375
  frame2 = 0;
371
376
  if (stopped) return;
372
- if (visible && !document.hidden) {
373
- applyFilter();
377
+ const on = visible && !document.hidden && active2();
378
+ if (on !== shown) {
379
+ shown = on;
380
+ layer.style.visibility = on ? "" : "hidden";
381
+ }
382
+ if (on) {
383
+ applyZoom();
374
384
  sync();
375
385
  align();
376
386
  }
@@ -455,7 +465,7 @@ function bevelProfile(bezel, thickness, ior) {
455
465
  return a + (b - a) * (f - i);
456
466
  };
457
467
  }
458
- function bake(width, height, radius, shape, pad = 0) {
468
+ function bake(width, height, radius, shape, pad = 0, simple = false) {
459
469
  const W = width + pad * 2;
460
470
  const H = height + pad * 2;
461
471
  let density = Math.min(typeof devicePixelRatio === "number" ? devicePixelRatio : 1, 2);
@@ -471,6 +481,7 @@ function bake(width, height, radius, shape, pad = 0) {
471
481
  const rimA = new Float32Array(cw * ch);
472
482
  const rimHue = new Float32Array(cw * ch);
473
483
  const inside = new Float32Array(cw * ch);
484
+ const depth = new Float32Array(cw * ch);
474
485
  let max = 1e-4;
475
486
  const hw = width / 2;
476
487
  const hh = height / 2;
@@ -499,20 +510,26 @@ function bake(width, height, radius, shape, pad = 0) {
499
510
  }
500
511
  const i = y * cw + x;
501
512
  const s = shift(d) * shape.strength * direction;
502
- const vx = nx * s;
503
- const vy = ny * s;
513
+ let vx = nx * s;
514
+ let vy = ny * s;
515
+ if (simple && magnify) {
516
+ vx -= px * magnify;
517
+ vy -= py * magnify;
518
+ }
504
519
  dx[i] = vx;
505
520
  dy[i] = vy;
506
521
  const m = Math.max(Math.abs(vx), Math.abs(vy));
507
522
  if (m > max) max = m;
508
523
  inside[i] = clamp(d / px1 + 0.5, 0, 1);
524
+ const t = clamp(d / (shape.bezel * 0.9), 0, 1);
525
+ depth[i] = t * t * (3 - 2 * t);
509
526
  if (d >= -px1) {
510
527
  const facing = -(nx + ny) * Math.SQRT1_2;
511
528
  const light = 0.22 + 0.78 * Math.abs(facing) ** 2.2 * (facing > 0 ? 1 : 0.62);
512
529
  const line = Math.exp(-(((d - 0.55) / 0.55) ** 2));
513
- const inner = Math.exp(-((d / (shape.bezel * 0.55)) ** 2)) * 0.14;
530
+ const inner2 = Math.exp(-((d / (shape.bezel * 0.55)) ** 2)) * 0.14;
514
531
  const edgeAA = clamp(d / px1 + 1, 0, 1);
515
- rimA[i] = clamp((line * (lens ? 0.95 : 0.7) + inner) * light * edgeAA, 0, 1);
532
+ rimA[i] = clamp((line * (lens ? 0.95 : 0.7) + inner2) * light * edgeAA, 0, 1);
516
533
  rimHue[i] = Math.atan2(ny, nx);
517
534
  }
518
535
  }
@@ -533,7 +550,7 @@ function bake(width, height, radius, shape, pad = 0) {
533
550
  const map = canvas.toDataURL();
534
551
  const reach = Math.max(W, H) / 2;
535
552
  let zoomMap = "";
536
- if (magnify) {
553
+ if (magnify && !simple) {
537
554
  const ramp = ctx.createImageData(cw, ch);
538
555
  for (let y = 0; y < ch; y++) {
539
556
  for (let x = 0; x < cw; x++) {
@@ -577,15 +594,18 @@ function bake(width, height, radius, shape, pad = 0) {
577
594
  }
578
595
  ctx.putImageData(shapeMask, 0, 0);
579
596
  const mask = canvas.toDataURL();
597
+ for (let i = 0; i < cw * ch; i++) shapeMask.data[i * 4 + 3] = Math.round(depth[i] * 255);
598
+ ctx.putImageData(shapeMask, 0, 0);
599
+ const inner = canvas.toDataURL();
580
600
  const id = `lg-refract-${++counter}`;
581
601
  const box = { x: 0, y: 0, width: W, height: H };
582
602
  const filter = el("filter", {
583
603
  id,
584
604
  // Generous region: the rim samples content from beyond the glass edge.
585
- x: -0.5,
586
- y: -0.5,
587
- width: 2,
588
- height: 2,
605
+ x: -0.25,
606
+ y: -0.25,
607
+ width: 1.5,
608
+ height: 1.5,
589
609
  "color-interpolation-filters": "sRGB"
590
610
  });
591
611
  filter.append(el("feImage", { href: map, ...box, preserveAspectRatio: "none", result: "field" }));
@@ -605,7 +625,7 @@ function bake(width, height, radius, shape, pad = 0) {
605
625
  source = "magnified";
606
626
  }
607
627
  const scale = max * 2;
608
- if (shape.dispersion > 0) {
628
+ if (shape.dispersion > 0 && !simple) {
609
629
  const spread = 0.16 * shape.dispersion;
610
630
  const channels = [
611
631
  ["r", 1 + spread, "1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0"],
@@ -646,7 +666,7 @@ function bake(width, height, radius, shape, pad = 0) {
646
666
  el("feComposite", { in: "bent", in2: "shape", operator: "in" })
647
667
  );
648
668
  container().append(filter);
649
- return { filter: `url(#${id})`, rim: `url("${rim}")` };
669
+ return { filter: `url(#${id})`, rim: `url("${rim}")`, inner: `url("${inner}")` };
650
670
  }
651
671
  function resolve(shape, width, height) {
652
672
  const kind = shape.kind ?? "surface";
@@ -662,15 +682,15 @@ function resolve(shape, width, height) {
662
682
  strength: shape.strength ?? 1
663
683
  };
664
684
  }
665
- function glassFor(width, height, radius, shape = {}, pad = 0) {
685
+ function glassFor(width, height, radius, shape = {}, pad = 0, simple = false) {
666
686
  const w = Math.round(width);
667
687
  const h = Math.round(height);
668
688
  const r = Math.min(w / 2, h / 2, Math.max(0, radius));
669
689
  const full = resolve(shape, w, h);
670
- 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, pad].join("|");
690
+ 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, pad, simple].join("|");
671
691
  let baked = cache.get(key);
672
692
  if (!baked) {
673
- baked = bake(w, h, r, full, pad);
693
+ baked = bake(w, h, r, full, pad, simple);
674
694
  cache.set(key, baked);
675
695
  }
676
696
  return baked;
@@ -689,7 +709,7 @@ function cornerRadius(element, width, height) {
689
709
  if (!Number.isFinite(n)) return 0;
690
710
  return value.endsWith("%") ? Math.min(width, height) * n / 100 : n;
691
711
  }
692
- function useRefraction(ref, shape = {}, enabled = true, mirror) {
712
+ function useRefraction(ref, shape = {}, enabled = true, mirror, mirrorActive) {
693
713
  const signature = JSON.stringify(shape);
694
714
  useEffect2(() => {
695
715
  const element = ref.current;
@@ -698,6 +718,7 @@ function useRefraction(ref, shape = {}, enabled = true, mirror) {
698
718
  let cancel;
699
719
  let filter = "";
700
720
  let pad = 0;
721
+ let zoom = 1;
701
722
  const fit = () => {
702
723
  const width = element.offsetWidth;
703
724
  const height = element.offsetHeight;
@@ -705,7 +726,11 @@ function useRefraction(ref, shape = {}, enabled = true, mirror) {
705
726
  const radius = cornerRadius(element, width, height);
706
727
  const baked = glassFor(width, height, radius, shape);
707
728
  filter = baked.filter;
708
- if (!live && mirror?.current) pad = Math.ceil(Math.min(width, height) * 0.45 + 6);
729
+ if (!live && mirror?.current) {
730
+ pad = Math.ceil(Math.min(width, height) * 0.45 + 6);
731
+ zoom = resolve(shape, width, height).zoom;
732
+ element.style.setProperty("--lg-inner-mask", baked.inner);
733
+ }
709
734
  if (live) element.style.setProperty("--lg-filter", baked.filter);
710
735
  element.style.setProperty("--lg-rim", baked.rim);
711
736
  };
@@ -716,7 +741,7 @@ function useRefraction(ref, shape = {}, enabled = true, mirror) {
716
741
  observer.observe(element);
717
742
  fit();
718
743
  const source = mirror?.current;
719
- const unmirror = !live && source ? mirrorBackdrop(element, source, () => filter, () => pad) : void 0;
744
+ const unmirror = !live && source ? mirrorBackdrop(element, source, () => pad, () => zoom, mirrorActive) : void 0;
720
745
  return () => {
721
746
  cancel?.();
722
747
  unmirror?.();
@@ -764,7 +789,8 @@ var GlassButton = React.forwardRef(
764
789
  shimmer = true,
765
790
  thickness = 1,
766
791
  size = "md",
767
- variant = "default",
792
+ variant = "glass",
793
+ destructive = false,
768
794
  icon,
769
795
  className,
770
796
  style,
@@ -784,7 +810,8 @@ var GlassButton = React.forwardRef(
784
810
  bezel: Math.min(height * 0.36, 22) * Math.max(0.5, thickness)
785
811
  });
786
812
  const iconOnly = icon != null && (children == null || children === false);
787
- const kind = variant === "ghost" ? "clear" : variant;
813
+ const red = destructive || variant === "destructive";
814
+ const kind = variant === "ghost" ? "clear" : variant === "default" || variant === "destructive" ? "glass" : variant;
788
815
  const classes = ["lg-glass", "lg-button", `lg-button--${size}`, `lg-button--${kind}`, className].filter(Boolean).join(" ");
789
816
  return /* @__PURE__ */ jsxs(
790
817
  "button",
@@ -795,6 +822,7 @@ var GlassButton = React.forwardRef(
795
822
  "data-liquid-glass": "",
796
823
  "data-icon-only": iconOnly ? "" : void 0,
797
824
  "data-shimmer": shimmer ? "" : void 0,
825
+ "data-destructive": red ? "" : void 0,
798
826
  style: tint ? { "--lg-tint": tint, ...style } : style,
799
827
  onPointerDown: (event) => {
800
828
  if (!rest.disabled) haptic();
@@ -1124,8 +1152,9 @@ function useLensTrack(refs, options) {
1124
1152
  if (!gesture) return 0;
1125
1153
  const raw = gesture.anchor + clientX - gesture.startX;
1126
1154
  const max = slot * (latest.current.count - 1);
1127
- if (raw < 0) return rubberband(raw, slot, 0.5);
1128
- if (raw > max) return max + rubberband(raw - max, slot, 0.5);
1155
+ const give = Math.min(14, slot * 0.12);
1156
+ if (raw < 0) return rubberband(raw, give, 0.55);
1157
+ if (raw > max) return max + rubberband(raw - max, give, 0.55);
1129
1158
  return raw;
1130
1159
  };
1131
1160
  const light = (clientX, box) => {
@@ -1280,7 +1309,13 @@ function LensTrack({
1280
1309
  const row = useRef6(null);
1281
1310
  const strength = Math.max(1, Math.min(10, intensity)) / 5;
1282
1311
  useRefraction(glass, { kind: "surface", strength });
1283
- useRefraction(lens, { kind: "lens", strength, zoom: variant === "tabbar" ? 1.14 : 1.1 }, true, bar);
1312
+ useRefraction(
1313
+ lens,
1314
+ { kind: "lens", strength, zoom: variant === "tabbar" ? 1.14 : 1.1 },
1315
+ true,
1316
+ bar,
1317
+ () => Boolean(bar.current?.hasAttribute("data-lifted"))
1318
+ );
1284
1319
  const tabbar = variant === "tabbar";
1285
1320
  useLensTrack(
1286
1321
  { bar, glass, lens, tint, row },
@@ -1288,9 +1323,9 @@ function LensTrack({
1288
1323
  count: items.length,
1289
1324
  index,
1290
1325
  onSelect: (next) => onChange?.(next),
1291
- overhang: tabbar ? 8 : 14,
1292
- spread: tabbar ? 0.22 : 0.3,
1293
- swell: tabbar ? 10 : 22
1326
+ overhang: tabbar ? 8 : 12,
1327
+ spread: tabbar ? 0.18 : 0.16,
1328
+ swell: tabbar ? 8 : 14
1294
1329
  }
1295
1330
  );
1296
1331
  return /* @__PURE__ */ jsxs4(
@@ -1421,7 +1456,13 @@ var GlassSlider = React8.forwardRef(
1421
1456
  useImperativeHandle4(forwardedRef, () => inputRef.current);
1422
1457
  const strength = Math.max(1, Math.min(10, intensity)) / 5;
1423
1458
  const progress = max > min ? clamp((Number(value) - min) / (max - min), 0, 1) : 0;
1424
- useRefraction(thumbRef, { kind: "lens", zoom: 1.95, strength, dispersion: 0.6 }, showThumb, rootRef);
1459
+ useRefraction(
1460
+ thumbRef,
1461
+ { kind: "lens", zoom: 1.95, strength, dispersion: 0.6 },
1462
+ showThumb,
1463
+ rootRef,
1464
+ () => parseFloat(rootRef.current?.style.getPropertyValue("--lg-lift") || "0") > 0.01
1465
+ );
1425
1466
  useRefraction(blockRef, { kind: "surface", strength }, !showThumb);
1426
1467
  useEffect5(() => {
1427
1468
  if (!showThumb) return;
@@ -1622,7 +1663,13 @@ var GlassSwitch = React9.forwardRef(
1622
1663
  }
1623
1664
  };
1624
1665
  const sync = useRef8(null);
1625
- useRefraction(thumbRef, { kind: "lens", zoom: 1.45, strength: Math.max(1, Math.min(10, intensity)) / 5 }, true, ref);
1666
+ useRefraction(
1667
+ thumbRef,
1668
+ { kind: "lens", zoom: 1.45, strength: Math.max(1, Math.min(10, intensity)) / 5 },
1669
+ true,
1670
+ ref,
1671
+ () => parseFloat(ref.current?.style.getPropertyValue("--lg-lift") || "0") > 0.01
1672
+ );
1626
1673
  useEffect6(() => {
1627
1674
  const el2 = ref.current;
1628
1675
  const thumb = thumbRef.current;