@zag-js/utils 1.24.2 → 1.26.0

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.d.mts CHANGED
@@ -90,7 +90,7 @@ declare const getValueTransformer: (va: number[], vb: number[]) => (v: number) =
90
90
  declare const toFixedNumber: (v: number, d?: number, b?: number) => number;
91
91
  declare const incrementValue: (v: number, s: number) => number;
92
92
  declare const decrementValue: (v: number, s: number) => number;
93
- declare const toPx: (v: number | undefined) => string | undefined;
93
+ declare const toPx: (v: number | string | undefined) => string | undefined;
94
94
 
95
95
  declare function compact<T extends Record<string, unknown> | undefined>(obj: T): T;
96
96
  declare const json: (v: any) => any;
@@ -111,12 +111,30 @@ interface Store<T extends Record<string, any>> {
111
111
  snapshot: () => T;
112
112
  }
113
113
 
114
- interface RafIntervalOptions {
114
+ interface TimerBaseContext {
115
115
  startMs: number;
116
116
  deltaMs: number;
117
117
  }
118
- declare function setRafInterval(callback: (options: RafIntervalOptions) => void, interval: number): () => void;
119
- declare function setRafTimeout(callback: () => void, delay: number): () => void;
118
+ interface TimerContext extends TimerBaseContext {
119
+ now: number;
120
+ }
121
+ type TimerContextFn = (ctx: TimerContext) => boolean | void;
122
+ declare class Timer {
123
+ #private;
124
+ private readonly onTick;
125
+ private frameId;
126
+ private pausedAtMs;
127
+ private context;
128
+ constructor(onTick: TimerContextFn);
129
+ private cancelFrame;
130
+ setStartMs: (startMs: number) => void;
131
+ get elapsedMs(): number;
132
+ start: () => void;
133
+ pause: () => void;
134
+ stop: () => void;
135
+ }
136
+ declare function setRafInterval(fn: (ctx: TimerBaseContext) => void, intervalMs: number): () => void;
137
+ declare function setRafTimeout(fn: () => void, delayMs: number): () => void;
120
138
 
121
139
  declare function warn(m: string): void;
122
140
  declare function warn(c: boolean, m: string): void;
@@ -126,4 +144,4 @@ declare function ensure<T>(c: T | null | undefined, m: () => string): asserts c
126
144
  type RequiredBy<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
127
145
  declare function ensureProps<T, K extends keyof T>(props: T, keys: K[], scope?: string): asserts props is T & RequiredBy<T, K>;
128
146
 
129
- export { type IndexOptions, type MaybeFunction, type Nullable, type RafIntervalOptions, type Store, type StoreCompareFn, type StoreListener, add, addOrRemove, callAll, cast, chunk, clampPercent, clampValue, clear, compact, createSplitProps, createStore, debounce, decrementValue, diff, ensure, ensureProps, first, flatArray, fromLength, getClosestValue, getClosestValueIndex, getMaxValueAtIndex, getMinValueAtIndex, getNextStepValue, getPercentValue, getPreviousStepValue, getValuePercent, getValueRanges, getValueSetterAtIndex, getValueTransformer, has, hasProp, identity, incrementValue, insertAt, invariant, isArray, isBoolean, isDev, isEmpty, isEqual, isFunction, isNaN, isNull, isNumber, isObject, isObjectLike, isPlainObject, isString, isValueAtMax, isValueAtMin, isValueWithinRange, json, last, match, mod, nan, next, nextIndex, noop, omit, partition, pick, prev, prevIndex, remove, removeAt, roundToDpr, roundToStepPrecision, roundValue, runIfFn, setRafInterval, setRafTimeout, setValueAtIndex, snapValueToStep, splitProps, throttle, toArray, toFixedNumber, toPx, tryCatch, uniq, uuid, warn, wrap };
147
+ export { type IndexOptions, type MaybeFunction, type Nullable, type Store, type StoreCompareFn, type StoreListener, Timer, type TimerBaseContext, type TimerContextFn, add, addOrRemove, callAll, cast, chunk, clampPercent, clampValue, clear, compact, createSplitProps, createStore, debounce, decrementValue, diff, ensure, ensureProps, first, flatArray, fromLength, getClosestValue, getClosestValueIndex, getMaxValueAtIndex, getMinValueAtIndex, getNextStepValue, getPercentValue, getPreviousStepValue, getValuePercent, getValueRanges, getValueSetterAtIndex, getValueTransformer, has, hasProp, identity, incrementValue, insertAt, invariant, isArray, isBoolean, isDev, isEmpty, isEqual, isFunction, isNaN, isNull, isNumber, isObject, isObjectLike, isPlainObject, isString, isValueAtMax, isValueAtMin, isValueWithinRange, json, last, match, mod, nan, next, nextIndex, noop, omit, partition, pick, prev, prevIndex, remove, removeAt, roundToDpr, roundToStepPrecision, roundValue, runIfFn, setRafInterval, setRafTimeout, setValueAtIndex, snapValueToStep, splitProps, throttle, toArray, toFixedNumber, toPx, tryCatch, uniq, uuid, warn, wrap };
package/dist/index.d.ts CHANGED
@@ -90,7 +90,7 @@ declare const getValueTransformer: (va: number[], vb: number[]) => (v: number) =
90
90
  declare const toFixedNumber: (v: number, d?: number, b?: number) => number;
91
91
  declare const incrementValue: (v: number, s: number) => number;
92
92
  declare const decrementValue: (v: number, s: number) => number;
93
- declare const toPx: (v: number | undefined) => string | undefined;
93
+ declare const toPx: (v: number | string | undefined) => string | undefined;
94
94
 
95
95
  declare function compact<T extends Record<string, unknown> | undefined>(obj: T): T;
96
96
  declare const json: (v: any) => any;
@@ -111,12 +111,30 @@ interface Store<T extends Record<string, any>> {
111
111
  snapshot: () => T;
112
112
  }
113
113
 
114
- interface RafIntervalOptions {
114
+ interface TimerBaseContext {
115
115
  startMs: number;
116
116
  deltaMs: number;
117
117
  }
118
- declare function setRafInterval(callback: (options: RafIntervalOptions) => void, interval: number): () => void;
119
- declare function setRafTimeout(callback: () => void, delay: number): () => void;
118
+ interface TimerContext extends TimerBaseContext {
119
+ now: number;
120
+ }
121
+ type TimerContextFn = (ctx: TimerContext) => boolean | void;
122
+ declare class Timer {
123
+ #private;
124
+ private readonly onTick;
125
+ private frameId;
126
+ private pausedAtMs;
127
+ private context;
128
+ constructor(onTick: TimerContextFn);
129
+ private cancelFrame;
130
+ setStartMs: (startMs: number) => void;
131
+ get elapsedMs(): number;
132
+ start: () => void;
133
+ pause: () => void;
134
+ stop: () => void;
135
+ }
136
+ declare function setRafInterval(fn: (ctx: TimerBaseContext) => void, intervalMs: number): () => void;
137
+ declare function setRafTimeout(fn: () => void, delayMs: number): () => void;
120
138
 
121
139
  declare function warn(m: string): void;
122
140
  declare function warn(c: boolean, m: string): void;
@@ -126,4 +144,4 @@ declare function ensure<T>(c: T | null | undefined, m: () => string): asserts c
126
144
  type RequiredBy<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
127
145
  declare function ensureProps<T, K extends keyof T>(props: T, keys: K[], scope?: string): asserts props is T & RequiredBy<T, K>;
128
146
 
129
- export { type IndexOptions, type MaybeFunction, type Nullable, type RafIntervalOptions, type Store, type StoreCompareFn, type StoreListener, add, addOrRemove, callAll, cast, chunk, clampPercent, clampValue, clear, compact, createSplitProps, createStore, debounce, decrementValue, diff, ensure, ensureProps, first, flatArray, fromLength, getClosestValue, getClosestValueIndex, getMaxValueAtIndex, getMinValueAtIndex, getNextStepValue, getPercentValue, getPreviousStepValue, getValuePercent, getValueRanges, getValueSetterAtIndex, getValueTransformer, has, hasProp, identity, incrementValue, insertAt, invariant, isArray, isBoolean, isDev, isEmpty, isEqual, isFunction, isNaN, isNull, isNumber, isObject, isObjectLike, isPlainObject, isString, isValueAtMax, isValueAtMin, isValueWithinRange, json, last, match, mod, nan, next, nextIndex, noop, omit, partition, pick, prev, prevIndex, remove, removeAt, roundToDpr, roundToStepPrecision, roundValue, runIfFn, setRafInterval, setRafTimeout, setValueAtIndex, snapValueToStep, splitProps, throttle, toArray, toFixedNumber, toPx, tryCatch, uniq, uuid, warn, wrap };
147
+ export { type IndexOptions, type MaybeFunction, type Nullable, type Store, type StoreCompareFn, type StoreListener, Timer, type TimerBaseContext, type TimerContextFn, add, addOrRemove, callAll, cast, chunk, clampPercent, clampValue, clear, compact, createSplitProps, createStore, debounce, decrementValue, diff, ensure, ensureProps, first, flatArray, fromLength, getClosestValue, getClosestValueIndex, getMaxValueAtIndex, getMinValueAtIndex, getNextStepValue, getPercentValue, getPreviousStepValue, getValuePercent, getValueRanges, getValueSetterAtIndex, getValueTransformer, has, hasProp, identity, incrementValue, insertAt, invariant, isArray, isBoolean, isDev, isEmpty, isEqual, isFunction, isNaN, isNull, isNumber, isObject, isObjectLike, isPlainObject, isString, isValueAtMax, isValueAtMin, isValueWithinRange, json, last, match, mod, nan, next, nextIndex, noop, omit, partition, pick, prev, prevIndex, remove, removeAt, roundToDpr, roundToStepPrecision, roundValue, runIfFn, setRafInterval, setRafTimeout, setValueAtIndex, snapValueToStep, splitProps, throttle, toArray, toFixedNumber, toPx, tryCatch, uniq, uuid, warn, wrap };
package/dist/index.js CHANGED
@@ -1,5 +1,15 @@
1
1
  'use strict';
2
2
 
3
+ var __defProp = Object.defineProperty;
4
+ var __typeError = (msg) => {
5
+ throw TypeError(msg);
6
+ };
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
9
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
10
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), member.get(obj));
11
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
12
+
3
13
  // src/array.ts
4
14
  function toArray(v) {
5
15
  if (v == null) return [];
@@ -322,7 +332,7 @@ var decimalOp = (a, op, b) => {
322
332
  };
323
333
  var incrementValue = (v, s) => decimalOp(nan(v), "+", s);
324
334
  var decrementValue = (v, s) => decimalOp(nan(v), "-", s);
325
- var toPx = (v) => v != null ? `${v}px` : void 0;
335
+ var toPx = (v) => typeof v === "number" ? `${v}px` : v;
326
336
 
327
337
  // src/object.ts
328
338
  function compact(obj) {
@@ -414,33 +424,83 @@ function createStore(initialState, compare = Object.is) {
414
424
  }
415
425
 
416
426
  // src/timers.ts
417
- function setRafInterval(callback, interval) {
418
- let start = performance.now();
419
- let handle;
420
- function loop(now) {
421
- const delta = now - start;
422
- if (delta >= interval) {
423
- start = interval > 0 ? now - delta % interval : now;
424
- callback({ startMs: start, deltaMs: delta });
427
+ var currentTime = () => performance.now();
428
+ var _tick;
429
+ var Timer = class {
430
+ constructor(onTick) {
431
+ this.onTick = onTick;
432
+ __publicField(this, "frameId", null);
433
+ __publicField(this, "pausedAtMs", null);
434
+ __publicField(this, "context");
435
+ __publicField(this, "cancelFrame", () => {
436
+ if (this.frameId === null) return;
437
+ cancelAnimationFrame(this.frameId);
438
+ this.frameId = null;
439
+ });
440
+ __publicField(this, "setStartMs", (startMs) => {
441
+ this.context.startMs = startMs;
442
+ });
443
+ __publicField(this, "start", () => {
444
+ if (this.frameId !== null) return;
445
+ const now = currentTime();
446
+ if (this.pausedAtMs !== null) {
447
+ this.context.startMs += now - this.pausedAtMs;
448
+ this.pausedAtMs = null;
449
+ } else {
450
+ this.context.startMs = now;
451
+ }
452
+ this.frameId = requestAnimationFrame(__privateGet(this, _tick));
453
+ });
454
+ __publicField(this, "pause", () => {
455
+ if (this.frameId === null) return;
456
+ this.cancelFrame();
457
+ this.pausedAtMs = currentTime();
458
+ });
459
+ __publicField(this, "stop", () => {
460
+ if (this.frameId === null) return;
461
+ this.cancelFrame();
462
+ this.pausedAtMs = null;
463
+ });
464
+ __privateAdd(this, _tick, (now) => {
465
+ this.context.now = now;
466
+ this.context.deltaMs = now - this.context.startMs;
467
+ const shouldContinue = this.onTick(this.context);
468
+ if (shouldContinue === false) {
469
+ this.stop();
470
+ return;
471
+ }
472
+ this.frameId = requestAnimationFrame(__privateGet(this, _tick));
473
+ });
474
+ this.context = { now: 0, startMs: currentTime(), deltaMs: 0 };
475
+ }
476
+ get elapsedMs() {
477
+ if (this.pausedAtMs !== null) {
478
+ return this.pausedAtMs - this.context.startMs;
425
479
  }
426
- handle = requestAnimationFrame(loop);
480
+ return currentTime() - this.context.startMs;
427
481
  }
428
- handle = requestAnimationFrame(loop);
429
- return () => cancelAnimationFrame(handle);
482
+ };
483
+ _tick = new WeakMap();
484
+ function setRafInterval(fn, intervalMs) {
485
+ const timer = new Timer(({ now, deltaMs }) => {
486
+ if (deltaMs >= intervalMs) {
487
+ const startMs = intervalMs > 0 ? now - deltaMs % intervalMs : now;
488
+ timer.setStartMs(startMs);
489
+ fn({ startMs, deltaMs });
490
+ }
491
+ });
492
+ timer.start();
493
+ return () => timer.stop();
430
494
  }
431
- function setRafTimeout(callback, delay) {
432
- const start = performance.now();
433
- let handle;
434
- function loop(now) {
435
- const delta = now - start;
436
- if (delta >= delay) {
437
- callback();
438
- return;
495
+ function setRafTimeout(fn, delayMs) {
496
+ const timer = new Timer(({ deltaMs }) => {
497
+ if (deltaMs >= delayMs) {
498
+ fn();
499
+ return false;
439
500
  }
440
- handle = requestAnimationFrame(loop);
441
- }
442
- handle = requestAnimationFrame(loop);
443
- return () => cancelAnimationFrame(handle);
501
+ });
502
+ timer.start();
503
+ return () => timer.stop();
444
504
  }
445
505
 
446
506
  // src/warning.ts
@@ -470,6 +530,7 @@ function ensureProps(props, keys, scope) {
470
530
  throw new Error(`[zag-js${scope ? ` > ${scope}` : ""}] missing required props: ${missingKeys.join(", ")}`);
471
531
  }
472
532
 
533
+ exports.Timer = Timer;
473
534
  exports.add = add;
474
535
  exports.addOrRemove = addOrRemove;
475
536
  exports.callAll = callAll;
package/dist/index.mjs CHANGED
@@ -1,3 +1,13 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __typeError = (msg) => {
3
+ throw TypeError(msg);
4
+ };
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
8
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), member.get(obj));
9
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
10
+
1
11
  // src/array.ts
2
12
  function toArray(v) {
3
13
  if (v == null) return [];
@@ -320,7 +330,7 @@ var decimalOp = (a, op, b) => {
320
330
  };
321
331
  var incrementValue = (v, s) => decimalOp(nan(v), "+", s);
322
332
  var decrementValue = (v, s) => decimalOp(nan(v), "-", s);
323
- var toPx = (v) => v != null ? `${v}px` : void 0;
333
+ var toPx = (v) => typeof v === "number" ? `${v}px` : v;
324
334
 
325
335
  // src/object.ts
326
336
  function compact(obj) {
@@ -412,33 +422,83 @@ function createStore(initialState, compare = Object.is) {
412
422
  }
413
423
 
414
424
  // src/timers.ts
415
- function setRafInterval(callback, interval) {
416
- let start = performance.now();
417
- let handle;
418
- function loop(now) {
419
- const delta = now - start;
420
- if (delta >= interval) {
421
- start = interval > 0 ? now - delta % interval : now;
422
- callback({ startMs: start, deltaMs: delta });
425
+ var currentTime = () => performance.now();
426
+ var _tick;
427
+ var Timer = class {
428
+ constructor(onTick) {
429
+ this.onTick = onTick;
430
+ __publicField(this, "frameId", null);
431
+ __publicField(this, "pausedAtMs", null);
432
+ __publicField(this, "context");
433
+ __publicField(this, "cancelFrame", () => {
434
+ if (this.frameId === null) return;
435
+ cancelAnimationFrame(this.frameId);
436
+ this.frameId = null;
437
+ });
438
+ __publicField(this, "setStartMs", (startMs) => {
439
+ this.context.startMs = startMs;
440
+ });
441
+ __publicField(this, "start", () => {
442
+ if (this.frameId !== null) return;
443
+ const now = currentTime();
444
+ if (this.pausedAtMs !== null) {
445
+ this.context.startMs += now - this.pausedAtMs;
446
+ this.pausedAtMs = null;
447
+ } else {
448
+ this.context.startMs = now;
449
+ }
450
+ this.frameId = requestAnimationFrame(__privateGet(this, _tick));
451
+ });
452
+ __publicField(this, "pause", () => {
453
+ if (this.frameId === null) return;
454
+ this.cancelFrame();
455
+ this.pausedAtMs = currentTime();
456
+ });
457
+ __publicField(this, "stop", () => {
458
+ if (this.frameId === null) return;
459
+ this.cancelFrame();
460
+ this.pausedAtMs = null;
461
+ });
462
+ __privateAdd(this, _tick, (now) => {
463
+ this.context.now = now;
464
+ this.context.deltaMs = now - this.context.startMs;
465
+ const shouldContinue = this.onTick(this.context);
466
+ if (shouldContinue === false) {
467
+ this.stop();
468
+ return;
469
+ }
470
+ this.frameId = requestAnimationFrame(__privateGet(this, _tick));
471
+ });
472
+ this.context = { now: 0, startMs: currentTime(), deltaMs: 0 };
473
+ }
474
+ get elapsedMs() {
475
+ if (this.pausedAtMs !== null) {
476
+ return this.pausedAtMs - this.context.startMs;
423
477
  }
424
- handle = requestAnimationFrame(loop);
478
+ return currentTime() - this.context.startMs;
425
479
  }
426
- handle = requestAnimationFrame(loop);
427
- return () => cancelAnimationFrame(handle);
480
+ };
481
+ _tick = new WeakMap();
482
+ function setRafInterval(fn, intervalMs) {
483
+ const timer = new Timer(({ now, deltaMs }) => {
484
+ if (deltaMs >= intervalMs) {
485
+ const startMs = intervalMs > 0 ? now - deltaMs % intervalMs : now;
486
+ timer.setStartMs(startMs);
487
+ fn({ startMs, deltaMs });
488
+ }
489
+ });
490
+ timer.start();
491
+ return () => timer.stop();
428
492
  }
429
- function setRafTimeout(callback, delay) {
430
- const start = performance.now();
431
- let handle;
432
- function loop(now) {
433
- const delta = now - start;
434
- if (delta >= delay) {
435
- callback();
436
- return;
493
+ function setRafTimeout(fn, delayMs) {
494
+ const timer = new Timer(({ deltaMs }) => {
495
+ if (deltaMs >= delayMs) {
496
+ fn();
497
+ return false;
437
498
  }
438
- handle = requestAnimationFrame(loop);
439
- }
440
- handle = requestAnimationFrame(loop);
441
- return () => cancelAnimationFrame(handle);
499
+ });
500
+ timer.start();
501
+ return () => timer.stop();
442
502
  }
443
503
 
444
504
  // src/warning.ts
@@ -468,4 +528,4 @@ function ensureProps(props, keys, scope) {
468
528
  throw new Error(`[zag-js${scope ? ` > ${scope}` : ""}] missing required props: ${missingKeys.join(", ")}`);
469
529
  }
470
530
 
471
- export { add, addOrRemove, callAll, cast, chunk, clampPercent, clampValue, clear, compact, createSplitProps, createStore, debounce, decrementValue, diff, ensure, ensureProps, first, flatArray, fromLength, getClosestValue, getClosestValueIndex, getMaxValueAtIndex, getMinValueAtIndex, getNextStepValue, getPercentValue, getPreviousStepValue, getValuePercent, getValueRanges, getValueSetterAtIndex, getValueTransformer, has, hasProp, identity, incrementValue, insertAt, invariant, isArray, isBoolean, isDev, isEmpty, isEqual, isFunction, isNaN, isNull, isNumber, isObject, isObjectLike, isPlainObject, isString, isValueAtMax, isValueAtMin, isValueWithinRange, json, last, match, mod, nan, next, nextIndex, noop, omit, partition, pick, prev, prevIndex, remove, removeAt, roundToDpr, roundToStepPrecision, roundValue, runIfFn, setRafInterval, setRafTimeout, setValueAtIndex, snapValueToStep, splitProps, throttle, toArray, toFixedNumber, toPx, tryCatch, uniq, uuid, warn, wrap };
531
+ export { Timer, add, addOrRemove, callAll, cast, chunk, clampPercent, clampValue, clear, compact, createSplitProps, createStore, debounce, decrementValue, diff, ensure, ensureProps, first, flatArray, fromLength, getClosestValue, getClosestValueIndex, getMaxValueAtIndex, getMinValueAtIndex, getNextStepValue, getPercentValue, getPreviousStepValue, getValuePercent, getValueRanges, getValueSetterAtIndex, getValueTransformer, has, hasProp, identity, incrementValue, insertAt, invariant, isArray, isBoolean, isDev, isEmpty, isEqual, isFunction, isNaN, isNull, isNumber, isObject, isObjectLike, isPlainObject, isString, isValueAtMax, isValueAtMin, isValueWithinRange, json, last, match, mod, nan, next, nextIndex, noop, omit, partition, pick, prev, prevIndex, remove, removeAt, roundToDpr, roundToStepPrecision, roundValue, runIfFn, setRafInterval, setRafTimeout, setValueAtIndex, snapValueToStep, splitProps, throttle, toArray, toFixedNumber, toPx, tryCatch, uniq, uuid, warn, wrap };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/utils",
3
- "version": "1.24.2",
3
+ "version": "1.26.0",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "js",