@zag-js/number-input 0.1.18 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs ADDED
@@ -0,0 +1,1032 @@
1
+ // ../../utilities/dom/dist/index.mjs
2
+ var dataAttr = (guard) => {
3
+ return guard ? "" : void 0;
4
+ };
5
+ var ariaAttr = (guard) => {
6
+ return guard ? "true" : void 0;
7
+ };
8
+ var MAX_Z_INDEX = 2147483647;
9
+ var runIfFn = (v, ...a) => {
10
+ const res = typeof v === "function" ? v(...a) : v;
11
+ return res != null ? res : void 0;
12
+ };
13
+ var callAll = (...fns) => (...a) => {
14
+ fns.forEach(function(fn) {
15
+ fn == null ? void 0 : fn(...a);
16
+ });
17
+ };
18
+ var isArray = (v) => Array.isArray(v);
19
+ var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
20
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
21
+ var isDom = () => typeof window !== "undefined";
22
+ function getPlatform() {
23
+ var _a;
24
+ const agent = navigator.userAgentData;
25
+ return (_a = agent == null ? void 0 : agent.platform) != null ? _a : navigator.platform;
26
+ }
27
+ var pt = (v) => isDom() && v.test(getPlatform());
28
+ var vn = (v) => isDom() && v.test(navigator.vendor);
29
+ var isSafari = () => isApple() && vn(/apple/i);
30
+ var isApple = () => pt(/mac|iphone|ipad|ipod/i);
31
+ function isDocument(el) {
32
+ return el.nodeType === Node.DOCUMENT_NODE;
33
+ }
34
+ function isWindow(value) {
35
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
36
+ }
37
+ function getDocument(el) {
38
+ var _a;
39
+ if (isWindow(el))
40
+ return el.document;
41
+ if (isDocument(el))
42
+ return el;
43
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
44
+ }
45
+ function defineDomHelpers(helpers) {
46
+ const dom2 = {
47
+ getRootNode: (ctx) => {
48
+ var _a, _b;
49
+ return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
50
+ },
51
+ getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
52
+ getWin: (ctx) => {
53
+ var _a;
54
+ return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
55
+ },
56
+ getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
57
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id),
58
+ createEmitter: (ctx, target) => {
59
+ const win = dom2.getWin(ctx);
60
+ return function emit(evt, detail, options) {
61
+ const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
62
+ const eventName = `zag:${evt}`;
63
+ const init = { bubbles, cancelable, composed, detail };
64
+ const event = new win.CustomEvent(eventName, init);
65
+ target.dispatchEvent(event);
66
+ };
67
+ },
68
+ createListener: (target) => {
69
+ return function listen(evt, handler) {
70
+ const eventName = `zag:${evt}`;
71
+ const listener = (e) => handler(e);
72
+ target.addEventListener(eventName, listener);
73
+ return () => target.removeEventListener(eventName, listener);
74
+ };
75
+ }
76
+ };
77
+ return {
78
+ ...dom2,
79
+ ...helpers
80
+ };
81
+ }
82
+ function getNativeEvent(e) {
83
+ var _a;
84
+ return (_a = e.nativeEvent) != null ? _a : e;
85
+ }
86
+ var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
87
+ var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
88
+ var isLeftClick = (v) => v.button === 0;
89
+ var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
90
+ var fallback = {
91
+ pageX: 0,
92
+ pageY: 0,
93
+ clientX: 0,
94
+ clientY: 0
95
+ };
96
+ function getEventPoint(event, type = "page") {
97
+ var _a, _b;
98
+ const point = isTouchEvent(event) ? (_b = (_a = event.touches[0]) != null ? _a : event.changedTouches[0]) != null ? _b : fallback : event;
99
+ return { x: point[`${type}X`], y: point[`${type}Y`] };
100
+ }
101
+ var PAGE_KEYS = /* @__PURE__ */ new Set(["PageUp", "PageDown"]);
102
+ var ARROW_KEYS = /* @__PURE__ */ new Set(["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
103
+ function getEventStep(event) {
104
+ if (event.ctrlKey || event.metaKey) {
105
+ return 0.1;
106
+ } else {
107
+ const isPageKey = PAGE_KEYS.has(event.key);
108
+ const isSkipKey = isPageKey || event.shiftKey && ARROW_KEYS.has(event.key);
109
+ return isSkipKey ? 10 : 1;
110
+ }
111
+ }
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
+ function observeAttributes(node, attributes, fn) {
121
+ if (!node)
122
+ return;
123
+ const attrs = Array.isArray(attributes) ? attributes : [attributes];
124
+ const win = node.ownerDocument.defaultView || window;
125
+ const obs = new win.MutationObserver((changes) => {
126
+ for (const change of changes) {
127
+ if (change.type === "attributes" && change.attributeName && attrs.includes(change.attributeName)) {
128
+ fn(change);
129
+ }
130
+ }
131
+ });
132
+ obs.observe(node, { attributes: true, attributeFilter: attrs });
133
+ return () => obs.disconnect();
134
+ }
135
+ function raf(fn) {
136
+ const id = globalThis.requestAnimationFrame(fn);
137
+ return function cleanup() {
138
+ globalThis.cancelAnimationFrame(id);
139
+ };
140
+ }
141
+ function addPointerlockChangeListener(doc, fn) {
142
+ return addDomEvent(doc, "pointerlockchange", fn, false);
143
+ }
144
+ function addPointerlockErrorListener(doc, fn) {
145
+ doc.addEventListener("pointerlockerror", fn, false);
146
+ return function cleanup() {
147
+ doc.removeEventListener("pointerlockerror", fn, false);
148
+ };
149
+ }
150
+ function requestPointerLock(doc, handlers = {}) {
151
+ const { onPointerLock, onPointerUnlock } = handlers;
152
+ const body = doc.body;
153
+ const supported = "pointerLockElement" in doc || "mozPointerLockElement" in doc;
154
+ const locked = !!doc.pointerLockElement;
155
+ function onPointerChange() {
156
+ if (locked)
157
+ onPointerLock == null ? void 0 : onPointerLock();
158
+ else
159
+ onPointerUnlock == null ? void 0 : onPointerUnlock();
160
+ }
161
+ function onPointerError(event) {
162
+ if (locked)
163
+ onPointerUnlock == null ? void 0 : onPointerUnlock();
164
+ console.error("PointerLock error occured:", event);
165
+ exit();
166
+ }
167
+ function exit() {
168
+ doc.exitPointerLock();
169
+ }
170
+ if (!supported)
171
+ return;
172
+ body.requestPointerLock();
173
+ const cleanup = callAll(
174
+ addPointerlockChangeListener(doc, onPointerChange),
175
+ addPointerlockErrorListener(doc, onPointerError)
176
+ );
177
+ return function dispose() {
178
+ if (!supported)
179
+ return;
180
+ cleanup();
181
+ exit();
182
+ };
183
+ }
184
+
185
+ // ../../utilities/number/dist/index.mjs
186
+ function wrap(num, max) {
187
+ return (num % max + max) % max;
188
+ }
189
+ function roundToDevicePixel(num) {
190
+ if (typeof window === "undefined")
191
+ return Math.round(num);
192
+ const dp = window.devicePixelRatio;
193
+ return Math.floor(num * dp + 0.5) / dp;
194
+ }
195
+ function clamp(v, o) {
196
+ return Math.min(Math.max(valueOf(v), o.min), o.max);
197
+ }
198
+ function countDecimals(value) {
199
+ if (!Number.isFinite(value))
200
+ return 0;
201
+ let e = 1, p = 0;
202
+ while (Math.round(value * e) / e !== value) {
203
+ e *= 10;
204
+ p += 1;
205
+ }
206
+ return p;
207
+ }
208
+ var increment = (v, s) => decimalOperation(valueOf(v), "+", s);
209
+ var decrement = (v, s) => decimalOperation(valueOf(v), "-", s);
210
+ function valueOf(v) {
211
+ if (typeof v === "number")
212
+ return v;
213
+ const num = parseFloat(v.toString().replace(/[^\w.-]+/g, ""));
214
+ return !Number.isNaN(num) ? num : 0;
215
+ }
216
+ function formatDecimal(v, o) {
217
+ return new Intl.NumberFormat("en-US", {
218
+ useGrouping: false,
219
+ style: "decimal",
220
+ minimumFractionDigits: o.minFractionDigits,
221
+ maximumFractionDigits: o.maxFractionDigits
222
+ }).format(valueOf(v));
223
+ }
224
+ function isAtMax(v, o) {
225
+ const val = valueOf(v);
226
+ return val >= o.max;
227
+ }
228
+ function isAtMin(v, o) {
229
+ const val = valueOf(v);
230
+ return val <= o.min;
231
+ }
232
+ function isWithinRange(v, o) {
233
+ const val = valueOf(v);
234
+ return val >= o.min && val <= o.max;
235
+ }
236
+ function decimalOperation(a, op, b) {
237
+ let result = op === "+" ? a + b : a - b;
238
+ if (a % 1 !== 0 || b % 1 !== 0) {
239
+ const multiplier = 10 ** Math.max(countDecimals(a), countDecimals(b));
240
+ a = Math.round(a * multiplier);
241
+ b = Math.round(b * multiplier);
242
+ result = op === "+" ? a + b : a - b;
243
+ result /= multiplier;
244
+ }
245
+ return result;
246
+ }
247
+ var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
248
+
249
+ // src/number-input.dom.ts
250
+ var dom = defineDomHelpers({
251
+ getRootId: (ctx) => {
252
+ var _a, _b;
253
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `number-input:${ctx.id}`;
254
+ },
255
+ getInputId: (ctx) => {
256
+ var _a, _b;
257
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.input) != null ? _b : `number-input:${ctx.id}:input`;
258
+ },
259
+ getIncButtonId: (ctx) => {
260
+ var _a, _b;
261
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.incBtn) != null ? _b : `number-input:${ctx.id}:inc-btn`;
262
+ },
263
+ getDecButtonId: (ctx) => {
264
+ var _a, _b;
265
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.decBtn) != null ? _b : `number-input:${ctx.id}:dec-btn`;
266
+ },
267
+ getScrubberId: (ctx) => {
268
+ var _a, _b;
269
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.scrubber) != null ? _b : `number-input:${ctx.id}:scrubber`;
270
+ },
271
+ getCursorId: (ctx) => `number-input:${ctx.id}:cursor`,
272
+ getLabelId: (ctx) => {
273
+ var _a, _b;
274
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `number-input:${ctx.id}:label`;
275
+ },
276
+ getInputEl: (ctx) => dom.getById(ctx, dom.getInputId(ctx)),
277
+ getIncButtonEl: (ctx) => dom.getById(ctx, dom.getIncButtonId(ctx)),
278
+ getDecButtonEl: (ctx) => dom.getById(ctx, dom.getDecButtonId(ctx)),
279
+ getScrubberEl: (ctx) => dom.getById(ctx, dom.getScrubberId(ctx)),
280
+ getCursorEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getCursorId(ctx)),
281
+ getActiveButton: (ctx, hint = ctx.hint) => {
282
+ let btnEl = null;
283
+ if (hint === "increment") {
284
+ btnEl = dom.getIncButtonEl(ctx);
285
+ }
286
+ if (hint === "decrement") {
287
+ btnEl = dom.getDecButtonEl(ctx);
288
+ }
289
+ return btnEl;
290
+ },
291
+ setupVirtualCursor(ctx) {
292
+ if (isSafari() || !supportsPointerEvent())
293
+ return;
294
+ dom.createVirtualCursor(ctx);
295
+ return () => {
296
+ var _a;
297
+ (_a = dom.getCursorEl(ctx)) == null ? void 0 : _a.remove();
298
+ };
299
+ },
300
+ preventTextSelection(ctx) {
301
+ const doc = dom.getDoc(ctx);
302
+ const html = doc.documentElement;
303
+ const body = doc.body;
304
+ body.style.pointerEvents = "none";
305
+ html.style.userSelect = "none";
306
+ html.style.cursor = "ew-resize";
307
+ return () => {
308
+ body.style.pointerEvents = "";
309
+ html.style.userSelect = "";
310
+ html.style.cursor = "";
311
+ if (!html.style.length) {
312
+ html.removeAttribute("style");
313
+ }
314
+ if (!body.style.length) {
315
+ body.removeAttribute("style");
316
+ }
317
+ };
318
+ },
319
+ getMousementValue(ctx, event) {
320
+ const x = roundToDevicePixel(event.movementX);
321
+ const y = roundToDevicePixel(event.movementY);
322
+ let hint = x > 0 ? "increment" : x < 0 ? "decrement" : null;
323
+ if (ctx.isRtl && hint === "increment")
324
+ hint = "decrement";
325
+ if (ctx.isRtl && hint === "decrement")
326
+ hint = "increment";
327
+ const point = {
328
+ x: ctx.scrubberCursorPoint.x + x,
329
+ y: ctx.scrubberCursorPoint.y + y
330
+ };
331
+ const win = dom.getWin(ctx);
332
+ const width = win.innerWidth;
333
+ const half = roundToDevicePixel(7.5);
334
+ point.x = wrap(point.x + half, width) - half;
335
+ return { hint, point };
336
+ },
337
+ createVirtualCursor(ctx) {
338
+ const doc = dom.getDoc(ctx);
339
+ const el = doc.createElement("div");
340
+ el.className = "scrubber--cursor";
341
+ el.id = dom.getCursorId(ctx);
342
+ Object.assign(el.style, {
343
+ width: "15px",
344
+ height: "15px",
345
+ position: "fixed",
346
+ pointerEvents: "none",
347
+ left: "0px",
348
+ top: "0px",
349
+ zIndex: MAX_Z_INDEX,
350
+ transform: ctx.scrubberCursorPoint ? `translate3d(${ctx.scrubberCursorPoint.x}px, ${ctx.scrubberCursorPoint.y}px, 0px)` : void 0,
351
+ willChange: "transform"
352
+ });
353
+ el.innerHTML = `
354
+ <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);">
355
+ <g transform="translate(2 3)">
356
+ <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>
357
+ <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>
358
+ </g>
359
+ </svg>`;
360
+ doc.body.appendChild(el);
361
+ }
362
+ });
363
+
364
+ // src/number-input.utils.ts
365
+ var utils = {
366
+ isValidNumericEvent: (ctx, event) => {
367
+ var _a, _b;
368
+ if (event.key == null)
369
+ return true;
370
+ const isModifier = isModifiedEvent(event);
371
+ const isSingleKey = event.key.length === 1;
372
+ if (isModifier || !isSingleKey)
373
+ return true;
374
+ return (_b = (_a = ctx.validateCharacter) == null ? void 0 : _a.call(ctx, event.key)) != null ? _b : utils.isFloatingPoint(event.key);
375
+ },
376
+ isFloatingPoint: (v) => /^[0-9+\-.]$/.test(v),
377
+ sanitize: (ctx, value) => {
378
+ var _a;
379
+ return value.split("").filter((_a = ctx.validateCharacter) != null ? _a : utils.isFloatingPoint).join("");
380
+ },
381
+ increment: (ctx, step) => {
382
+ const value = increment(ctx.value, step != null ? step : ctx.step);
383
+ return formatDecimal(clamp(value, ctx), ctx);
384
+ },
385
+ decrement: (ctx, step) => {
386
+ const value = decrement(ctx.value, step != null ? step : ctx.step);
387
+ return formatDecimal(clamp(value, ctx), ctx);
388
+ },
389
+ clamp: (ctx) => {
390
+ return formatDecimal(clamp(ctx.value, ctx), ctx);
391
+ },
392
+ parse: (ctx, value) => {
393
+ var _a, _b;
394
+ return (_b = (_a = ctx.parse) == null ? void 0 : _a.call(ctx, value)) != null ? _b : value;
395
+ },
396
+ format: (ctx, value) => {
397
+ var _a, _b;
398
+ const _val = value.toString();
399
+ return (_b = (_a = ctx.format) == null ? void 0 : _a.call(ctx, _val)) != null ? _b : _val;
400
+ },
401
+ round: (ctx) => {
402
+ return formatDecimal(ctx.value, ctx);
403
+ }
404
+ };
405
+
406
+ // src/number-input.connect.ts
407
+ function connect(state, send, normalize) {
408
+ const isFocused = state.hasTag("focus");
409
+ const isInvalid = state.context.isOutOfRange || !!state.context.invalid;
410
+ const isDisabled = !!state.context.disabled;
411
+ const isValueEmpty = state.context.isValueEmpty;
412
+ const isIncrementDisabled = isDisabled || !state.context.canIncrement;
413
+ const isDecrementDisabled = isDisabled || !state.context.canDecrement;
414
+ const translations = state.context.translations;
415
+ return {
416
+ isFocused,
417
+ isInvalid,
418
+ isValueEmpty,
419
+ value: state.context.formattedValue,
420
+ valueAsNumber: state.context.valueAsNumber,
421
+ setValue(value) {
422
+ send({ type: "SET_VALUE", value: value.toString() });
423
+ },
424
+ clearValue() {
425
+ send("CLEAR_VALUE");
426
+ },
427
+ increment() {
428
+ send("INCREMENT");
429
+ },
430
+ decrement() {
431
+ send("DECREMENT");
432
+ },
433
+ setToMax() {
434
+ send({ type: "SET_VALUE", value: state.context.max });
435
+ },
436
+ setToMin() {
437
+ send({ type: "SET_VALUE", value: state.context.min });
438
+ },
439
+ focus() {
440
+ var _a;
441
+ (_a = dom.getInputEl(state.context)) == null ? void 0 : _a.focus();
442
+ },
443
+ blur() {
444
+ var _a;
445
+ (_a = dom.getInputEl(state.context)) == null ? void 0 : _a.blur();
446
+ },
447
+ rootProps: normalize.element({
448
+ id: dom.getRootId(state.context),
449
+ "data-part": "root",
450
+ "data-disabled": dataAttr(isDisabled)
451
+ }),
452
+ labelProps: normalize.label({
453
+ "data-part": "label",
454
+ "data-disabled": dataAttr(isDisabled),
455
+ "data-invalid": dataAttr(isInvalid),
456
+ id: dom.getLabelId(state.context),
457
+ htmlFor: dom.getInputId(state.context)
458
+ }),
459
+ groupProps: normalize.element({
460
+ "data-part": "group",
461
+ role: "group",
462
+ "aria-disabled": isDisabled,
463
+ "data-disabled": dataAttr(isDisabled),
464
+ "data-invalid": dataAttr(isInvalid),
465
+ "aria-invalid": ariaAttr(state.context.invalid)
466
+ }),
467
+ inputProps: normalize.input({
468
+ "data-part": "input",
469
+ name: state.context.name,
470
+ id: dom.getInputId(state.context),
471
+ role: "spinbutton",
472
+ defaultValue: state.context.formattedValue,
473
+ pattern: state.context.pattern,
474
+ inputMode: state.context.inputMode,
475
+ "aria-invalid": isInvalid || void 0,
476
+ "data-invalid": dataAttr(isInvalid),
477
+ disabled: isDisabled,
478
+ "data-disabled": dataAttr(isDisabled),
479
+ readOnly: !!state.context.readonly,
480
+ autoComplete: "off",
481
+ autoCorrect: "off",
482
+ spellCheck: "false",
483
+ type: "text",
484
+ "aria-roledescription": "numberfield",
485
+ "aria-valuemin": state.context.min,
486
+ "aria-valuemax": state.context.max,
487
+ "aria-valuenow": isNaN(state.context.valueAsNumber) ? void 0 : state.context.valueAsNumber,
488
+ "aria-valuetext": state.context.valueText,
489
+ onFocus() {
490
+ send("FOCUS");
491
+ },
492
+ onBlur() {
493
+ send("BLUR");
494
+ },
495
+ onChange(event) {
496
+ const evt = getNativeEvent(event);
497
+ if (evt.isComposing)
498
+ return;
499
+ send({ type: "CHANGE", target: event.currentTarget, hint: "set" });
500
+ },
501
+ onKeyDown(event) {
502
+ const evt = getNativeEvent(event);
503
+ if (evt.isComposing)
504
+ return;
505
+ if (!utils.isValidNumericEvent(state.context, event)) {
506
+ event.preventDefault();
507
+ }
508
+ const step = getEventStep(event) * state.context.step;
509
+ const keyMap = {
510
+ ArrowUp() {
511
+ send({ type: "ARROW_UP", step });
512
+ },
513
+ ArrowDown() {
514
+ send({ type: "ARROW_DOWN", step });
515
+ },
516
+ Home() {
517
+ send("HOME");
518
+ },
519
+ End() {
520
+ send("END");
521
+ }
522
+ };
523
+ const exec = keyMap[event.key];
524
+ if (exec) {
525
+ exec(event);
526
+ event.preventDefault();
527
+ }
528
+ }
529
+ }),
530
+ decrementButtonProps: normalize.button({
531
+ "data-part": "spin-button",
532
+ "data-type": "decrement",
533
+ id: dom.getDecButtonId(state.context),
534
+ disabled: isDecrementDisabled,
535
+ "data-disabled": dataAttr(isDecrementDisabled),
536
+ "aria-label": translations.decrementLabel,
537
+ type: "button",
538
+ tabIndex: -1,
539
+ "aria-controls": dom.getInputId(state.context),
540
+ onPointerDown(event) {
541
+ if (isDecrementDisabled)
542
+ return;
543
+ send(isLeftClick(event) ? { type: "PRESS_DOWN", hint: "decrement" } : { type: "FOCUS" });
544
+ event.preventDefault();
545
+ },
546
+ onPointerUp() {
547
+ send({ type: "PRESS_UP", hint: "decrement" });
548
+ },
549
+ onPointerLeave() {
550
+ if (isDecrementDisabled)
551
+ return;
552
+ send({ type: "PRESS_UP", hint: "decrement" });
553
+ }
554
+ }),
555
+ incrementButtonProps: normalize.button({
556
+ "data-part": "spin-button",
557
+ "data-type": "increment",
558
+ id: dom.getIncButtonId(state.context),
559
+ disabled: isIncrementDisabled,
560
+ "data-disabled": dataAttr(isIncrementDisabled),
561
+ "aria-label": translations.incrementLabel,
562
+ type: "button",
563
+ tabIndex: -1,
564
+ "aria-controls": dom.getInputId(state.context),
565
+ onPointerDown(event) {
566
+ if (isIncrementDisabled)
567
+ return;
568
+ send(isLeftClick(event) ? { type: "PRESS_DOWN", hint: "increment" } : { type: "FOCUS" });
569
+ event.preventDefault();
570
+ },
571
+ onPointerUp() {
572
+ send({ type: "PRESS_UP", hint: "increment" });
573
+ },
574
+ onPointerLeave() {
575
+ send({ type: "PRESS_UP", hint: "increment" });
576
+ }
577
+ }),
578
+ scrubberProps: normalize.element({
579
+ "data-disabled": dataAttr(isDisabled),
580
+ "data-part": "scrubber",
581
+ id: dom.getScrubberId(state.context),
582
+ role: "presentation",
583
+ onMouseDown(event) {
584
+ if (isDisabled)
585
+ return;
586
+ const evt = getNativeEvent(event);
587
+ event.preventDefault();
588
+ const point = getEventPoint(evt);
589
+ point.x = point.x - roundToDevicePixel(7.5);
590
+ point.y = point.y - roundToDevicePixel(7.5);
591
+ send({ type: "PRESS_DOWN_SCRUBBER", point });
592
+ },
593
+ style: {
594
+ cursor: isDisabled ? void 0 : "ew-resize"
595
+ }
596
+ })
597
+ };
598
+ }
599
+
600
+ // src/number-input.machine.ts
601
+ import { choose, createMachine, guards } from "@zag-js/core";
602
+
603
+ // ../../utilities/core/dist/index.mjs
604
+ var callAll2 = (...fns) => (...a) => {
605
+ fns.forEach(function(fn) {
606
+ fn == null ? void 0 : fn(...a);
607
+ });
608
+ };
609
+
610
+ // ../../utilities/form-utils/dist/index.mjs
611
+ function getWindow(el) {
612
+ var _a;
613
+ return (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
614
+ }
615
+ function getDescriptor(el, options) {
616
+ var _a;
617
+ const { type, property } = options;
618
+ const proto = getWindow(el)[type].prototype;
619
+ return (_a = Object.getOwnPropertyDescriptor(proto, property)) != null ? _a : {};
620
+ }
621
+ function dispatchInputValueEvent(el, value) {
622
+ var _a;
623
+ if (!el)
624
+ return;
625
+ const win = getWindow(el);
626
+ if (!(el instanceof win.HTMLInputElement))
627
+ return;
628
+ const desc = getDescriptor(el, { type: "HTMLInputElement", property: "value" });
629
+ (_a = desc.set) == null ? void 0 : _a.call(el, value);
630
+ const event = new win.Event("input", { bubbles: true });
631
+ el.dispatchEvent(event);
632
+ }
633
+
634
+ // src/number-input.machine.ts
635
+ var { not, and } = guards;
636
+ function machine(ctx) {
637
+ return createMachine(
638
+ {
639
+ id: "number-input",
640
+ initial: "unknown",
641
+ context: {
642
+ dir: "ltr",
643
+ focusInputOnChange: true,
644
+ clampValueOnBlur: true,
645
+ allowOverflow: false,
646
+ inputMode: "decimal",
647
+ pattern: "[0-9]*(.[0-9]+)?",
648
+ hint: null,
649
+ value: "",
650
+ step: 1,
651
+ min: Number.MIN_SAFE_INTEGER,
652
+ max: Number.MAX_SAFE_INTEGER,
653
+ scrubberCursorPoint: null,
654
+ invalid: false,
655
+ spinOnPress: true,
656
+ ...ctx,
657
+ translations: {
658
+ incrementLabel: "increment value",
659
+ decrementLabel: "decrease value",
660
+ ...ctx.translations
661
+ }
662
+ },
663
+ computed: {
664
+ isRtl: (ctx2) => ctx2.dir === "rtl",
665
+ valueAsNumber: (ctx2) => valueOf(ctx2.value),
666
+ isAtMin: (ctx2) => isAtMin(ctx2.value, ctx2),
667
+ isAtMax: (ctx2) => isAtMax(ctx2.value, ctx2),
668
+ isOutOfRange: (ctx2) => !isWithinRange(ctx2.value, ctx2),
669
+ isValueEmpty: (ctx2) => ctx2.value === "",
670
+ canIncrement: (ctx2) => ctx2.allowOverflow || !ctx2.isAtMax,
671
+ canDecrement: (ctx2) => ctx2.allowOverflow || !ctx2.isAtMin,
672
+ valueText: (ctx2) => {
673
+ var _a, _b;
674
+ return (_b = (_a = ctx2.translations).valueText) == null ? void 0 : _b.call(_a, ctx2.value);
675
+ },
676
+ formattedValue: (ctx2) => {
677
+ var _a, _b;
678
+ return (_b = (_a = ctx2.format) == null ? void 0 : _a.call(ctx2, ctx2.value).toString()) != null ? _b : ctx2.value;
679
+ }
680
+ },
681
+ watch: {
682
+ value: ["invokeOnChange", "dispatchChangeEvent"],
683
+ isOutOfRange: ["invokeOnInvalid"],
684
+ scrubberCursorPoint: ["setVirtualCursorPosition"]
685
+ },
686
+ on: {
687
+ SET_VALUE: [
688
+ {
689
+ guard: "clampOnBlur",
690
+ actions: ["setValue", "clampValue", "setHintToSet"]
691
+ },
692
+ {
693
+ actions: ["setValue", "setHintToSet"]
694
+ }
695
+ ],
696
+ CLEAR_VALUE: {
697
+ actions: ["clearValue"]
698
+ },
699
+ INCREMENT: {
700
+ actions: ["increment"]
701
+ },
702
+ DECREMENT: {
703
+ actions: ["decrement"]
704
+ }
705
+ },
706
+ states: {
707
+ unknown: {
708
+ on: {
709
+ SETUP: {
710
+ target: "idle",
711
+ actions: "syncInputValue"
712
+ }
713
+ }
714
+ },
715
+ idle: {
716
+ exit: "invokeOnFocus",
717
+ on: {
718
+ PRESS_DOWN: {
719
+ target: "before:spin",
720
+ actions: ["focusInput", "setHint"]
721
+ },
722
+ PRESS_DOWN_SCRUBBER: {
723
+ target: "scrubbing",
724
+ actions: ["focusInput", "setHint", "setCursorPoint"]
725
+ },
726
+ FOCUS: "focused"
727
+ }
728
+ },
729
+ focused: {
730
+ tags: "focus",
731
+ entry: "focusInput",
732
+ activities: "attachWheelListener",
733
+ on: {
734
+ PRESS_DOWN: {
735
+ target: "before:spin",
736
+ actions: ["focusInput", "setHint"]
737
+ },
738
+ PRESS_DOWN_SCRUBBER: {
739
+ target: "scrubbing",
740
+ actions: ["focusInput", "setHint", "setCursorPoint"]
741
+ },
742
+ ARROW_UP: {
743
+ actions: "increment"
744
+ },
745
+ ARROW_DOWN: {
746
+ actions: "decrement"
747
+ },
748
+ HOME: {
749
+ actions: "setToMin"
750
+ },
751
+ END: {
752
+ actions: "setToMax"
753
+ },
754
+ CHANGE: {
755
+ actions: ["setValue", "setHint"]
756
+ },
757
+ BLUR: [
758
+ {
759
+ guard: "isInvalidExponential",
760
+ target: "idle",
761
+ actions: ["clearValue", "clearHint", "invokeOnBlur"]
762
+ },
763
+ {
764
+ guard: and("clampOnBlur", not("isInRange"), not("isEmptyValue")),
765
+ target: "idle",
766
+ actions: ["clampValue", "clearHint", "invokeOnBlur"]
767
+ },
768
+ {
769
+ target: "idle",
770
+ actions: ["roundValue", "invokeOnBlur"]
771
+ }
772
+ ]
773
+ }
774
+ },
775
+ "before:spin": {
776
+ tags: "focus",
777
+ activities: "trackButtonDisabled",
778
+ entry: choose([
779
+ { guard: "isIncrementHint", actions: "increment" },
780
+ { guard: "isDecrementHint", actions: "decrement" }
781
+ ]),
782
+ after: {
783
+ CHANGE_DELAY: {
784
+ target: "spinning",
785
+ guard: and("isInRange", "spinOnPress")
786
+ }
787
+ },
788
+ on: {
789
+ PRESS_UP: {
790
+ target: "focused",
791
+ actions: "clearHint"
792
+ }
793
+ }
794
+ },
795
+ spinning: {
796
+ tags: "focus",
797
+ activities: "trackButtonDisabled",
798
+ every: [
799
+ {
800
+ delay: "CHANGE_INTERVAL",
801
+ guard: and(not("isAtMin"), "isIncrementHint"),
802
+ actions: "increment"
803
+ },
804
+ {
805
+ delay: "CHANGE_INTERVAL",
806
+ guard: and(not("isAtMax"), "isDecrementHint"),
807
+ actions: "decrement"
808
+ }
809
+ ],
810
+ on: {
811
+ PRESS_UP: {
812
+ target: "focused",
813
+ actions: "clearHint"
814
+ }
815
+ }
816
+ },
817
+ scrubbing: {
818
+ tags: "focus",
819
+ exit: "clearCursorPoint",
820
+ activities: ["activatePointerLock", "trackMousemove", "setupVirtualCursor", "preventTextSelection"],
821
+ on: {
822
+ POINTER_UP_SCRUBBER: "focused",
823
+ POINTER_MOVE_SCRUBBER: [
824
+ {
825
+ guard: "isIncrementHint",
826
+ actions: ["increment", "setCursorPoint"]
827
+ },
828
+ {
829
+ guard: "isDecrementHint",
830
+ actions: ["decrement", "setCursorPoint"]
831
+ }
832
+ ]
833
+ }
834
+ }
835
+ }
836
+ },
837
+ {
838
+ delays: {
839
+ CHANGE_INTERVAL: 50,
840
+ CHANGE_DELAY: 300
841
+ },
842
+ guards: {
843
+ clampOnBlur: (ctx2) => !!ctx2.clampValueOnBlur,
844
+ isAtMin: (ctx2) => ctx2.isAtMin,
845
+ spinOnPress: (ctx2) => !!ctx2.spinOnPress,
846
+ isAtMax: (ctx2) => ctx2.isAtMax,
847
+ isInRange: (ctx2) => !ctx2.isOutOfRange,
848
+ isDecrementHint: (ctx2, evt) => {
849
+ var _a;
850
+ return ((_a = evt.hint) != null ? _a : ctx2.hint) === "decrement";
851
+ },
852
+ isEmptyValue: (ctx2) => ctx2.isValueEmpty,
853
+ isIncrementHint: (ctx2, evt) => {
854
+ var _a;
855
+ return ((_a = evt.hint) != null ? _a : ctx2.hint) === "increment";
856
+ },
857
+ isInvalidExponential: (ctx2) => ctx2.value.toString().startsWith("e")
858
+ },
859
+ activities: {
860
+ setupVirtualCursor(ctx2) {
861
+ return dom.setupVirtualCursor(ctx2);
862
+ },
863
+ preventTextSelection(ctx2) {
864
+ return dom.preventTextSelection(ctx2);
865
+ },
866
+ trackButtonDisabled(ctx2, _evt, { send }) {
867
+ const btn = dom.getActiveButton(ctx2, ctx2.hint);
868
+ return observeAttributes(btn, "disabled", () => send("PRESS_UP"));
869
+ },
870
+ attachWheelListener(ctx2, _evt, { send }) {
871
+ const input = dom.getInputEl(ctx2);
872
+ if (!input)
873
+ return;
874
+ function onWheel(event) {
875
+ const isInputFocused = dom.getDoc(ctx2).activeElement === input;
876
+ if (!ctx2.allowMouseWheel || !isInputFocused)
877
+ return;
878
+ event.preventDefault();
879
+ const dir = Math.sign(event.deltaY) * -1;
880
+ if (dir === 1) {
881
+ send("INCREMENT");
882
+ } else if (dir === -1) {
883
+ send("DECREMENT");
884
+ }
885
+ }
886
+ return addDomEvent(input, "wheel", onWheel, { passive: false });
887
+ },
888
+ activatePointerLock(ctx2) {
889
+ if (isSafari() || !supportsPointerEvent())
890
+ return;
891
+ return requestPointerLock(dom.getDoc(ctx2));
892
+ },
893
+ trackMousemove(ctx2, _evt, { send }) {
894
+ const doc = dom.getDoc(ctx2);
895
+ function onMousemove(event) {
896
+ if (!ctx2.scrubberCursorPoint)
897
+ return;
898
+ const value = dom.getMousementValue(ctx2, event);
899
+ if (!value.hint)
900
+ return;
901
+ send({
902
+ type: "POINTER_MOVE_SCRUBBER",
903
+ hint: value.hint,
904
+ point: value.point
905
+ });
906
+ }
907
+ function onMouseup() {
908
+ send("POINTER_UP_SCRUBBER");
909
+ }
910
+ return callAll2(
911
+ addDomEvent(doc, "mousemove", onMousemove, false),
912
+ addDomEvent(doc, "mouseup", onMouseup, false)
913
+ );
914
+ }
915
+ },
916
+ actions: {
917
+ focusInput(ctx2) {
918
+ if (!ctx2.focusInputOnChange)
919
+ return;
920
+ const input = dom.getInputEl(ctx2);
921
+ raf(() => input == null ? void 0 : input.focus());
922
+ },
923
+ increment(ctx2, evt) {
924
+ ctx2.value = utils.increment(ctx2, evt.step);
925
+ },
926
+ decrement(ctx2, evt) {
927
+ ctx2.value = utils.decrement(ctx2, evt.step);
928
+ },
929
+ clampValue(ctx2) {
930
+ ctx2.value = utils.clamp(ctx2);
931
+ },
932
+ roundValue(ctx2) {
933
+ if (ctx2.value !== "") {
934
+ ctx2.value = utils.round(ctx2);
935
+ }
936
+ },
937
+ setValue(ctx2, evt) {
938
+ var _a, _b;
939
+ const value = (_b = (_a = evt.target) == null ? void 0 : _a.value) != null ? _b : evt.value;
940
+ ctx2.value = utils.sanitize(ctx2, utils.parse(ctx2, value.toString()));
941
+ },
942
+ clearValue(ctx2) {
943
+ ctx2.value = "";
944
+ },
945
+ setToMax(ctx2) {
946
+ ctx2.value = ctx2.max.toString();
947
+ },
948
+ setToMin(ctx2) {
949
+ ctx2.value = ctx2.min.toString();
950
+ },
951
+ setHint(ctx2, evt) {
952
+ ctx2.hint = evt.hint;
953
+ },
954
+ clearHint(ctx2) {
955
+ ctx2.hint = null;
956
+ },
957
+ setHintToSet(ctx2) {
958
+ ctx2.hint = "set";
959
+ },
960
+ invokeOnChange(ctx2) {
961
+ var _a;
962
+ (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, {
963
+ value: ctx2.value,
964
+ valueAsNumber: ctx2.valueAsNumber
965
+ });
966
+ },
967
+ invokeOnFocus(ctx2, evt) {
968
+ var _a;
969
+ let srcElement = null;
970
+ if (evt.type === "PRESS_DOWN") {
971
+ srcElement = dom.getActiveButton(ctx2, evt.hint);
972
+ } else if (evt.type === "FOCUS") {
973
+ srcElement = dom.getInputEl(ctx2);
974
+ } else if (evt.type === "PRESS_DOWN_SCRUBBER") {
975
+ srcElement = dom.getScrubberEl(ctx2);
976
+ }
977
+ (_a = ctx2.onFocus) == null ? void 0 : _a.call(ctx2, {
978
+ value: ctx2.value,
979
+ valueAsNumber: ctx2.valueAsNumber,
980
+ srcElement
981
+ });
982
+ },
983
+ invokeOnBlur(ctx2) {
984
+ var _a;
985
+ (_a = ctx2.onBlur) == null ? void 0 : _a.call(ctx2, {
986
+ value: ctx2.value,
987
+ valueAsNumber: ctx2.valueAsNumber
988
+ });
989
+ },
990
+ invokeOnInvalid(ctx2) {
991
+ var _a;
992
+ if (!ctx2.isOutOfRange)
993
+ return;
994
+ const reason = ctx2.valueAsNumber > ctx2.max ? "rangeOverflow" : "rangeUnderflow";
995
+ (_a = ctx2.onInvalid) == null ? void 0 : _a.call(ctx2, {
996
+ reason,
997
+ value: ctx2.formattedValue,
998
+ valueAsNumber: ctx2.valueAsNumber
999
+ });
1000
+ },
1001
+ syncInputValue(ctx2) {
1002
+ const input = dom.getInputEl(ctx2);
1003
+ if (!input || input.value == ctx2.value)
1004
+ return;
1005
+ const value = utils.parse(ctx2, input.value);
1006
+ ctx2.value = utils.sanitize(ctx2, value);
1007
+ },
1008
+ setCursorPoint(ctx2, evt) {
1009
+ ctx2.scrubberCursorPoint = evt.point;
1010
+ },
1011
+ clearCursorPoint(ctx2) {
1012
+ ctx2.scrubberCursorPoint = null;
1013
+ },
1014
+ setVirtualCursorPosition(ctx2) {
1015
+ const cursor = dom.getCursorEl(ctx2);
1016
+ if (!cursor || !ctx2.scrubberCursorPoint)
1017
+ return;
1018
+ const { x, y } = ctx2.scrubberCursorPoint;
1019
+ cursor.style.transform = `translate3d(${x}px, ${y}px, 0px)`;
1020
+ },
1021
+ dispatchChangeEvent(ctx2) {
1022
+ dispatchInputValueEvent(dom.getInputEl(ctx2), ctx2.formattedValue);
1023
+ }
1024
+ },
1025
+ hookSync: true
1026
+ }
1027
+ );
1028
+ }
1029
+ export {
1030
+ connect,
1031
+ machine
1032
+ };