@zakisheriff/liquid-glass 0.2.5 → 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
1140
  H = Math.round(bar.offsetHeight + overhang);
1138
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
  );