@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.js CHANGED
@@ -1167,11 +1167,13 @@ function useLensTrack(refs, options) {
1167
1167
  let slot = 0;
1168
1168
  let height = 0;
1169
1169
  let pad = 0;
1170
+ let padY = 0;
1170
1171
  let W = 0;
1171
1172
  let H = 0;
1172
1173
  const state = { x: 0, lift: 0, swell: 0, squash: 0 };
1173
1174
  let current = latest.current.index;
1174
1175
  let gesture = null;
1176
+ let dropTimer = 0;
1175
1177
  let suppressClick = false;
1176
1178
  const buttons = Array.from(bar.querySelectorAll("[data-lg-item]"));
1177
1179
  let glyphs = [];
@@ -1183,37 +1185,41 @@ function useLensTrack(refs, options) {
1183
1185
  const measure = () => {
1184
1186
  const style = getComputedStyle(bar);
1185
1187
  pad = parseFloat(style.paddingLeft) || 0;
1188
+ padY = parseFloat(style.paddingTop) || 0;
1186
1189
  slot = (bar.clientWidth - pad - (parseFloat(style.paddingRight) || 0)) / latest.current.count;
1187
- height = bar.clientHeight - (parseFloat(style.paddingTop) || 0) - (parseFloat(style.paddingBottom) || 0);
1190
+ height = bar.clientHeight - padY - (parseFloat(style.paddingBottom) || 0);
1188
1191
  const { overhang, spread } = latest.current;
1189
- W = Math.round(slot * (1 + spread));
1190
1192
  H = Math.round(bar.offsetHeight + overhang);
1193
+ W = Math.round(Math.max(slot * (1 + spread), slot + H - height));
1191
1194
  lens.style.width = `${W}px`;
1192
1195
  lens.style.height = `${H}px`;
1193
1196
  lens.style.left = `${pad + (slot - W) / 2}px`;
1194
- lens.style.top = `${pad + (height - H) / 2}px`;
1197
+ lens.style.top = `${padY + (height - H) / 2}px`;
1195
1198
  select.style.width = `${slot}px`;
1199
+ select.style.left = `${pad}px`;
1196
1200
  glyphs = buttons.flatMap(
1197
1201
  (button) => Array.from(button.querySelectorAll(".lg-nav__label, .lg-nav__icon > .lg-symbol")).map((node) => ({
1198
1202
  node,
1199
1203
  x: button.offsetLeft + node.offsetLeft - pad,
1200
- y: node.offsetTop + node.offsetHeight / 2,
1204
+ y: node.offsetTop,
1201
1205
  width: node.offsetWidth,
1206
+ height: node.offsetHeight,
1202
1207
  label: node.classList.contains("lg-nav__label")
1203
1208
  }))
1204
1209
  );
1205
1210
  };
1206
1211
  const paint = () => {
1207
- const { x: x2, swell: swell2, squash } = state;
1212
+ const { swell: swell2, squash } = state;
1213
+ const x2 = state.x;
1208
1214
  const lift2 = clamp(state.lift, 0, 1.08);
1209
1215
  if (!W || !H) return;
1210
1216
  const kx = (slot + (W - slot) * lift2) / W * (1 + squash);
1211
1217
  const ky = (height + (H - height) * lift2) / H * (1 - squash * 0.6);
1218
+ const grow = 1 + latest.current.swell / Math.max(bar.offsetWidth, 1) * swell2;
1212
1219
  const transform = `translateX(${x2}px) scale(${kx}, ${ky})`;
1213
1220
  lens.style.transform = transform;
1214
1221
  select.style.transform = `translateX(${x2}px)`;
1215
1222
  lens.style.opacity = String(clamp(lift2 * 4, 0, 1));
1216
- const grow = 1 + latest.current.swell / Math.max(bar.offsetWidth, 1) * swell2;
1217
1223
  bar.style.scale = `${grow}`;
1218
1224
  lens.style.setProperty("--lg-lift", String(clamp(lift2, 0, 1)));
1219
1225
  bar.style.setProperty("--lg-lift", String(clamp(lift2, 0, 1)));
@@ -1223,16 +1229,31 @@ function useLensTrack(refs, options) {
1223
1229
  glyph.node.style.removeProperty("transform");
1224
1230
  continue;
1225
1231
  }
1226
- const ry = H * ky / 2;
1227
- const dy = clamp(Math.abs(glyph.y - height / 2) / ry, 0, 1);
1228
- const half = Math.max(0, (W - H) * kx / 2) + H * kx / 2 * Math.sqrt(1 - dy * dy);
1232
+ const zoom = latest.current.contentZoom ?? 1;
1233
+ const ry = H * ky / (2 * zoom);
1234
+ const rx = H * kx / (2 * zoom);
1235
+ const straight = Math.max(0, (W - H) * kx / (2 * zoom));
1236
+ const dy = clamp(Math.abs(glyph.y + glyph.height / 2 - height / 2) / ry, 0, 1);
1237
+ const half = straight + rx * Math.sqrt(1 - dy * dy);
1229
1238
  const from = x2 + slot / 2 - half - glyph.x;
1230
1239
  const to = x2 + slot / 2 + half - glyph.x;
1231
1240
  const normal = "var(--lg-fg)";
1232
1241
  const active2 = "var(--lg-track-active, var(--lg-accent))";
1233
- glyph.node.style.setProperty("--lg-content-tint", `linear-gradient(90deg, ${normal} ${from}px, ${active2} ${from}px, ${active2} ${to}px, ${normal} ${to}px)`);
1234
1242
  const coverage = clamp((Math.min(glyph.width, to) - Math.max(0, from)) / Math.max(1, glyph.width), 0, 1);
1235
- glyph.node.style.transform = `scale(${1 + clamp(lift2, 0, 1) * coverage * (glyph.label ? 0.035 : 0.08)})`;
1243
+ const scale = 1 + clamp(lift2, 0, 1) * coverage * (glyph.label ? 0.035 : 0.08);
1244
+ const cx = (x2 + slot / 2 - glyph.x - glyph.width / 2) / scale + glyph.width / 2;
1245
+ const cy = (height / 2 - glyph.y - glyph.height / 2) / scale + glyph.height / 2;
1246
+ const left = cx - straight / scale;
1247
+ const right = cx + straight / scale;
1248
+ const radiusX = rx / scale;
1249
+ const radiusY = ry / scale;
1250
+ glyph.node.style.setProperty("--lg-content-tint", [
1251
+ `radial-gradient(ellipse ${radiusX}px ${radiusY}px at ${left}px ${cy}px, ${active2} calc(100% - 0.5px), transparent 100%)`,
1252
+ `radial-gradient(ellipse ${radiusX}px ${radiusY}px at ${right}px ${cy}px, ${active2} calc(100% - 0.5px), transparent 100%)`,
1253
+ `linear-gradient(${active2}, ${active2}) ${left}px ${cy - radiusY}px / ${right - left}px ${radiusY * 2}px no-repeat`,
1254
+ `linear-gradient(${normal}, ${normal})`
1255
+ ].join(", "));
1256
+ glyph.node.style.transform = `scale(${scale})`;
1236
1257
  }
1237
1258
  if (!gesture && lift2 < 0.02) delete bar.dataset.lifted;
1238
1259
  };
@@ -1274,12 +1295,14 @@ function useLensTrack(refs, options) {
1274
1295
  return target && bar.contains(target) ? Number(target.dataset.lgItem) : -1;
1275
1296
  };
1276
1297
  const onDown = (event) => {
1298
+ delete bar.dataset.keyboard;
1277
1299
  if (!event.isPrimary || event.button !== 0 || gesture) return;
1278
1300
  const index = itemAt(event);
1279
1301
  if (index < 0) return;
1302
+ clearTimeout(dropTimer);
1280
1303
  measure();
1281
1304
  const box = bar.getBoundingClientRect();
1282
- gesture = { id: event.pointerId, startX: event.clientX, anchor: index * slot, moved: false, box, hovered: index };
1305
+ gesture = { id: event.pointerId, startX: event.clientX, anchor: index * slot, moved: false, box, hovered: index, startedAt: performance.now() };
1283
1306
  suppressClick = false;
1284
1307
  bar.dataset.lifted = "";
1285
1308
  preview(index);
@@ -1317,7 +1340,7 @@ function useLensTrack(refs, options) {
1317
1340
  };
1318
1341
  const finish = (event, commit) => {
1319
1342
  if (event.pointerId !== gesture?.id) return;
1320
- const { moved, box } = gesture;
1343
+ const { moved, box, startedAt } = gesture;
1321
1344
  const at = moved ? position(event.clientX) : gesture.anchor;
1322
1345
  const index = clamp(Math.round(at / slot), 0, latest.current.count - 1);
1323
1346
  gesture = null;
@@ -1325,8 +1348,13 @@ function useLensTrack(refs, options) {
1325
1348
  delete lens.dataset.pressed;
1326
1349
  if (bar.hasPointerCapture?.(event.pointerId)) bar.releasePointerCapture(event.pointerId);
1327
1350
  const inside = event.clientY >= box.top - 30 && event.clientY <= box.bottom + 30;
1328
- lift.tune(SPRINGS.settle);
1329
- lift.set(0);
1351
+ const drop = () => {
1352
+ lift.tune(SPRINGS.settle);
1353
+ lift.set(0);
1354
+ };
1355
+ const hold = commit && !moved ? Math.max(0, 200 - (performance.now() - startedAt)) : 0;
1356
+ if (hold) dropTimer = window.setTimeout(drop, hold);
1357
+ else drop();
1330
1358
  swell.tune(SPRINGS.release);
1331
1359
  swell.set(0);
1332
1360
  x.tune(SPRINGS.snap);
@@ -1342,7 +1370,12 @@ function useLensTrack(refs, options) {
1342
1370
  const onUp = (event) => finish(event, true);
1343
1371
  const onCancel = (event) => finish(event, false);
1344
1372
  const onBlur = () => {
1345
- if (!gesture) return;
1373
+ clearTimeout(dropTimer);
1374
+ if (!gesture) {
1375
+ lift.tune(SPRINGS.settle);
1376
+ lift.set(0);
1377
+ return;
1378
+ }
1346
1379
  const id = gesture.id;
1347
1380
  gesture = null;
1348
1381
  preview();
@@ -1370,6 +1403,7 @@ function useLensTrack(refs, options) {
1370
1403
  const onKey = (event) => {
1371
1404
  const keys = ["ArrowLeft", "ArrowRight", "Home", "End"];
1372
1405
  if (!keys.includes(event.key)) return;
1406
+ bar.dataset.keyboard = "";
1373
1407
  event.preventDefault();
1374
1408
  const { count, index } = latest.current;
1375
1409
  const next = event.key === "Home" ? 0 : event.key === "End" ? count - 1 : (index + (event.key === "ArrowRight" ? 1 : count - 1)) % count;
@@ -1397,6 +1431,7 @@ function useLensTrack(refs, options) {
1397
1431
  bar.addEventListener("touchmove", onTouchMove, { passive: false });
1398
1432
  api.current = { setIndex: place };
1399
1433
  return () => {
1434
+ clearTimeout(dropTimer);
1400
1435
  api.current = null;
1401
1436
  [x, lift, swell].forEach((s) => s.stop());
1402
1437
  observer.disconnect();
@@ -1440,17 +1475,19 @@ function LensTrack({
1440
1475
  const lens = (0, import_react8.useRef)(null);
1441
1476
  const select = (0, import_react8.useRef)(null);
1442
1477
  const strength = Math.max(1, Math.min(10, intensity)) / 5;
1443
- useRefraction(glass, { kind: "surface", strength });
1444
- useRefraction(lens, { kind: "lens", strength, zoom: variant === "tabbar" ? 1.14 : 1.1 });
1445
1478
  const tabbar = variant === "tabbar";
1479
+ const zoom = tabbar ? 1.14 : 1.1;
1480
+ useRefraction(glass, { kind: "surface", strength });
1481
+ useRefraction(lens, { kind: "lens", strength: 0, zoom, dispersion: 0, bezel: 2 });
1446
1482
  useLensTrack(
1447
1483
  { bar, glass, lens, select },
1448
1484
  {
1449
1485
  count: items.length,
1450
1486
  index,
1451
1487
  onSelect: (next) => onChange?.(next),
1452
- overhang: tabbar ? 8 : 12,
1453
- spread: tabbar ? 0.18 : 0.16,
1488
+ overhang: tabbar ? 14 : 12,
1489
+ spread: tabbar ? 0.3 : 0.16,
1490
+ contentZoom: canRefractBackdrop() ? zoom : 1,
1454
1491
  swell: tabbar ? 8 : 14
1455
1492
  }
1456
1493
  );
@@ -1560,7 +1597,7 @@ var GlassSlider = import_react11.default.forwardRef(
1560
1597
  value = 0,
1561
1598
  min = 0,
1562
1599
  max = 100,
1563
- step: step2 = 1,
1600
+ step: step2 = "any",
1564
1601
  intensity = 5,
1565
1602
  tint,
1566
1603
  shimmer: _shimmer,
@@ -1580,6 +1617,74 @@ var GlassSlider = import_react11.default.forwardRef(
1580
1617
  const faceRef = (0, import_react11.useRef)(null);
1581
1618
  const blockRef = (0, import_react11.useRef)(null);
1582
1619
  (0, import_react11.useImperativeHandle)(forwardedRef, () => inputRef.current);
1620
+ (0, import_react11.useEffect)(() => {
1621
+ const input2 = inputRef.current;
1622
+ const root = rootRef.current;
1623
+ const setValue = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value").set;
1624
+ let drag = null;
1625
+ const update = (clientX) => {
1626
+ if (!drag || max <= min) return;
1627
+ const inset = showThumb ? 19 * drag.width / root.offsetWidth : 0;
1628
+ const travel = Math.max(1, drag.width - inset * 2);
1629
+ const fraction = clamp((clientX - drag.left - inset - drag.offset) / travel, 0, 1);
1630
+ let next = min + fraction * (max - min);
1631
+ const increment = Number(step2);
1632
+ if (step2 !== "any" && Number.isFinite(increment) && increment > 0) {
1633
+ next = clamp(min + Math.round((next - min) / increment) * increment, min, max);
1634
+ next = Number(next.toFixed(10));
1635
+ }
1636
+ root.style.setProperty("--lg-progress", String((next - min) / (max - min)));
1637
+ if (next === input2.valueAsNumber) return;
1638
+ setValue.call(input2, String(next));
1639
+ input2.dispatchEvent(new Event("input", { bubbles: true }));
1640
+ };
1641
+ const down = (event) => {
1642
+ if (input2.disabled || !event.isPrimary || event.button !== 0 || drag) return;
1643
+ event.preventDefault();
1644
+ input2.focus({ preventScroll: true });
1645
+ const box = root.getBoundingClientRect();
1646
+ const inset = showThumb ? 19 * box.width / root.offsetWidth : 0;
1647
+ const current = max > min ? clamp((input2.valueAsNumber - min) / (max - min), 0, 1) : 0;
1648
+ const center = box.left + inset + current * (box.width - inset * 2);
1649
+ const offset = showThumb && Math.abs(event.clientX - center) <= inset ? event.clientX - center : 0;
1650
+ drag = { id: event.pointerId, left: box.left, width: box.width, offset };
1651
+ root.dataset.dragging = "";
1652
+ input2.setPointerCapture(event.pointerId);
1653
+ update(event.clientX);
1654
+ };
1655
+ const move = (event) => {
1656
+ if (event.pointerId !== drag?.id) return;
1657
+ event.preventDefault();
1658
+ update(event.clientX);
1659
+ };
1660
+ const release = () => {
1661
+ if (!drag) return;
1662
+ const id = drag.id;
1663
+ drag = null;
1664
+ delete root.dataset.dragging;
1665
+ if (input2.hasPointerCapture(id)) input2.releasePointerCapture(id);
1666
+ };
1667
+ const up = (event) => {
1668
+ if (event.pointerId !== drag?.id) return;
1669
+ update(event.clientX);
1670
+ release();
1671
+ };
1672
+ input2.addEventListener("pointerdown", down);
1673
+ input2.addEventListener("pointermove", move);
1674
+ input2.addEventListener("pointerup", up);
1675
+ input2.addEventListener("pointercancel", release);
1676
+ input2.addEventListener("lostpointercapture", release);
1677
+ window.addEventListener("blur", release);
1678
+ return () => {
1679
+ release();
1680
+ input2.removeEventListener("pointerdown", down);
1681
+ input2.removeEventListener("pointermove", move);
1682
+ input2.removeEventListener("pointerup", up);
1683
+ input2.removeEventListener("pointercancel", release);
1684
+ input2.removeEventListener("lostpointercapture", release);
1685
+ window.removeEventListener("blur", release);
1686
+ };
1687
+ }, [min, max, step2, showThumb]);
1583
1688
  const strength = Math.max(1, Math.min(10, intensity)) / 5;
1584
1689
  const [mirrored, setMirrored] = import_react11.default.useState(false);
1585
1690
  (0, import_react11.useEffect)(() => setMirrored(!canRefractBackdrop()), []);
@@ -1604,6 +1709,7 @@ var GlassSlider = import_react11.default.forwardRef(
1604
1709
  thumb.style.scale = `${s * (1 + v.stretch)} ${s * (1 - v.stretch * 0.5)}`;
1605
1710
  face.style.opacity = String(1 - clamp(v.lift, 0, 1) * 0.92);
1606
1711
  root.style.setProperty("--lg-lift", String(clamp(v.lift, 0, 1)));
1712
+ if (v.lift < 0.01 && lift.target === 0) delete root.dataset.lifted;
1607
1713
  };
1608
1714
  const lift = createSpring(0, SPRINGS.lift, (x) => {
1609
1715
  v.lift = x;
@@ -1620,6 +1726,7 @@ var GlassSlider = import_react11.default.forwardRef(
1620
1726
  if (input2.disabled) return;
1621
1727
  lastX = event.clientX;
1622
1728
  lastT = event.timeStamp;
1729
+ root.dataset.lifted = "";
1623
1730
  lift.tune(SPRINGS.lift);
1624
1731
  lift.set(1);
1625
1732
  haptic();
@@ -1645,6 +1752,7 @@ var GlassSlider = import_react11.default.forwardRef(
1645
1752
  window.addEventListener("pointerup", up);
1646
1753
  window.addEventListener("pointercancel", up);
1647
1754
  input2.addEventListener("blur", up);
1755
+ window.addEventListener("blur", up);
1648
1756
  return () => {
1649
1757
  lift.stop();
1650
1758
  stretch.stop();
@@ -1653,6 +1761,8 @@ var GlassSlider = import_react11.default.forwardRef(
1653
1761
  window.removeEventListener("pointerup", up);
1654
1762
  window.removeEventListener("pointercancel", up);
1655
1763
  input2.removeEventListener("blur", up);
1764
+ window.removeEventListener("blur", up);
1765
+ delete root.dataset.lifted;
1656
1766
  };
1657
1767
  }, [showThumb]);
1658
1768
  (0, import_react11.useEffect)(() => {
@@ -1743,12 +1853,12 @@ var GlassSlider = import_react11.default.forwardRef(
1743
1853
  /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)("span", { className: "lg-slider__block-content", "aria-hidden": "true", children: [
1744
1854
  icon && /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("span", { className: "lg-slider__icon", children: icon }),
1745
1855
  label2 && /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("span", { className: "lg-slider__label", children: label2 }),
1746
- /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("span", { className: "lg-slider__value", children: Number(value) })
1856
+ /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("span", { className: "lg-slider__value", children: Math.round(Number(value)) })
1747
1857
  ] }),
1748
1858
  /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("span", { className: "lg-slider__block-fill", "aria-hidden": "true", children: /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)("span", { className: "lg-slider__block-content lg-slider__block-content--lit", children: [
1749
1859
  icon && /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("span", { className: "lg-slider__icon", children: icon }),
1750
1860
  label2 && /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("span", { className: "lg-slider__label", children: label2 }),
1751
- /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("span", { className: "lg-slider__value", children: Number(value) })
1861
+ /* @__PURE__ */ (0, import_jsx_runtime9.jsx)("span", { className: "lg-slider__value", children: Math.round(Number(value)) })
1752
1862
  ] }) })
1753
1863
  ] }),
1754
1864
  input
@@ -1813,7 +1923,6 @@ var GlassSwitch = import_react12.default.forwardRef(
1813
1923
  const el2 = ref.current;
1814
1924
  const thumb = thumbRef.current;
1815
1925
  const fill = fillRef.current;
1816
- const face = faceRef.current;
1817
1926
  const v = { x: checked ? 1 : 0, lift: 0, stretch: 0 };
1818
1927
  const paint = () => {
1819
1928
  const s = REST + (1 - REST) * v.lift;
@@ -1821,8 +1930,8 @@ var GlassSwitch = import_react12.default.forwardRef(
1821
1930
  const sy = s * (1 - v.stretch * 0.5);
1822
1931
  thumb.style.transform = `translate(${v.x * TRAVEL}px, -50%) scale(${sx}, ${sy})`;
1823
1932
  const l = clamp(v.lift, 0, 1);
1824
- face.style.opacity = String(1 - l * 0.92);
1825
1933
  el2.style.setProperty("--lg-lift", String(l));
1934
+ el2.toggleAttribute("data-lifted", l > 0.01);
1826
1935
  };
1827
1936
  const position = createSpring(v.x, SPRINGS.snap, (value, velocity) => {
1828
1937
  v.x = value;