@zag-js/number-input 2.0.0-next.3 → 2.0.0-next.4
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.d.mts +6 -9
- package/dist/index.d.ts +6 -9
- package/dist/index.js +12 -38
- package/dist/index.mjs +2 -7
- package/dist/number-input.anatomy.d.mts +2 -6
- package/dist/number-input.anatomy.d.ts +2 -6
- package/dist/number-input.anatomy.js +8 -43
- package/dist/number-input.anatomy.mjs +4 -17
- package/dist/number-input.connect.d.mts +3 -8
- package/dist/number-input.connect.d.ts +3 -8
- package/dist/number-input.connect.js +483 -377
- package/dist/number-input.connect.mjs +478 -353
- package/dist/number-input.dom.d.mts +33 -33
- package/dist/number-input.dom.d.ts +33 -33
- package/dist/number-input.dom.js +99 -142
- package/dist/number-input.dom.mjs +78 -103
- package/dist/number-input.machine.d.mts +2 -8
- package/dist/number-input.machine.d.ts +2 -8
- package/dist/number-input.machine.js +600 -528
- package/dist/number-input.machine.mjs +596 -512
- package/dist/number-input.props.d.mts +3 -9
- package/dist/number-input.props.d.ts +3 -9
- package/dist/number-input.props.js +43 -69
- package/dist/number-input.props.mjs +39 -43
- package/dist/number-input.translations.d.mts +3 -8
- package/dist/number-input.translations.d.ts +3 -8
- package/dist/number-input.translations.js +9 -31
- package/dist/number-input.translations.mjs +5 -6
- package/dist/number-input.types.d.mts +418 -408
- package/dist/number-input.types.d.ts +418 -408
- package/dist/number-input.types.js +4 -18
- package/dist/number-input.types.mjs +1 -0
- package/dist/number-input.utils.d.mts +16 -12
- package/dist/number-input.utils.d.ts +16 -12
- package/dist/number-input.utils.js +51 -57
- package/dist/number-input.utils.mjs +39 -27
- package/package.json +8 -8
- package/dist/cursor.d.mts +0 -12
- package/dist/cursor.d.ts +0 -12
- package/dist/cursor.js +0 -111
- package/dist/cursor.mjs +0 -84
|
@@ -1,514 +1,598 @@
|
|
|
1
|
-
|
|
1
|
+
import { dispatchHiddenInputValue, getHiddenInputEl, getInputEl, getMousemoveValue, getScrubberEl, getTriggerEl, preventTextSelection } from "./number-input.dom.mjs";
|
|
2
|
+
import { createFormatter, createParser, formatNumber, formatValue, getDefaultStep, parseValue, snapToStep } from "./number-input.utils.mjs";
|
|
3
|
+
import { AnimationFrame, addDomEvent, isFirefox, isIPhone, isIos, isSafari, observeAttributes, recordCursor, requestPointerLock, restoreCursor, setElementValue, trackFormControl } from "@zag-js/dom-query";
|
|
4
|
+
import { callAll, clampValue, decrementValue, incrementValue, isValueWithinRange } from "@zag-js/utils";
|
|
2
5
|
import { memo, setup } from "@zag-js/core";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
6
|
+
// src/number-input.machine.ts
|
|
7
|
+
const { choose, guards, createMachine } = setup();
|
|
8
|
+
const { not, and } = guards;
|
|
9
|
+
const machine = createMachine({
|
|
10
|
+
props({ props }) {
|
|
11
|
+
const step = getDefaultStep(props.step, props.formatOptions);
|
|
12
|
+
return {
|
|
13
|
+
dir: "ltr",
|
|
14
|
+
locale: "en-US",
|
|
15
|
+
focusInputOnChange: true,
|
|
16
|
+
allowOutOfRange: false,
|
|
17
|
+
pattern: "-?[0-9]*(.[0-9]+)?",
|
|
18
|
+
defaultValue: null,
|
|
19
|
+
step,
|
|
20
|
+
spinOnPress: true,
|
|
21
|
+
scrubberPixelSensitivity: 2,
|
|
22
|
+
scrubberDirection: "horizontal",
|
|
23
|
+
snapOnStep: false,
|
|
24
|
+
...props,
|
|
25
|
+
largeStep: props.largeStep ?? 10 * step,
|
|
26
|
+
smallStep: props.smallStep ?? step / 10
|
|
27
|
+
};
|
|
28
|
+
},
|
|
29
|
+
initialState() {
|
|
30
|
+
return "idle";
|
|
31
|
+
},
|
|
32
|
+
refs({ prop }) {
|
|
33
|
+
return {
|
|
34
|
+
syncFrame: AnimationFrame.create(),
|
|
35
|
+
pressDirection: 1,
|
|
36
|
+
focusFrame: AnimationFrame.create(),
|
|
37
|
+
committedValue: prop("value") ?? prop("defaultValue") ?? null
|
|
38
|
+
};
|
|
39
|
+
},
|
|
40
|
+
exit: ["cancelFrames"],
|
|
41
|
+
context({ prop, bindable, getComputed, getEvent }) {
|
|
42
|
+
return {
|
|
43
|
+
value: bindable(() => ({
|
|
44
|
+
defaultValue: prop("defaultValue"),
|
|
45
|
+
value: prop("value"),
|
|
46
|
+
onChange(value) {
|
|
47
|
+
const formattedValue = formatValue(value, {
|
|
48
|
+
computed: getComputed(),
|
|
49
|
+
prop
|
|
50
|
+
});
|
|
51
|
+
prop("onValueChange")?.({
|
|
52
|
+
value,
|
|
53
|
+
formattedValue,
|
|
54
|
+
reason: getEvent().src
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
})),
|
|
58
|
+
inputValue: bindable(() => ({ defaultValue: formatNumber(prop("value") ?? prop("defaultValue"), createFormatter(prop("locale"), prop("formatOptions")), prop("formatOptions")) })),
|
|
59
|
+
scrubberCursorPoint: bindable(() => ({
|
|
60
|
+
defaultValue: null,
|
|
61
|
+
hash(value) {
|
|
62
|
+
return value ? `x:${value.x}, y:${value.y}` : "";
|
|
63
|
+
}
|
|
64
|
+
})),
|
|
65
|
+
fieldsetDisabled: bindable(() => ({ defaultValue: false })),
|
|
66
|
+
cumulativeDelta: bindable(() => ({ defaultValue: 0 })),
|
|
67
|
+
platform: bindable(() => ({ defaultValue: {
|
|
68
|
+
iPhone: false,
|
|
69
|
+
ios: false,
|
|
70
|
+
supportsPointerLock: false
|
|
71
|
+
} })),
|
|
72
|
+
visualScale: bindable(() => ({ defaultValue: 1 }))
|
|
73
|
+
};
|
|
74
|
+
},
|
|
75
|
+
computed: {
|
|
76
|
+
isRtl: ({ prop }) => prop("dir") === "rtl",
|
|
77
|
+
min: ({ prop }) => prop("min") ?? -1e400,
|
|
78
|
+
max: ({ prop }) => prop("max") ?? 1e400,
|
|
79
|
+
formattedValue: ({ context, computed, prop }) => formatValue(context.get("value"), {
|
|
80
|
+
computed,
|
|
81
|
+
prop
|
|
82
|
+
}),
|
|
83
|
+
isAtMin: ({ context, computed }) => {
|
|
84
|
+
const value = context.get("value");
|
|
85
|
+
return value != null && value <= computed("min");
|
|
86
|
+
},
|
|
87
|
+
isAtMax: ({ context, computed }) => {
|
|
88
|
+
const value = context.get("value");
|
|
89
|
+
return value != null && value >= computed("max");
|
|
90
|
+
},
|
|
91
|
+
isOutOfRange: ({ context, prop }) => {
|
|
92
|
+
const value = context.get("value");
|
|
93
|
+
return value != null && !isValueWithinRange(value, prop("min"), prop("max"));
|
|
94
|
+
},
|
|
95
|
+
isValueEmpty: ({ context }) => context.get("value") == null,
|
|
96
|
+
isDisabled: ({ prop, context }) => !!prop("disabled") || context.get("fieldsetDisabled"),
|
|
97
|
+
canIncrement: ({ computed }) => !computed("isAtMax"),
|
|
98
|
+
canDecrement: ({ computed }) => !computed("isAtMin"),
|
|
99
|
+
formatter: memo(({ prop }) => [prop("locale"), prop("formatOptions")], ([locale, formatOptions]) => createFormatter(locale, formatOptions)),
|
|
100
|
+
parser: memo(({ prop }) => [prop("locale"), prop("formatOptions")], ([locale, formatOptions]) => createParser(locale, formatOptions))
|
|
101
|
+
},
|
|
102
|
+
watch({ track, action, context, computed, prop }) {
|
|
103
|
+
track([
|
|
104
|
+
() => context.get("value"),
|
|
105
|
+
() => prop("locale"),
|
|
106
|
+
() => JSON.stringify(prop("formatOptions"))
|
|
107
|
+
], () => {
|
|
108
|
+
action(["syncInputValue", "syncHiddenInput"]);
|
|
109
|
+
});
|
|
110
|
+
track([() => context.get("inputValue")], () => {
|
|
111
|
+
action(["syncInputValue", "syncInputElement"]);
|
|
112
|
+
});
|
|
113
|
+
track([() => computed("isOutOfRange")], () => {
|
|
114
|
+
if (!computed("isOutOfRange")) return;
|
|
115
|
+
action(["invokeOnInvalid"]);
|
|
116
|
+
});
|
|
117
|
+
track([() => context.hash("scrubberCursorPoint")], () => {
|
|
118
|
+
action(["setVirtualCursorPosition"]);
|
|
119
|
+
});
|
|
120
|
+
},
|
|
121
|
+
effects: ["trackFormControl", "detectPlatform"],
|
|
122
|
+
on: {
|
|
123
|
+
"INPUT.FOCUS": { actions: ["invokeOnFocus"] },
|
|
124
|
+
"VALUE.SET": { actions: ["setRawValue", "invokeOnValueCommit"] },
|
|
125
|
+
"VALUE.CLEAR": { actions: ["clearValue", "invokeOnValueCommit"] },
|
|
126
|
+
"VALUE.INCREMENT": { actions: ["increment", "invokeOnValueCommit"] },
|
|
127
|
+
"VALUE.DECREMENT": { actions: ["decrement", "invokeOnValueCommit"] }
|
|
128
|
+
},
|
|
129
|
+
states: {
|
|
130
|
+
idle: { on: {
|
|
131
|
+
"TRIGGER.PRESS_DOWN": [{
|
|
132
|
+
guard: "isTouchPointer",
|
|
133
|
+
target: "pressed",
|
|
134
|
+
actions: ["setPressDirection"]
|
|
135
|
+
}, {
|
|
136
|
+
target: "pressed",
|
|
137
|
+
actions: ["focusInput", "setPressDirection"]
|
|
138
|
+
}],
|
|
139
|
+
"SCRUBBER.PRESS_DOWN": {
|
|
140
|
+
target: "scrubbing",
|
|
141
|
+
actions: ["focusInput", "setCursorPoint"]
|
|
142
|
+
},
|
|
143
|
+
"INPUT.FOCUS": {
|
|
144
|
+
target: "focused",
|
|
145
|
+
actions: ["focusInput", "invokeOnFocus"]
|
|
146
|
+
}
|
|
147
|
+
} },
|
|
148
|
+
focused: {
|
|
149
|
+
tags: ["focus"],
|
|
150
|
+
effects: ["attachWheelListener"],
|
|
151
|
+
on: {
|
|
152
|
+
"TRIGGER.PRESS_DOWN": [{
|
|
153
|
+
guard: "isTouchPointer",
|
|
154
|
+
target: "pressed",
|
|
155
|
+
actions: ["setPressDirection"]
|
|
156
|
+
}, {
|
|
157
|
+
target: "pressed",
|
|
158
|
+
actions: ["focusInput", "setPressDirection"]
|
|
159
|
+
}],
|
|
160
|
+
"SCRUBBER.PRESS_DOWN": {
|
|
161
|
+
target: "scrubbing",
|
|
162
|
+
actions: ["focusInput", "setCursorPoint"]
|
|
163
|
+
},
|
|
164
|
+
"INPUT.ARROW_UP": { actions: ["increment", "invokeOnValueCommit"] },
|
|
165
|
+
"INPUT.ARROW_DOWN": { actions: ["decrement", "invokeOnValueCommit"] },
|
|
166
|
+
"INPUT.HOME": { actions: ["decrementToMin", "invokeOnValueCommit"] },
|
|
167
|
+
"INPUT.END": { actions: ["incrementToMax", "invokeOnValueCommit"] },
|
|
168
|
+
"INPUT.CHANGE": { actions: ["setInputValue"] },
|
|
169
|
+
"INPUT.BLUR": [{
|
|
170
|
+
guard: and(not("allowOutOfRange"), not("isInRange")),
|
|
171
|
+
target: "idle",
|
|
172
|
+
actions: [
|
|
173
|
+
"setClampedValue",
|
|
174
|
+
"invokeOnBlur",
|
|
175
|
+
"invokeOnValueCommit"
|
|
176
|
+
]
|
|
177
|
+
}, {
|
|
178
|
+
target: "idle",
|
|
179
|
+
actions: [
|
|
180
|
+
"setFormattedValue",
|
|
181
|
+
"invokeOnBlur",
|
|
182
|
+
"invokeOnValueCommit"
|
|
183
|
+
]
|
|
184
|
+
}],
|
|
185
|
+
"INPUT.ENTER": [{
|
|
186
|
+
guard: and(not("allowOutOfRange"), not("isInRange")),
|
|
187
|
+
actions: ["setClampedValue", "invokeOnValueCommit"]
|
|
188
|
+
}, { actions: ["setFormattedValue", "invokeOnValueCommit"] }]
|
|
189
|
+
}
|
|
190
|
+
},
|
|
191
|
+
pressed: {
|
|
192
|
+
tags: ["focus"],
|
|
193
|
+
initial: "waiting",
|
|
194
|
+
effects: ["trackButtonDisabled", "preventContextMenu"],
|
|
195
|
+
on: { "TRIGGER.PRESS_UP": [{
|
|
196
|
+
guard: "isTouchPointer",
|
|
197
|
+
target: "focused",
|
|
198
|
+
actions: ["invokeOnValueCommit"]
|
|
199
|
+
}, {
|
|
200
|
+
target: "focused",
|
|
201
|
+
actions: ["focusInput", "invokeOnValueCommit"]
|
|
202
|
+
}] },
|
|
203
|
+
states: {
|
|
204
|
+
waiting: {
|
|
205
|
+
effects: ["waitForChangeDelay"],
|
|
206
|
+
entry: choose([{
|
|
207
|
+
guard: not("isTouchPointer"),
|
|
208
|
+
actions: ["step"]
|
|
209
|
+
}]),
|
|
210
|
+
on: {
|
|
211
|
+
"TRIGGER.PRESS_UP": [
|
|
212
|
+
{
|
|
213
|
+
guard: and("isTouchPointer", "isTouchTap"),
|
|
214
|
+
target: "focused",
|
|
215
|
+
actions: ["step", "invokeOnValueCommit"]
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
guard: "isTouchPointer",
|
|
219
|
+
target: "focused"
|
|
220
|
+
},
|
|
221
|
+
{
|
|
222
|
+
target: "focused",
|
|
223
|
+
actions: ["focusInput", "invokeOnValueCommit"]
|
|
224
|
+
}
|
|
225
|
+
],
|
|
226
|
+
CHANGE_DELAY: {
|
|
227
|
+
target: "repeating",
|
|
228
|
+
guard: and("isInRange", "spinOnPress")
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
repeating: {
|
|
233
|
+
effects: ["spinValue"],
|
|
234
|
+
on: { SPIN: { actions: ["step"] } }
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
},
|
|
238
|
+
scrubbing: {
|
|
239
|
+
tags: ["focus"],
|
|
240
|
+
effects: [
|
|
241
|
+
"activatePointerLock",
|
|
242
|
+
"trackMousemove",
|
|
243
|
+
"preventTextSelection",
|
|
244
|
+
"trackVisualViewport"
|
|
245
|
+
],
|
|
246
|
+
entry: ["clearCumulativeDelta"],
|
|
247
|
+
on: {
|
|
248
|
+
"SCRUBBER.STEP": { actions: ["step"] },
|
|
249
|
+
"SCRUBBER.POINTER_UP": {
|
|
250
|
+
target: "focused",
|
|
251
|
+
actions: [
|
|
252
|
+
"focusInput",
|
|
253
|
+
"clearCursorPoint",
|
|
254
|
+
"clearCumulativeDelta",
|
|
255
|
+
"invokeOnValueCommit"
|
|
256
|
+
]
|
|
257
|
+
},
|
|
258
|
+
"SCRUBBER.POINTER_MOVE": { actions: ["setCursorPoint", "accumulateDelta"] }
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
},
|
|
262
|
+
implementations: {
|
|
263
|
+
guards: {
|
|
264
|
+
allowOutOfRange: ({ prop }) => !!prop("allowOutOfRange"),
|
|
265
|
+
spinOnPress: ({ prop }) => !!prop("spinOnPress"),
|
|
266
|
+
isInRange: ({ computed }) => !computed("isOutOfRange"),
|
|
267
|
+
isTouchPointer: ({ event }) => event.pointerType === "touch",
|
|
268
|
+
isTouchTap: ({ event }) => !event.cancelled
|
|
269
|
+
},
|
|
270
|
+
effects: {
|
|
271
|
+
waitForChangeDelay({ send }) {
|
|
272
|
+
const id = setTimeout(() => {
|
|
273
|
+
send({ type: "CHANGE_DELAY" });
|
|
274
|
+
}, 400);
|
|
275
|
+
return () => clearTimeout(id);
|
|
276
|
+
},
|
|
277
|
+
preventContextMenu({ scope }) {
|
|
278
|
+
return addDomEvent(scope.getWin(), "contextmenu", (event) => event.preventDefault());
|
|
279
|
+
},
|
|
280
|
+
spinValue({ send, refs }) {
|
|
281
|
+
const id = setInterval(() => {
|
|
282
|
+
send({
|
|
283
|
+
type: "SPIN",
|
|
284
|
+
src: getPressSource(refs.get("pressDirection"))
|
|
285
|
+
});
|
|
286
|
+
}, 60);
|
|
287
|
+
return () => clearInterval(id);
|
|
288
|
+
},
|
|
289
|
+
detectPlatform({ context }) {
|
|
290
|
+
context.set("platform", {
|
|
291
|
+
iPhone: isIPhone(),
|
|
292
|
+
ios: isIos(),
|
|
293
|
+
supportsPointerLock: !isSafari()
|
|
294
|
+
});
|
|
295
|
+
},
|
|
296
|
+
trackFormControl({ context, scope }) {
|
|
297
|
+
const inputEl = getHiddenInputEl(scope) ?? getInputEl(scope);
|
|
298
|
+
return trackFormControl(inputEl, {
|
|
299
|
+
onFieldsetDisabledChange(disabled) {
|
|
300
|
+
context.set("fieldsetDisabled", disabled);
|
|
301
|
+
},
|
|
302
|
+
onFormReset() {
|
|
303
|
+
context.set("value", context.initial("value") ?? null);
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
},
|
|
307
|
+
preventTextSelection({ scope, prop }) {
|
|
308
|
+
return preventTextSelection(scope, prop("scrubberDirection"));
|
|
309
|
+
},
|
|
310
|
+
trackButtonDisabled({ refs, scope, send }) {
|
|
311
|
+
const direction = refs.get("pressDirection");
|
|
312
|
+
const btn = getTriggerEl(scope, direction);
|
|
313
|
+
return observeAttributes(btn, {
|
|
314
|
+
attributes: ["disabled"],
|
|
315
|
+
callback() {
|
|
316
|
+
send({
|
|
317
|
+
type: "TRIGGER.PRESS_UP",
|
|
318
|
+
src: getPressSource(direction)
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
},
|
|
323
|
+
attachWheelListener({ scope, send, prop }) {
|
|
324
|
+
const inputEl = getInputEl(scope);
|
|
325
|
+
if (!inputEl || !scope.isActiveElement(inputEl) || !prop("allowMouseWheel")) return;
|
|
326
|
+
function onWheel(event) {
|
|
327
|
+
if (prop("readOnly")) return;
|
|
328
|
+
if (event.ctrlKey) return;
|
|
329
|
+
const isHorizontal = Math.abs(event.deltaX) > Math.abs(event.deltaY);
|
|
330
|
+
const delta = event.shiftKey && isHorizontal ? event.deltaX : event.deltaY;
|
|
331
|
+
if (delta === 0 || isHorizontal && !event.shiftKey) return;
|
|
332
|
+
event.preventDefault();
|
|
333
|
+
const step = event.altKey ? prop("smallStep") : event.shiftKey ? prop("largeStep") : prop("step");
|
|
334
|
+
send({
|
|
335
|
+
type: delta < 0 ? "VALUE.INCREMENT" : "VALUE.DECREMENT",
|
|
336
|
+
step,
|
|
337
|
+
src: "wheel"
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
return addDomEvent(inputEl, "wheel", onWheel, { passive: false });
|
|
341
|
+
},
|
|
342
|
+
trackVisualViewport({ scope, context }) {
|
|
343
|
+
const vV = scope.getWin().visualViewport;
|
|
344
|
+
if (!vV) return;
|
|
345
|
+
function onResize() {
|
|
346
|
+
context.set("visualScale", vV.scale);
|
|
347
|
+
}
|
|
348
|
+
onResize();
|
|
349
|
+
return addDomEvent(vV, "resize", onResize);
|
|
350
|
+
},
|
|
351
|
+
activatePointerLock({ scope }) {
|
|
352
|
+
if (isSafari()) return;
|
|
353
|
+
const cleanup = requestPointerLock(scope.getDoc());
|
|
354
|
+
return () => {
|
|
355
|
+
if (isFirefox()) setTimeout(() => cleanup?.(), 20);
|
|
356
|
+
else cleanup?.();
|
|
357
|
+
};
|
|
358
|
+
},
|
|
359
|
+
trackMousemove({ scope, send, context, computed, prop }) {
|
|
360
|
+
const doc = scope.getDoc();
|
|
361
|
+
function onMousemove(event) {
|
|
362
|
+
const point = context.get("scrubberCursorPoint");
|
|
363
|
+
const isRtl = computed("isRtl");
|
|
364
|
+
const axis = prop("scrubberDirection");
|
|
365
|
+
const teleportDistance = prop("scrubberTeleportDistance");
|
|
366
|
+
const value = getMousemoveValue(scope, {
|
|
367
|
+
point,
|
|
368
|
+
isRtl,
|
|
369
|
+
event,
|
|
370
|
+
axis,
|
|
371
|
+
teleportDistance
|
|
372
|
+
});
|
|
373
|
+
send({
|
|
374
|
+
type: "SCRUBBER.POINTER_MOVE",
|
|
375
|
+
direction: value.direction,
|
|
376
|
+
point: value.point,
|
|
377
|
+
delta: value.delta
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
function onMouseup() {
|
|
381
|
+
send({
|
|
382
|
+
type: "SCRUBBER.POINTER_UP",
|
|
383
|
+
src: "scrub"
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
return callAll(addDomEvent(doc, "mousemove", onMousemove, false), addDomEvent(doc, "mouseup", onMouseup, false));
|
|
387
|
+
}
|
|
388
|
+
},
|
|
389
|
+
actions: {
|
|
390
|
+
focusInput({ scope, prop, refs }) {
|
|
391
|
+
if (!prop("focusInputOnChange")) return;
|
|
392
|
+
const inputEl = getInputEl(scope);
|
|
393
|
+
if (scope.isActiveElement(inputEl)) return;
|
|
394
|
+
refs.get("focusFrame").request(() => inputEl?.focus({ preventScroll: true }));
|
|
395
|
+
},
|
|
396
|
+
cancelFrames({ refs }) {
|
|
397
|
+
refs.get("syncFrame").cancel();
|
|
398
|
+
refs.get("focusFrame").cancel();
|
|
399
|
+
},
|
|
400
|
+
increment({ event, prop, computed, context }) {
|
|
401
|
+
const value = context.get("value");
|
|
402
|
+
const step = event.step ?? prop("step");
|
|
403
|
+
const next = value == null ? 0 : incrementValue(value, step);
|
|
404
|
+
setNumber({
|
|
405
|
+
context,
|
|
406
|
+
prop,
|
|
407
|
+
computed
|
|
408
|
+
}, getStepValue(next, step, 1, {
|
|
409
|
+
prop,
|
|
410
|
+
computed
|
|
411
|
+
}));
|
|
412
|
+
},
|
|
413
|
+
decrement({ event, prop, computed, context }) {
|
|
414
|
+
const value = context.get("value");
|
|
415
|
+
const step = event.step ?? prop("step");
|
|
416
|
+
const next = value == null ? 0 : decrementValue(value, step);
|
|
417
|
+
setNumber({
|
|
418
|
+
context,
|
|
419
|
+
prop,
|
|
420
|
+
computed
|
|
421
|
+
}, getStepValue(next, step, -1, {
|
|
422
|
+
prop,
|
|
423
|
+
computed
|
|
424
|
+
}));
|
|
425
|
+
},
|
|
426
|
+
setClampedValue({ context, prop, computed }) {
|
|
427
|
+
const value = context.get("value");
|
|
428
|
+
setNumber({
|
|
429
|
+
context,
|
|
430
|
+
prop,
|
|
431
|
+
computed
|
|
432
|
+
}, value == null ? null : clampValue(value, computed("min"), computed("max")));
|
|
433
|
+
},
|
|
434
|
+
setRawValue({ context, event, prop, computed }) {
|
|
435
|
+
let value = event.value;
|
|
436
|
+
if (!prop("allowOutOfRange")) value = clampValue(value, computed("min"), computed("max"));
|
|
437
|
+
setNumber({
|
|
438
|
+
context,
|
|
439
|
+
prop,
|
|
440
|
+
computed
|
|
441
|
+
}, value);
|
|
442
|
+
},
|
|
443
|
+
setInputValue({ context, event, computed, prop }) {
|
|
444
|
+
const text = event.value;
|
|
445
|
+
context.set("inputValue", text);
|
|
446
|
+
if (text.trim() === "") {
|
|
447
|
+
context.set("value", null);
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
const value = parseValue(text, {
|
|
451
|
+
computed,
|
|
452
|
+
prop
|
|
453
|
+
});
|
|
454
|
+
if (!Number.isNaN(value)) context.set("value", normalizeZero(value));
|
|
455
|
+
},
|
|
456
|
+
clearValue({ context }) {
|
|
457
|
+
context.set("value", null);
|
|
458
|
+
context.set("inputValue", "");
|
|
459
|
+
},
|
|
460
|
+
incrementToMax({ context, prop, computed }) {
|
|
461
|
+
setNumber({
|
|
462
|
+
context,
|
|
463
|
+
prop,
|
|
464
|
+
computed
|
|
465
|
+
}, computed("max"));
|
|
466
|
+
},
|
|
467
|
+
decrementToMin({ context, prop, computed }) {
|
|
468
|
+
setNumber({
|
|
469
|
+
context,
|
|
470
|
+
prop,
|
|
471
|
+
computed
|
|
472
|
+
}, computed("min"));
|
|
473
|
+
},
|
|
474
|
+
setPressDirection({ refs, event }) {
|
|
475
|
+
refs.set("pressDirection", event.direction);
|
|
476
|
+
},
|
|
477
|
+
step({ event, refs, action }) {
|
|
478
|
+
action([(event.direction ?? refs.get("pressDirection")) > 0 ? "increment" : "decrement"]);
|
|
479
|
+
},
|
|
480
|
+
invokeOnFocus({ context, computed, prop }) {
|
|
481
|
+
prop("onFocusChange")?.({
|
|
482
|
+
focused: true,
|
|
483
|
+
value: context.get("value"),
|
|
484
|
+
formattedValue: computed("formattedValue")
|
|
485
|
+
});
|
|
486
|
+
},
|
|
487
|
+
invokeOnBlur({ context, computed, prop }) {
|
|
488
|
+
prop("onFocusChange")?.({
|
|
489
|
+
focused: false,
|
|
490
|
+
value: context.get("value"),
|
|
491
|
+
formattedValue: computed("formattedValue")
|
|
492
|
+
});
|
|
493
|
+
},
|
|
494
|
+
invokeOnInvalid({ context, computed, prop }) {
|
|
495
|
+
const value = context.get("value");
|
|
496
|
+
if (value == null) return;
|
|
497
|
+
prop("onValueInvalid")?.({
|
|
498
|
+
reason: value > computed("max") ? "rangeOverflow" : "rangeUnderflow",
|
|
499
|
+
value,
|
|
500
|
+
formattedValue: computed("formattedValue")
|
|
501
|
+
});
|
|
502
|
+
},
|
|
503
|
+
invokeOnValueCommit({ context, computed, prop, event, refs }) {
|
|
504
|
+
const reason = event.src;
|
|
505
|
+
queueMicrotask(() => {
|
|
506
|
+
const value = context.get("value");
|
|
507
|
+
if (value === refs.get("committedValue")) return;
|
|
508
|
+
refs.set("committedValue", value);
|
|
509
|
+
prop("onValueCommit")?.({
|
|
510
|
+
value,
|
|
511
|
+
formattedValue: computed("formattedValue"),
|
|
512
|
+
reason
|
|
513
|
+
});
|
|
514
|
+
});
|
|
515
|
+
},
|
|
516
|
+
syncInputValue({ context, computed, prop, state }) {
|
|
517
|
+
const text = context.get("inputValue");
|
|
518
|
+
const formatted = computed("formattedValue");
|
|
519
|
+
if (text === formatted) return;
|
|
520
|
+
if (state.hasTag("focus") && representsValue(text, context.get("value"), {
|
|
521
|
+
computed,
|
|
522
|
+
prop
|
|
523
|
+
})) return;
|
|
524
|
+
context.set("inputValue", formatted);
|
|
525
|
+
},
|
|
526
|
+
syncInputElement({ context, event, scope, refs }) {
|
|
527
|
+
const value = context.get("inputValue");
|
|
528
|
+
const inputEl = getInputEl(scope);
|
|
529
|
+
const sel = event.selection ?? recordCursor(inputEl);
|
|
530
|
+
refs.get("syncFrame").request(() => {
|
|
531
|
+
const unchanged = inputEl?.value === value;
|
|
532
|
+
setElementValue(inputEl, value);
|
|
533
|
+
if (!unchanged) restoreCursor(inputEl, sel);
|
|
534
|
+
});
|
|
535
|
+
},
|
|
536
|
+
syncHiddenInput({ scope, context }) {
|
|
537
|
+
const value = context.get("value");
|
|
538
|
+
dispatchHiddenInputValue(scope, value == null ? "" : String(value));
|
|
539
|
+
},
|
|
540
|
+
setFormattedValue({ context, computed }) {
|
|
541
|
+
context.set("inputValue", computed("formattedValue"));
|
|
542
|
+
},
|
|
543
|
+
setCursorPoint({ context, event }) {
|
|
544
|
+
context.set("scrubberCursorPoint", event.point);
|
|
545
|
+
},
|
|
546
|
+
clearCursorPoint({ context }) {
|
|
547
|
+
context.set("scrubberCursorPoint", null);
|
|
548
|
+
},
|
|
549
|
+
setVirtualCursorPosition({ context, scope }) {
|
|
550
|
+
const scrubberEl = getScrubberEl(scope);
|
|
551
|
+
const point = context.get("scrubberCursorPoint");
|
|
552
|
+
if (!scrubberEl || !point) return;
|
|
553
|
+
scrubberEl.style.setProperty("--scrubber-x", `${point.x}px`);
|
|
554
|
+
scrubberEl.style.setProperty("--scrubber-y", `${point.y}px`);
|
|
555
|
+
},
|
|
556
|
+
accumulateDelta({ context, event, prop, send }) {
|
|
557
|
+
const delta = event.delta ?? 0;
|
|
558
|
+
const newDelta = context.get("cumulativeDelta") + delta;
|
|
559
|
+
const pixels = prop("scrubberPixelSensitivity");
|
|
560
|
+
const sensitivity = pixels > 0 ? pixels : 1;
|
|
561
|
+
if (Math.abs(newDelta) >= sensitivity) {
|
|
562
|
+
const step = prop("step");
|
|
563
|
+
if (event.direction) send({
|
|
564
|
+
type: "SCRUBBER.STEP",
|
|
565
|
+
step,
|
|
566
|
+
direction: event.direction,
|
|
567
|
+
src: "scrub"
|
|
568
|
+
});
|
|
569
|
+
context.set("cumulativeDelta", newDelta % sensitivity);
|
|
570
|
+
} else context.set("cumulativeDelta", newDelta);
|
|
571
|
+
},
|
|
572
|
+
clearCumulativeDelta({ context }) {
|
|
573
|
+
context.set("cumulativeDelta", 0);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
}
|
|
511
577
|
});
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
}
|
|
578
|
+
const getPressSource = (direction) => direction > 0 ? "increment-trigger" : "decrement-trigger";
|
|
579
|
+
/** Sets the number and shows it formatted */
|
|
580
|
+
function setNumber({ context, prop, computed }, value) {
|
|
581
|
+
value = value == null ? null : normalizeZero(value);
|
|
582
|
+
context.set("value", value);
|
|
583
|
+
context.set("inputValue", formatValue(value, {
|
|
584
|
+
computed,
|
|
585
|
+
prop
|
|
586
|
+
}));
|
|
587
|
+
}
|
|
588
|
+
function getStepValue(value, step, direction, { prop, computed }) {
|
|
589
|
+
if (prop("snapOnStep")) value = snapToStep(value, prop("min") ?? 0, step, direction);
|
|
590
|
+
return clampValue(value, computed("min"), computed("max"));
|
|
591
|
+
}
|
|
592
|
+
function representsValue(text, value, params) {
|
|
593
|
+
if (text.trim() === "") return value == null;
|
|
594
|
+
const parsed = parseValue(text, params);
|
|
595
|
+
return Number.isNaN(parsed) || parsed === value;
|
|
596
|
+
}
|
|
597
|
+
const normalizeZero = (value) => value === 0 ? 0 : value;
|
|
598
|
+
export { machine };
|