@zakisheriff/liquid-glass 0.2.4 → 0.2.6

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
@@ -1115,11 +1115,13 @@ function useLensTrack(refs, options) {
1115
1115
  let slot = 0;
1116
1116
  let height = 0;
1117
1117
  let pad = 0;
1118
+ let padY = 0;
1118
1119
  let W = 0;
1119
1120
  let H = 0;
1120
1121
  const state = { x: 0, lift: 0, swell: 0, squash: 0 };
1121
1122
  let current = latest.current.index;
1122
1123
  let gesture = null;
1124
+ let dropTimer = 0;
1123
1125
  let suppressClick = false;
1124
1126
  const buttons = Array.from(bar.querySelectorAll("[data-lg-item]"));
1125
1127
  let glyphs = [];
@@ -1131,37 +1133,41 @@ function useLensTrack(refs, options) {
1131
1133
  const measure = () => {
1132
1134
  const style = getComputedStyle(bar);
1133
1135
  pad = parseFloat(style.paddingLeft) || 0;
1136
+ padY = parseFloat(style.paddingTop) || 0;
1134
1137
  slot = (bar.clientWidth - pad - (parseFloat(style.paddingRight) || 0)) / latest.current.count;
1135
- height = bar.clientHeight - (parseFloat(style.paddingTop) || 0) - (parseFloat(style.paddingBottom) || 0);
1138
+ height = bar.clientHeight - padY - (parseFloat(style.paddingBottom) || 0);
1136
1139
  const { overhang, spread } = latest.current;
1137
- W = Math.round(slot * (1 + spread));
1138
1140
  H = Math.round(bar.offsetHeight + overhang);
1141
+ W = Math.round(Math.max(slot * (1 + spread), slot + H - height));
1139
1142
  lens.style.width = `${W}px`;
1140
1143
  lens.style.height = `${H}px`;
1141
1144
  lens.style.left = `${pad + (slot - W) / 2}px`;
1142
- lens.style.top = `${pad + (height - H) / 2}px`;
1145
+ lens.style.top = `${padY + (height - H) / 2}px`;
1143
1146
  select.style.width = `${slot}px`;
1147
+ select.style.left = `${pad}px`;
1144
1148
  glyphs = buttons.flatMap(
1145
1149
  (button) => Array.from(button.querySelectorAll(".lg-nav__label, .lg-nav__icon > .lg-symbol")).map((node) => ({
1146
1150
  node,
1147
1151
  x: button.offsetLeft + node.offsetLeft - pad,
1148
- y: node.offsetTop + node.offsetHeight / 2,
1152
+ y: node.offsetTop,
1149
1153
  width: node.offsetWidth,
1154
+ height: node.offsetHeight,
1150
1155
  label: node.classList.contains("lg-nav__label")
1151
1156
  }))
1152
1157
  );
1153
1158
  };
1154
1159
  const paint = () => {
1155
- const { x: x2, swell: swell2, squash } = state;
1160
+ const { swell: swell2, squash } = state;
1161
+ const x2 = state.x;
1156
1162
  const lift2 = clamp(state.lift, 0, 1.08);
1157
1163
  if (!W || !H) return;
1158
1164
  const kx = (slot + (W - slot) * lift2) / W * (1 + squash);
1159
1165
  const ky = (height + (H - height) * lift2) / H * (1 - squash * 0.6);
1166
+ const grow = 1 + latest.current.swell / Math.max(bar.offsetWidth, 1) * swell2;
1160
1167
  const transform = `translateX(${x2}px) scale(${kx}, ${ky})`;
1161
1168
  lens.style.transform = transform;
1162
1169
  select.style.transform = `translateX(${x2}px)`;
1163
1170
  lens.style.opacity = String(clamp(lift2 * 4, 0, 1));
1164
- const grow = 1 + latest.current.swell / Math.max(bar.offsetWidth, 1) * swell2;
1165
1171
  bar.style.scale = `${grow}`;
1166
1172
  lens.style.setProperty("--lg-lift", String(clamp(lift2, 0, 1)));
1167
1173
  bar.style.setProperty("--lg-lift", String(clamp(lift2, 0, 1)));
@@ -1171,16 +1177,31 @@ function useLensTrack(refs, options) {
1171
1177
  glyph.node.style.removeProperty("transform");
1172
1178
  continue;
1173
1179
  }
1174
- const ry = H * ky / 2;
1175
- const dy = clamp(Math.abs(glyph.y - height / 2) / ry, 0, 1);
1176
- const half = Math.max(0, (W - H) * kx / 2) + H * kx / 2 * Math.sqrt(1 - dy * dy);
1180
+ const zoom = latest.current.contentZoom ?? 1;
1181
+ const ry = H * ky / (2 * zoom);
1182
+ const rx = H * kx / (2 * zoom);
1183
+ const straight = Math.max(0, (W - H) * kx / (2 * zoom));
1184
+ const dy = clamp(Math.abs(glyph.y + glyph.height / 2 - height / 2) / ry, 0, 1);
1185
+ const half = straight + rx * Math.sqrt(1 - dy * dy);
1177
1186
  const from = x2 + slot / 2 - half - glyph.x;
1178
1187
  const to = x2 + slot / 2 + half - glyph.x;
1179
1188
  const normal = "var(--lg-fg)";
1180
1189
  const active2 = "var(--lg-track-active, var(--lg-accent))";
1181
- glyph.node.style.setProperty("--lg-content-tint", `linear-gradient(90deg, ${normal} ${from}px, ${active2} ${from}px, ${active2} ${to}px, ${normal} ${to}px)`);
1182
1190
  const coverage = clamp((Math.min(glyph.width, to) - Math.max(0, from)) / Math.max(1, glyph.width), 0, 1);
1183
- glyph.node.style.transform = `scale(${1 + clamp(lift2, 0, 1) * coverage * (glyph.label ? 0.035 : 0.08)})`;
1191
+ const scale = 1 + clamp(lift2, 0, 1) * coverage * (glyph.label ? 0.035 : 0.08);
1192
+ const cx = (x2 + slot / 2 - glyph.x - glyph.width / 2) / scale + glyph.width / 2;
1193
+ const cy = (height / 2 - glyph.y - glyph.height / 2) / scale + glyph.height / 2;
1194
+ const left = cx - straight / scale;
1195
+ const right = cx + straight / scale;
1196
+ const radiusX = rx / scale;
1197
+ const radiusY = ry / scale;
1198
+ glyph.node.style.setProperty("--lg-content-tint", [
1199
+ `radial-gradient(ellipse ${radiusX}px ${radiusY}px at ${left}px ${cy}px, ${active2} calc(100% - 0.5px), transparent 100%)`,
1200
+ `radial-gradient(ellipse ${radiusX}px ${radiusY}px at ${right}px ${cy}px, ${active2} calc(100% - 0.5px), transparent 100%)`,
1201
+ `linear-gradient(${active2}, ${active2}) ${left}px ${cy - radiusY}px / ${right - left}px ${radiusY * 2}px no-repeat`,
1202
+ `linear-gradient(${normal}, ${normal})`
1203
+ ].join(", "));
1204
+ glyph.node.style.transform = `scale(${scale})`;
1184
1205
  }
1185
1206
  if (!gesture && lift2 < 0.02) delete bar.dataset.lifted;
1186
1207
  };
@@ -1222,12 +1243,14 @@ function useLensTrack(refs, options) {
1222
1243
  return target && bar.contains(target) ? Number(target.dataset.lgItem) : -1;
1223
1244
  };
1224
1245
  const onDown = (event) => {
1246
+ delete bar.dataset.keyboard;
1225
1247
  if (!event.isPrimary || event.button !== 0 || gesture) return;
1226
1248
  const index = itemAt(event);
1227
1249
  if (index < 0) return;
1250
+ clearTimeout(dropTimer);
1228
1251
  measure();
1229
1252
  const box = bar.getBoundingClientRect();
1230
- gesture = { id: event.pointerId, startX: event.clientX, anchor: index * slot, moved: false, box, hovered: index };
1253
+ gesture = { id: event.pointerId, startX: event.clientX, anchor: index * slot, moved: false, box, hovered: index, startedAt: performance.now() };
1231
1254
  suppressClick = false;
1232
1255
  bar.dataset.lifted = "";
1233
1256
  preview(index);
@@ -1265,7 +1288,7 @@ function useLensTrack(refs, options) {
1265
1288
  };
1266
1289
  const finish = (event, commit) => {
1267
1290
  if (event.pointerId !== gesture?.id) return;
1268
- const { moved, box } = gesture;
1291
+ const { moved, box, startedAt } = gesture;
1269
1292
  const at = moved ? position(event.clientX) : gesture.anchor;
1270
1293
  const index = clamp(Math.round(at / slot), 0, latest.current.count - 1);
1271
1294
  gesture = null;
@@ -1273,8 +1296,13 @@ function useLensTrack(refs, options) {
1273
1296
  delete lens.dataset.pressed;
1274
1297
  if (bar.hasPointerCapture?.(event.pointerId)) bar.releasePointerCapture(event.pointerId);
1275
1298
  const inside = event.clientY >= box.top - 30 && event.clientY <= box.bottom + 30;
1276
- lift.tune(SPRINGS.settle);
1277
- lift.set(0);
1299
+ const drop = () => {
1300
+ lift.tune(SPRINGS.settle);
1301
+ lift.set(0);
1302
+ };
1303
+ const hold = commit && !moved ? Math.max(0, 200 - (performance.now() - startedAt)) : 0;
1304
+ if (hold) dropTimer = window.setTimeout(drop, hold);
1305
+ else drop();
1278
1306
  swell.tune(SPRINGS.release);
1279
1307
  swell.set(0);
1280
1308
  x.tune(SPRINGS.snap);
@@ -1290,7 +1318,12 @@ function useLensTrack(refs, options) {
1290
1318
  const onUp = (event) => finish(event, true);
1291
1319
  const onCancel = (event) => finish(event, false);
1292
1320
  const onBlur = () => {
1293
- if (!gesture) return;
1321
+ clearTimeout(dropTimer);
1322
+ if (!gesture) {
1323
+ lift.tune(SPRINGS.settle);
1324
+ lift.set(0);
1325
+ return;
1326
+ }
1294
1327
  const id = gesture.id;
1295
1328
  gesture = null;
1296
1329
  preview();
@@ -1318,6 +1351,7 @@ function useLensTrack(refs, options) {
1318
1351
  const onKey = (event) => {
1319
1352
  const keys = ["ArrowLeft", "ArrowRight", "Home", "End"];
1320
1353
  if (!keys.includes(event.key)) return;
1354
+ bar.dataset.keyboard = "";
1321
1355
  event.preventDefault();
1322
1356
  const { count, index } = latest.current;
1323
1357
  const next = event.key === "Home" ? 0 : event.key === "End" ? count - 1 : (index + (event.key === "ArrowRight" ? 1 : count - 1)) % count;
@@ -1345,6 +1379,7 @@ function useLensTrack(refs, options) {
1345
1379
  bar.addEventListener("touchmove", onTouchMove, { passive: false });
1346
1380
  api.current = { setIndex: place };
1347
1381
  return () => {
1382
+ clearTimeout(dropTimer);
1348
1383
  api.current = null;
1349
1384
  [x, lift, swell].forEach((s) => s.stop());
1350
1385
  observer.disconnect();
@@ -1388,17 +1423,19 @@ function LensTrack({
1388
1423
  const lens = useRef6(null);
1389
1424
  const select = useRef6(null);
1390
1425
  const strength = Math.max(1, Math.min(10, intensity)) / 5;
1391
- useRefraction(glass, { kind: "surface", strength });
1392
- useRefraction(lens, { kind: "lens", strength, zoom: variant === "tabbar" ? 1.14 : 1.1 });
1393
1426
  const tabbar = variant === "tabbar";
1427
+ const zoom = tabbar ? 1.14 : 1.1;
1428
+ useRefraction(glass, { kind: "surface", strength });
1429
+ useRefraction(lens, { kind: "lens", strength: 0, zoom, dispersion: 0, bezel: 2 });
1394
1430
  useLensTrack(
1395
1431
  { bar, glass, lens, select },
1396
1432
  {
1397
1433
  count: items.length,
1398
1434
  index,
1399
1435
  onSelect: (next) => onChange?.(next),
1400
- overhang: tabbar ? 8 : 12,
1401
- spread: tabbar ? 0.18 : 0.16,
1436
+ overhang: tabbar ? 14 : 12,
1437
+ spread: tabbar ? 0.3 : 0.16,
1438
+ contentZoom: canRefractBackdrop() ? zoom : 1,
1402
1439
  swell: tabbar ? 8 : 14
1403
1440
  }
1404
1441
  );
@@ -1508,7 +1545,7 @@ var GlassSlider = React8.forwardRef(
1508
1545
  value = 0,
1509
1546
  min = 0,
1510
1547
  max = 100,
1511
- step: step2 = 1,
1548
+ step: step2 = "any",
1512
1549
  intensity = 5,
1513
1550
  tint,
1514
1551
  shimmer: _shimmer,
@@ -1528,6 +1565,74 @@ var GlassSlider = React8.forwardRef(
1528
1565
  const faceRef = useRef7(null);
1529
1566
  const blockRef = useRef7(null);
1530
1567
  useImperativeHandle4(forwardedRef, () => inputRef.current);
1568
+ useEffect5(() => {
1569
+ const input2 = inputRef.current;
1570
+ const root = rootRef.current;
1571
+ const setValue = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value").set;
1572
+ let drag = null;
1573
+ const update = (clientX) => {
1574
+ if (!drag || max <= min) return;
1575
+ const inset = showThumb ? 19 * drag.width / root.offsetWidth : 0;
1576
+ const travel = Math.max(1, drag.width - inset * 2);
1577
+ const fraction = clamp((clientX - drag.left - inset - drag.offset) / travel, 0, 1);
1578
+ let next = min + fraction * (max - min);
1579
+ const increment = Number(step2);
1580
+ if (step2 !== "any" && Number.isFinite(increment) && increment > 0) {
1581
+ next = clamp(min + Math.round((next - min) / increment) * increment, min, max);
1582
+ next = Number(next.toFixed(10));
1583
+ }
1584
+ root.style.setProperty("--lg-progress", String((next - min) / (max - min)));
1585
+ if (next === input2.valueAsNumber) return;
1586
+ setValue.call(input2, String(next));
1587
+ input2.dispatchEvent(new Event("input", { bubbles: true }));
1588
+ };
1589
+ const down = (event) => {
1590
+ if (input2.disabled || !event.isPrimary || event.button !== 0 || drag) return;
1591
+ event.preventDefault();
1592
+ input2.focus({ preventScroll: true });
1593
+ const box = root.getBoundingClientRect();
1594
+ const inset = showThumb ? 19 * box.width / root.offsetWidth : 0;
1595
+ const current = max > min ? clamp((input2.valueAsNumber - min) / (max - min), 0, 1) : 0;
1596
+ const center = box.left + inset + current * (box.width - inset * 2);
1597
+ const offset = showThumb && Math.abs(event.clientX - center) <= inset ? event.clientX - center : 0;
1598
+ drag = { id: event.pointerId, left: box.left, width: box.width, offset };
1599
+ root.dataset.dragging = "";
1600
+ input2.setPointerCapture(event.pointerId);
1601
+ update(event.clientX);
1602
+ };
1603
+ const move = (event) => {
1604
+ if (event.pointerId !== drag?.id) return;
1605
+ event.preventDefault();
1606
+ update(event.clientX);
1607
+ };
1608
+ const release = () => {
1609
+ if (!drag) return;
1610
+ const id = drag.id;
1611
+ drag = null;
1612
+ delete root.dataset.dragging;
1613
+ if (input2.hasPointerCapture(id)) input2.releasePointerCapture(id);
1614
+ };
1615
+ const up = (event) => {
1616
+ if (event.pointerId !== drag?.id) return;
1617
+ update(event.clientX);
1618
+ release();
1619
+ };
1620
+ input2.addEventListener("pointerdown", down);
1621
+ input2.addEventListener("pointermove", move);
1622
+ input2.addEventListener("pointerup", up);
1623
+ input2.addEventListener("pointercancel", release);
1624
+ input2.addEventListener("lostpointercapture", release);
1625
+ window.addEventListener("blur", release);
1626
+ return () => {
1627
+ release();
1628
+ input2.removeEventListener("pointerdown", down);
1629
+ input2.removeEventListener("pointermove", move);
1630
+ input2.removeEventListener("pointerup", up);
1631
+ input2.removeEventListener("pointercancel", release);
1632
+ input2.removeEventListener("lostpointercapture", release);
1633
+ window.removeEventListener("blur", release);
1634
+ };
1635
+ }, [min, max, step2, showThumb]);
1531
1636
  const strength = Math.max(1, Math.min(10, intensity)) / 5;
1532
1637
  const [mirrored, setMirrored] = React8.useState(false);
1533
1638
  useEffect5(() => setMirrored(!canRefractBackdrop()), []);
@@ -1552,6 +1657,7 @@ var GlassSlider = React8.forwardRef(
1552
1657
  thumb.style.scale = `${s * (1 + v.stretch)} ${s * (1 - v.stretch * 0.5)}`;
1553
1658
  face.style.opacity = String(1 - clamp(v.lift, 0, 1) * 0.92);
1554
1659
  root.style.setProperty("--lg-lift", String(clamp(v.lift, 0, 1)));
1660
+ if (v.lift < 0.01 && lift.target === 0) delete root.dataset.lifted;
1555
1661
  };
1556
1662
  const lift = createSpring(0, SPRINGS.lift, (x) => {
1557
1663
  v.lift = x;
@@ -1568,6 +1674,7 @@ var GlassSlider = React8.forwardRef(
1568
1674
  if (input2.disabled) return;
1569
1675
  lastX = event.clientX;
1570
1676
  lastT = event.timeStamp;
1677
+ root.dataset.lifted = "";
1571
1678
  lift.tune(SPRINGS.lift);
1572
1679
  lift.set(1);
1573
1680
  haptic();
@@ -1593,6 +1700,7 @@ var GlassSlider = React8.forwardRef(
1593
1700
  window.addEventListener("pointerup", up);
1594
1701
  window.addEventListener("pointercancel", up);
1595
1702
  input2.addEventListener("blur", up);
1703
+ window.addEventListener("blur", up);
1596
1704
  return () => {
1597
1705
  lift.stop();
1598
1706
  stretch.stop();
@@ -1601,6 +1709,8 @@ var GlassSlider = React8.forwardRef(
1601
1709
  window.removeEventListener("pointerup", up);
1602
1710
  window.removeEventListener("pointercancel", up);
1603
1711
  input2.removeEventListener("blur", up);
1712
+ window.removeEventListener("blur", up);
1713
+ delete root.dataset.lifted;
1604
1714
  };
1605
1715
  }, [showThumb]);
1606
1716
  useEffect5(() => {
@@ -1691,12 +1801,12 @@ var GlassSlider = React8.forwardRef(
1691
1801
  /* @__PURE__ */ jsxs7("span", { className: "lg-slider__block-content", "aria-hidden": "true", children: [
1692
1802
  icon && /* @__PURE__ */ jsx9("span", { className: "lg-slider__icon", children: icon }),
1693
1803
  label2 && /* @__PURE__ */ jsx9("span", { className: "lg-slider__label", children: label2 }),
1694
- /* @__PURE__ */ jsx9("span", { className: "lg-slider__value", children: Number(value) })
1804
+ /* @__PURE__ */ jsx9("span", { className: "lg-slider__value", children: Math.round(Number(value)) })
1695
1805
  ] }),
1696
1806
  /* @__PURE__ */ jsx9("span", { className: "lg-slider__block-fill", "aria-hidden": "true", children: /* @__PURE__ */ jsxs7("span", { className: "lg-slider__block-content lg-slider__block-content--lit", children: [
1697
1807
  icon && /* @__PURE__ */ jsx9("span", { className: "lg-slider__icon", children: icon }),
1698
1808
  label2 && /* @__PURE__ */ jsx9("span", { className: "lg-slider__label", children: label2 }),
1699
- /* @__PURE__ */ jsx9("span", { className: "lg-slider__value", children: Number(value) })
1809
+ /* @__PURE__ */ jsx9("span", { className: "lg-slider__value", children: Math.round(Number(value)) })
1700
1810
  ] }) })
1701
1811
  ] }),
1702
1812
  input
@@ -1761,7 +1871,6 @@ var GlassSwitch = React9.forwardRef(
1761
1871
  const el2 = ref.current;
1762
1872
  const thumb = thumbRef.current;
1763
1873
  const fill = fillRef.current;
1764
- const face = faceRef.current;
1765
1874
  const v = { x: checked ? 1 : 0, lift: 0, stretch: 0 };
1766
1875
  const paint = () => {
1767
1876
  const s = REST + (1 - REST) * v.lift;
@@ -1769,8 +1878,8 @@ var GlassSwitch = React9.forwardRef(
1769
1878
  const sy = s * (1 - v.stretch * 0.5);
1770
1879
  thumb.style.transform = `translate(${v.x * TRAVEL}px, -50%) scale(${sx}, ${sy})`;
1771
1880
  const l = clamp(v.lift, 0, 1);
1772
- face.style.opacity = String(1 - l * 0.92);
1773
1881
  el2.style.setProperty("--lg-lift", String(l));
1882
+ el2.toggleAttribute("data-lifted", l > 0.01);
1774
1883
  };
1775
1884
  const position = createSpring(v.x, SPRINGS.snap, (value, velocity) => {
1776
1885
  v.x = value;