@zag-js/number-input 0.2.6 → 0.2.8

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.
@@ -0,0 +1,832 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/number-input.machine.ts
21
+ var number_input_machine_exports = {};
22
+ __export(number_input_machine_exports, {
23
+ machine: () => machine
24
+ });
25
+ module.exports = __toCommonJS(number_input_machine_exports);
26
+ var import_core = require("@zag-js/core");
27
+
28
+ // ../../utilities/dom/src/constants.ts
29
+ var MAX_Z_INDEX = 2147483647;
30
+
31
+ // ../../utilities/core/src/functions.ts
32
+ var runIfFn = (v, ...a) => {
33
+ const res = typeof v === "function" ? v(...a) : v;
34
+ return res != null ? res : void 0;
35
+ };
36
+ var callAll = (...fns) => (...a) => {
37
+ fns.forEach(function(fn) {
38
+ fn == null ? void 0 : fn(...a);
39
+ });
40
+ };
41
+
42
+ // ../../utilities/core/src/guard.ts
43
+ var isArray = (v) => Array.isArray(v);
44
+ var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
45
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
46
+
47
+ // ../../utilities/core/src/object.ts
48
+ function compact(obj) {
49
+ if (obj === void 0)
50
+ return obj;
51
+ return Object.fromEntries(
52
+ Object.entries(obj).filter(([, value]) => value !== void 0).map(([key, value]) => [key, isObject(value) ? compact(value) : value])
53
+ );
54
+ }
55
+
56
+ // ../../utilities/dom/src/platform.ts
57
+ var isDom = () => typeof window !== "undefined";
58
+ function getPlatform() {
59
+ var _a;
60
+ const agent = navigator.userAgentData;
61
+ return (_a = agent == null ? void 0 : agent.platform) != null ? _a : navigator.platform;
62
+ }
63
+ var pt = (v) => isDom() && v.test(getPlatform());
64
+ var vn = (v) => isDom() && v.test(navigator.vendor);
65
+ var isSafari = () => isApple() && vn(/apple/i);
66
+ var isApple = () => pt(/mac|iphone|ipad|ipod/i);
67
+
68
+ // ../../utilities/dom/src/query.ts
69
+ function isDocument(el) {
70
+ return el.nodeType === Node.DOCUMENT_NODE;
71
+ }
72
+ function isWindow(value) {
73
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
74
+ }
75
+ function getDocument(el) {
76
+ var _a;
77
+ if (isWindow(el))
78
+ return el.document;
79
+ if (isDocument(el))
80
+ return el;
81
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
82
+ }
83
+ function getWindow(el) {
84
+ var _a;
85
+ return (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
86
+ }
87
+ function defineDomHelpers(helpers) {
88
+ const dom2 = {
89
+ getRootNode: (ctx) => {
90
+ var _a, _b;
91
+ return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
92
+ },
93
+ getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
94
+ getWin: (ctx) => {
95
+ var _a;
96
+ return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
97
+ },
98
+ getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
99
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
100
+ };
101
+ return {
102
+ ...dom2,
103
+ ...helpers
104
+ };
105
+ }
106
+
107
+ // ../../utilities/dom/src/event.ts
108
+ var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
109
+ var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
110
+
111
+ // ../../utilities/dom/src/listener.ts
112
+ var isRef = (v) => hasProp(v, "current");
113
+ function addDomEvent(target, eventName, handler, options) {
114
+ const node = isRef(target) ? target.current : runIfFn(target);
115
+ node == null ? void 0 : node.addEventListener(eventName, handler, options);
116
+ return () => {
117
+ node == null ? void 0 : node.removeEventListener(eventName, handler, options);
118
+ };
119
+ }
120
+
121
+ // ../../utilities/dom/src/mutation-observer.ts
122
+ function observeAttributes(node, attributes, fn) {
123
+ if (!node)
124
+ return;
125
+ const attrs = Array.isArray(attributes) ? attributes : [attributes];
126
+ const win = node.ownerDocument.defaultView || window;
127
+ const obs = new win.MutationObserver((changes) => {
128
+ for (const change of changes) {
129
+ if (change.type === "attributes" && change.attributeName && attrs.includes(change.attributeName)) {
130
+ fn(change);
131
+ }
132
+ }
133
+ });
134
+ obs.observe(node, { attributes: true, attributeFilter: attrs });
135
+ return () => obs.disconnect();
136
+ }
137
+
138
+ // ../../utilities/dom/src/next-tick.ts
139
+ function raf(fn) {
140
+ const id = globalThis.requestAnimationFrame(fn);
141
+ return function cleanup() {
142
+ globalThis.cancelAnimationFrame(id);
143
+ };
144
+ }
145
+
146
+ // ../../utilities/dom/src/pointerlock.ts
147
+ function addPointerlockChangeListener(doc, fn) {
148
+ return addDomEvent(doc, "pointerlockchange", fn, false);
149
+ }
150
+ function addPointerlockErrorListener(doc, fn) {
151
+ doc.addEventListener("pointerlockerror", fn, false);
152
+ return function cleanup() {
153
+ doc.removeEventListener("pointerlockerror", fn, false);
154
+ };
155
+ }
156
+ function requestPointerLock(doc, handlers = {}) {
157
+ const { onPointerLock, onPointerUnlock } = handlers;
158
+ const body = doc.body;
159
+ const supported = "pointerLockElement" in doc || "mozPointerLockElement" in doc;
160
+ const locked = !!doc.pointerLockElement;
161
+ function onPointerChange() {
162
+ if (locked)
163
+ onPointerLock == null ? void 0 : onPointerLock();
164
+ else
165
+ onPointerUnlock == null ? void 0 : onPointerUnlock();
166
+ }
167
+ function onPointerError(event) {
168
+ if (locked)
169
+ onPointerUnlock == null ? void 0 : onPointerUnlock();
170
+ console.error("PointerLock error occured:", event);
171
+ exit();
172
+ }
173
+ function exit() {
174
+ doc.exitPointerLock();
175
+ }
176
+ if (!supported)
177
+ return;
178
+ try {
179
+ body.requestPointerLock();
180
+ } catch {
181
+ }
182
+ const cleanup = callAll(
183
+ addPointerlockChangeListener(doc, onPointerChange),
184
+ addPointerlockErrorListener(doc, onPointerError)
185
+ );
186
+ return function dispose() {
187
+ if (!supported)
188
+ return;
189
+ cleanup();
190
+ exit();
191
+ };
192
+ }
193
+
194
+ // ../../utilities/number/src/number.ts
195
+ function wrap(num, max) {
196
+ return (num % max + max) % max;
197
+ }
198
+ function roundToDevicePixel(num) {
199
+ if (typeof window === "undefined")
200
+ return Math.round(num);
201
+ const dp = window.devicePixelRatio;
202
+ return Math.floor(num * dp + 0.5) / dp;
203
+ }
204
+ function clamp(v, o) {
205
+ return Math.min(Math.max(valueOf(v), o.min), o.max);
206
+ }
207
+ function countDecimals(value) {
208
+ if (!Number.isFinite(value))
209
+ return 0;
210
+ let e = 1, p = 0;
211
+ while (Math.round(value * e) / e !== value) {
212
+ e *= 10;
213
+ p += 1;
214
+ }
215
+ return p;
216
+ }
217
+ var increment = (v, s) => decimalOperation(valueOf(v), "+", s);
218
+ var decrement = (v, s) => decimalOperation(valueOf(v), "-", s);
219
+ function valueOf(v) {
220
+ if (typeof v === "number")
221
+ return v;
222
+ const num = parseFloat(v.toString().replace(/[^\w.-]+/g, ""));
223
+ return !Number.isNaN(num) ? num : 0;
224
+ }
225
+ function formatDecimal(v, o) {
226
+ return new Intl.NumberFormat("en-US", {
227
+ useGrouping: false,
228
+ style: "decimal",
229
+ minimumFractionDigits: o.minFractionDigits,
230
+ maximumFractionDigits: o.maxFractionDigits
231
+ }).format(valueOf(v));
232
+ }
233
+ function isAtMax(v, o) {
234
+ const val = valueOf(v);
235
+ return val >= o.max;
236
+ }
237
+ function isAtMin(v, o) {
238
+ const val = valueOf(v);
239
+ return val <= o.min;
240
+ }
241
+ function isWithinRange(v, o) {
242
+ const val = valueOf(v);
243
+ return val >= o.min && val <= o.max;
244
+ }
245
+ function decimalOperation(a, op, b) {
246
+ let result = op === "+" ? a + b : a - b;
247
+ if (a % 1 !== 0 || b % 1 !== 0) {
248
+ const multiplier = 10 ** Math.max(countDecimals(a), countDecimals(b));
249
+ a = Math.round(a * multiplier);
250
+ b = Math.round(b * multiplier);
251
+ result = op === "+" ? a + b : a - b;
252
+ result /= multiplier;
253
+ }
254
+ return result;
255
+ }
256
+
257
+ // ../../utilities/form-utils/src/input-event.ts
258
+ function getDescriptor(el, options) {
259
+ var _a;
260
+ const { type, property = "value" } = options;
261
+ const proto = getWindow(el)[type].prototype;
262
+ return (_a = Object.getOwnPropertyDescriptor(proto, property)) != null ? _a : {};
263
+ }
264
+ function dispatchInputValueEvent(el, value) {
265
+ var _a;
266
+ if (!el)
267
+ return;
268
+ const win = getWindow(el);
269
+ if (!(el instanceof win.HTMLInputElement))
270
+ return;
271
+ const desc = getDescriptor(el, { type: "HTMLInputElement", property: "value" });
272
+ (_a = desc.set) == null ? void 0 : _a.call(el, value);
273
+ const event = new win.Event("input", { bubbles: true });
274
+ el.dispatchEvent(event);
275
+ }
276
+
277
+ // src/number-input.dom.ts
278
+ var dom = defineDomHelpers({
279
+ getRootId: (ctx) => {
280
+ var _a, _b;
281
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `number-input:${ctx.id}`;
282
+ },
283
+ getInputId: (ctx) => {
284
+ var _a, _b;
285
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.input) != null ? _b : `number-input:${ctx.id}:input`;
286
+ },
287
+ getIncrementTriggerId: (ctx) => {
288
+ var _a, _b;
289
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.incrementTrigger) != null ? _b : `number-input:${ctx.id}:inc`;
290
+ },
291
+ getDecrementTriggerId: (ctx) => {
292
+ var _a, _b;
293
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.decrementTrigger) != null ? _b : `number-input:${ctx.id}:dec`;
294
+ },
295
+ getScrubberId: (ctx) => {
296
+ var _a, _b;
297
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.scrubber) != null ? _b : `number-input:${ctx.id}:scrubber`;
298
+ },
299
+ getCursorId: (ctx) => `number-input:${ctx.id}:cursor`,
300
+ getLabelId: (ctx) => {
301
+ var _a, _b;
302
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `number-input:${ctx.id}:label`;
303
+ },
304
+ getInputEl: (ctx) => dom.getById(ctx, dom.getInputId(ctx)),
305
+ getIncrementTriggerEl: (ctx) => dom.getById(ctx, dom.getIncrementTriggerId(ctx)),
306
+ getDecrementTriggerEl: (ctx) => dom.getById(ctx, dom.getDecrementTriggerId(ctx)),
307
+ getScrubberEl: (ctx) => dom.getById(ctx, dom.getScrubberId(ctx)),
308
+ getCursorEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getCursorId(ctx)),
309
+ getPressedTriggerEl: (ctx, hint = ctx.hint) => {
310
+ let btnEl = null;
311
+ if (hint === "increment") {
312
+ btnEl = dom.getIncrementTriggerEl(ctx);
313
+ }
314
+ if (hint === "decrement") {
315
+ btnEl = dom.getDecrementTriggerEl(ctx);
316
+ }
317
+ return btnEl;
318
+ },
319
+ setupVirtualCursor(ctx) {
320
+ if (isSafari() || !supportsPointerEvent())
321
+ return;
322
+ dom.createVirtualCursor(ctx);
323
+ return () => {
324
+ var _a;
325
+ (_a = dom.getCursorEl(ctx)) == null ? void 0 : _a.remove();
326
+ };
327
+ },
328
+ preventTextSelection(ctx) {
329
+ const doc = dom.getDoc(ctx);
330
+ const html = doc.documentElement;
331
+ const body = doc.body;
332
+ body.style.pointerEvents = "none";
333
+ html.style.userSelect = "none";
334
+ html.style.cursor = "ew-resize";
335
+ return () => {
336
+ body.style.pointerEvents = "";
337
+ html.style.userSelect = "";
338
+ html.style.cursor = "";
339
+ if (!html.style.length) {
340
+ html.removeAttribute("style");
341
+ }
342
+ if (!body.style.length) {
343
+ body.removeAttribute("style");
344
+ }
345
+ };
346
+ },
347
+ getMousementValue(ctx, event) {
348
+ const x = roundToDevicePixel(event.movementX);
349
+ const y = roundToDevicePixel(event.movementY);
350
+ let hint = x > 0 ? "increment" : x < 0 ? "decrement" : null;
351
+ if (ctx.isRtl && hint === "increment")
352
+ hint = "decrement";
353
+ if (ctx.isRtl && hint === "decrement")
354
+ hint = "increment";
355
+ const point = {
356
+ x: ctx.scrubberCursorPoint.x + x,
357
+ y: ctx.scrubberCursorPoint.y + y
358
+ };
359
+ const win = dom.getWin(ctx);
360
+ const width = win.innerWidth;
361
+ const half = roundToDevicePixel(7.5);
362
+ point.x = wrap(point.x + half, width) - half;
363
+ return { hint, point };
364
+ },
365
+ createVirtualCursor(ctx) {
366
+ const doc = dom.getDoc(ctx);
367
+ const el = doc.createElement("div");
368
+ el.className = "scrubber--cursor";
369
+ el.id = dom.getCursorId(ctx);
370
+ Object.assign(el.style, {
371
+ width: "15px",
372
+ height: "15px",
373
+ position: "fixed",
374
+ pointerEvents: "none",
375
+ left: "0px",
376
+ top: "0px",
377
+ zIndex: MAX_Z_INDEX,
378
+ transform: ctx.scrubberCursorPoint ? `translate3d(${ctx.scrubberCursorPoint.x}px, ${ctx.scrubberCursorPoint.y}px, 0px)` : void 0,
379
+ willChange: "transform"
380
+ });
381
+ el.innerHTML = `
382
+ <svg width="46" height="15" style="left: -15.5px; position: absolute; top: 0; filter: drop-shadow(rgba(0, 0, 0, 0.4) 0px 1px 1.1px);">
383
+ <g transform="translate(2 3)">
384
+ <path fill-rule="evenodd" d="M 15 4.5L 15 2L 11.5 5.5L 15 9L 15 6.5L 31 6.5L 31 9L 34.5 5.5L 31 2L 31 4.5Z" style="stroke-width: 2px; stroke: white;"></path>
385
+ <path fill-rule="evenodd" d="M 15 4.5L 15 2L 11.5 5.5L 15 9L 15 6.5L 31 6.5L 31 9L 34.5 5.5L 31 2L 31 4.5Z"></path>
386
+ </g>
387
+ </svg>`;
388
+ doc.body.appendChild(el);
389
+ }
390
+ });
391
+
392
+ // src/number-input.utils.ts
393
+ var utils = {
394
+ isValidNumericEvent: (ctx, event) => {
395
+ var _a, _b;
396
+ if (event.key == null)
397
+ return true;
398
+ const isModifier = isModifiedEvent(event);
399
+ const isSingleKey = event.key.length === 1;
400
+ if (isModifier || !isSingleKey)
401
+ return true;
402
+ return (_b = (_a = ctx.validateCharacter) == null ? void 0 : _a.call(ctx, event.key)) != null ? _b : utils.isFloatingPoint(event.key);
403
+ },
404
+ isFloatingPoint: (v) => /^[0-9+\-.]$/.test(v),
405
+ sanitize: (ctx, value) => {
406
+ var _a;
407
+ return value.split("").filter((_a = ctx.validateCharacter) != null ? _a : utils.isFloatingPoint).join("");
408
+ },
409
+ increment: (ctx, step) => {
410
+ const value = increment(ctx.value, step != null ? step : ctx.step);
411
+ return formatDecimal(clamp(value, ctx), ctx);
412
+ },
413
+ decrement: (ctx, step) => {
414
+ const value = decrement(ctx.value, step != null ? step : ctx.step);
415
+ return formatDecimal(clamp(value, ctx), ctx);
416
+ },
417
+ clamp: (ctx) => {
418
+ return formatDecimal(clamp(ctx.value, ctx), ctx);
419
+ },
420
+ parse: (ctx, value) => {
421
+ var _a, _b;
422
+ return (_b = (_a = ctx.parse) == null ? void 0 : _a.call(ctx, value)) != null ? _b : value;
423
+ },
424
+ format: (ctx, value) => {
425
+ var _a, _b;
426
+ const _val = value.toString();
427
+ return (_b = (_a = ctx.format) == null ? void 0 : _a.call(ctx, _val)) != null ? _b : _val;
428
+ },
429
+ round: (ctx) => {
430
+ return formatDecimal(ctx.value, ctx);
431
+ }
432
+ };
433
+
434
+ // src/number-input.machine.ts
435
+ var { not, and } = import_core.guards;
436
+ function machine(userContext) {
437
+ const ctx = compact(userContext);
438
+ return (0, import_core.createMachine)(
439
+ {
440
+ id: "number-input",
441
+ initial: "unknown",
442
+ context: {
443
+ dir: "ltr",
444
+ focusInputOnChange: true,
445
+ clampValueOnBlur: true,
446
+ allowOverflow: false,
447
+ inputMode: "decimal",
448
+ pattern: "[0-9]*(.[0-9]+)?",
449
+ hint: null,
450
+ value: "",
451
+ step: 1,
452
+ min: Number.MIN_SAFE_INTEGER,
453
+ max: Number.MAX_SAFE_INTEGER,
454
+ scrubberCursorPoint: null,
455
+ invalid: false,
456
+ spinOnPress: true,
457
+ ...ctx,
458
+ translations: {
459
+ incrementLabel: "increment value",
460
+ decrementLabel: "decrease value",
461
+ ...ctx.translations
462
+ }
463
+ },
464
+ computed: {
465
+ isRtl: (ctx2) => ctx2.dir === "rtl",
466
+ valueAsNumber: (ctx2) => valueOf(ctx2.value),
467
+ isAtMin: (ctx2) => isAtMin(ctx2.value, ctx2),
468
+ isAtMax: (ctx2) => isAtMax(ctx2.value, ctx2),
469
+ isOutOfRange: (ctx2) => !isWithinRange(ctx2.value, ctx2),
470
+ isValueEmpty: (ctx2) => ctx2.value === "",
471
+ canIncrement: (ctx2) => ctx2.allowOverflow || !ctx2.isAtMax,
472
+ canDecrement: (ctx2) => ctx2.allowOverflow || !ctx2.isAtMin,
473
+ valueText: (ctx2) => {
474
+ var _a, _b;
475
+ return (_b = (_a = ctx2.translations).valueText) == null ? void 0 : _b.call(_a, ctx2.value);
476
+ },
477
+ formattedValue: (ctx2) => {
478
+ var _a, _b;
479
+ return (_b = (_a = ctx2.format) == null ? void 0 : _a.call(ctx2, ctx2.value).toString()) != null ? _b : ctx2.value;
480
+ }
481
+ },
482
+ watch: {
483
+ value: ["invokeOnChange", "dispatchChangeEvent"],
484
+ isOutOfRange: ["invokeOnInvalid"],
485
+ scrubberCursorPoint: ["setVirtualCursorPosition"]
486
+ },
487
+ on: {
488
+ SET_VALUE: [
489
+ {
490
+ guard: "clampOnBlur",
491
+ actions: ["setValue", "clampValue", "setHintToSet"]
492
+ },
493
+ {
494
+ actions: ["setValue", "setHintToSet"]
495
+ }
496
+ ],
497
+ CLEAR_VALUE: {
498
+ actions: ["clearValue"]
499
+ },
500
+ INCREMENT: {
501
+ actions: ["increment"]
502
+ },
503
+ DECREMENT: {
504
+ actions: ["decrement"]
505
+ }
506
+ },
507
+ states: {
508
+ unknown: {
509
+ on: {
510
+ SETUP: {
511
+ target: "idle",
512
+ actions: "syncInputValue"
513
+ }
514
+ }
515
+ },
516
+ idle: {
517
+ exit: "invokeOnFocus",
518
+ on: {
519
+ PRESS_DOWN: {
520
+ target: "before:spin",
521
+ actions: ["focusInput", "setHint"]
522
+ },
523
+ PRESS_DOWN_SCRUBBER: {
524
+ target: "scrubbing",
525
+ actions: ["focusInput", "setHint", "setCursorPoint"]
526
+ },
527
+ FOCUS: "focused"
528
+ }
529
+ },
530
+ focused: {
531
+ tags: "focus",
532
+ entry: "focusInput",
533
+ activities: "attachWheelListener",
534
+ on: {
535
+ PRESS_DOWN: {
536
+ target: "before:spin",
537
+ actions: ["focusInput", "setHint"]
538
+ },
539
+ PRESS_DOWN_SCRUBBER: {
540
+ target: "scrubbing",
541
+ actions: ["focusInput", "setHint", "setCursorPoint"]
542
+ },
543
+ ARROW_UP: {
544
+ actions: "increment"
545
+ },
546
+ ARROW_DOWN: {
547
+ actions: "decrement"
548
+ },
549
+ HOME: {
550
+ actions: "setToMin"
551
+ },
552
+ END: {
553
+ actions: "setToMax"
554
+ },
555
+ CHANGE: {
556
+ actions: ["setValue", "setHint"]
557
+ },
558
+ BLUR: [
559
+ {
560
+ guard: "isInvalidExponential",
561
+ target: "idle",
562
+ actions: ["clearValue", "clearHint", "invokeOnBlur"]
563
+ },
564
+ {
565
+ guard: and("clampOnBlur", not("isInRange"), not("isEmptyValue")),
566
+ target: "idle",
567
+ actions: ["clampValue", "clearHint", "invokeOnBlur"]
568
+ },
569
+ {
570
+ target: "idle",
571
+ actions: ["roundValue", "invokeOnBlur"]
572
+ }
573
+ ]
574
+ }
575
+ },
576
+ "before:spin": {
577
+ tags: "focus",
578
+ activities: "trackButtonDisabled",
579
+ entry: (0, import_core.choose)([
580
+ { guard: "isIncrementHint", actions: "increment" },
581
+ { guard: "isDecrementHint", actions: "decrement" }
582
+ ]),
583
+ after: {
584
+ CHANGE_DELAY: {
585
+ target: "spinning",
586
+ guard: and("isInRange", "spinOnPress")
587
+ }
588
+ },
589
+ on: {
590
+ PRESS_UP: {
591
+ target: "focused",
592
+ actions: "clearHint"
593
+ }
594
+ }
595
+ },
596
+ spinning: {
597
+ tags: "focus",
598
+ activities: "trackButtonDisabled",
599
+ every: [
600
+ {
601
+ delay: "CHANGE_INTERVAL",
602
+ guard: and(not("isAtMin"), "isIncrementHint"),
603
+ actions: "increment"
604
+ },
605
+ {
606
+ delay: "CHANGE_INTERVAL",
607
+ guard: and(not("isAtMax"), "isDecrementHint"),
608
+ actions: "decrement"
609
+ }
610
+ ],
611
+ on: {
612
+ PRESS_UP: {
613
+ target: "focused",
614
+ actions: "clearHint"
615
+ }
616
+ }
617
+ },
618
+ scrubbing: {
619
+ tags: "focus",
620
+ exit: "clearCursorPoint",
621
+ activities: ["activatePointerLock", "trackMousemove", "setupVirtualCursor", "preventTextSelection"],
622
+ on: {
623
+ POINTER_UP_SCRUBBER: "focused",
624
+ POINTER_MOVE_SCRUBBER: [
625
+ {
626
+ guard: "isIncrementHint",
627
+ actions: ["increment", "setCursorPoint"]
628
+ },
629
+ {
630
+ guard: "isDecrementHint",
631
+ actions: ["decrement", "setCursorPoint"]
632
+ }
633
+ ]
634
+ }
635
+ }
636
+ }
637
+ },
638
+ {
639
+ delays: {
640
+ CHANGE_INTERVAL: 50,
641
+ CHANGE_DELAY: 300
642
+ },
643
+ guards: {
644
+ clampOnBlur: (ctx2) => !!ctx2.clampValueOnBlur,
645
+ isAtMin: (ctx2) => ctx2.isAtMin,
646
+ spinOnPress: (ctx2) => !!ctx2.spinOnPress,
647
+ isAtMax: (ctx2) => ctx2.isAtMax,
648
+ isInRange: (ctx2) => !ctx2.isOutOfRange,
649
+ isDecrementHint: (ctx2, evt) => {
650
+ var _a;
651
+ return ((_a = evt.hint) != null ? _a : ctx2.hint) === "decrement";
652
+ },
653
+ isEmptyValue: (ctx2) => ctx2.isValueEmpty,
654
+ isIncrementHint: (ctx2, evt) => {
655
+ var _a;
656
+ return ((_a = evt.hint) != null ? _a : ctx2.hint) === "increment";
657
+ },
658
+ isInvalidExponential: (ctx2) => ctx2.value.toString().startsWith("e")
659
+ },
660
+ activities: {
661
+ setupVirtualCursor(ctx2) {
662
+ return dom.setupVirtualCursor(ctx2);
663
+ },
664
+ preventTextSelection(ctx2) {
665
+ return dom.preventTextSelection(ctx2);
666
+ },
667
+ trackButtonDisabled(ctx2, _evt, { send }) {
668
+ const btn = dom.getPressedTriggerEl(ctx2, ctx2.hint);
669
+ return observeAttributes(btn, "disabled", () => send("PRESS_UP"));
670
+ },
671
+ attachWheelListener(ctx2, _evt, { send }) {
672
+ const input = dom.getInputEl(ctx2);
673
+ if (!input)
674
+ return;
675
+ function onWheel(event) {
676
+ const isInputFocused = dom.getDoc(ctx2).activeElement === input;
677
+ if (!ctx2.allowMouseWheel || !isInputFocused)
678
+ return;
679
+ event.preventDefault();
680
+ const dir = Math.sign(event.deltaY) * -1;
681
+ if (dir === 1) {
682
+ send("INCREMENT");
683
+ } else if (dir === -1) {
684
+ send("DECREMENT");
685
+ }
686
+ }
687
+ return addDomEvent(input, "wheel", onWheel, { passive: false });
688
+ },
689
+ activatePointerLock(ctx2) {
690
+ if (isSafari() || !supportsPointerEvent())
691
+ return;
692
+ return requestPointerLock(dom.getDoc(ctx2));
693
+ },
694
+ trackMousemove(ctx2, _evt, { send }) {
695
+ const doc = dom.getDoc(ctx2);
696
+ function onMousemove(event) {
697
+ if (!ctx2.scrubberCursorPoint)
698
+ return;
699
+ const value = dom.getMousementValue(ctx2, event);
700
+ if (!value.hint)
701
+ return;
702
+ send({
703
+ type: "POINTER_MOVE_SCRUBBER",
704
+ hint: value.hint,
705
+ point: value.point
706
+ });
707
+ }
708
+ function onMouseup() {
709
+ send("POINTER_UP_SCRUBBER");
710
+ }
711
+ return callAll(
712
+ addDomEvent(doc, "mousemove", onMousemove, false),
713
+ addDomEvent(doc, "mouseup", onMouseup, false)
714
+ );
715
+ }
716
+ },
717
+ actions: {
718
+ focusInput(ctx2) {
719
+ if (!ctx2.focusInputOnChange)
720
+ return;
721
+ const input = dom.getInputEl(ctx2);
722
+ raf(() => input == null ? void 0 : input.focus());
723
+ },
724
+ increment(ctx2, evt) {
725
+ ctx2.value = utils.increment(ctx2, evt.step);
726
+ },
727
+ decrement(ctx2, evt) {
728
+ ctx2.value = utils.decrement(ctx2, evt.step);
729
+ },
730
+ clampValue(ctx2) {
731
+ ctx2.value = utils.clamp(ctx2);
732
+ },
733
+ roundValue(ctx2) {
734
+ if (ctx2.value !== "") {
735
+ ctx2.value = utils.round(ctx2);
736
+ }
737
+ },
738
+ setValue(ctx2, evt) {
739
+ var _a, _b;
740
+ const value = (_b = (_a = evt.target) == null ? void 0 : _a.value) != null ? _b : evt.value;
741
+ ctx2.value = utils.sanitize(ctx2, utils.parse(ctx2, value.toString()));
742
+ },
743
+ clearValue(ctx2) {
744
+ ctx2.value = "";
745
+ },
746
+ setToMax(ctx2) {
747
+ ctx2.value = ctx2.max.toString();
748
+ },
749
+ setToMin(ctx2) {
750
+ ctx2.value = ctx2.min.toString();
751
+ },
752
+ setHint(ctx2, evt) {
753
+ ctx2.hint = evt.hint;
754
+ },
755
+ clearHint(ctx2) {
756
+ ctx2.hint = null;
757
+ },
758
+ setHintToSet(ctx2) {
759
+ ctx2.hint = "set";
760
+ },
761
+ invokeOnChange(ctx2) {
762
+ var _a;
763
+ (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, {
764
+ value: ctx2.value,
765
+ valueAsNumber: ctx2.valueAsNumber
766
+ });
767
+ },
768
+ invokeOnFocus(ctx2, evt) {
769
+ var _a;
770
+ let srcElement = null;
771
+ if (evt.type === "PRESS_DOWN") {
772
+ srcElement = dom.getPressedTriggerEl(ctx2, evt.hint);
773
+ } else if (evt.type === "FOCUS") {
774
+ srcElement = dom.getInputEl(ctx2);
775
+ } else if (evt.type === "PRESS_DOWN_SCRUBBER") {
776
+ srcElement = dom.getScrubberEl(ctx2);
777
+ }
778
+ (_a = ctx2.onFocus) == null ? void 0 : _a.call(ctx2, {
779
+ value: ctx2.value,
780
+ valueAsNumber: ctx2.valueAsNumber,
781
+ srcElement
782
+ });
783
+ },
784
+ invokeOnBlur(ctx2) {
785
+ var _a;
786
+ (_a = ctx2.onBlur) == null ? void 0 : _a.call(ctx2, {
787
+ value: ctx2.value,
788
+ valueAsNumber: ctx2.valueAsNumber
789
+ });
790
+ },
791
+ invokeOnInvalid(ctx2) {
792
+ var _a;
793
+ if (!ctx2.isOutOfRange)
794
+ return;
795
+ const reason = ctx2.valueAsNumber > ctx2.max ? "rangeOverflow" : "rangeUnderflow";
796
+ (_a = ctx2.onInvalid) == null ? void 0 : _a.call(ctx2, {
797
+ reason,
798
+ value: ctx2.formattedValue,
799
+ valueAsNumber: ctx2.valueAsNumber
800
+ });
801
+ },
802
+ syncInputValue(ctx2) {
803
+ const input = dom.getInputEl(ctx2);
804
+ if (!input || input.value == ctx2.value)
805
+ return;
806
+ const value = utils.parse(ctx2, input.value);
807
+ ctx2.value = utils.sanitize(ctx2, value);
808
+ },
809
+ setCursorPoint(ctx2, evt) {
810
+ ctx2.scrubberCursorPoint = evt.point;
811
+ },
812
+ clearCursorPoint(ctx2) {
813
+ ctx2.scrubberCursorPoint = null;
814
+ },
815
+ setVirtualCursorPosition(ctx2) {
816
+ const cursor = dom.getCursorEl(ctx2);
817
+ if (!cursor || !ctx2.scrubberCursorPoint)
818
+ return;
819
+ const { x, y } = ctx2.scrubberCursorPoint;
820
+ cursor.style.transform = `translate3d(${x}px, ${y}px, 0px)`;
821
+ },
822
+ dispatchChangeEvent(ctx2) {
823
+ dispatchInputValueEvent(dom.getInputEl(ctx2), ctx2.formattedValue);
824
+ }
825
+ }
826
+ }
827
+ );
828
+ }
829
+ // Annotate the CommonJS export names for ESM import in node:
830
+ 0 && (module.exports = {
831
+ machine
832
+ });