@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.js CHANGED
@@ -308,12 +308,12 @@ function backdropChain(glass, source) {
308
308
  const image = cs.backgroundImage !== "none";
309
309
  const a = alpha(cs.backgroundColor);
310
310
  if (image || a > 0) chain.unshift(node);
311
- if (image || a >= 1) break;
311
+ if (image || a >= 1 || node.classList.contains("lg-glass")) break;
312
312
  }
313
313
  return chain;
314
314
  }
315
315
  var BACKGROUND = ["background-color", "background-image", "background-size", "background-position", "background-repeat", "border-radius"];
316
- function mirrorBackdrop(glass, source, filter, padding) {
316
+ function mirrorBackdrop(glass, source, padding, zoom, active2 = () => true) {
317
317
  const layer = document.createElement("div");
318
318
  layer.className = "lg-mirror";
319
319
  layer.setAttribute("aria-hidden", "true");
@@ -322,6 +322,9 @@ function mirrorBackdrop(glass, source, filter, padding) {
322
322
  const viewport = document.createElement("div");
323
323
  viewport.className = "lg-mirror__viewport";
324
324
  layer.append(viewport);
325
+ const zoomer = document.createElement("div");
326
+ zoomer.className = "lg-mirror__zoom";
327
+ viewport.append(zoomer);
325
328
  let copy = clone(source);
326
329
  let under = [];
327
330
  const buildUnder = () => {
@@ -336,14 +339,15 @@ function mirrorBackdrop(glass, source, filter, padding) {
336
339
  div.style.transformOrigin = "0 0";
337
340
  return { original, copy: div, placed: "" };
338
341
  });
339
- viewport.prepend(...under.map((u) => u.copy));
342
+ zoomer.prepend(...under.map((u) => u.copy));
340
343
  };
341
- viewport.append(copy.root);
344
+ zoomer.append(copy.root);
342
345
  buildUnder();
343
346
  let stopped = false;
344
347
  let frame2 = 0;
345
348
  let visible = true;
346
349
  let placed = "";
350
+ let shown = true;
347
351
  let pad = 0;
348
352
  const place = (from, g, sgx, sgy) => {
349
353
  const sw = from.offsetWidth;
@@ -414,15 +418,21 @@ function mirrorBackdrop(glass, source, filter, padding) {
414
418
  }
415
419
  }
416
420
  };
417
- const applyFilter = () => {
418
- const value = filter();
419
- if (layer.style.filter !== value) layer.style.filter = value;
421
+ let zoomed = "";
422
+ const applyZoom = () => {
423
+ const value = `scale(${zoom()})`;
424
+ if (value !== zoomed) zoomer.style.transform = zoomed = value;
420
425
  };
421
426
  const tick = () => {
422
427
  frame2 = 0;
423
428
  if (stopped) return;
424
- if (visible && !document.hidden) {
425
- applyFilter();
429
+ const on = visible && !document.hidden && active2();
430
+ if (on !== shown) {
431
+ shown = on;
432
+ layer.style.visibility = on ? "" : "hidden";
433
+ }
434
+ if (on) {
435
+ applyZoom();
426
436
  sync();
427
437
  align();
428
438
  }
@@ -507,7 +517,7 @@ function bevelProfile(bezel, thickness, ior) {
507
517
  return a + (b - a) * (f - i);
508
518
  };
509
519
  }
510
- function bake(width, height, radius, shape, pad = 0) {
520
+ function bake(width, height, radius, shape, pad = 0, simple = false) {
511
521
  const W = width + pad * 2;
512
522
  const H = height + pad * 2;
513
523
  let density = Math.min(typeof devicePixelRatio === "number" ? devicePixelRatio : 1, 2);
@@ -523,6 +533,7 @@ function bake(width, height, radius, shape, pad = 0) {
523
533
  const rimA = new Float32Array(cw * ch);
524
534
  const rimHue = new Float32Array(cw * ch);
525
535
  const inside = new Float32Array(cw * ch);
536
+ const depth = new Float32Array(cw * ch);
526
537
  let max = 1e-4;
527
538
  const hw = width / 2;
528
539
  const hh = height / 2;
@@ -551,20 +562,26 @@ function bake(width, height, radius, shape, pad = 0) {
551
562
  }
552
563
  const i = y * cw + x;
553
564
  const s = shift(d) * shape.strength * direction;
554
- const vx = nx * s;
555
- const vy = ny * s;
565
+ let vx = nx * s;
566
+ let vy = ny * s;
567
+ if (simple && magnify) {
568
+ vx -= px * magnify;
569
+ vy -= py * magnify;
570
+ }
556
571
  dx[i] = vx;
557
572
  dy[i] = vy;
558
573
  const m = Math.max(Math.abs(vx), Math.abs(vy));
559
574
  if (m > max) max = m;
560
575
  inside[i] = clamp(d / px1 + 0.5, 0, 1);
576
+ const t = clamp(d / (shape.bezel * 0.9), 0, 1);
577
+ depth[i] = t * t * (3 - 2 * t);
561
578
  if (d >= -px1) {
562
579
  const facing = -(nx + ny) * Math.SQRT1_2;
563
580
  const light = 0.22 + 0.78 * Math.abs(facing) ** 2.2 * (facing > 0 ? 1 : 0.62);
564
581
  const line = Math.exp(-(((d - 0.55) / 0.55) ** 2));
565
- const inner = Math.exp(-((d / (shape.bezel * 0.55)) ** 2)) * 0.14;
582
+ const inner2 = Math.exp(-((d / (shape.bezel * 0.55)) ** 2)) * 0.14;
566
583
  const edgeAA = clamp(d / px1 + 1, 0, 1);
567
- rimA[i] = clamp((line * (lens ? 0.95 : 0.7) + inner) * light * edgeAA, 0, 1);
584
+ rimA[i] = clamp((line * (lens ? 0.95 : 0.7) + inner2) * light * edgeAA, 0, 1);
568
585
  rimHue[i] = Math.atan2(ny, nx);
569
586
  }
570
587
  }
@@ -585,7 +602,7 @@ function bake(width, height, radius, shape, pad = 0) {
585
602
  const map = canvas.toDataURL();
586
603
  const reach = Math.max(W, H) / 2;
587
604
  let zoomMap = "";
588
- if (magnify) {
605
+ if (magnify && !simple) {
589
606
  const ramp = ctx.createImageData(cw, ch);
590
607
  for (let y = 0; y < ch; y++) {
591
608
  for (let x = 0; x < cw; x++) {
@@ -629,15 +646,18 @@ function bake(width, height, radius, shape, pad = 0) {
629
646
  }
630
647
  ctx.putImageData(shapeMask, 0, 0);
631
648
  const mask = canvas.toDataURL();
649
+ for (let i = 0; i < cw * ch; i++) shapeMask.data[i * 4 + 3] = Math.round(depth[i] * 255);
650
+ ctx.putImageData(shapeMask, 0, 0);
651
+ const inner = canvas.toDataURL();
632
652
  const id = `lg-refract-${++counter}`;
633
653
  const box = { x: 0, y: 0, width: W, height: H };
634
654
  const filter = el("filter", {
635
655
  id,
636
656
  // Generous region: the rim samples content from beyond the glass edge.
637
- x: -0.5,
638
- y: -0.5,
639
- width: 2,
640
- height: 2,
657
+ x: -0.25,
658
+ y: -0.25,
659
+ width: 1.5,
660
+ height: 1.5,
641
661
  "color-interpolation-filters": "sRGB"
642
662
  });
643
663
  filter.append(el("feImage", { href: map, ...box, preserveAspectRatio: "none", result: "field" }));
@@ -657,7 +677,7 @@ function bake(width, height, radius, shape, pad = 0) {
657
677
  source = "magnified";
658
678
  }
659
679
  const scale = max * 2;
660
- if (shape.dispersion > 0) {
680
+ if (shape.dispersion > 0 && !simple) {
661
681
  const spread = 0.16 * shape.dispersion;
662
682
  const channels = [
663
683
  ["r", 1 + spread, "1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0"],
@@ -698,7 +718,7 @@ function bake(width, height, radius, shape, pad = 0) {
698
718
  el("feComposite", { in: "bent", in2: "shape", operator: "in" })
699
719
  );
700
720
  container().append(filter);
701
- return { filter: `url(#${id})`, rim: `url("${rim}")` };
721
+ return { filter: `url(#${id})`, rim: `url("${rim}")`, inner: `url("${inner}")` };
702
722
  }
703
723
  function resolve(shape, width, height) {
704
724
  const kind = shape.kind ?? "surface";
@@ -714,15 +734,15 @@ function resolve(shape, width, height) {
714
734
  strength: shape.strength ?? 1
715
735
  };
716
736
  }
717
- function glassFor(width, height, radius, shape = {}, pad = 0) {
737
+ function glassFor(width, height, radius, shape = {}, pad = 0, simple = false) {
718
738
  const w = Math.round(width);
719
739
  const h = Math.round(height);
720
740
  const r = Math.min(w / 2, h / 2, Math.max(0, radius));
721
741
  const full = resolve(shape, w, h);
722
- 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("|");
742
+ 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("|");
723
743
  let baked = cache.get(key);
724
744
  if (!baked) {
725
- baked = bake(w, h, r, full, pad);
745
+ baked = bake(w, h, r, full, pad, simple);
726
746
  cache.set(key, baked);
727
747
  }
728
748
  return baked;
@@ -741,7 +761,7 @@ function cornerRadius(element, width, height) {
741
761
  if (!Number.isFinite(n)) return 0;
742
762
  return value.endsWith("%") ? Math.min(width, height) * n / 100 : n;
743
763
  }
744
- function useRefraction(ref, shape = {}, enabled = true, mirror) {
764
+ function useRefraction(ref, shape = {}, enabled = true, mirror, mirrorActive) {
745
765
  const signature = JSON.stringify(shape);
746
766
  (0, import_react2.useEffect)(() => {
747
767
  const element = ref.current;
@@ -750,6 +770,7 @@ function useRefraction(ref, shape = {}, enabled = true, mirror) {
750
770
  let cancel;
751
771
  let filter = "";
752
772
  let pad = 0;
773
+ let zoom = 1;
753
774
  const fit = () => {
754
775
  const width = element.offsetWidth;
755
776
  const height = element.offsetHeight;
@@ -757,7 +778,11 @@ function useRefraction(ref, shape = {}, enabled = true, mirror) {
757
778
  const radius = cornerRadius(element, width, height);
758
779
  const baked = glassFor(width, height, radius, shape);
759
780
  filter = baked.filter;
760
- if (!live && mirror?.current) pad = Math.ceil(Math.min(width, height) * 0.45 + 6);
781
+ if (!live && mirror?.current) {
782
+ pad = Math.ceil(Math.min(width, height) * 0.45 + 6);
783
+ zoom = resolve(shape, width, height).zoom;
784
+ element.style.setProperty("--lg-inner-mask", baked.inner);
785
+ }
761
786
  if (live) element.style.setProperty("--lg-filter", baked.filter);
762
787
  element.style.setProperty("--lg-rim", baked.rim);
763
788
  };
@@ -768,7 +793,7 @@ function useRefraction(ref, shape = {}, enabled = true, mirror) {
768
793
  observer.observe(element);
769
794
  fit();
770
795
  const source = mirror?.current;
771
- const unmirror = !live && source ? mirrorBackdrop(element, source, () => filter, () => pad) : void 0;
796
+ const unmirror = !live && source ? mirrorBackdrop(element, source, () => pad, () => zoom, mirrorActive) : void 0;
772
797
  return () => {
773
798
  cancel?.();
774
799
  unmirror?.();
@@ -816,7 +841,8 @@ var GlassButton = import_react3.default.forwardRef(
816
841
  shimmer = true,
817
842
  thickness = 1,
818
843
  size = "md",
819
- variant = "default",
844
+ variant = "glass",
845
+ destructive = false,
820
846
  icon,
821
847
  className,
822
848
  style,
@@ -836,7 +862,8 @@ var GlassButton = import_react3.default.forwardRef(
836
862
  bezel: Math.min(height * 0.36, 22) * Math.max(0.5, thickness)
837
863
  });
838
864
  const iconOnly = icon != null && (children == null || children === false);
839
- const kind = variant === "ghost" ? "clear" : variant;
865
+ const red = destructive || variant === "destructive";
866
+ const kind = variant === "ghost" ? "clear" : variant === "default" || variant === "destructive" ? "glass" : variant;
840
867
  const classes = ["lg-glass", "lg-button", `lg-button--${size}`, `lg-button--${kind}`, className].filter(Boolean).join(" ");
841
868
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
842
869
  "button",
@@ -847,6 +874,7 @@ var GlassButton = import_react3.default.forwardRef(
847
874
  "data-liquid-glass": "",
848
875
  "data-icon-only": iconOnly ? "" : void 0,
849
876
  "data-shimmer": shimmer ? "" : void 0,
877
+ "data-destructive": red ? "" : void 0,
850
878
  style: tint ? { "--lg-tint": tint, ...style } : style,
851
879
  onPointerDown: (event) => {
852
880
  if (!rest.disabled) haptic();
@@ -1176,8 +1204,9 @@ function useLensTrack(refs, options) {
1176
1204
  if (!gesture) return 0;
1177
1205
  const raw = gesture.anchor + clientX - gesture.startX;
1178
1206
  const max = slot * (latest.current.count - 1);
1179
- if (raw < 0) return rubberband(raw, slot, 0.5);
1180
- if (raw > max) return max + rubberband(raw - max, slot, 0.5);
1207
+ const give = Math.min(14, slot * 0.12);
1208
+ if (raw < 0) return rubberband(raw, give, 0.55);
1209
+ if (raw > max) return max + rubberband(raw - max, give, 0.55);
1181
1210
  return raw;
1182
1211
  };
1183
1212
  const light = (clientX, box) => {
@@ -1332,7 +1361,13 @@ function LensTrack({
1332
1361
  const row = (0, import_react8.useRef)(null);
1333
1362
  const strength = Math.max(1, Math.min(10, intensity)) / 5;
1334
1363
  useRefraction(glass, { kind: "surface", strength });
1335
- useRefraction(lens, { kind: "lens", strength, zoom: variant === "tabbar" ? 1.14 : 1.1 }, true, bar);
1364
+ useRefraction(
1365
+ lens,
1366
+ { kind: "lens", strength, zoom: variant === "tabbar" ? 1.14 : 1.1 },
1367
+ true,
1368
+ bar,
1369
+ () => Boolean(bar.current?.hasAttribute("data-lifted"))
1370
+ );
1336
1371
  const tabbar = variant === "tabbar";
1337
1372
  useLensTrack(
1338
1373
  { bar, glass, lens, tint, row },
@@ -1340,9 +1375,9 @@ function LensTrack({
1340
1375
  count: items.length,
1341
1376
  index,
1342
1377
  onSelect: (next) => onChange?.(next),
1343
- overhang: tabbar ? 8 : 14,
1344
- spread: tabbar ? 0.22 : 0.3,
1345
- swell: tabbar ? 10 : 22
1378
+ overhang: tabbar ? 8 : 12,
1379
+ spread: tabbar ? 0.18 : 0.16,
1380
+ swell: tabbar ? 8 : 14
1346
1381
  }
1347
1382
  );
1348
1383
  return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
@@ -1473,7 +1508,13 @@ var GlassSlider = import_react11.default.forwardRef(
1473
1508
  (0, import_react11.useImperativeHandle)(forwardedRef, () => inputRef.current);
1474
1509
  const strength = Math.max(1, Math.min(10, intensity)) / 5;
1475
1510
  const progress = max > min ? clamp((Number(value) - min) / (max - min), 0, 1) : 0;
1476
- useRefraction(thumbRef, { kind: "lens", zoom: 1.95, strength, dispersion: 0.6 }, showThumb, rootRef);
1511
+ useRefraction(
1512
+ thumbRef,
1513
+ { kind: "lens", zoom: 1.95, strength, dispersion: 0.6 },
1514
+ showThumb,
1515
+ rootRef,
1516
+ () => parseFloat(rootRef.current?.style.getPropertyValue("--lg-lift") || "0") > 0.01
1517
+ );
1477
1518
  useRefraction(blockRef, { kind: "surface", strength }, !showThumb);
1478
1519
  (0, import_react11.useEffect)(() => {
1479
1520
  if (!showThumb) return;
@@ -1674,7 +1715,13 @@ var GlassSwitch = import_react12.default.forwardRef(
1674
1715
  }
1675
1716
  };
1676
1717
  const sync = (0, import_react12.useRef)(null);
1677
- useRefraction(thumbRef, { kind: "lens", zoom: 1.45, strength: Math.max(1, Math.min(10, intensity)) / 5 }, true, ref);
1718
+ useRefraction(
1719
+ thumbRef,
1720
+ { kind: "lens", zoom: 1.45, strength: Math.max(1, Math.min(10, intensity)) / 5 },
1721
+ true,
1722
+ ref,
1723
+ () => parseFloat(ref.current?.style.getPropertyValue("--lg-lift") || "0") > 0.01
1724
+ );
1678
1725
  (0, import_react12.useEffect)(() => {
1679
1726
  const el2 = ref.current;
1680
1727
  const thumb = thumbRef.current;