@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.mjs CHANGED
@@ -33,6 +33,12 @@ function nextTick(fn) {
33
33
  });
34
34
  };
35
35
  }
36
+ function raf(fn) {
37
+ const id = globalThis.requestAnimationFrame(fn);
38
+ return function cleanup() {
39
+ globalThis.cancelAnimationFrame(id);
40
+ };
41
+ }
36
42
  var noop = () => {
37
43
  };
38
44
  var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
@@ -212,6 +218,30 @@ function getEventStep(event) {
212
218
  return isSkipKey ? 10 : 1;
213
219
  }
214
220
  }
221
+ function itemById(v, id) {
222
+ return v.find((node) => node.id === id);
223
+ }
224
+ function indexOfId(v, id) {
225
+ const item = itemById(v, id);
226
+ return item ? v.indexOf(item) : -1;
227
+ }
228
+ var getValueText = (item) => {
229
+ var _a, _b;
230
+ return (_b = (_a = item.dataset.valuetext) != null ? _a : item.textContent) != null ? _b : "";
231
+ };
232
+ var match = (valueText, query2) => valueText.toLowerCase().startsWith(query2.toLowerCase());
233
+ var wrap = (v, idx) => {
234
+ return v.map((_, index) => v[(Math.max(idx, 0) + index) % v.length]);
235
+ };
236
+ function findByText(v, text, currentId) {
237
+ const index = currentId ? indexOfId(v, currentId) : -1;
238
+ let items = currentId ? wrap(v, index) : v;
239
+ const isSingleKey = text.length === 1;
240
+ if (isSingleKey) {
241
+ items = items.filter((item) => item.id !== currentId);
242
+ }
243
+ return items.find((item) => match(getValueText(item), text));
244
+ }
215
245
  var state = "default";
216
246
  var savedUserSelect = "";
217
247
  var modifiedElementMap = /* @__PURE__ */ new WeakMap();
@@ -275,6 +305,34 @@ function trackPointerMove(opts) {
275
305
  };
276
306
  return pipe(addPointerEvent(doc, "pointermove", handlePointerMove, false), addPointerEvent(doc, "pointerup", onPointerUp, false), addPointerEvent(doc, "pointercancel", onPointerUp, false), addPointerEvent(doc, "contextmenu", onPointerUp, false), disableTextSelection({ doc }));
277
307
  }
308
+ function findByTypeahead(_items, options) {
309
+ const { state: state2, activeId, key, timeout = 350 } = options;
310
+ const search = state2.keysSoFar + key;
311
+ const isRepeated = search.length > 1 && Array.from(search).every((char) => char === search[0]);
312
+ const query2 = isRepeated ? search[0] : search;
313
+ let items = _items.slice();
314
+ const next = findByText(items, query2, activeId);
315
+ function cleanup() {
316
+ clearTimeout(state2.timer);
317
+ state2.timer = -1;
318
+ }
319
+ function update(value) {
320
+ state2.keysSoFar = value;
321
+ cleanup();
322
+ if (value !== "") {
323
+ state2.timer = +setTimeout(() => {
324
+ update("");
325
+ cleanup();
326
+ }, timeout);
327
+ }
328
+ }
329
+ update(search);
330
+ return next;
331
+ }
332
+ findByTypeahead.defaultOptions = {
333
+ keysSoFar: "",
334
+ timer: -1
335
+ };
278
336
 
279
337
  // ../../types/dist/index.mjs
280
338
  function createNormalizer(fn) {
@@ -288,6 +346,10 @@ var dom = {
288
346
  var _a;
289
347
  return (_a = ctx.doc) != null ? _a : document;
290
348
  },
349
+ getRootNode: (ctx) => {
350
+ var _a;
351
+ return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
352
+ },
291
353
  getRootId: (ctx) => {
292
354
  var _a, _b;
293
355
  return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `splitter:${ctx.uid}`;
@@ -312,8 +374,8 @@ var dom = {
312
374
  var _a, _b;
313
375
  return (_b = (_a = ctx.ids) == null ? void 0 : _a.secondaryPane) != null ? _b : `splitter:${ctx.uid}:secondary`;
314
376
  },
315
- getSplitterEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getSplitterId(ctx)),
316
- getPrimaryPaneEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getPrimaryPaneId(ctx)),
377
+ getSplitterEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getSplitterId(ctx)),
378
+ getPrimaryPaneEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getPrimaryPaneId(ctx)),
317
379
  getCursor(ctx) {
318
380
  if (ctx.disabled || ctx.fixed)
319
381
  return "default";
@@ -496,10 +558,6 @@ import { createMachine, guards, ref } from "@zag-js/core";
496
558
 
497
559
  // ../../utilities/number/dist/index.mjs
498
560
  var __pow2 = Math.pow;
499
- var nf = new Intl.NumberFormat("en-US", { style: "decimal" });
500
- function formatter(n) {
501
- return parseFloat(nf.format(n));
502
- }
503
561
  function round(v, t2) {
504
562
  let num = valueOf(v);
505
563
  const p = __pow2(10, t2 != null ? t2 : 10);
@@ -509,12 +567,18 @@ function round(v, t2) {
509
567
  function clamp(v, o) {
510
568
  return Math.min(Math.max(valueOf(v), o.min), o.max);
511
569
  }
512
- function countDecimals(v) {
513
- var _a, _b;
514
- return (_b = (_a = nf.formatToParts(v).find((p) => p.type === "fraction")) == null ? void 0 : _a.value.length) != null ? _b : 0;
570
+ function countDecimals(value) {
571
+ if (!Number.isFinite(value))
572
+ return 0;
573
+ let e = 1, p = 0;
574
+ while (Math.round(value * e) / e !== value) {
575
+ e *= 10;
576
+ p += 1;
577
+ }
578
+ return p;
515
579
  }
516
- var increment = (v, s) => formatter(valueOf(v) + s);
517
- var decrement = (v, s) => formatter(valueOf(v) - s);
580
+ var increment = (v, s) => decimalOperation(valueOf(v), "+", s);
581
+ var decrement = (v, s) => decimalOperation(valueOf(v), "-", s);
518
582
  function snapToStep(value, step) {
519
583
  const num = valueOf(value);
520
584
  const p = countDecimals(step);
@@ -527,6 +591,18 @@ function valueOf(v) {
527
591
  const num = parseFloat(v.toString().replace(/[^\w.-]+/g, ""));
528
592
  return !Number.isNaN(num) ? num : 0;
529
593
  }
594
+ function decimalOperation(a, op, b) {
595
+ let result = op === "+" ? a + b : a - b;
596
+ if (a % 1 !== 0 || b % 1 !== 0) {
597
+ const multiplier = __pow2(10, Math.max(countDecimals(a), countDecimals(b)));
598
+ a = Math.round(a * multiplier);
599
+ b = Math.round(b * multiplier);
600
+ result = op === "+" ? a + b : a - b;
601
+ result /= multiplier;
602
+ }
603
+ return result;
604
+ }
605
+ var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
530
606
 
531
607
  // ../../utilities/rect/dist/index.mjs
532
608
  function relativeToNode(p, el) {
@@ -721,6 +797,8 @@ function machine(ctx = {}) {
721
797
  setupDocument(ctx2, evt) {
722
798
  if (evt.doc)
723
799
  ctx2.doc = ref(evt.doc);
800
+ if (evt.root)
801
+ ctx2.rootNode = ref(evt.root);
724
802
  ctx2.uid = evt.id;
725
803
  },
726
804
  setToMin(ctx2) {
@@ -736,7 +814,7 @@ function machine(ctx = {}) {
736
814
  ctx2.value = clamp(decrement(ctx2.value, evt.step), ctx2);
737
815
  },
738
816
  focusSplitter(ctx2) {
739
- nextTick(() => {
817
+ raf(() => {
740
818
  var _a;
741
819
  return (_a = dom.getSplitterEl(ctx2)) == null ? void 0 : _a.focus();
742
820
  });