@zag-js/number-input 0.1.11 → 0.1.14

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
@@ -1,23 +1,3 @@
1
- var __defProp = Object.defineProperty;
2
- var __defProps = Object.defineProperties;
3
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
- var __spreadValues = (a, b) => {
9
- for (var prop in b || (b = {}))
10
- if (__hasOwnProp.call(b, prop))
11
- __defNormalProp(a, prop, b[prop]);
12
- if (__getOwnPropSymbols)
13
- for (var prop of __getOwnPropSymbols(b)) {
14
- if (__propIsEnum.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- }
17
- return a;
18
- };
19
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
-
21
1
  // ../../utilities/dom/dist/index.mjs
22
2
  var dataAttr = (guard) => {
23
3
  return guard ? "" : void 0;
@@ -28,7 +8,7 @@ var ariaAttr = (guard) => {
28
8
  var MAX_Z_INDEX = 2147483647;
29
9
  var runIfFn = (v, ...a) => {
30
10
  const res = typeof v === "function" ? v(...a) : v;
31
- return res != null ? res : void 0;
11
+ return res ?? void 0;
32
12
  };
33
13
  var callAll = (...fns) => (...a) => {
34
14
  fns.forEach(function(fn) {
@@ -40,9 +20,8 @@ var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
40
20
  var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
41
21
  var isDom = () => typeof window !== "undefined";
42
22
  function getPlatform() {
43
- var _a;
44
23
  const agent = navigator.userAgentData;
45
- return (_a = agent == null ? void 0 : agent.platform) != null ? _a : navigator.platform;
24
+ return (agent == null ? void 0 : agent.platform) ?? navigator.platform;
46
25
  }
47
26
  var pt = (v) => isDom() && v.test(getPlatform());
48
27
  var vn = (v) => isDom() && v.test(navigator.vendor);
@@ -51,29 +30,42 @@ var isMac = () => pt(/^Mac/) && !isTouchDevice;
51
30
  var isSafari = () => isApple() && vn(/apple/i);
52
31
  var isApple = () => pt(/mac|iphone|ipad|ipod/i);
53
32
  var isIos = () => isApple() && !isMac();
33
+ function isDocument(el) {
34
+ return el.nodeType === Node.DOCUMENT_NODE;
35
+ }
36
+ function isWindow(value) {
37
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
38
+ }
39
+ function getDocument(el) {
40
+ if (isWindow(el))
41
+ return el.document;
42
+ if (isDocument(el))
43
+ return el;
44
+ return (el == null ? void 0 : el.ownerDocument) ?? document;
45
+ }
46
+ function defineDomHelpers(helpers) {
47
+ const dom2 = {
48
+ getRootNode: (ctx) => {
49
+ var _a;
50
+ return ((_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) ?? document;
51
+ },
52
+ getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
53
+ getWin: (ctx) => dom2.getDoc(ctx).defaultView ?? window,
54
+ getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
55
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
56
+ };
57
+ return {
58
+ ...dom2,
59
+ ...helpers
60
+ };
61
+ }
54
62
  function getNativeEvent(e) {
55
- var _a;
56
- return (_a = e.nativeEvent) != null ? _a : e;
63
+ return e.nativeEvent ?? e;
57
64
  }
58
65
  var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
59
66
  var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
60
67
  var isLeftClick = (v) => v.button === 0;
61
68
  var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
62
- function observeAttributes(node, attributes, fn) {
63
- if (!node)
64
- return;
65
- const attrs = Array.isArray(attributes) ? attributes : [attributes];
66
- const win = node.ownerDocument.defaultView || window;
67
- const obs = new win.MutationObserver((changes) => {
68
- for (const change of changes) {
69
- if (change.type === "attributes" && change.attributeName && attrs.includes(change.attributeName)) {
70
- fn(change);
71
- }
72
- }
73
- });
74
- obs.observe(node, { attributes: true, attributeFilter: attrs });
75
- return () => obs.disconnect();
76
- }
77
69
  var fallback = {
78
70
  pageX: 0,
79
71
  pageY: 0,
@@ -81,8 +73,7 @@ var fallback = {
81
73
  clientY: 0
82
74
  };
83
75
  function getEventPoint(event, type = "page") {
84
- var _a, _b;
85
- const point = isTouchEvent(event) ? (_b = (_a = event.touches[0]) != null ? _a : event.changedTouches[0]) != null ? _b : fallback : event;
76
+ const point = isTouchEvent(event) ? event.touches[0] ?? event.changedTouches[0] ?? fallback : event;
86
77
  return { x: point[`${type}X`], y: point[`${type}Y`] };
87
78
  }
88
79
  var PAGE_KEYS = /* @__PURE__ */ new Set(["PageUp", "PageDown"]);
@@ -104,6 +95,21 @@ function addDomEvent(target, eventName, handler, options) {
104
95
  node == null ? void 0 : node.removeEventListener(eventName, handler, options);
105
96
  };
106
97
  }
98
+ function observeAttributes(node, attributes, fn) {
99
+ if (!node)
100
+ return;
101
+ const attrs = Array.isArray(attributes) ? attributes : [attributes];
102
+ const win = node.ownerDocument.defaultView || window;
103
+ const obs = new win.MutationObserver((changes) => {
104
+ for (const change of changes) {
105
+ if (change.type === "attributes" && change.attributeName && attrs.includes(change.attributeName)) {
106
+ fn(change);
107
+ }
108
+ }
109
+ });
110
+ obs.observe(node, { attributes: true, attributeFilter: attrs });
111
+ return () => obs.disconnect();
112
+ }
107
113
  function raf(fn) {
108
114
  const id = globalThis.requestAnimationFrame(fn);
109
115
  return function cleanup() {
@@ -142,7 +148,10 @@ function requestPointerLock(doc, handlers = {}) {
142
148
  if (!supported)
143
149
  return;
144
150
  body.requestPointerLock();
145
- const cleanup = callAll(addPointerlockChangeListener(doc, onPointerChange), addPointerlockErrorListener(doc, onPointerError));
151
+ const cleanup = callAll(
152
+ addPointerlockChangeListener(doc, onPointerChange),
153
+ addPointerlockErrorListener(doc, onPointerError)
154
+ );
146
155
  return function dispose() {
147
156
  if (!supported)
148
157
  return;
@@ -152,7 +161,6 @@ function requestPointerLock(doc, handlers = {}) {
152
161
  }
153
162
 
154
163
  // ../../utilities/number/dist/index.mjs
155
- var __pow = Math.pow;
156
164
  function wrap(num, max) {
157
165
  return (num % max + max) % max;
158
166
  }
@@ -206,7 +214,7 @@ function isWithinRange(v, o) {
206
214
  function decimalOperation(a, op, b) {
207
215
  let result = op === "+" ? a + b : a - b;
208
216
  if (a % 1 !== 0 || b % 1 !== 0) {
209
- const multiplier = __pow(10, Math.max(countDecimals(a), countDecimals(b)));
217
+ const multiplier = 10 ** Math.max(countDecimals(a), countDecimals(b));
210
218
  a = Math.round(a * multiplier);
211
219
  b = Math.round(b * multiplier);
212
220
  result = op === "+" ? a + b : a - b;
@@ -217,49 +225,75 @@ function decimalOperation(a, op, b) {
217
225
  var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
218
226
 
219
227
  // src/number-input.dom.ts
220
- var dom = {
221
- getDoc: (ctx) => {
222
- var _a;
223
- return (_a = ctx.doc) != null ? _a : document;
224
- },
225
- getWin: (ctx) => {
226
- var _a;
227
- return (_a = dom.getDoc(ctx).defaultView) != null ? _a : window;
228
- },
229
- getRootNode: (ctx) => {
230
- var _a;
231
- return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
232
- },
228
+ var dom = defineDomHelpers({
233
229
  getRootId: (ctx) => {
234
- var _a, _b;
235
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `number-input:${ctx.uid}`;
230
+ var _a;
231
+ return ((_a = ctx.ids) == null ? void 0 : _a.root) ?? `number-input:${ctx.id}`;
236
232
  },
237
233
  getInputId: (ctx) => {
238
- var _a, _b;
239
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.input) != null ? _b : `number-input:${ctx.uid}:input`;
234
+ var _a;
235
+ return ((_a = ctx.ids) == null ? void 0 : _a.input) ?? `number-input:${ctx.id}:input`;
240
236
  },
241
237
  getIncButtonId: (ctx) => {
242
- var _a, _b;
243
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.incBtn) != null ? _b : `number-input:${ctx.uid}:inc-btn`;
238
+ var _a;
239
+ return ((_a = ctx.ids) == null ? void 0 : _a.incBtn) ?? `number-input:${ctx.id}:inc-btn`;
244
240
  },
245
241
  getDecButtonId: (ctx) => {
246
- var _a, _b;
247
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.decBtn) != null ? _b : `number-input:${ctx.uid}:dec-btn`;
242
+ var _a;
243
+ return ((_a = ctx.ids) == null ? void 0 : _a.decBtn) ?? `number-input:${ctx.id}:dec-btn`;
248
244
  },
249
245
  getScrubberId: (ctx) => {
250
- var _a, _b;
251
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.scrubber) != null ? _b : `number-input:${ctx.uid}:scrubber`;
246
+ var _a;
247
+ return ((_a = ctx.ids) == null ? void 0 : _a.scrubber) ?? `number-input:${ctx.id}:scrubber`;
252
248
  },
253
- getCursorId: (ctx) => `number-input:${ctx.uid}:cursor`,
249
+ getCursorId: (ctx) => `number-input:${ctx.id}:cursor`,
254
250
  getLabelId: (ctx) => {
255
- var _a, _b;
256
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `number-input:${ctx.uid}:label`;
251
+ var _a;
252
+ return ((_a = ctx.ids) == null ? void 0 : _a.label) ?? `number-input:${ctx.id}:label`;
257
253
  },
258
- getInputEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getInputId(ctx)),
259
- getIncButtonEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getIncButtonId(ctx)),
260
- getDecButtonEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getDecButtonId(ctx)),
261
- getScrubberEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getScrubberId(ctx)),
254
+ getInputEl: (ctx) => dom.getById(ctx, dom.getInputId(ctx)),
255
+ getIncButtonEl: (ctx) => dom.getById(ctx, dom.getIncButtonId(ctx)),
256
+ getDecButtonEl: (ctx) => dom.getById(ctx, dom.getDecButtonId(ctx)),
257
+ getScrubberEl: (ctx) => dom.getById(ctx, dom.getScrubberId(ctx)),
262
258
  getCursorEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getCursorId(ctx)),
259
+ getActiveButton: (ctx, hint = ctx.hint) => {
260
+ let btnEl = null;
261
+ if (hint === "increment") {
262
+ btnEl = dom.getIncButtonEl(ctx);
263
+ }
264
+ if (hint === "decrement") {
265
+ btnEl = dom.getDecButtonEl(ctx);
266
+ }
267
+ return btnEl;
268
+ },
269
+ setupVirtualCursor(ctx) {
270
+ if (isSafari() || !supportsPointerEvent())
271
+ return;
272
+ dom.createVirtualCursor(ctx);
273
+ return () => {
274
+ var _a;
275
+ (_a = dom.getCursorEl(ctx)) == null ? void 0 : _a.remove();
276
+ };
277
+ },
278
+ preventTextSelection(ctx) {
279
+ const doc = dom.getDoc(ctx);
280
+ const html = doc.documentElement;
281
+ const body = doc.body;
282
+ body.style.pointerEvents = "none";
283
+ html.style.userSelect = "none";
284
+ html.style.cursor = "ew-resize";
285
+ return () => {
286
+ body.style.pointerEvents = "";
287
+ html.style.userSelect = "";
288
+ html.style.cursor = "";
289
+ if (!html.style.length) {
290
+ html.removeAttribute("style");
291
+ }
292
+ if (!body.style.length) {
293
+ body.removeAttribute("style");
294
+ }
295
+ };
296
+ },
263
297
  getMousementValue(ctx, event) {
264
298
  const x = roundToDevicePixel(event.movementX);
265
299
  const y = roundToDevicePixel(event.movementY);
@@ -303,44 +337,43 @@ var dom = {
303
337
  </svg>`;
304
338
  doc.body.appendChild(el);
305
339
  }
306
- };
340
+ });
307
341
 
308
342
  // src/number-input.utils.ts
309
343
  var utils = {
310
344
  isValidNumericEvent: (ctx, event) => {
311
- var _a, _b;
345
+ var _a;
312
346
  if (event.key == null)
313
347
  return true;
314
348
  const isModifier = isModifiedEvent(event);
315
349
  const isSingleKey = event.key.length === 1;
316
350
  if (isModifier || !isSingleKey)
317
351
  return true;
318
- return (_b = (_a = ctx.validateCharacter) == null ? void 0 : _a.call(ctx, event.key)) != null ? _b : utils.isFloatingPoint(event.key);
352
+ return ((_a = ctx.validateCharacter) == null ? void 0 : _a.call(ctx, event.key)) ?? utils.isFloatingPoint(event.key);
319
353
  },
320
354
  isFloatingPoint: (v) => /^[Ee0-9+\-.]$/.test(v),
321
355
  sanitize: (ctx, value) => {
322
- var _a;
323
- return value.split("").filter((_a = ctx.validateCharacter) != null ? _a : utils.isFloatingPoint).join("");
356
+ return value.split("").filter(ctx.validateCharacter ?? utils.isFloatingPoint).join("");
324
357
  },
325
358
  increment: (ctx, step) => {
326
- const value = increment(ctx.value, step != null ? step : ctx.step);
359
+ const value = increment(ctx.value, step ?? ctx.step);
327
360
  return formatDecimal(clamp(value, ctx), ctx);
328
361
  },
329
362
  decrement: (ctx, step) => {
330
- const value = decrement(ctx.value, step != null ? step : ctx.step);
363
+ const value = decrement(ctx.value, step ?? ctx.step);
331
364
  return formatDecimal(clamp(value, ctx), ctx);
332
365
  },
333
366
  clamp: (ctx) => {
334
367
  return formatDecimal(clamp(ctx.value, ctx), ctx);
335
368
  },
336
369
  parse: (ctx, value) => {
337
- var _a, _b;
338
- return (_b = (_a = ctx.parse) == null ? void 0 : _a.call(ctx, value)) != null ? _b : value;
370
+ var _a;
371
+ return ((_a = ctx.parse) == null ? void 0 : _a.call(ctx, value)) ?? value;
339
372
  },
340
373
  format: (ctx, value) => {
341
- var _a, _b;
374
+ var _a;
342
375
  const _val = value.toString();
343
- return (_b = (_a = ctx.format) == null ? void 0 : _a.call(ctx, _val)) != null ? _b : _val;
376
+ return ((_a = ctx.format) == null ? void 0 : _a.call(ctx, _val)) ?? _val;
344
377
  },
345
378
  round: (ctx) => {
346
379
  return formatDecimal(ctx.value, ctx);
@@ -384,6 +417,10 @@ function connect(state, send, normalize) {
384
417
  var _a;
385
418
  (_a = dom.getInputEl(state.context)) == null ? void 0 : _a.focus();
386
419
  },
420
+ blur() {
421
+ var _a;
422
+ (_a = dom.getInputEl(state.context)) == null ? void 0 : _a.blur();
423
+ },
387
424
  rootProps: normalize.element({
388
425
  id: dom.getRootId(state.context),
389
426
  "data-part": "root",
@@ -538,7 +575,7 @@ function connect(state, send, normalize) {
538
575
  }
539
576
 
540
577
  // src/number-input.machine.ts
541
- import { choose, createMachine, guards, ref } from "@zag-js/core";
578
+ import { choose, createMachine, guards } from "@zag-js/core";
542
579
 
543
580
  // ../../utilities/core/dist/index.mjs
544
581
  var callAll2 = (...fns) => (...a) => {
@@ -549,392 +586,383 @@ var callAll2 = (...fns) => (...a) => {
549
586
 
550
587
  // src/number-input.machine.ts
551
588
  var { not, and } = guards;
552
- function machine(ctx = {}) {
553
- return createMachine({
554
- id: "number-input",
555
- initial: "unknown",
556
- context: __spreadProps(__spreadValues({
557
- uid: "",
558
- dir: "ltr",
559
- focusInputOnChange: true,
560
- clampValueOnBlur: true,
561
- allowOverflow: false,
562
- inputMode: "decimal",
563
- pattern: "[0-9]*(.[0-9]+)?",
564
- hint: null,
565
- value: "",
566
- step: 1,
567
- min: Number.MIN_SAFE_INTEGER,
568
- max: Number.MAX_SAFE_INTEGER,
569
- scrubberCursorPoint: null,
570
- invalid: false
571
- }, ctx), {
572
- messages: __spreadValues({
573
- incrementLabel: "increment value",
574
- decrementLabel: "decrease value"
575
- }, ctx.messages)
576
- }),
577
- computed: {
578
- isRtl: (ctx2) => ctx2.dir === "rtl",
579
- valueAsNumber: (ctx2) => valueOf(ctx2.value),
580
- isAtMin: (ctx2) => isAtMin(ctx2.value, ctx2),
581
- isAtMax: (ctx2) => isAtMax(ctx2.value, ctx2),
582
- isOutOfRange: (ctx2) => !isWithinRange(ctx2.value, ctx2),
583
- isValueEmpty: (ctx2) => ctx2.value === "",
584
- canIncrement: (ctx2) => ctx2.allowOverflow || !ctx2.isAtMax,
585
- canDecrement: (ctx2) => ctx2.allowOverflow || !ctx2.isAtMin,
586
- valueText: (ctx2) => {
587
- var _a, _b;
588
- return (_b = (_a = ctx2.messages).valueText) == null ? void 0 : _b.call(_a, ctx2.value);
589
- },
590
- formattedValue: (ctx2) => {
591
- var _a, _b;
592
- return (_b = (_a = ctx2.format) == null ? void 0 : _a.call(ctx2, ctx2.value).toString()) != null ? _b : ctx2.value;
593
- }
594
- },
595
- watch: {
596
- value: ["invokeOnChange"],
597
- isOutOfRange: ["invokeOnInvalid"]
598
- },
599
- on: {
600
- SET_VALUE: {
601
- actions: ["setValue", "setHintToSet"]
602
- },
603
- CLEAR_VALUE: {
604
- actions: ["clearValue"]
605
- },
606
- INCREMENT: {
607
- actions: ["increment"]
589
+ function machine(ctx) {
590
+ return createMachine(
591
+ {
592
+ id: "number-input",
593
+ initial: "unknown",
594
+ context: {
595
+ dir: "ltr",
596
+ focusInputOnChange: true,
597
+ clampValueOnBlur: true,
598
+ allowOverflow: false,
599
+ inputMode: "decimal",
600
+ pattern: "[0-9]*(.[0-9]+)?",
601
+ hint: null,
602
+ value: "",
603
+ step: 1,
604
+ min: Number.MIN_SAFE_INTEGER,
605
+ max: Number.MAX_SAFE_INTEGER,
606
+ scrubberCursorPoint: null,
607
+ invalid: false,
608
+ spinOnPress: true,
609
+ ...ctx,
610
+ messages: {
611
+ incrementLabel: "increment value",
612
+ decrementLabel: "decrease value",
613
+ ...ctx.messages
614
+ }
608
615
  },
609
- DECREMENT: {
610
- actions: ["decrement"]
611
- }
612
- },
613
- states: {
614
- unknown: {
615
- on: {
616
- SETUP: {
617
- target: "idle",
618
- actions: ["setupDocument", "syncInputValue"]
619
- }
616
+ computed: {
617
+ isRtl: (ctx2) => ctx2.dir === "rtl",
618
+ valueAsNumber: (ctx2) => valueOf(ctx2.value),
619
+ isAtMin: (ctx2) => isAtMin(ctx2.value, ctx2),
620
+ isAtMax: (ctx2) => isAtMax(ctx2.value, ctx2),
621
+ isOutOfRange: (ctx2) => !isWithinRange(ctx2.value, ctx2),
622
+ isValueEmpty: (ctx2) => ctx2.value === "",
623
+ canIncrement: (ctx2) => ctx2.allowOverflow || !ctx2.isAtMax,
624
+ canDecrement: (ctx2) => ctx2.allowOverflow || !ctx2.isAtMin,
625
+ valueText: (ctx2) => {
626
+ var _a, _b;
627
+ return (_b = (_a = ctx2.messages).valueText) == null ? void 0 : _b.call(_a, ctx2.value);
628
+ },
629
+ formattedValue: (ctx2) => {
630
+ var _a;
631
+ return ((_a = ctx2.format) == null ? void 0 : _a.call(ctx2, ctx2.value).toString()) ?? ctx2.value;
620
632
  }
621
633
  },
622
- idle: {
623
- on: {
624
- PRESS_DOWN: {
625
- target: "before:spin",
626
- actions: ["focusInput", "setHint"]
627
- },
628
- PRESS_DOWN_SCRUBBER: {
629
- target: "scrubbing",
630
- actions: ["focusInput", "setHint", "setCursorPoint"]
631
- },
632
- FOCUS: "focused"
634
+ watch: {
635
+ value: ["invokeOnChange"],
636
+ isOutOfRange: ["invokeOnInvalid"],
637
+ scrubberCursorPoint: ["setVirtualCursorPosition"]
638
+ },
639
+ on: {
640
+ SET_VALUE: {
641
+ actions: ["setValue", "setHintToSet"]
642
+ },
643
+ CLEAR_VALUE: {
644
+ actions: ["clearValue"]
645
+ },
646
+ INCREMENT: {
647
+ actions: ["increment"]
648
+ },
649
+ DECREMENT: {
650
+ actions: ["decrement"]
633
651
  }
634
652
  },
635
- focused: {
636
- tags: ["focus"],
637
- entry: "focusInput",
638
- activities: "attachWheelListener",
639
- on: {
640
- PRESS_DOWN: {
641
- target: "before:spin",
642
- actions: ["focusInput", "setHint"]
643
- },
644
- PRESS_DOWN_SCRUBBER: {
645
- target: "scrubbing",
646
- actions: ["focusInput", "setHint", "setCursorPoint"]
647
- },
648
- ARROW_UP: {
649
- actions: "increment"
650
- },
651
- ARROW_DOWN: {
652
- actions: "decrement"
653
- },
654
- HOME: {
655
- actions: "setToMin"
656
- },
657
- END: {
658
- actions: "setToMax"
659
- },
660
- CHANGE: {
661
- actions: ["setValue", "setHint"]
662
- },
663
- BLUR: [
664
- {
665
- guard: "isInvalidExponential",
653
+ states: {
654
+ unknown: {
655
+ on: {
656
+ SETUP: {
666
657
  target: "idle",
667
- actions: ["clearValue", "clearHint"]
658
+ actions: "syncInputValue"
659
+ }
660
+ }
661
+ },
662
+ idle: {
663
+ exit: "invokeOnFocus",
664
+ on: {
665
+ PRESS_DOWN: {
666
+ target: "before:spin",
667
+ actions: ["focusInput", "setHint"]
668
668
  },
669
- {
670
- guard: and("clampOnBlur", not("isInRange"), not("isEmptyValue")),
671
- target: "idle",
672
- actions: ["clampValue", "clearHint"]
669
+ PRESS_DOWN_SCRUBBER: {
670
+ target: "scrubbing",
671
+ actions: ["focusInput", "setHint", "setCursorPoint"]
673
672
  },
674
- {
675
- target: "idle",
676
- actions: "roundValue"
677
- }
678
- ]
679
- }
680
- },
681
- "before:spin": {
682
- tags: ["focus"],
683
- activities: "trackButtonDisabled",
684
- entry: choose([
685
- { guard: "isIncrementHint", actions: "increment" },
686
- { guard: "isDecrementHint", actions: "decrement" }
687
- ]),
688
- after: {
689
- CHANGE_DELAY: {
690
- target: "spinning",
691
- guard: "isInRange"
673
+ FOCUS: "focused"
692
674
  }
693
675
  },
694
- on: {
695
- PRESS_UP: {
696
- target: "focused",
697
- actions: "clearHint"
676
+ focused: {
677
+ tags: "focus",
678
+ entry: "focusInput",
679
+ activities: "attachWheelListener",
680
+ on: {
681
+ PRESS_DOWN: {
682
+ target: "before:spin",
683
+ actions: ["focusInput", "setHint"]
684
+ },
685
+ PRESS_DOWN_SCRUBBER: {
686
+ target: "scrubbing",
687
+ actions: ["focusInput", "setHint", "setCursorPoint"]
688
+ },
689
+ ARROW_UP: {
690
+ actions: "increment"
691
+ },
692
+ ARROW_DOWN: {
693
+ actions: "decrement"
694
+ },
695
+ HOME: {
696
+ actions: "setToMin"
697
+ },
698
+ END: {
699
+ actions: "setToMax"
700
+ },
701
+ CHANGE: {
702
+ actions: ["setValue", "setHint"]
703
+ },
704
+ BLUR: [
705
+ {
706
+ guard: "isInvalidExponential",
707
+ target: "idle",
708
+ actions: ["clearValue", "clearHint", "invokeOnBlur"]
709
+ },
710
+ {
711
+ guard: and("clampOnBlur", not("isInRange"), not("isEmptyValue")),
712
+ target: "idle",
713
+ actions: ["clampValue", "clearHint", "invokeOnBlur"]
714
+ },
715
+ {
716
+ target: "idle",
717
+ actions: ["roundValue", "invokeOnBlur"]
718
+ }
719
+ ]
698
720
  }
699
- }
700
- },
701
- spinning: {
702
- tags: ["focus"],
703
- activities: "trackButtonDisabled",
704
- every: [
705
- {
706
- delay: "CHANGE_INTERVAL",
707
- guard: and(not("isAtMin"), "isIncrementHint"),
708
- actions: "increment"
721
+ },
722
+ "before:spin": {
723
+ tags: "focus",
724
+ activities: "trackButtonDisabled",
725
+ entry: choose([
726
+ { guard: "isIncrementHint", actions: "increment" },
727
+ { guard: "isDecrementHint", actions: "decrement" }
728
+ ]),
729
+ after: {
730
+ CHANGE_DELAY: {
731
+ target: "spinning",
732
+ guard: and("isInRange", "spinOnPress")
733
+ }
709
734
  },
710
- {
711
- delay: "CHANGE_INTERVAL",
712
- guard: and(not("isAtMax"), "isDecrementHint"),
713
- actions: "decrement"
714
- }
715
- ],
716
- on: {
717
- PRESS_UP: {
718
- target: "focused",
719
- actions: "clearHint"
735
+ on: {
736
+ PRESS_UP: {
737
+ target: "focused",
738
+ actions: "clearHint"
739
+ }
720
740
  }
721
- }
722
- },
723
- scrubbing: {
724
- tags: ["focus"],
725
- entry: ["addCustomCursor", "disableTextSelection"],
726
- exit: ["removeCustomCursor", "restoreTextSelection"],
727
- activities: ["activatePointerLock", "trackMousemove"],
728
- on: {
729
- POINTER_UP_SCRUBBER: {
730
- target: "focused",
731
- actions: "clearCursorPoint"
732
- },
733
- POINTER_MOVE_SCRUBBER: [
741
+ },
742
+ spinning: {
743
+ tags: "focus",
744
+ activities: "trackButtonDisabled",
745
+ every: [
734
746
  {
735
- guard: "isIncrementHint",
736
- actions: ["increment", "setCursorPoint", "updateCursor"]
747
+ delay: "CHANGE_INTERVAL",
748
+ guard: and(not("isAtMin"), "isIncrementHint"),
749
+ actions: "increment"
737
750
  },
738
751
  {
739
- guard: "isDecrementHint",
740
- actions: ["decrement", "setCursorPoint", "updateCursor"]
752
+ delay: "CHANGE_INTERVAL",
753
+ guard: and(not("isAtMax"), "isDecrementHint"),
754
+ actions: "decrement"
755
+ }
756
+ ],
757
+ on: {
758
+ PRESS_UP: {
759
+ target: "focused",
760
+ actions: "clearHint"
741
761
  }
742
- ]
762
+ }
763
+ },
764
+ scrubbing: {
765
+ tags: "focus",
766
+ exit: "clearCursorPoint",
767
+ activities: ["activatePointerLock", "trackMousemove", "setupVirtualCursor", "preventTextSelection"],
768
+ on: {
769
+ POINTER_UP_SCRUBBER: "focused",
770
+ POINTER_MOVE_SCRUBBER: [
771
+ {
772
+ guard: "isIncrementHint",
773
+ actions: ["increment", "setCursorPoint"]
774
+ },
775
+ {
776
+ guard: "isDecrementHint",
777
+ actions: ["decrement", "setCursorPoint"]
778
+ }
779
+ ]
780
+ }
743
781
  }
744
782
  }
745
- }
746
- }, {
747
- delays: {
748
- CHANGE_INTERVAL: 50,
749
- CHANGE_DELAY: 300
750
783
  },
751
- guards: {
752
- clampOnBlur: (ctx2) => !!ctx2.clampValueOnBlur,
753
- isAtMin: (ctx2) => ctx2.isAtMin,
754
- isAtMax: (ctx2) => ctx2.isAtMax,
755
- isInRange: (ctx2) => !ctx2.isOutOfRange,
756
- isDecrementHint: (ctx2, evt) => {
757
- var _a;
758
- return ((_a = evt.hint) != null ? _a : ctx2.hint) === "decrement";
759
- },
760
- isEmptyValue: (ctx2) => ctx2.isValueEmpty,
761
- isIncrementHint: (ctx2, evt) => {
762
- var _a;
763
- return ((_a = evt.hint) != null ? _a : ctx2.hint) === "increment";
764
- },
765
- isInvalidExponential: (ctx2) => ctx2.value.toString().startsWith("e")
766
- },
767
- activities: {
768
- trackButtonDisabled(ctx2, _evt, { send }) {
769
- let btnEl = null;
770
- if (ctx2.hint === "increment") {
771
- btnEl = dom.getIncButtonEl(ctx2);
772
- }
773
- if (ctx2.hint === "decrement") {
774
- btnEl = dom.getDecButtonEl(ctx2);
775
- }
776
- return observeAttributes(btnEl, "disabled", function onDisable() {
777
- send("PRESS_UP");
778
- });
779
- },
780
- attachWheelListener(ctx2, _evt, { send }) {
781
- const input = dom.getInputEl(ctx2);
782
- if (!input)
783
- return;
784
- function onWheel(event) {
785
- const isInputFocused = dom.getDoc(ctx2).activeElement === input;
786
- if (!ctx2.allowMouseWheel || !isInputFocused)
784
+ {
785
+ delays: {
786
+ CHANGE_INTERVAL: 50,
787
+ CHANGE_DELAY: 300
788
+ },
789
+ guards: {
790
+ clampOnBlur: (ctx2) => !!ctx2.clampValueOnBlur,
791
+ isAtMin: (ctx2) => ctx2.isAtMin,
792
+ spinOnPress: (ctx2) => !!ctx2.spinOnPress,
793
+ isAtMax: (ctx2) => ctx2.isAtMax,
794
+ isInRange: (ctx2) => !ctx2.isOutOfRange,
795
+ isDecrementHint: (ctx2, evt) => (evt.hint ?? ctx2.hint) === "decrement",
796
+ isEmptyValue: (ctx2) => ctx2.isValueEmpty,
797
+ isIncrementHint: (ctx2, evt) => (evt.hint ?? ctx2.hint) === "increment",
798
+ isInvalidExponential: (ctx2) => ctx2.value.toString().startsWith("e")
799
+ },
800
+ activities: {
801
+ setupVirtualCursor(ctx2) {
802
+ return dom.setupVirtualCursor(ctx2);
803
+ },
804
+ preventTextSelection(ctx2) {
805
+ return dom.preventTextSelection(ctx2);
806
+ },
807
+ trackButtonDisabled(ctx2, _evt, { send }) {
808
+ const btn = dom.getActiveButton(ctx2, ctx2.hint);
809
+ return observeAttributes(btn, "disabled", () => send("PRESS_UP"));
810
+ },
811
+ attachWheelListener(ctx2, _evt, { send }) {
812
+ const input = dom.getInputEl(ctx2);
813
+ if (!input)
787
814
  return;
788
- event.preventDefault();
789
- const dir = Math.sign(event.deltaY) * -1;
790
- if (dir === 1) {
791
- send("INCREMENT");
792
- } else if (dir === -1) {
793
- send("DECREMENT");
815
+ function onWheel(event) {
816
+ const isInputFocused = dom.getDoc(ctx2).activeElement === input;
817
+ if (!ctx2.allowMouseWheel || !isInputFocused)
818
+ return;
819
+ event.preventDefault();
820
+ const dir = Math.sign(event.deltaY) * -1;
821
+ if (dir === 1) {
822
+ send("INCREMENT");
823
+ } else if (dir === -1) {
824
+ send("DECREMENT");
825
+ }
826
+ }
827
+ return addDomEvent(input, "wheel", onWheel, { passive: false });
828
+ },
829
+ activatePointerLock(ctx2) {
830
+ if (isSafari() || !supportsPointerEvent())
831
+ return;
832
+ return requestPointerLock(dom.getDoc(ctx2));
833
+ },
834
+ trackMousemove(ctx2, _evt, { send }) {
835
+ const doc = dom.getDoc(ctx2);
836
+ function onMousemove(event) {
837
+ if (!ctx2.scrubberCursorPoint)
838
+ return;
839
+ const value = dom.getMousementValue(ctx2, event);
840
+ if (!value.hint)
841
+ return;
842
+ send({
843
+ type: "POINTER_MOVE_SCRUBBER",
844
+ hint: value.hint,
845
+ point: value.point
846
+ });
847
+ }
848
+ function onMouseup() {
849
+ send("POINTER_UP_SCRUBBER");
794
850
  }
851
+ return callAll2(
852
+ addDomEvent(doc, "mousemove", onMousemove, false),
853
+ addDomEvent(doc, "mouseup", onMouseup, false)
854
+ );
795
855
  }
796
- return addDomEvent(input, "wheel", onWheel, { passive: false });
797
- },
798
- activatePointerLock(ctx2) {
799
- if (isSafari() || !supportsPointerEvent())
800
- return;
801
- return requestPointerLock(dom.getDoc(ctx2));
802
856
  },
803
- trackMousemove(ctx2, _evt, { send }) {
804
- const doc = dom.getDoc(ctx2);
805
- function onMousemove(event) {
806
- if (!ctx2.scrubberCursorPoint)
857
+ actions: {
858
+ focusInput(ctx2) {
859
+ if (!ctx2.focusInputOnChange)
807
860
  return;
808
- const value = dom.getMousementValue(ctx2, event);
809
- if (!value.hint)
861
+ const input = dom.getInputEl(ctx2);
862
+ raf(() => input == null ? void 0 : input.focus());
863
+ },
864
+ increment(ctx2, evt) {
865
+ ctx2.value = utils.increment(ctx2, evt.step);
866
+ },
867
+ decrement(ctx2, evt) {
868
+ ctx2.value = utils.decrement(ctx2, evt.step);
869
+ },
870
+ clampValue(ctx2) {
871
+ ctx2.value = utils.clamp(ctx2);
872
+ },
873
+ roundValue(ctx2) {
874
+ if (ctx2.value !== "") {
875
+ ctx2.value = utils.round(ctx2);
876
+ }
877
+ },
878
+ setValue(ctx2, evt) {
879
+ var _a;
880
+ const value = ((_a = evt.target) == null ? void 0 : _a.value) ?? evt.value;
881
+ ctx2.value = utils.sanitize(ctx2, utils.parse(ctx2, value.toString()));
882
+ },
883
+ clearValue(ctx2) {
884
+ ctx2.value = "";
885
+ },
886
+ setToMax(ctx2) {
887
+ ctx2.value = ctx2.max.toString();
888
+ },
889
+ setToMin(ctx2) {
890
+ ctx2.value = ctx2.min.toString();
891
+ },
892
+ setHint(ctx2, evt) {
893
+ ctx2.hint = evt.hint;
894
+ },
895
+ clearHint(ctx2) {
896
+ ctx2.hint = null;
897
+ },
898
+ setHintToSet(ctx2) {
899
+ ctx2.hint = "set";
900
+ },
901
+ invokeOnChange(ctx2) {
902
+ var _a;
903
+ (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, {
904
+ value: ctx2.value,
905
+ valueAsNumber: ctx2.valueAsNumber
906
+ });
907
+ },
908
+ invokeOnFocus(ctx2, evt) {
909
+ var _a;
910
+ let srcElement = null;
911
+ if (evt.type === "PRESS_DOWN") {
912
+ srcElement = dom.getActiveButton(ctx2, evt.hint);
913
+ } else if (evt.type === "FOCUS") {
914
+ srcElement = dom.getInputEl(ctx2);
915
+ } else if (evt.type === "PRESS_DOWN_SCRUBBER") {
916
+ srcElement = dom.getScrubberEl(ctx2);
917
+ }
918
+ (_a = ctx2.onFocus) == null ? void 0 : _a.call(ctx2, {
919
+ value: ctx2.value,
920
+ valueAsNumber: ctx2.valueAsNumber,
921
+ srcElement
922
+ });
923
+ },
924
+ invokeOnBlur(ctx2) {
925
+ var _a;
926
+ (_a = ctx2.onBlur) == null ? void 0 : _a.call(ctx2, {
927
+ value: ctx2.value,
928
+ valueAsNumber: ctx2.valueAsNumber
929
+ });
930
+ },
931
+ invokeOnInvalid(ctx2) {
932
+ var _a;
933
+ if (!ctx2.isOutOfRange)
810
934
  return;
811
- send({
812
- type: "POINTER_MOVE_SCRUBBER",
813
- hint: value.hint,
814
- point: value.point
935
+ const reason = ctx2.valueAsNumber > ctx2.max ? "rangeOverflow" : "rangeUnderflow";
936
+ (_a = ctx2.onInvalid) == null ? void 0 : _a.call(ctx2, {
937
+ reason,
938
+ value: ctx2.formattedValue,
939
+ valueAsNumber: ctx2.valueAsNumber
815
940
  });
941
+ },
942
+ syncInputValue(ctx2) {
943
+ const input = dom.getInputEl(ctx2);
944
+ if (!input || input.value == ctx2.value)
945
+ return;
946
+ const value = utils.parse(ctx2, input.value);
947
+ ctx2.value = utils.sanitize(ctx2, value);
948
+ },
949
+ setCursorPoint(ctx2, evt) {
950
+ ctx2.scrubberCursorPoint = evt.point;
951
+ },
952
+ clearCursorPoint(ctx2) {
953
+ ctx2.scrubberCursorPoint = null;
954
+ },
955
+ setVirtualCursorPosition(ctx2) {
956
+ const cursor = dom.getCursorEl(ctx2);
957
+ if (!cursor || !ctx2.scrubberCursorPoint)
958
+ return;
959
+ const { x, y } = ctx2.scrubberCursorPoint;
960
+ cursor.style.transform = `translate3d(${x}px, ${y}px, 0px)`;
816
961
  }
817
- function onMouseup() {
818
- send("POINTER_UP_SCRUBBER");
819
- }
820
- return callAll2(addDomEvent(doc, "mousemove", onMousemove, false), addDomEvent(doc, "mouseup", onMouseup, false));
821
- }
822
- },
823
- actions: {
824
- setupDocument: (ctx2, evt) => {
825
- if (evt.doc)
826
- ctx2.doc = ref(evt.doc);
827
- if (evt.root)
828
- ctx2.rootNode = ref(evt.root);
829
- ctx2.uid = evt.id;
830
- },
831
- focusInput(ctx2) {
832
- if (!ctx2.focusInputOnChange)
833
- return;
834
- const input = dom.getInputEl(ctx2);
835
- raf(() => input == null ? void 0 : input.focus());
836
- },
837
- increment(ctx2, evt) {
838
- ctx2.value = utils.increment(ctx2, evt.step);
839
- },
840
- decrement(ctx2, evt) {
841
- ctx2.value = utils.decrement(ctx2, evt.step);
842
- },
843
- clampValue(ctx2) {
844
- ctx2.value = utils.clamp(ctx2);
845
- },
846
- roundValue(ctx2) {
847
- if (ctx2.value !== "") {
848
- ctx2.value = utils.round(ctx2);
849
- }
850
- },
851
- setValue(ctx2, evt) {
852
- var _a, _b;
853
- const value = (_b = (_a = evt.target) == null ? void 0 : _a.value) != null ? _b : evt.value;
854
- ctx2.value = utils.sanitize(ctx2, utils.parse(ctx2, value.toString()));
855
- },
856
- clearValue(ctx2) {
857
- ctx2.value = "";
858
- },
859
- setToMax(ctx2) {
860
- ctx2.value = ctx2.max.toString();
861
- },
862
- setToMin(ctx2) {
863
- ctx2.value = ctx2.min.toString();
864
- },
865
- setHint(ctx2, evt) {
866
- ctx2.hint = evt.hint;
867
- },
868
- clearHint(ctx2) {
869
- ctx2.hint = null;
870
- },
871
- setHintToSet(ctx2) {
872
- ctx2.hint = "set";
873
- },
874
- invokeOnChange(ctx2) {
875
- var _a;
876
- (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, {
877
- value: ctx2.value,
878
- valueAsNumber: ctx2.valueAsNumber
879
- });
880
- },
881
- invokeOnInvalid(ctx2) {
882
- var _a;
883
- if (!ctx2.isOutOfRange)
884
- return;
885
- const reason = ctx2.valueAsNumber > ctx2.max ? "rangeOverflow" : "rangeUnderflow";
886
- (_a = ctx2.onInvalid) == null ? void 0 : _a.call(ctx2, {
887
- reason,
888
- value: ctx2.formattedValue,
889
- valueAsNumber: ctx2.valueAsNumber
890
- });
891
- },
892
- syncInputValue(ctx2) {
893
- const input = dom.getInputEl(ctx2);
894
- if (!input || input.value == ctx2.value)
895
- return;
896
- const value = utils.parse(ctx2, input.value);
897
- ctx2.value = utils.sanitize(ctx2, value);
898
962
  },
899
- setCursorPoint(ctx2, evt) {
900
- ctx2.scrubberCursorPoint = evt.point;
901
- },
902
- clearCursorPoint(ctx2) {
903
- ctx2.scrubberCursorPoint = null;
904
- },
905
- updateCursor(ctx2) {
906
- const cursor = dom.getCursorEl(ctx2);
907
- if (!cursor || !ctx2.scrubberCursorPoint)
908
- return;
909
- const { x, y } = ctx2.scrubberCursorPoint;
910
- cursor.style.transform = `translate3d(${x}px, ${y}px, 0px)`;
911
- },
912
- addCustomCursor(ctx2) {
913
- if (isSafari() || !supportsPointerEvent())
914
- return;
915
- dom.createVirtualCursor(ctx2);
916
- },
917
- removeCustomCursor(ctx2) {
918
- var _a;
919
- if (isSafari() || !supportsPointerEvent())
920
- return;
921
- (_a = dom.getCursorEl(ctx2)) == null ? void 0 : _a.remove();
922
- },
923
- disableTextSelection(ctx2) {
924
- const doc = dom.getDoc(ctx2);
925
- doc.body.style.pointerEvents = "none";
926
- doc.documentElement.style.userSelect = "none";
927
- doc.documentElement.style.cursor = "ew-resize";
928
- },
929
- restoreTextSelection(ctx2) {
930
- const doc = dom.getDoc(ctx2);
931
- doc.body.style.pointerEvents = "";
932
- doc.documentElement.style.userSelect = "";
933
- doc.documentElement.style.cursor = "";
934
- }
935
- },
936
- hookSync: true
937
- });
963
+ hookSync: true
964
+ }
965
+ );
938
966
  }
939
967
  export {
940
968
  connect,