@zag-js/number-input 0.0.0-20220802150625

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 ADDED
@@ -0,0 +1,998 @@
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/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ connect: () => connect,
24
+ machine: () => machine
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+
28
+ // ../../utilities/dom/dist/index.mjs
29
+ var dataAttr = (guard) => {
30
+ return guard ? "" : void 0;
31
+ };
32
+ var ariaAttr = (guard) => {
33
+ return guard ? "true" : void 0;
34
+ };
35
+ var MAX_Z_INDEX = 2147483647;
36
+ var runIfFn = (v, ...a) => {
37
+ const res = typeof v === "function" ? v(...a) : v;
38
+ return res ?? void 0;
39
+ };
40
+ var callAll = (...fns) => (...a) => {
41
+ fns.forEach(function(fn) {
42
+ fn == null ? void 0 : fn(...a);
43
+ });
44
+ };
45
+ var isArray = (v) => Array.isArray(v);
46
+ var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
47
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
48
+ var isDom = () => typeof window !== "undefined";
49
+ function getPlatform() {
50
+ const agent = navigator.userAgentData;
51
+ return (agent == null ? void 0 : agent.platform) ?? navigator.platform;
52
+ }
53
+ var pt = (v) => isDom() && v.test(getPlatform());
54
+ var vn = (v) => isDom() && v.test(navigator.vendor);
55
+ var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
56
+ var isMac = () => pt(/^Mac/) && !isTouchDevice;
57
+ var isSafari = () => isApple() && vn(/apple/i);
58
+ var isApple = () => pt(/mac|iphone|ipad|ipod/i);
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
+ }
89
+ function getNativeEvent(e) {
90
+ return e.nativeEvent ?? e;
91
+ }
92
+ var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
93
+ var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
94
+ var isLeftClick = (v) => v.button === 0;
95
+ var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
96
+ function observeAttributes(node, attributes, fn) {
97
+ if (!node)
98
+ return;
99
+ const attrs = Array.isArray(attributes) ? attributes : [attributes];
100
+ const win = node.ownerDocument.defaultView || window;
101
+ const obs = new win.MutationObserver((changes) => {
102
+ for (const change of changes) {
103
+ if (change.type === "attributes" && change.attributeName && attrs.includes(change.attributeName)) {
104
+ fn(change);
105
+ }
106
+ }
107
+ });
108
+ obs.observe(node, { attributes: true, attributeFilter: attrs });
109
+ return () => obs.disconnect();
110
+ }
111
+ var fallback = {
112
+ pageX: 0,
113
+ pageY: 0,
114
+ clientX: 0,
115
+ clientY: 0
116
+ };
117
+ function getEventPoint(event, type = "page") {
118
+ const point = isTouchEvent(event) ? event.touches[0] ?? event.changedTouches[0] ?? fallback : event;
119
+ return { x: point[`${type}X`], y: point[`${type}Y`] };
120
+ }
121
+ var PAGE_KEYS = /* @__PURE__ */ new Set(["PageUp", "PageDown"]);
122
+ var ARROW_KEYS = /* @__PURE__ */ new Set(["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
123
+ function getEventStep(event) {
124
+ if (event.ctrlKey || event.metaKey) {
125
+ return 0.1;
126
+ } else {
127
+ const isPageKey = PAGE_KEYS.has(event.key);
128
+ const isSkipKey = isPageKey || event.shiftKey && ARROW_KEYS.has(event.key);
129
+ return isSkipKey ? 10 : 1;
130
+ }
131
+ }
132
+ var isRef = (v) => hasProp(v, "current");
133
+ function addDomEvent(target, eventName, handler, options) {
134
+ const node = isRef(target) ? target.current : runIfFn(target);
135
+ node == null ? void 0 : node.addEventListener(eventName, handler, options);
136
+ return () => {
137
+ node == null ? void 0 : node.removeEventListener(eventName, handler, options);
138
+ };
139
+ }
140
+ function raf(fn) {
141
+ const id = globalThis.requestAnimationFrame(fn);
142
+ return function cleanup() {
143
+ globalThis.cancelAnimationFrame(id);
144
+ };
145
+ }
146
+ function addPointerlockChangeListener(doc, fn) {
147
+ return addDomEvent(doc, "pointerlockchange", fn, false);
148
+ }
149
+ function addPointerlockErrorListener(doc, fn) {
150
+ doc.addEventListener("pointerlockerror", fn, false);
151
+ return function cleanup() {
152
+ doc.removeEventListener("pointerlockerror", fn, false);
153
+ };
154
+ }
155
+ function requestPointerLock(doc, handlers = {}) {
156
+ const { onPointerLock, onPointerUnlock } = handlers;
157
+ const body = doc.body;
158
+ const supported = "pointerLockElement" in doc || "mozPointerLockElement" in doc;
159
+ const locked = !!doc.pointerLockElement;
160
+ function onPointerChange() {
161
+ if (locked)
162
+ onPointerLock == null ? void 0 : onPointerLock();
163
+ else
164
+ onPointerUnlock == null ? void 0 : onPointerUnlock();
165
+ }
166
+ function onPointerError(event) {
167
+ if (locked)
168
+ onPointerUnlock == null ? void 0 : onPointerUnlock();
169
+ console.error("PointerLock error occured:", event);
170
+ exit();
171
+ }
172
+ function exit() {
173
+ doc.exitPointerLock();
174
+ }
175
+ if (!supported)
176
+ return;
177
+ body.requestPointerLock();
178
+ const cleanup = callAll(
179
+ addPointerlockChangeListener(doc, onPointerChange),
180
+ addPointerlockErrorListener(doc, onPointerError)
181
+ );
182
+ return function dispose() {
183
+ if (!supported)
184
+ return;
185
+ cleanup();
186
+ exit();
187
+ };
188
+ }
189
+
190
+ // ../../utilities/number/dist/index.mjs
191
+ function wrap(num, max) {
192
+ return (num % max + max) % max;
193
+ }
194
+ function roundToDevicePixel(num) {
195
+ if (typeof window === "undefined")
196
+ return Math.round(num);
197
+ const dp = window.devicePixelRatio;
198
+ return Math.floor(num * dp + 0.5) / dp;
199
+ }
200
+ function clamp(v, o) {
201
+ return Math.min(Math.max(valueOf(v), o.min), o.max);
202
+ }
203
+ function countDecimals(value) {
204
+ if (!Number.isFinite(value))
205
+ return 0;
206
+ let e = 1, p = 0;
207
+ while (Math.round(value * e) / e !== value) {
208
+ e *= 10;
209
+ p += 1;
210
+ }
211
+ return p;
212
+ }
213
+ var increment = (v, s) => decimalOperation(valueOf(v), "+", s);
214
+ var decrement = (v, s) => decimalOperation(valueOf(v), "-", s);
215
+ function valueOf(v) {
216
+ if (typeof v === "number")
217
+ return v;
218
+ const num = parseFloat(v.toString().replace(/[^\w.-]+/g, ""));
219
+ return !Number.isNaN(num) ? num : 0;
220
+ }
221
+ function formatDecimal(v, o) {
222
+ return new Intl.NumberFormat("en-US", {
223
+ useGrouping: false,
224
+ style: "decimal",
225
+ minimumFractionDigits: o.minFractionDigits,
226
+ maximumFractionDigits: o.maxFractionDigits
227
+ }).format(valueOf(v));
228
+ }
229
+ function isAtMax(v, o) {
230
+ const val = valueOf(v);
231
+ return val >= o.max;
232
+ }
233
+ function isAtMin(v, o) {
234
+ const val = valueOf(v);
235
+ return val <= o.min;
236
+ }
237
+ function isWithinRange(v, o) {
238
+ const val = valueOf(v);
239
+ return val >= o.min && val <= o.max;
240
+ }
241
+ function decimalOperation(a, op, b) {
242
+ let result = op === "+" ? a + b : a - b;
243
+ if (a % 1 !== 0 || b % 1 !== 0) {
244
+ const multiplier = 10 ** Math.max(countDecimals(a), countDecimals(b));
245
+ a = Math.round(a * multiplier);
246
+ b = Math.round(b * multiplier);
247
+ result = op === "+" ? a + b : a - b;
248
+ result /= multiplier;
249
+ }
250
+ return result;
251
+ }
252
+ var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
253
+
254
+ // src/number-input.dom.ts
255
+ var dom = defineDomHelpers({
256
+ getRootId: (ctx) => {
257
+ var _a;
258
+ return ((_a = ctx.ids) == null ? void 0 : _a.root) ?? `number-input:${ctx.id}`;
259
+ },
260
+ getInputId: (ctx) => {
261
+ var _a;
262
+ return ((_a = ctx.ids) == null ? void 0 : _a.input) ?? `number-input:${ctx.id}:input`;
263
+ },
264
+ getIncButtonId: (ctx) => {
265
+ var _a;
266
+ return ((_a = ctx.ids) == null ? void 0 : _a.incBtn) ?? `number-input:${ctx.id}:inc-btn`;
267
+ },
268
+ getDecButtonId: (ctx) => {
269
+ var _a;
270
+ return ((_a = ctx.ids) == null ? void 0 : _a.decBtn) ?? `number-input:${ctx.id}:dec-btn`;
271
+ },
272
+ getScrubberId: (ctx) => {
273
+ var _a;
274
+ return ((_a = ctx.ids) == null ? void 0 : _a.scrubber) ?? `number-input:${ctx.id}:scrubber`;
275
+ },
276
+ getCursorId: (ctx) => `number-input:${ctx.id}:cursor`,
277
+ getLabelId: (ctx) => {
278
+ var _a;
279
+ return ((_a = ctx.ids) == null ? void 0 : _a.label) ?? `number-input:${ctx.id}:label`;
280
+ },
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)),
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
+ },
324
+ getMousementValue(ctx, event) {
325
+ const x = roundToDevicePixel(event.movementX);
326
+ const y = roundToDevicePixel(event.movementY);
327
+ let hint = x > 0 ? "increment" : x < 0 ? "decrement" : null;
328
+ if (ctx.isRtl && hint === "increment")
329
+ hint = "decrement";
330
+ if (ctx.isRtl && hint === "decrement")
331
+ hint = "increment";
332
+ const point = {
333
+ x: ctx.scrubberCursorPoint.x + x,
334
+ y: ctx.scrubberCursorPoint.y + y
335
+ };
336
+ const win = dom.getWin(ctx);
337
+ const width = win.innerWidth;
338
+ const half = roundToDevicePixel(7.5);
339
+ point.x = wrap(point.x + half, width) - half;
340
+ return { hint, point };
341
+ },
342
+ createVirtualCursor(ctx) {
343
+ const doc = dom.getDoc(ctx);
344
+ const el = doc.createElement("div");
345
+ el.className = "scrubber--cursor";
346
+ el.id = dom.getCursorId(ctx);
347
+ Object.assign(el.style, {
348
+ width: "15px",
349
+ height: "15px",
350
+ position: "fixed",
351
+ pointerEvents: "none",
352
+ left: "0px",
353
+ top: "0px",
354
+ zIndex: MAX_Z_INDEX,
355
+ transform: ctx.scrubberCursorPoint ? `translate3d(${ctx.scrubberCursorPoint.x}px, ${ctx.scrubberCursorPoint.y}px, 0px)` : void 0,
356
+ willChange: "transform"
357
+ });
358
+ el.innerHTML = `
359
+ <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);">
360
+ <g transform="translate(2 3)">
361
+ <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>
362
+ <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>
363
+ </g>
364
+ </svg>`;
365
+ doc.body.appendChild(el);
366
+ }
367
+ });
368
+
369
+ // src/number-input.utils.ts
370
+ var utils = {
371
+ isValidNumericEvent: (ctx, event) => {
372
+ var _a;
373
+ if (event.key == null)
374
+ return true;
375
+ const isModifier = isModifiedEvent(event);
376
+ const isSingleKey = event.key.length === 1;
377
+ if (isModifier || !isSingleKey)
378
+ return true;
379
+ return ((_a = ctx.validateCharacter) == null ? void 0 : _a.call(ctx, event.key)) ?? utils.isFloatingPoint(event.key);
380
+ },
381
+ isFloatingPoint: (v) => /^[Ee0-9+\-.]$/.test(v),
382
+ sanitize: (ctx, value) => {
383
+ return value.split("").filter(ctx.validateCharacter ?? utils.isFloatingPoint).join("");
384
+ },
385
+ increment: (ctx, step) => {
386
+ const value = increment(ctx.value, step ?? ctx.step);
387
+ return formatDecimal(clamp(value, ctx), ctx);
388
+ },
389
+ decrement: (ctx, step) => {
390
+ const value = decrement(ctx.value, step ?? ctx.step);
391
+ return formatDecimal(clamp(value, ctx), ctx);
392
+ },
393
+ clamp: (ctx) => {
394
+ return formatDecimal(clamp(ctx.value, ctx), ctx);
395
+ },
396
+ parse: (ctx, value) => {
397
+ var _a;
398
+ return ((_a = ctx.parse) == null ? void 0 : _a.call(ctx, value)) ?? value;
399
+ },
400
+ format: (ctx, value) => {
401
+ var _a;
402
+ const _val = value.toString();
403
+ return ((_a = ctx.format) == null ? void 0 : _a.call(ctx, _val)) ?? _val;
404
+ },
405
+ round: (ctx) => {
406
+ return formatDecimal(ctx.value, ctx);
407
+ }
408
+ };
409
+
410
+ // src/number-input.connect.ts
411
+ function connect(state, send, normalize) {
412
+ const isFocused = state.hasTag("focus");
413
+ const isInvalid = state.context.isOutOfRange || !!state.context.invalid;
414
+ const isDisabled = !!state.context.disabled;
415
+ const isValueEmpty = state.context.isValueEmpty;
416
+ const isIncrementDisabled = isDisabled || !state.context.canIncrement;
417
+ const isDecrementDisabled = isDisabled || !state.context.canDecrement;
418
+ const messages = state.context.messages;
419
+ return {
420
+ isFocused,
421
+ isInvalid,
422
+ isValueEmpty,
423
+ value: state.context.formattedValue,
424
+ valueAsNumber: state.context.valueAsNumber,
425
+ setValue(value) {
426
+ send({ type: "SET_VALUE", value: value.toString() });
427
+ },
428
+ clearValue() {
429
+ send("CLEAR_VALUE");
430
+ },
431
+ increment() {
432
+ send("INCREMENT");
433
+ },
434
+ decrement() {
435
+ send("DECREMENT");
436
+ },
437
+ setToMax() {
438
+ send({ type: "SET_VALUE", value: state.context.max });
439
+ },
440
+ setToMin() {
441
+ send({ type: "SET_VALUE", value: state.context.min });
442
+ },
443
+ focus() {
444
+ var _a;
445
+ (_a = dom.getInputEl(state.context)) == null ? void 0 : _a.focus();
446
+ },
447
+ blur() {
448
+ var _a;
449
+ (_a = dom.getInputEl(state.context)) == null ? void 0 : _a.blur();
450
+ },
451
+ rootProps: normalize.element({
452
+ id: dom.getRootId(state.context),
453
+ "data-part": "root",
454
+ "data-disabled": dataAttr(isDisabled)
455
+ }),
456
+ labelProps: normalize.label({
457
+ "data-part": "label",
458
+ "data-disabled": dataAttr(isDisabled),
459
+ "data-invalid": dataAttr(isInvalid),
460
+ id: dom.getLabelId(state.context),
461
+ htmlFor: dom.getInputId(state.context)
462
+ }),
463
+ groupProps: normalize.element({
464
+ "data-part": "group",
465
+ role: "group",
466
+ "aria-disabled": isDisabled,
467
+ "data-disabled": dataAttr(isDisabled),
468
+ "data-invalid": dataAttr(isInvalid),
469
+ "aria-invalid": ariaAttr(state.context.invalid)
470
+ }),
471
+ inputProps: normalize.input({
472
+ "data-part": "input",
473
+ name: state.context.name,
474
+ id: dom.getInputId(state.context),
475
+ role: "spinbutton",
476
+ value: state.context.formattedValue,
477
+ pattern: state.context.pattern,
478
+ inputMode: state.context.inputMode,
479
+ "aria-invalid": isInvalid || void 0,
480
+ "data-invalid": dataAttr(isInvalid),
481
+ disabled: isDisabled,
482
+ "data-disabled": dataAttr(isDisabled),
483
+ readOnly: !!state.context.readonly,
484
+ autoComplete: "off",
485
+ autoCorrect: "off",
486
+ spellCheck: "false",
487
+ type: "text",
488
+ "aria-roledescription": !isIos() ? "number field" : void 0,
489
+ "aria-valuemin": state.context.min,
490
+ "aria-valuemax": state.context.max,
491
+ "aria-valuenow": isNaN(state.context.valueAsNumber) ? void 0 : state.context.valueAsNumber,
492
+ "aria-valuetext": state.context.valueText,
493
+ onFocus() {
494
+ send("FOCUS");
495
+ },
496
+ onBlur() {
497
+ send("BLUR");
498
+ },
499
+ onChange(event) {
500
+ const evt = getNativeEvent(event);
501
+ if (evt.isComposing)
502
+ return;
503
+ send({ type: "CHANGE", target: event.currentTarget, hint: "set" });
504
+ },
505
+ onKeyDown(event) {
506
+ const evt = getNativeEvent(event);
507
+ if (evt.isComposing)
508
+ return;
509
+ if (!utils.isValidNumericEvent(state.context, event)) {
510
+ event.preventDefault();
511
+ }
512
+ const step = getEventStep(event) * state.context.step;
513
+ const keyMap = {
514
+ ArrowUp() {
515
+ send({ type: "ARROW_UP", step });
516
+ },
517
+ ArrowDown() {
518
+ send({ type: "ARROW_DOWN", step });
519
+ },
520
+ Home() {
521
+ send("HOME");
522
+ },
523
+ End() {
524
+ send("END");
525
+ }
526
+ };
527
+ const exec = keyMap[event.key];
528
+ if (exec) {
529
+ exec(event);
530
+ event.preventDefault();
531
+ }
532
+ }
533
+ }),
534
+ decrementButtonProps: normalize.button({
535
+ "data-part": "spin-button",
536
+ "data-type": "decrement",
537
+ id: dom.getDecButtonId(state.context),
538
+ disabled: isDecrementDisabled,
539
+ "data-disabled": dataAttr(isDecrementDisabled),
540
+ "aria-label": messages.decrementLabel,
541
+ type: "button",
542
+ tabIndex: -1,
543
+ "aria-controls": dom.getInputId(state.context),
544
+ onPointerDown(event) {
545
+ if (isDecrementDisabled)
546
+ return;
547
+ send(isLeftClick(event) ? { type: "PRESS_DOWN", hint: "decrement" } : { type: "FOCUS" });
548
+ event.preventDefault();
549
+ },
550
+ onPointerUp() {
551
+ send({ type: "PRESS_UP", hint: "decrement" });
552
+ },
553
+ onPointerLeave() {
554
+ if (isDecrementDisabled)
555
+ return;
556
+ send({ type: "PRESS_UP", hint: "decrement" });
557
+ }
558
+ }),
559
+ incrementButtonProps: normalize.button({
560
+ "data-part": "spin-button",
561
+ "data-type": "increment",
562
+ id: dom.getIncButtonId(state.context),
563
+ disabled: isIncrementDisabled,
564
+ "data-disabled": dataAttr(isIncrementDisabled),
565
+ "aria-label": messages.incrementLabel,
566
+ type: "button",
567
+ tabIndex: -1,
568
+ "aria-controls": dom.getInputId(state.context),
569
+ onPointerDown(event) {
570
+ if (isIncrementDisabled)
571
+ return;
572
+ send(isLeftClick(event) ? { type: "PRESS_DOWN", hint: "increment" } : { type: "FOCUS" });
573
+ event.preventDefault();
574
+ },
575
+ onPointerUp() {
576
+ send({ type: "PRESS_UP", hint: "increment" });
577
+ },
578
+ onPointerLeave() {
579
+ send({ type: "PRESS_UP", hint: "increment" });
580
+ }
581
+ }),
582
+ scrubberProps: normalize.element({
583
+ "data-disabled": dataAttr(isDisabled),
584
+ "data-part": "scrubber",
585
+ id: dom.getScrubberId(state.context),
586
+ role: "presentation",
587
+ onMouseDown(event) {
588
+ if (isDisabled)
589
+ return;
590
+ const evt = getNativeEvent(event);
591
+ event.preventDefault();
592
+ const point = getEventPoint(evt);
593
+ point.x = point.x - roundToDevicePixel(7.5);
594
+ point.y = point.y - roundToDevicePixel(7.5);
595
+ send({ type: "PRESS_DOWN_SCRUBBER", point });
596
+ },
597
+ style: {
598
+ cursor: isDisabled ? void 0 : "ew-resize"
599
+ }
600
+ })
601
+ };
602
+ }
603
+
604
+ // src/number-input.machine.ts
605
+ var import_core = require("@zag-js/core");
606
+
607
+ // ../../utilities/core/dist/index.mjs
608
+ var callAll2 = (...fns) => (...a) => {
609
+ fns.forEach(function(fn) {
610
+ fn == null ? void 0 : fn(...a);
611
+ });
612
+ };
613
+
614
+ // src/number-input.machine.ts
615
+ var { not, and } = import_core.guards;
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
+ }
642
+ },
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;
659
+ }
660
+ },
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"]
678
+ }
679
+ },
680
+ states: {
681
+ unknown: {
682
+ on: {
683
+ SETUP: {
684
+ target: "idle",
685
+ actions: "syncInputValue"
686
+ }
687
+ }
688
+ },
689
+ idle: {
690
+ exit: "invokeOnFocus",
691
+ on: {
692
+ PRESS_DOWN: {
693
+ target: "before:spin",
694
+ actions: ["focusInput", "setHint"]
695
+ },
696
+ PRESS_DOWN_SCRUBBER: {
697
+ target: "scrubbing",
698
+ actions: ["focusInput", "setHint", "setCursorPoint"]
699
+ },
700
+ FOCUS: "focused"
701
+ }
702
+ },
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
+ ]
747
+ }
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
+ }
761
+ },
762
+ on: {
763
+ PRESS_UP: {
764
+ target: "focused",
765
+ actions: "clearHint"
766
+ }
767
+ }
768
+ },
769
+ spinning: {
770
+ tags: "focus",
771
+ activities: "trackButtonDisabled",
772
+ every: [
773
+ {
774
+ delay: "CHANGE_INTERVAL",
775
+ guard: and(not("isAtMin"), "isIncrementHint"),
776
+ actions: "increment"
777
+ },
778
+ {
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"
788
+ }
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
+ }
808
+ }
809
+ }
810
+ },
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)
841
+ return;
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");
877
+ }
878
+ return callAll2(
879
+ addDomEvent(doc, "mousemove", onMousemove, false),
880
+ addDomEvent(doc, "mouseup", onMouseup, false)
881
+ );
882
+ }
883
+ },
884
+ actions: {
885
+ focusInput(ctx2) {
886
+ if (!ctx2.focusInputOnChange)
887
+ return;
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)
961
+ return;
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
967
+ });
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)`;
988
+ }
989
+ },
990
+ hookSync: true
991
+ }
992
+ );
993
+ }
994
+ // Annotate the CommonJS export names for ESM import in node:
995
+ 0 && (module.exports = {
996
+ connect,
997
+ machine
998
+ });