@zag-js/number-input 1.46.0 → 1.48.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.
Files changed (41) hide show
  1. package/dist/cursor.d.mts +8 -10
  2. package/dist/cursor.d.ts +8 -10
  3. package/dist/cursor.js +65 -104
  4. package/dist/cursor.mjs +59 -77
  5. package/dist/index.d.mts +6 -9
  6. package/dist/index.d.ts +6 -9
  7. package/dist/index.js +12 -38
  8. package/dist/index.mjs +2 -7
  9. package/dist/number-input.anatomy.d.mts +2 -6
  10. package/dist/number-input.anatomy.d.ts +2 -6
  11. package/dist/number-input.anatomy.js +8 -42
  12. package/dist/number-input.anatomy.mjs +4 -16
  13. package/dist/number-input.connect.d.mts +3 -8
  14. package/dist/number-input.connect.d.ts +3 -8
  15. package/dist/number-input.connect.js +310 -310
  16. package/dist/number-input.connect.mjs +304 -284
  17. package/dist/number-input.dom.d.mts +28 -32
  18. package/dist/number-input.dom.d.ts +28 -32
  19. package/dist/number-input.dom.js +96 -138
  20. package/dist/number-input.dom.mjs +76 -97
  21. package/dist/number-input.machine.d.mts +2 -8
  22. package/dist/number-input.machine.d.ts +2 -8
  23. package/dist/number-input.machine.js +484 -460
  24. package/dist/number-input.machine.mjs +478 -440
  25. package/dist/number-input.props.d.mts +3 -9
  26. package/dist/number-input.props.d.ts +3 -9
  27. package/dist/number-input.props.js +40 -65
  28. package/dist/number-input.props.mjs +36 -39
  29. package/dist/number-input.translations.d.mts +3 -8
  30. package/dist/number-input.translations.d.ts +3 -8
  31. package/dist/number-input.translations.js +8 -31
  32. package/dist/number-input.translations.mjs +4 -6
  33. package/dist/number-input.types.d.mts +298 -300
  34. package/dist/number-input.types.d.ts +298 -300
  35. package/dist/number-input.types.js +4 -18
  36. package/dist/number-input.types.mjs +1 -0
  37. package/dist/number-input.utils.d.mts +9 -12
  38. package/dist/number-input.utils.d.ts +9 -12
  39. package/dist/number-input.utils.js +28 -57
  40. package/dist/number-input.utils.mjs +20 -28
  41. package/package.json +8 -8
@@ -1,462 +1,486 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/number-input.machine.ts
31
- var number_input_machine_exports = {};
32
- __export(number_input_machine_exports, {
33
- machine: () => machine
34
- });
35
- module.exports = __toCommonJS(number_input_machine_exports);
36
- var import_core = require("@zag-js/core");
37
- var import_dom_query = require("@zag-js/dom-query");
38
- var import_utils = require("@zag-js/utils");
39
- var import_cursor = require("./cursor.js");
40
- var dom = __toESM(require("./number-input.dom.js"));
41
- var import_number_input = require("./number-input.translations.js");
42
- var import_number_input2 = require("./number-input.utils.js");
43
- var { choose, guards, createMachine } = (0, import_core.setup)();
44
- var { not, and } = guards;
45
- var machine = createMachine({
46
- props({ props }) {
47
- const step = (0, import_number_input2.getDefaultStep)(props.step, props.formatOptions);
48
- return {
49
- dir: "ltr",
50
- locale: "en-US",
51
- focusInputOnChange: true,
52
- clampValueOnBlur: !props.allowOverflow,
53
- allowOverflow: false,
54
- inputMode: "decimal",
55
- pattern: "-?[0-9]*(.[0-9]+)?",
56
- defaultValue: "",
57
- step,
58
- min: Number.MIN_SAFE_INTEGER,
59
- max: Number.MAX_SAFE_INTEGER,
60
- spinOnPress: true,
61
- ...props,
62
- largeStep: props.largeStep ?? 10 * step,
63
- smallStep: props.smallStep ?? step / 10
64
- };
65
- },
66
- initialState() {
67
- return "idle";
68
- },
69
- context({ prop, bindable, getComputed }) {
70
- return {
71
- value: bindable(() => ({
72
- defaultValue: prop("defaultValue"),
73
- value: prop("value"),
74
- onChange(value) {
75
- const computed = getComputed();
76
- const valueAsNumber = (0, import_number_input2.parseValue)(value, { computed, prop });
77
- prop("onValueChange")?.({ value, valueAsNumber });
78
- }
79
- })),
80
- hint: bindable(() => ({ defaultValue: null })),
81
- scrubberCursorPoint: bindable(() => ({
82
- defaultValue: null,
83
- hash(value) {
84
- return value ? `x:${value.x}, y:${value.y}` : "";
85
- }
86
- })),
87
- fieldsetDisabled: bindable(() => ({ defaultValue: false }))
88
- };
89
- },
90
- computed: {
91
- isRtl: ({ prop }) => prop("dir") === "rtl",
92
- valueAsNumber: ({ context, computed, prop }) => (0, import_number_input2.parseValue)(context.get("value"), { computed, prop }),
93
- formattedValue: ({ computed, prop }) => (0, import_number_input2.formatValue)(computed("valueAsNumber"), { computed, prop }),
94
- isAtMin: ({ computed, prop }) => (0, import_utils.isValueAtMin)(computed("valueAsNumber"), prop("min")),
95
- isAtMax: ({ computed, prop }) => (0, import_utils.isValueAtMax)(computed("valueAsNumber"), prop("max")),
96
- isOutOfRange: ({ computed, prop }) => !(0, import_utils.isValueWithinRange)(computed("valueAsNumber"), prop("min"), prop("max")),
97
- isValueEmpty: ({ context }) => context.get("value") === "",
98
- isDisabled: ({ prop, context }) => !!prop("disabled") || context.get("fieldsetDisabled"),
99
- canIncrement: ({ prop, computed }) => prop("allowOverflow") || !computed("isAtMax"),
100
- canDecrement: ({ prop, computed }) => prop("allowOverflow") || !computed("isAtMin"),
101
- valueText: ({ prop, context }) => {
102
- const translations = (0, import_utils.mergeWithDefault)(import_number_input.defaultTranslations, prop("translations"));
103
- return translations.valueText?.(context.get("value"));
104
- },
105
- formatter: (0, import_core.memo)(
106
- ({ prop }) => [prop("locale"), prop("formatOptions")],
107
- ([locale, formatOptions]) => (0, import_number_input2.createFormatter)(locale, formatOptions)
108
- ),
109
- parser: (0, import_core.memo)(
110
- ({ prop }) => [prop("locale"), prop("formatOptions")],
111
- ([locale, formatOptions]) => (0, import_number_input2.createParser)(locale, formatOptions)
112
- )
113
- },
114
- watch({ track, action, context, computed, prop }) {
115
- track([() => context.get("value"), () => prop("locale"), () => JSON.stringify(prop("formatOptions"))], () => {
116
- action(["syncInputElement"]);
117
- });
118
- track([() => computed("isOutOfRange")], () => {
119
- action(["invokeOnInvalid"]);
120
- });
121
- track([() => context.hash("scrubberCursorPoint")], () => {
122
- action(["setVirtualCursorPosition"]);
123
- });
124
- },
125
- effects: ["trackFormControl"],
126
- on: {
127
- "VALUE.SET": {
128
- actions: ["setRawValue"]
129
- },
130
- "VALUE.CLEAR": {
131
- actions: ["clearValue"]
132
- },
133
- "VALUE.INCREMENT": {
134
- actions: ["increment"]
135
- },
136
- "VALUE.DECREMENT": {
137
- actions: ["decrement"]
138
- }
139
- },
140
- states: {
141
- idle: {
142
- on: {
143
- "TRIGGER.PRESS_DOWN": [
144
- { guard: "isTouchPointer", target: "before:spin", actions: ["setHint"] },
145
- {
146
- target: "before:spin",
147
- actions: ["focusInput", "invokeOnFocus", "setHint"]
148
- }
149
- ],
150
- "SCRUBBER.PRESS_DOWN": {
151
- target: "scrubbing",
152
- actions: ["focusInput", "invokeOnFocus", "setHint", "setCursorPoint"]
153
- },
154
- "INPUT.FOCUS": {
155
- target: "focused",
156
- actions: ["focusInput", "invokeOnFocus"]
157
- }
158
- }
159
- },
160
- focused: {
161
- tags: ["focus"],
162
- effects: ["attachWheelListener"],
163
- on: {
164
- "TRIGGER.PRESS_DOWN": [
165
- { guard: "isTouchPointer", target: "before:spin", actions: ["setHint"] },
166
- { target: "before:spin", actions: ["focusInput", "setHint"] }
167
- ],
168
- "SCRUBBER.PRESS_DOWN": {
169
- target: "scrubbing",
170
- actions: ["focusInput", "setHint", "setCursorPoint"]
171
- },
172
- "INPUT.ARROW_UP": {
173
- actions: ["increment"]
174
- },
175
- "INPUT.ARROW_DOWN": {
176
- actions: ["decrement"]
177
- },
178
- "INPUT.HOME": {
179
- actions: ["decrementToMin"]
180
- },
181
- "INPUT.END": {
182
- actions: ["incrementToMax"]
183
- },
184
- "INPUT.CHANGE": {
185
- actions: ["setValue", "setHint"]
186
- },
187
- "INPUT.BLUR": [
188
- {
189
- guard: and("clampValueOnBlur", not("isValueEmpty"), not("isInRange")),
190
- target: "idle",
191
- actions: ["setClampedValue", "clearHint", "invokeOnBlur", "invokeOnValueCommit"]
192
- },
193
- {
194
- guard: not("isInRange"),
195
- target: "idle",
196
- actions: ["setFormattedValue", "clearHint", "invokeOnBlur", "invokeOnInvalid", "invokeOnValueCommit"]
197
- },
198
- {
199
- target: "idle",
200
- actions: ["setFormattedValue", "clearHint", "invokeOnBlur", "invokeOnValueCommit"]
201
- }
202
- ],
203
- "INPUT.ENTER": {
204
- actions: ["setFormattedValue", "clearHint", "invokeOnBlur", "invokeOnValueCommit"]
205
- }
206
- }
207
- },
208
- "before:spin": {
209
- tags: ["focus"],
210
- effects: ["trackButtonDisabled", "waitForChangeDelay"],
211
- entry: choose([
212
- { guard: "isIncrementHint", actions: ["increment"] },
213
- { guard: "isDecrementHint", actions: ["decrement"] }
214
- ]),
215
- on: {
216
- CHANGE_DELAY: {
217
- target: "spinning",
218
- guard: and("isInRange", "spinOnPress")
219
- },
220
- "TRIGGER.PRESS_UP": [
221
- { guard: "isTouchPointer", target: "focused", actions: ["clearHint"] },
222
- { target: "focused", actions: ["focusInput", "clearHint"] }
223
- ]
224
- }
225
- },
226
- spinning: {
227
- tags: ["focus"],
228
- effects: ["trackButtonDisabled", "spinValue"],
229
- on: {
230
- SPIN: [
231
- {
232
- guard: "isIncrementHint",
233
- actions: ["increment"]
234
- },
235
- {
236
- guard: "isDecrementHint",
237
- actions: ["decrement"]
238
- }
239
- ],
240
- "TRIGGER.PRESS_UP": {
241
- target: "focused",
242
- actions: ["focusInput", "clearHint"]
243
- }
244
- }
245
- },
246
- scrubbing: {
247
- tags: ["focus"],
248
- effects: ["activatePointerLock", "trackMousemove", "setupVirtualCursor", "preventTextSelection"],
249
- on: {
250
- "SCRUBBER.POINTER_UP": {
251
- target: "focused",
252
- actions: ["focusInput", "clearCursorPoint"]
253
- },
254
- "SCRUBBER.POINTER_MOVE": [
255
- {
256
- guard: "isIncrementHint",
257
- actions: ["increment", "setCursorPoint"]
258
- },
259
- {
260
- guard: "isDecrementHint",
261
- actions: ["decrement", "setCursorPoint"]
262
- }
263
- ]
264
- }
265
- }
266
- },
267
- implementations: {
268
- guards: {
269
- clampValueOnBlur: ({ prop }) => prop("clampValueOnBlur"),
270
- spinOnPress: ({ prop }) => !!prop("spinOnPress"),
271
- isInRange: ({ computed }) => !computed("isOutOfRange"),
272
- isValueEmpty: ({ computed }) => computed("isValueEmpty"),
273
- isDecrementHint: ({ context, event }) => (event.hint ?? context.get("hint")) === "decrement",
274
- isIncrementHint: ({ context, event }) => (event.hint ?? context.get("hint")) === "increment",
275
- isTouchPointer: ({ event }) => event.pointerType === "touch"
276
- },
277
- effects: {
278
- waitForChangeDelay({ send }) {
279
- const id = setTimeout(() => {
280
- send({ type: "CHANGE_DELAY" });
281
- }, 300);
282
- return () => clearTimeout(id);
283
- },
284
- spinValue({ send }) {
285
- const id = setInterval(() => {
286
- send({ type: "SPIN" });
287
- }, 50);
288
- return () => clearInterval(id);
289
- },
290
- trackFormControl({ context, scope }) {
291
- const inputEl = dom.getInputEl(scope);
292
- return (0, import_dom_query.trackFormControl)(inputEl, {
293
- onFieldsetDisabledChange(disabled) {
294
- context.set("fieldsetDisabled", disabled);
295
- },
296
- onFormReset() {
297
- context.set("value", context.initial("value"));
298
- }
299
- });
300
- },
301
- setupVirtualCursor({ context, scope }) {
302
- const point = context.get("scrubberCursorPoint");
303
- return dom.setupVirtualCursor(scope, point);
304
- },
305
- preventTextSelection({ scope }) {
306
- return dom.preventTextSelection(scope);
307
- },
308
- trackButtonDisabled({ context, scope, send }) {
309
- const hint = context.get("hint");
310
- const btn = dom.getPressedTriggerEl(scope, hint);
311
- return (0, import_dom_query.observeAttributes)(btn, {
312
- attributes: ["disabled"],
313
- callback() {
314
- send({ type: "TRIGGER.PRESS_UP", src: "attr" });
315
- }
316
- });
317
- },
318
- attachWheelListener({ scope, send, prop }) {
319
- const inputEl = dom.getInputEl(scope);
320
- if (!inputEl || !scope.isActiveElement(inputEl) || !prop("allowMouseWheel")) return;
321
- function onWheel(event) {
322
- event.preventDefault();
323
- const dir = Math.sign(event.deltaY) * -1;
324
- if (dir === 1) {
325
- send({ type: "VALUE.INCREMENT" });
326
- } else if (dir === -1) {
327
- send({ type: "VALUE.DECREMENT" });
328
- }
329
- }
330
- return (0, import_dom_query.addDomEvent)(inputEl, "wheel", onWheel, { passive: false });
331
- },
332
- activatePointerLock({ scope }) {
333
- if ((0, import_dom_query.isSafari)()) return;
334
- return (0, import_dom_query.requestPointerLock)(scope.getDoc());
335
- },
336
- trackMousemove({ scope, send, context, computed }) {
337
- const doc = scope.getDoc();
338
- function onMousemove(event) {
339
- const point = context.get("scrubberCursorPoint");
340
- const isRtl = computed("isRtl");
341
- const value = dom.getMousemoveValue(scope, { point, isRtl, event });
342
- if (!value.hint) return;
343
- send({
344
- type: "SCRUBBER.POINTER_MOVE",
345
- hint: value.hint,
346
- point: value.point
347
- });
348
- }
349
- function onMouseup() {
350
- send({ type: "SCRUBBER.POINTER_UP" });
351
- }
352
- return (0, import_utils.callAll)((0, import_dom_query.addDomEvent)(doc, "mousemove", onMousemove, false), (0, import_dom_query.addDomEvent)(doc, "mouseup", onMouseup, false));
353
- }
354
- },
355
- actions: {
356
- focusInput({ scope, prop }) {
357
- if (!prop("focusInputOnChange")) return;
358
- const inputEl = dom.getInputEl(scope);
359
- if (scope.isActiveElement(inputEl)) return;
360
- (0, import_dom_query.raf)(() => inputEl?.focus({ preventScroll: true }));
361
- },
362
- increment({ context, event, prop, computed }) {
363
- let nextValue = (0, import_utils.incrementValue)(computed("valueAsNumber"), event.step ?? prop("step"));
364
- if (!prop("allowOverflow")) nextValue = (0, import_utils.clampValue)(nextValue, prop("min"), prop("max"));
365
- context.set("value", (0, import_number_input2.formatValue)(nextValue, { computed, prop }));
366
- },
367
- decrement({ context, event, prop, computed }) {
368
- let nextValue = (0, import_utils.decrementValue)(computed("valueAsNumber"), event.step ?? prop("step"));
369
- if (!prop("allowOverflow")) nextValue = (0, import_utils.clampValue)(nextValue, prop("min"), prop("max"));
370
- context.set("value", (0, import_number_input2.formatValue)(nextValue, { computed, prop }));
371
- },
372
- setClampedValue({ context, prop, computed }) {
373
- const nextValue = (0, import_utils.clampValue)(computed("valueAsNumber"), prop("min"), prop("max"));
374
- context.set("value", (0, import_number_input2.formatValue)(nextValue, { computed, prop }));
375
- },
376
- setRawValue({ context, event, prop, computed }) {
377
- let nextValue = typeof event.value === "number" ? event.value : (0, import_number_input2.parseValue)(event.value, { computed, prop });
378
- if (!prop("allowOverflow")) nextValue = (0, import_utils.clampValue)(nextValue, prop("min"), prop("max"));
379
- context.set("value", (0, import_number_input2.formatValue)(nextValue, { computed, prop }));
380
- },
381
- setValue({ context, event }) {
382
- const value = event.target?.value ?? event.value;
383
- context.set("value", value);
384
- },
385
- clearValue({ context }) {
386
- context.set("value", "");
387
- },
388
- incrementToMax({ context, prop, computed }) {
389
- const value = (0, import_number_input2.formatValue)(prop("max"), { computed, prop });
390
- context.set("value", value);
391
- },
392
- decrementToMin({ context, prop, computed }) {
393
- const value = (0, import_number_input2.formatValue)(prop("min"), { computed, prop });
394
- context.set("value", value);
395
- },
396
- setHint({ context, event }) {
397
- context.set("hint", event.hint);
398
- },
399
- clearHint({ context }) {
400
- context.set("hint", null);
401
- },
402
- invokeOnFocus({ computed, prop }) {
403
- prop("onFocusChange")?.({
404
- focused: true,
405
- value: computed("formattedValue"),
406
- valueAsNumber: computed("valueAsNumber")
407
- });
408
- },
409
- invokeOnBlur({ computed, prop }) {
410
- prop("onFocusChange")?.({
411
- focused: false,
412
- value: computed("formattedValue"),
413
- valueAsNumber: computed("valueAsNumber")
414
- });
415
- },
416
- invokeOnInvalid({ computed, prop, event }) {
417
- if (event.type === "INPUT.CHANGE") return;
418
- const reason = computed("valueAsNumber") > prop("max") ? "rangeOverflow" : "rangeUnderflow";
419
- prop("onValueInvalid")?.({
420
- reason,
421
- value: computed("formattedValue"),
422
- valueAsNumber: computed("valueAsNumber")
423
- });
424
- },
425
- invokeOnValueCommit({ computed, prop }) {
426
- prop("onValueCommit")?.({
427
- value: computed("formattedValue"),
428
- valueAsNumber: computed("valueAsNumber")
429
- });
430
- },
431
- syncInputElement({ context, event, computed, scope }) {
432
- const value = event.type.endsWith("CHANGE") ? context.get("value") : computed("formattedValue");
433
- const inputEl = dom.getInputEl(scope);
434
- const sel = event.selection ?? (0, import_cursor.recordCursor)(inputEl, scope);
435
- (0, import_dom_query.raf)(() => {
436
- (0, import_dom_query.setElementValue)(inputEl, value);
437
- (0, import_cursor.restoreCursor)(inputEl, sel, scope);
438
- });
439
- },
440
- setFormattedValue({ context, computed, action }) {
441
- context.set("value", computed("formattedValue"));
442
- action(["syncInputElement"]);
443
- },
444
- setCursorPoint({ context, event }) {
445
- context.set("scrubberCursorPoint", event.point);
446
- },
447
- clearCursorPoint({ context }) {
448
- context.set("scrubberCursorPoint", null);
449
- },
450
- setVirtualCursorPosition({ context, scope }) {
451
- const cursorEl = dom.getCursorEl(scope);
452
- const point = context.get("scrubberCursorPoint");
453
- if (!cursorEl || !point) return;
454
- cursorEl.style.transform = `translate3d(${point.x}px, ${point.y}px, 0px)`;
455
- }
456
- }
457
- }
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
458
4
  });
459
- // Annotate the CommonJS export names for ESM import in node:
460
- 0 && (module.exports = {
461
- machine
5
+ const require_cursor = require("./cursor.js");
6
+ const require_number_input_dom = require("./number-input.dom.js");
7
+ const require_number_input_translations = require("./number-input.translations.js");
8
+ const require_number_input_utils = require("./number-input.utils.js");
9
+ let _zag_js_dom_query = require("@zag-js/dom-query");
10
+ let _zag_js_utils = require("@zag-js/utils");
11
+ let _zag_js_core = require("@zag-js/core");
12
+ // src/number-input.machine.ts
13
+ const { choose, guards, createMachine } = (0, _zag_js_core.setup)();
14
+ const { not, and } = guards;
15
+ const machine = createMachine({
16
+ props({ props }) {
17
+ const step = require_number_input_utils.getDefaultStep(props.step, props.formatOptions);
18
+ return {
19
+ dir: "ltr",
20
+ locale: "en-US",
21
+ focusInputOnChange: true,
22
+ clampValueOnBlur: !props.allowOverflow,
23
+ allowOverflow: false,
24
+ inputMode: "decimal",
25
+ pattern: "-?[0-9]*(.[0-9]+)?",
26
+ defaultValue: "",
27
+ step,
28
+ min: Number.MIN_SAFE_INTEGER,
29
+ max: Number.MAX_SAFE_INTEGER,
30
+ spinOnPress: true,
31
+ ...props,
32
+ largeStep: props.largeStep ?? 10 * step,
33
+ smallStep: props.smallStep ?? step / 10
34
+ };
35
+ },
36
+ initialState() {
37
+ return "idle";
38
+ },
39
+ context({ prop, bindable, getComputed }) {
40
+ return {
41
+ value: bindable(() => ({
42
+ defaultValue: prop("defaultValue"),
43
+ value: prop("value"),
44
+ onChange(value) {
45
+ const computed = getComputed();
46
+ const valueAsNumber = require_number_input_utils.parseValue(value, {
47
+ computed,
48
+ prop
49
+ });
50
+ prop("onValueChange")?.({
51
+ value,
52
+ valueAsNumber
53
+ });
54
+ }
55
+ })),
56
+ hint: bindable(() => ({ defaultValue: null })),
57
+ scrubberCursorPoint: bindable(() => ({
58
+ defaultValue: null,
59
+ hash(value) {
60
+ return value ? `x:${value.x}, y:${value.y}` : "";
61
+ }
62
+ })),
63
+ fieldsetDisabled: bindable(() => ({ defaultValue: false }))
64
+ };
65
+ },
66
+ computed: {
67
+ isRtl: ({ prop }) => prop("dir") === "rtl",
68
+ valueAsNumber: ({ context, computed, prop }) => require_number_input_utils.parseValue(context.get("value"), {
69
+ computed,
70
+ prop
71
+ }),
72
+ formattedValue: ({ computed, prop }) => require_number_input_utils.formatValue(computed("valueAsNumber"), {
73
+ computed,
74
+ prop
75
+ }),
76
+ isAtMin: ({ computed, prop }) => (0, _zag_js_utils.isValueAtMin)(computed("valueAsNumber"), prop("min")),
77
+ isAtMax: ({ computed, prop }) => (0, _zag_js_utils.isValueAtMax)(computed("valueAsNumber"), prop("max")),
78
+ isOutOfRange: ({ computed, prop }) => !(0, _zag_js_utils.isValueWithinRange)(computed("valueAsNumber"), prop("min"), prop("max")),
79
+ isValueEmpty: ({ context }) => context.get("value") === "",
80
+ isDisabled: ({ prop, context }) => !!prop("disabled") || context.get("fieldsetDisabled"),
81
+ canIncrement: ({ prop, computed }) => prop("allowOverflow") || !computed("isAtMax"),
82
+ canDecrement: ({ prop, computed }) => prop("allowOverflow") || !computed("isAtMin"),
83
+ valueText: ({ prop, context }) => {
84
+ return (0, _zag_js_utils.mergeWithDefault)(require_number_input_translations.defaultTranslations, prop("translations")).valueText?.(context.get("value"));
85
+ },
86
+ formatter: (0, _zag_js_core.memo)(({ prop }) => [prop("locale"), prop("formatOptions")], ([locale, formatOptions]) => require_number_input_utils.createFormatter(locale, formatOptions)),
87
+ parser: (0, _zag_js_core.memo)(({ prop }) => [prop("locale"), prop("formatOptions")], ([locale, formatOptions]) => require_number_input_utils.createParser(locale, formatOptions))
88
+ },
89
+ watch({ track, action, context, computed, prop }) {
90
+ track([
91
+ () => context.get("value"),
92
+ () => prop("locale"),
93
+ () => JSON.stringify(prop("formatOptions"))
94
+ ], () => {
95
+ action(["syncInputElement"]);
96
+ });
97
+ track([() => computed("isOutOfRange")], () => {
98
+ action(["invokeOnInvalid"]);
99
+ });
100
+ track([() => context.hash("scrubberCursorPoint")], () => {
101
+ action(["setVirtualCursorPosition"]);
102
+ });
103
+ },
104
+ effects: ["trackFormControl"],
105
+ on: {
106
+ "VALUE.SET": { actions: ["setRawValue"] },
107
+ "VALUE.CLEAR": { actions: ["clearValue"] },
108
+ "VALUE.INCREMENT": { actions: ["increment"] },
109
+ "VALUE.DECREMENT": { actions: ["decrement"] }
110
+ },
111
+ states: {
112
+ idle: { on: {
113
+ "TRIGGER.PRESS_DOWN": [{
114
+ guard: "isTouchPointer",
115
+ target: "before:spin",
116
+ actions: ["setHint"]
117
+ }, {
118
+ target: "before:spin",
119
+ actions: [
120
+ "focusInput",
121
+ "invokeOnFocus",
122
+ "setHint"
123
+ ]
124
+ }],
125
+ "SCRUBBER.PRESS_DOWN": {
126
+ target: "scrubbing",
127
+ actions: [
128
+ "focusInput",
129
+ "invokeOnFocus",
130
+ "setHint",
131
+ "setCursorPoint"
132
+ ]
133
+ },
134
+ "INPUT.FOCUS": {
135
+ target: "focused",
136
+ actions: ["focusInput", "invokeOnFocus"]
137
+ }
138
+ } },
139
+ focused: {
140
+ tags: ["focus"],
141
+ effects: ["attachWheelListener"],
142
+ on: {
143
+ "TRIGGER.PRESS_DOWN": [{
144
+ guard: "isTouchPointer",
145
+ target: "before:spin",
146
+ actions: ["setHint"]
147
+ }, {
148
+ target: "before:spin",
149
+ actions: ["focusInput", "setHint"]
150
+ }],
151
+ "SCRUBBER.PRESS_DOWN": {
152
+ target: "scrubbing",
153
+ actions: [
154
+ "focusInput",
155
+ "setHint",
156
+ "setCursorPoint"
157
+ ]
158
+ },
159
+ "INPUT.ARROW_UP": { actions: ["increment"] },
160
+ "INPUT.ARROW_DOWN": { actions: ["decrement"] },
161
+ "INPUT.HOME": { actions: ["decrementToMin"] },
162
+ "INPUT.END": { actions: ["incrementToMax"] },
163
+ "INPUT.CHANGE": { actions: ["setValue", "setHint"] },
164
+ "INPUT.BLUR": [
165
+ {
166
+ guard: and("clampValueOnBlur", not("isValueEmpty"), not("isInRange")),
167
+ target: "idle",
168
+ actions: [
169
+ "setClampedValue",
170
+ "clearHint",
171
+ "invokeOnBlur",
172
+ "invokeOnValueCommit"
173
+ ]
174
+ },
175
+ {
176
+ guard: not("isInRange"),
177
+ target: "idle",
178
+ actions: [
179
+ "setFormattedValue",
180
+ "clearHint",
181
+ "invokeOnBlur",
182
+ "invokeOnInvalid",
183
+ "invokeOnValueCommit"
184
+ ]
185
+ },
186
+ {
187
+ target: "idle",
188
+ actions: [
189
+ "setFormattedValue",
190
+ "clearHint",
191
+ "invokeOnBlur",
192
+ "invokeOnValueCommit"
193
+ ]
194
+ }
195
+ ],
196
+ "INPUT.ENTER": { actions: [
197
+ "setFormattedValue",
198
+ "clearHint",
199
+ "invokeOnBlur",
200
+ "invokeOnValueCommit"
201
+ ] }
202
+ }
203
+ },
204
+ "before:spin": {
205
+ tags: ["focus"],
206
+ effects: ["trackButtonDisabled", "waitForChangeDelay"],
207
+ entry: choose([{
208
+ guard: "isIncrementHint",
209
+ actions: ["increment"]
210
+ }, {
211
+ guard: "isDecrementHint",
212
+ actions: ["decrement"]
213
+ }]),
214
+ on: {
215
+ CHANGE_DELAY: {
216
+ target: "spinning",
217
+ guard: and("isInRange", "spinOnPress")
218
+ },
219
+ "TRIGGER.PRESS_UP": [{
220
+ guard: "isTouchPointer",
221
+ target: "focused",
222
+ actions: ["clearHint"]
223
+ }, {
224
+ target: "focused",
225
+ actions: ["focusInput", "clearHint"]
226
+ }]
227
+ }
228
+ },
229
+ spinning: {
230
+ tags: ["focus"],
231
+ effects: ["trackButtonDisabled", "spinValue"],
232
+ on: {
233
+ SPIN: [{
234
+ guard: "isIncrementHint",
235
+ actions: ["increment"]
236
+ }, {
237
+ guard: "isDecrementHint",
238
+ actions: ["decrement"]
239
+ }],
240
+ "TRIGGER.PRESS_UP": {
241
+ target: "focused",
242
+ actions: ["focusInput", "clearHint"]
243
+ }
244
+ }
245
+ },
246
+ scrubbing: {
247
+ tags: ["focus"],
248
+ effects: [
249
+ "activatePointerLock",
250
+ "trackMousemove",
251
+ "setupVirtualCursor",
252
+ "preventTextSelection"
253
+ ],
254
+ on: {
255
+ "SCRUBBER.POINTER_UP": {
256
+ target: "focused",
257
+ actions: ["focusInput", "clearCursorPoint"]
258
+ },
259
+ "SCRUBBER.POINTER_MOVE": [{
260
+ guard: "isIncrementHint",
261
+ actions: ["increment", "setCursorPoint"]
262
+ }, {
263
+ guard: "isDecrementHint",
264
+ actions: ["decrement", "setCursorPoint"]
265
+ }]
266
+ }
267
+ }
268
+ },
269
+ implementations: {
270
+ guards: {
271
+ clampValueOnBlur: ({ prop }) => prop("clampValueOnBlur"),
272
+ spinOnPress: ({ prop }) => !!prop("spinOnPress"),
273
+ isInRange: ({ computed }) => !computed("isOutOfRange"),
274
+ isValueEmpty: ({ computed }) => computed("isValueEmpty"),
275
+ isDecrementHint: ({ context, event }) => (event.hint ?? context.get("hint")) === "decrement",
276
+ isIncrementHint: ({ context, event }) => (event.hint ?? context.get("hint")) === "increment",
277
+ isTouchPointer: ({ event }) => event.pointerType === "touch"
278
+ },
279
+ effects: {
280
+ waitForChangeDelay({ send }) {
281
+ const id = setTimeout(() => {
282
+ send({ type: "CHANGE_DELAY" });
283
+ }, 300);
284
+ return () => clearTimeout(id);
285
+ },
286
+ spinValue({ send }) {
287
+ const id = setInterval(() => {
288
+ send({ type: "SPIN" });
289
+ }, 50);
290
+ return () => clearInterval(id);
291
+ },
292
+ trackFormControl({ context, scope }) {
293
+ const inputEl = require_number_input_dom.getInputEl(scope);
294
+ return (0, _zag_js_dom_query.trackFormControl)(inputEl, {
295
+ onFieldsetDisabledChange(disabled) {
296
+ context.set("fieldsetDisabled", disabled);
297
+ },
298
+ onFormReset() {
299
+ context.set("value", context.initial("value"));
300
+ }
301
+ });
302
+ },
303
+ setupVirtualCursor({ context, scope }) {
304
+ const point = context.get("scrubberCursorPoint");
305
+ return require_number_input_dom.setupVirtualCursor(scope, point);
306
+ },
307
+ preventTextSelection({ scope }) {
308
+ return require_number_input_dom.preventTextSelection(scope);
309
+ },
310
+ trackButtonDisabled({ context, scope, send }) {
311
+ const hint = context.get("hint");
312
+ const btn = require_number_input_dom.getPressedTriggerEl(scope, hint);
313
+ return (0, _zag_js_dom_query.observeAttributes)(btn, {
314
+ attributes: ["disabled"],
315
+ callback() {
316
+ send({
317
+ type: "TRIGGER.PRESS_UP",
318
+ src: "attr"
319
+ });
320
+ }
321
+ });
322
+ },
323
+ attachWheelListener({ scope, send, prop }) {
324
+ const inputEl = require_number_input_dom.getInputEl(scope);
325
+ if (!inputEl || !scope.isActiveElement(inputEl) || !prop("allowMouseWheel")) return;
326
+ function onWheel(event) {
327
+ event.preventDefault();
328
+ const dir = Math.sign(event.deltaY) * -1;
329
+ if (dir === 1) send({ type: "VALUE.INCREMENT" });
330
+ else if (dir === -1) send({ type: "VALUE.DECREMENT" });
331
+ }
332
+ return (0, _zag_js_dom_query.addDomEvent)(inputEl, "wheel", onWheel, { passive: false });
333
+ },
334
+ activatePointerLock({ scope }) {
335
+ if ((0, _zag_js_dom_query.isSafari)()) return;
336
+ return (0, _zag_js_dom_query.requestPointerLock)(scope.getDoc());
337
+ },
338
+ trackMousemove({ scope, send, context, computed }) {
339
+ const doc = scope.getDoc();
340
+ function onMousemove(event) {
341
+ const point = context.get("scrubberCursorPoint");
342
+ const isRtl = computed("isRtl");
343
+ const value = require_number_input_dom.getMousemoveValue(scope, {
344
+ point,
345
+ isRtl,
346
+ event
347
+ });
348
+ if (!value.hint) return;
349
+ send({
350
+ type: "SCRUBBER.POINTER_MOVE",
351
+ hint: value.hint,
352
+ point: value.point
353
+ });
354
+ }
355
+ function onMouseup() {
356
+ send({ type: "SCRUBBER.POINTER_UP" });
357
+ }
358
+ return (0, _zag_js_utils.callAll)((0, _zag_js_dom_query.addDomEvent)(doc, "mousemove", onMousemove, false), (0, _zag_js_dom_query.addDomEvent)(doc, "mouseup", onMouseup, false));
359
+ }
360
+ },
361
+ actions: {
362
+ focusInput({ scope, prop }) {
363
+ if (!prop("focusInputOnChange")) return;
364
+ const inputEl = require_number_input_dom.getInputEl(scope);
365
+ if (scope.isActiveElement(inputEl)) return;
366
+ (0, _zag_js_dom_query.raf)(() => inputEl?.focus({ preventScroll: true }));
367
+ },
368
+ increment({ context, event, prop, computed }) {
369
+ let nextValue = (0, _zag_js_utils.incrementValue)(computed("valueAsNumber"), event.step ?? prop("step"));
370
+ if (!prop("allowOverflow")) nextValue = (0, _zag_js_utils.clampValue)(nextValue, prop("min"), prop("max"));
371
+ context.set("value", require_number_input_utils.formatValue(nextValue, {
372
+ computed,
373
+ prop
374
+ }));
375
+ },
376
+ decrement({ context, event, prop, computed }) {
377
+ let nextValue = (0, _zag_js_utils.decrementValue)(computed("valueAsNumber"), event.step ?? prop("step"));
378
+ if (!prop("allowOverflow")) nextValue = (0, _zag_js_utils.clampValue)(nextValue, prop("min"), prop("max"));
379
+ context.set("value", require_number_input_utils.formatValue(nextValue, {
380
+ computed,
381
+ prop
382
+ }));
383
+ },
384
+ setClampedValue({ context, prop, computed }) {
385
+ const nextValue = (0, _zag_js_utils.clampValue)(computed("valueAsNumber"), prop("min"), prop("max"));
386
+ context.set("value", require_number_input_utils.formatValue(nextValue, {
387
+ computed,
388
+ prop
389
+ }));
390
+ },
391
+ setRawValue({ context, event, prop, computed }) {
392
+ let nextValue = typeof event.value === "number" ? event.value : require_number_input_utils.parseValue(event.value, {
393
+ computed,
394
+ prop
395
+ });
396
+ if (!prop("allowOverflow")) nextValue = (0, _zag_js_utils.clampValue)(nextValue, prop("min"), prop("max"));
397
+ context.set("value", require_number_input_utils.formatValue(nextValue, {
398
+ computed,
399
+ prop
400
+ }));
401
+ },
402
+ setValue({ context, event }) {
403
+ const value = event.target?.value ?? event.value;
404
+ context.set("value", value);
405
+ },
406
+ clearValue({ context }) {
407
+ context.set("value", "");
408
+ },
409
+ incrementToMax({ context, prop, computed }) {
410
+ const value = require_number_input_utils.formatValue(prop("max"), {
411
+ computed,
412
+ prop
413
+ });
414
+ context.set("value", value);
415
+ },
416
+ decrementToMin({ context, prop, computed }) {
417
+ const value = require_number_input_utils.formatValue(prop("min"), {
418
+ computed,
419
+ prop
420
+ });
421
+ context.set("value", value);
422
+ },
423
+ setHint({ context, event }) {
424
+ context.set("hint", event.hint);
425
+ },
426
+ clearHint({ context }) {
427
+ context.set("hint", null);
428
+ },
429
+ invokeOnFocus({ computed, prop }) {
430
+ prop("onFocusChange")?.({
431
+ focused: true,
432
+ value: computed("formattedValue"),
433
+ valueAsNumber: computed("valueAsNumber")
434
+ });
435
+ },
436
+ invokeOnBlur({ computed, prop }) {
437
+ prop("onFocusChange")?.({
438
+ focused: false,
439
+ value: computed("formattedValue"),
440
+ valueAsNumber: computed("valueAsNumber")
441
+ });
442
+ },
443
+ invokeOnInvalid({ computed, prop, event }) {
444
+ if (event.type === "INPUT.CHANGE") return;
445
+ const reason = computed("valueAsNumber") > prop("max") ? "rangeOverflow" : "rangeUnderflow";
446
+ prop("onValueInvalid")?.({
447
+ reason,
448
+ value: computed("formattedValue"),
449
+ valueAsNumber: computed("valueAsNumber")
450
+ });
451
+ },
452
+ invokeOnValueCommit({ computed, prop }) {
453
+ prop("onValueCommit")?.({
454
+ value: computed("formattedValue"),
455
+ valueAsNumber: computed("valueAsNumber")
456
+ });
457
+ },
458
+ syncInputElement({ context, event, computed, scope }) {
459
+ const value = event.type.endsWith("CHANGE") ? context.get("value") : computed("formattedValue");
460
+ const inputEl = require_number_input_dom.getInputEl(scope);
461
+ const sel = event.selection ?? require_cursor.recordCursor(inputEl, scope);
462
+ (0, _zag_js_dom_query.raf)(() => {
463
+ (0, _zag_js_dom_query.setElementValue)(inputEl, value);
464
+ require_cursor.restoreCursor(inputEl, sel, scope);
465
+ });
466
+ },
467
+ setFormattedValue({ context, computed, action }) {
468
+ context.set("value", computed("formattedValue"));
469
+ action(["syncInputElement"]);
470
+ },
471
+ setCursorPoint({ context, event }) {
472
+ context.set("scrubberCursorPoint", event.point);
473
+ },
474
+ clearCursorPoint({ context }) {
475
+ context.set("scrubberCursorPoint", null);
476
+ },
477
+ setVirtualCursorPosition({ context, scope }) {
478
+ const cursorEl = require_number_input_dom.getCursorEl(scope);
479
+ const point = context.get("scrubberCursorPoint");
480
+ if (!cursorEl || !point) return;
481
+ cursorEl.style.transform = `translate3d(${point.x}px, ${point.y}px, 0px)`;
482
+ }
483
+ }
484
+ }
462
485
  });
486
+ exports.machine = machine;