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