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