@zag-js/splitter 0.1.1 → 0.1.4

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
@@ -56,6 +56,12 @@ function nextTick(fn) {
56
56
  });
57
57
  };
58
58
  }
59
+ function raf(fn) {
60
+ const id = globalThis.requestAnimationFrame(fn);
61
+ return function cleanup() {
62
+ globalThis.cancelAnimationFrame(id);
63
+ };
64
+ }
59
65
  var noop = () => {
60
66
  };
61
67
  var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
@@ -235,6 +241,30 @@ function getEventStep(event) {
235
241
  return isSkipKey ? 10 : 1;
236
242
  }
237
243
  }
244
+ function itemById(v, id) {
245
+ return v.find((node) => node.id === id);
246
+ }
247
+ function indexOfId(v, id) {
248
+ const item = itemById(v, id);
249
+ return item ? v.indexOf(item) : -1;
250
+ }
251
+ var getValueText = (item) => {
252
+ var _a, _b;
253
+ return (_b = (_a = item.dataset.valuetext) != null ? _a : item.textContent) != null ? _b : "";
254
+ };
255
+ var match = (valueText, query2) => valueText.toLowerCase().startsWith(query2.toLowerCase());
256
+ var wrap = (v, idx) => {
257
+ return v.map((_, index) => v[(Math.max(idx, 0) + index) % v.length]);
258
+ };
259
+ function findByText(v, text, currentId) {
260
+ const index = currentId ? indexOfId(v, currentId) : -1;
261
+ let items = currentId ? wrap(v, index) : v;
262
+ const isSingleKey = text.length === 1;
263
+ if (isSingleKey) {
264
+ items = items.filter((item) => item.id !== currentId);
265
+ }
266
+ return items.find((item) => match(getValueText(item), text));
267
+ }
238
268
  var state = "default";
239
269
  var savedUserSelect = "";
240
270
  var modifiedElementMap = /* @__PURE__ */ new WeakMap();
@@ -298,6 +328,34 @@ function trackPointerMove(opts) {
298
328
  };
299
329
  return pipe(addPointerEvent(doc, "pointermove", handlePointerMove, false), addPointerEvent(doc, "pointerup", onPointerUp, false), addPointerEvent(doc, "pointercancel", onPointerUp, false), addPointerEvent(doc, "contextmenu", onPointerUp, false), disableTextSelection({ doc }));
300
330
  }
331
+ function findByTypeahead(_items, options) {
332
+ const { state: state2, activeId, key, timeout = 350 } = options;
333
+ const search = state2.keysSoFar + key;
334
+ const isRepeated = search.length > 1 && Array.from(search).every((char) => char === search[0]);
335
+ const query2 = isRepeated ? search[0] : search;
336
+ let items = _items.slice();
337
+ const next = findByText(items, query2, activeId);
338
+ function cleanup() {
339
+ clearTimeout(state2.timer);
340
+ state2.timer = -1;
341
+ }
342
+ function update(value) {
343
+ state2.keysSoFar = value;
344
+ cleanup();
345
+ if (value !== "") {
346
+ state2.timer = +setTimeout(() => {
347
+ update("");
348
+ cleanup();
349
+ }, timeout);
350
+ }
351
+ }
352
+ update(search);
353
+ return next;
354
+ }
355
+ findByTypeahead.defaultOptions = {
356
+ keysSoFar: "",
357
+ timer: -1
358
+ };
301
359
 
302
360
  // ../../types/dist/index.mjs
303
361
  function createNormalizer(fn) {
@@ -311,6 +369,10 @@ var dom = {
311
369
  var _a;
312
370
  return (_a = ctx.doc) != null ? _a : document;
313
371
  },
372
+ getRootNode: (ctx) => {
373
+ var _a;
374
+ return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
375
+ },
314
376
  getRootId: (ctx) => {
315
377
  var _a, _b;
316
378
  return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `splitter:${ctx.uid}`;
@@ -335,8 +397,8 @@ var dom = {
335
397
  var _a, _b;
336
398
  return (_b = (_a = ctx.ids) == null ? void 0 : _a.secondaryPane) != null ? _b : `splitter:${ctx.uid}:secondary`;
337
399
  },
338
- getSplitterEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getSplitterId(ctx)),
339
- getPrimaryPaneEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getPrimaryPaneId(ctx)),
400
+ getSplitterEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getSplitterId(ctx)),
401
+ getPrimaryPaneEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getPrimaryPaneId(ctx)),
340
402
  getCursor(ctx) {
341
403
  if (ctx.disabled || ctx.fixed)
342
404
  return "default";
@@ -519,10 +581,6 @@ var import_core = require("@zag-js/core");
519
581
 
520
582
  // ../../utilities/number/dist/index.mjs
521
583
  var __pow2 = Math.pow;
522
- var nf = new Intl.NumberFormat("en-US", { style: "decimal" });
523
- function formatter(n) {
524
- return parseFloat(nf.format(n));
525
- }
526
584
  function round(v, t2) {
527
585
  let num = valueOf(v);
528
586
  const p = __pow2(10, t2 != null ? t2 : 10);
@@ -532,12 +590,18 @@ function round(v, t2) {
532
590
  function clamp(v, o) {
533
591
  return Math.min(Math.max(valueOf(v), o.min), o.max);
534
592
  }
535
- function countDecimals(v) {
536
- var _a, _b;
537
- return (_b = (_a = nf.formatToParts(v).find((p) => p.type === "fraction")) == null ? void 0 : _a.value.length) != null ? _b : 0;
593
+ function countDecimals(value) {
594
+ if (!Number.isFinite(value))
595
+ return 0;
596
+ let e = 1, p = 0;
597
+ while (Math.round(value * e) / e !== value) {
598
+ e *= 10;
599
+ p += 1;
600
+ }
601
+ return p;
538
602
  }
539
- var increment = (v, s) => formatter(valueOf(v) + s);
540
- var decrement = (v, s) => formatter(valueOf(v) - s);
603
+ var increment = (v, s) => decimalOperation(valueOf(v), "+", s);
604
+ var decrement = (v, s) => decimalOperation(valueOf(v), "-", s);
541
605
  function snapToStep(value, step) {
542
606
  const num = valueOf(value);
543
607
  const p = countDecimals(step);
@@ -550,6 +614,18 @@ function valueOf(v) {
550
614
  const num = parseFloat(v.toString().replace(/[^\w.-]+/g, ""));
551
615
  return !Number.isNaN(num) ? num : 0;
552
616
  }
617
+ function decimalOperation(a, op, b) {
618
+ let result = op === "+" ? a + b : a - b;
619
+ if (a % 1 !== 0 || b % 1 !== 0) {
620
+ const multiplier = __pow2(10, Math.max(countDecimals(a), countDecimals(b)));
621
+ a = Math.round(a * multiplier);
622
+ b = Math.round(b * multiplier);
623
+ result = op === "+" ? a + b : a - b;
624
+ result /= multiplier;
625
+ }
626
+ return result;
627
+ }
628
+ var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
553
629
 
554
630
  // ../../utilities/rect/dist/index.mjs
555
631
  function relativeToNode(p, el) {
@@ -744,6 +820,8 @@ function machine(ctx = {}) {
744
820
  setupDocument(ctx2, evt) {
745
821
  if (evt.doc)
746
822
  ctx2.doc = (0, import_core.ref)(evt.doc);
823
+ if (evt.root)
824
+ ctx2.rootNode = (0, import_core.ref)(evt.root);
747
825
  ctx2.uid = evt.id;
748
826
  },
749
827
  setToMin(ctx2) {
@@ -759,7 +837,7 @@ function machine(ctx = {}) {
759
837
  ctx2.value = clamp(decrement(ctx2.value, evt.step), ctx2);
760
838
  },
761
839
  focusSplitter(ctx2) {
762
- nextTick(() => {
840
+ raf(() => {
763
841
  var _a;
764
842
  return (_a = dom.getSplitterEl(ctx2)) == null ? void 0 : _a.focus();
765
843
  });