@zag-js/number-input 0.10.4 → 0.11.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.
@@ -1,390 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const core = require('@zag-js/core');
6
- const domEvent = require('@zag-js/dom-event');
7
- const domQuery = require('@zag-js/dom-query');
8
- const formUtils = require('@zag-js/form-utils');
9
- const mutationObserver = require('@zag-js/mutation-observer');
10
- const numberUtils = require('@zag-js/number-utils');
11
- const utils = require('@zag-js/utils');
12
- const numberInput_dom = require('./number-input.dom.js');
13
- const numberInput_utils = require('./number-input.utils.js');
14
-
15
- const { not, and } = core.guards;
16
- function machine(userContext) {
17
- const ctx = utils.compact(userContext);
18
- return core.createMachine(
19
- {
20
- id: "number-input",
21
- initial: "idle",
22
- context: {
23
- dir: "ltr",
24
- focusInputOnChange: true,
25
- clampValueOnBlur: true,
26
- allowOverflow: false,
27
- inputMode: "decimal",
28
- pattern: "[0-9]*(.[0-9]+)?",
29
- hint: null,
30
- value: "",
31
- step: 1,
32
- min: Number.MIN_SAFE_INTEGER,
33
- max: Number.MAX_SAFE_INTEGER,
34
- scrubberCursorPoint: null,
35
- invalid: false,
36
- spinOnPress: true,
37
- ...ctx,
38
- translations: {
39
- incrementLabel: "increment value",
40
- decrementLabel: "decrease value",
41
- ...ctx.translations
42
- }
43
- },
44
- computed: {
45
- isRtl: (ctx2) => ctx2.dir === "rtl",
46
- valueAsNumber: (ctx2) => numberUtils.valueOf(ctx2.value),
47
- isAtMin: (ctx2) => numberUtils.isAtMin(ctx2.value, ctx2),
48
- isAtMax: (ctx2) => numberUtils.isAtMax(ctx2.value, ctx2),
49
- isOutOfRange: (ctx2) => !numberUtils.isWithinRange(ctx2.value, ctx2),
50
- isValueEmpty: (ctx2) => ctx2.value === "",
51
- canIncrement: (ctx2) => ctx2.allowOverflow || !ctx2.isAtMax,
52
- canDecrement: (ctx2) => ctx2.allowOverflow || !ctx2.isAtMin,
53
- valueText: (ctx2) => ctx2.translations.valueText?.(ctx2.value),
54
- formattedValue: (ctx2) => ctx2.format?.(ctx2.value).toString() ?? ctx2.value
55
- },
56
- watch: {
57
- value: ["invokeOnChange", "dispatchChangeEvent"],
58
- isOutOfRange: ["invokeOnInvalid"],
59
- scrubberCursorPoint: ["setVirtualCursorPosition"]
60
- },
61
- entry: ["syncInputValue"],
62
- on: {
63
- SET_VALUE: [
64
- {
65
- guard: "clampOnBlur",
66
- actions: ["setValue", "clampValue", "setHintToSet"]
67
- },
68
- {
69
- actions: ["setValue", "setHintToSet"]
70
- }
71
- ],
72
- CLEAR_VALUE: {
73
- actions: ["clearValue"]
74
- },
75
- INCREMENT: {
76
- actions: ["increment"]
77
- },
78
- DECREMENT: {
79
- actions: ["decrement"]
80
- }
81
- },
82
- states: {
83
- idle: {
84
- exit: "invokeOnFocus",
85
- on: {
86
- PRESS_DOWN: {
87
- target: "before:spin",
88
- actions: ["focusInput", "setHint"]
89
- },
90
- PRESS_DOWN_SCRUBBER: {
91
- target: "scrubbing",
92
- actions: ["focusInput", "setHint", "setCursorPoint"]
93
- },
94
- FOCUS: "focused"
95
- }
96
- },
97
- focused: {
98
- tags: "focus",
99
- entry: "focusInput",
100
- activities: "attachWheelListener",
101
- on: {
102
- PRESS_DOWN: {
103
- target: "before:spin",
104
- actions: ["focusInput", "setHint"]
105
- },
106
- PRESS_DOWN_SCRUBBER: {
107
- target: "scrubbing",
108
- actions: ["focusInput", "setHint", "setCursorPoint"]
109
- },
110
- ARROW_UP: {
111
- actions: "increment"
112
- },
113
- ARROW_DOWN: {
114
- actions: "decrement"
115
- },
116
- HOME: {
117
- actions: "setToMin"
118
- },
119
- END: {
120
- actions: "setToMax"
121
- },
122
- CHANGE: {
123
- actions: ["setValue", "setHint"]
124
- },
125
- BLUR: [
126
- {
127
- guard: "isInvalidExponential",
128
- target: "idle",
129
- actions: ["clearValue", "clearHint", "invokeOnBlur"]
130
- },
131
- {
132
- guard: and("clampOnBlur", not("isInRange"), not("isEmptyValue")),
133
- target: "idle",
134
- actions: ["clampValue", "clearHint", "invokeOnBlur"]
135
- },
136
- {
137
- target: "idle",
138
- actions: ["roundValue", "invokeOnBlur"]
139
- }
140
- ]
141
- }
142
- },
143
- "before:spin": {
144
- tags: "focus",
145
- activities: "trackButtonDisabled",
146
- entry: core.choose([
147
- { guard: "isIncrementHint", actions: "increment" },
148
- { guard: "isDecrementHint", actions: "decrement" }
149
- ]),
150
- after: {
151
- CHANGE_DELAY: {
152
- target: "spinning",
153
- guard: and("isInRange", "spinOnPress")
154
- }
155
- },
156
- on: {
157
- PRESS_UP: {
158
- target: "focused",
159
- actions: "clearHint"
160
- }
161
- }
162
- },
163
- spinning: {
164
- tags: "focus",
165
- activities: "trackButtonDisabled",
166
- every: [
167
- {
168
- delay: "CHANGE_INTERVAL",
169
- guard: and(not("isAtMin"), "isIncrementHint"),
170
- actions: "increment"
171
- },
172
- {
173
- delay: "CHANGE_INTERVAL",
174
- guard: and(not("isAtMax"), "isDecrementHint"),
175
- actions: "decrement"
176
- }
177
- ],
178
- on: {
179
- PRESS_UP: {
180
- target: "focused",
181
- actions: "clearHint"
182
- }
183
- }
184
- },
185
- scrubbing: {
186
- tags: "focus",
187
- exit: "clearCursorPoint",
188
- activities: ["activatePointerLock", "trackMousemove", "setupVirtualCursor", "preventTextSelection"],
189
- on: {
190
- POINTER_UP_SCRUBBER: "focused",
191
- POINTER_MOVE_SCRUBBER: [
192
- {
193
- guard: "isIncrementHint",
194
- actions: ["increment", "setCursorPoint"]
195
- },
196
- {
197
- guard: "isDecrementHint",
198
- actions: ["decrement", "setCursorPoint"]
199
- }
200
- ]
201
- }
202
- }
203
- }
204
- },
205
- {
206
- delays: {
207
- CHANGE_INTERVAL: 50,
208
- CHANGE_DELAY: 300
209
- },
210
- guards: {
211
- clampOnBlur: (ctx2) => !!ctx2.clampValueOnBlur,
212
- isAtMin: (ctx2) => ctx2.isAtMin,
213
- spinOnPress: (ctx2) => !!ctx2.spinOnPress,
214
- isAtMax: (ctx2) => ctx2.isAtMax,
215
- isInRange: (ctx2) => !ctx2.isOutOfRange,
216
- isDecrementHint: (ctx2, evt) => (evt.hint ?? ctx2.hint) === "decrement",
217
- isEmptyValue: (ctx2) => ctx2.isValueEmpty,
218
- isIncrementHint: (ctx2, evt) => (evt.hint ?? ctx2.hint) === "increment",
219
- isInvalidExponential: (ctx2) => ctx2.value.toString().startsWith("e")
220
- },
221
- activities: {
222
- setupVirtualCursor(ctx2) {
223
- return numberInput_dom.dom.setupVirtualCursor(ctx2);
224
- },
225
- preventTextSelection(ctx2) {
226
- return numberInput_dom.dom.preventTextSelection(ctx2);
227
- },
228
- trackButtonDisabled(ctx2, _evt, { send }) {
229
- const btn = numberInput_dom.dom.getPressedTriggerEl(ctx2, ctx2.hint);
230
- return mutationObserver.observeAttributes(btn, ["disabled"], () => {
231
- send("PRESS_UP");
232
- });
233
- },
234
- attachWheelListener(ctx2, _evt, { send }) {
235
- const input = numberInput_dom.dom.getInputEl(ctx2);
236
- if (!input)
237
- return;
238
- function onWheel(event) {
239
- const isInputFocused = numberInput_dom.dom.getDoc(ctx2).activeElement === input;
240
- if (!ctx2.allowMouseWheel || !isInputFocused)
241
- return;
242
- event.preventDefault();
243
- const dir = Math.sign(event.deltaY) * -1;
244
- if (dir === 1) {
245
- send("INCREMENT");
246
- } else if (dir === -1) {
247
- send("DECREMENT");
248
- }
249
- }
250
- return domEvent.addDomEvent(input, "wheel", onWheel, { passive: false });
251
- },
252
- activatePointerLock(ctx2) {
253
- if (domQuery.isSafari())
254
- return;
255
- return domEvent.requestPointerLock(numberInput_dom.dom.getDoc(ctx2));
256
- },
257
- trackMousemove(ctx2, _evt, { send }) {
258
- const doc = numberInput_dom.dom.getDoc(ctx2);
259
- function onMousemove(event) {
260
- if (!ctx2.scrubberCursorPoint)
261
- return;
262
- const value = numberInput_dom.dom.getMousementValue(ctx2, event);
263
- if (!value.hint)
264
- return;
265
- send({
266
- type: "POINTER_MOVE_SCRUBBER",
267
- hint: value.hint,
268
- point: value.point
269
- });
270
- }
271
- function onMouseup() {
272
- send("POINTER_UP_SCRUBBER");
273
- }
274
- return utils.callAll(
275
- domEvent.addDomEvent(doc, "mousemove", onMousemove, false),
276
- domEvent.addDomEvent(doc, "mouseup", onMouseup, false)
277
- );
278
- }
279
- },
280
- actions: {
281
- focusInput(ctx2) {
282
- if (!ctx2.focusInputOnChange)
283
- return;
284
- const input = numberInput_dom.dom.getInputEl(ctx2);
285
- domQuery.raf(() => input?.focus());
286
- },
287
- increment(ctx2, evt) {
288
- ctx2.value = numberInput_utils.utils.increment(ctx2, evt.step);
289
- },
290
- decrement(ctx2, evt) {
291
- ctx2.value = numberInput_utils.utils.decrement(ctx2, evt.step);
292
- },
293
- clampValue(ctx2) {
294
- ctx2.value = numberInput_utils.utils.clamp(ctx2);
295
- },
296
- roundValue(ctx2) {
297
- if (ctx2.value !== "") {
298
- ctx2.value = numberInput_utils.utils.round(ctx2);
299
- }
300
- },
301
- setValue(ctx2, evt) {
302
- const value = evt.target?.value ?? evt.value;
303
- ctx2.value = numberInput_utils.utils.sanitize(ctx2, numberInput_utils.utils.parse(ctx2, value.toString()));
304
- },
305
- clearValue(ctx2) {
306
- ctx2.value = "";
307
- },
308
- setToMax(ctx2) {
309
- ctx2.value = ctx2.max.toString();
310
- },
311
- setToMin(ctx2) {
312
- ctx2.value = ctx2.min.toString();
313
- },
314
- setHint(ctx2, evt) {
315
- ctx2.hint = evt.hint;
316
- },
317
- clearHint(ctx2) {
318
- ctx2.hint = null;
319
- },
320
- setHintToSet(ctx2) {
321
- ctx2.hint = "set";
322
- },
323
- invokeOnChange(ctx2) {
324
- ctx2.onChange?.({
325
- value: ctx2.value,
326
- valueAsNumber: ctx2.valueAsNumber
327
- });
328
- },
329
- invokeOnFocus(ctx2, evt) {
330
- let srcElement = null;
331
- if (evt.type === "PRESS_DOWN") {
332
- srcElement = numberInput_dom.dom.getPressedTriggerEl(ctx2, evt.hint);
333
- } else if (evt.type === "FOCUS") {
334
- srcElement = numberInput_dom.dom.getInputEl(ctx2);
335
- } else if (evt.type === "PRESS_DOWN_SCRUBBER") {
336
- srcElement = numberInput_dom.dom.getScrubberEl(ctx2);
337
- }
338
- ctx2.onFocus?.({
339
- value: ctx2.value,
340
- valueAsNumber: ctx2.valueAsNumber,
341
- srcElement
342
- });
343
- },
344
- invokeOnBlur(ctx2) {
345
- ctx2.onBlur?.({
346
- value: ctx2.value,
347
- valueAsNumber: ctx2.valueAsNumber
348
- });
349
- },
350
- invokeOnInvalid(ctx2) {
351
- if (!ctx2.isOutOfRange)
352
- return;
353
- const reason = ctx2.valueAsNumber > ctx2.max ? "rangeOverflow" : "rangeUnderflow";
354
- ctx2.onInvalid?.({
355
- reason,
356
- value: ctx2.formattedValue,
357
- valueAsNumber: ctx2.valueAsNumber
358
- });
359
- },
360
- // sync input value, in event it was set from form libraries via `ref`, `bind:this`, etc.
361
- syncInputValue(ctx2) {
362
- const input = numberInput_dom.dom.getInputEl(ctx2);
363
- if (!input || input.value == ctx2.value)
364
- return;
365
- const value = numberInput_utils.utils.parse(ctx2, input.value);
366
- ctx2.value = numberInput_utils.utils.sanitize(ctx2, value);
367
- },
368
- setCursorPoint(ctx2, evt) {
369
- ctx2.scrubberCursorPoint = evt.point;
370
- },
371
- clearCursorPoint(ctx2) {
372
- ctx2.scrubberCursorPoint = null;
373
- },
374
- setVirtualCursorPosition(ctx2) {
375
- const cursor = numberInput_dom.dom.getCursorEl(ctx2);
376
- if (!cursor || !ctx2.scrubberCursorPoint)
377
- return;
378
- const { x, y } = ctx2.scrubberCursorPoint;
379
- cursor.style.transform = `translate3d(${x}px, ${y}px, 0px)`;
380
- },
381
- dispatchChangeEvent(ctx2) {
382
- const inputEl = numberInput_dom.dom.getInputEl(ctx2);
383
- formUtils.dispatchInputValueEvent(inputEl, { value: ctx2.formattedValue });
384
- }
385
- }
386
- }
387
- );
388
- }
389
-
390
- exports.machine = machine;