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