@zag-js/number-input 0.10.2 → 0.10.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.
@@ -1,179 +1,21 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
1
+ 'use strict';
19
2
 
20
- // src/number-input.machine.ts
21
- var number_input_machine_exports = {};
22
- __export(number_input_machine_exports, {
23
- machine: () => machine
24
- });
25
- module.exports = __toCommonJS(number_input_machine_exports);
26
- var import_core = require("@zag-js/core");
27
- var import_dom_event2 = require("@zag-js/dom-event");
28
- var import_dom_query2 = require("@zag-js/dom-query");
29
- var import_form_utils = require("@zag-js/form-utils");
30
- var import_mutation_observer = require("@zag-js/mutation-observer");
31
- var import_number_utils3 = require("@zag-js/number-utils");
32
- var import_utils = require("@zag-js/utils");
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
33
4
 
34
- // src/number-input.dom.ts
35
- var import_dom_query = require("@zag-js/dom-query");
36
- var import_number_utils = require("@zag-js/number-utils");
37
- var dom = (0, import_dom_query.createScope)({
38
- getRootId: (ctx) => ctx.ids?.root ?? `number-input:${ctx.id}`,
39
- getInputId: (ctx) => ctx.ids?.input ?? `number-input:${ctx.id}:input`,
40
- getIncrementTriggerId: (ctx) => ctx.ids?.incrementTrigger ?? `number-input:${ctx.id}:inc`,
41
- getDecrementTriggerId: (ctx) => ctx.ids?.decrementTrigger ?? `number-input:${ctx.id}:dec`,
42
- getScrubberId: (ctx) => ctx.ids?.scrubber ?? `number-input:${ctx.id}:scrubber`,
43
- getCursorId: (ctx) => `number-input:${ctx.id}:cursor`,
44
- getLabelId: (ctx) => ctx.ids?.label ?? `number-input:${ctx.id}:label`,
45
- getInputEl: (ctx) => dom.getById(ctx, dom.getInputId(ctx)),
46
- getIncrementTriggerEl: (ctx) => dom.getById(ctx, dom.getIncrementTriggerId(ctx)),
47
- getDecrementTriggerEl: (ctx) => dom.getById(ctx, dom.getDecrementTriggerId(ctx)),
48
- getScrubberEl: (ctx) => dom.getById(ctx, dom.getScrubberId(ctx)),
49
- getCursorEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getCursorId(ctx)),
50
- getPressedTriggerEl: (ctx, hint = ctx.hint) => {
51
- let btnEl = null;
52
- if (hint === "increment") {
53
- btnEl = dom.getIncrementTriggerEl(ctx);
54
- }
55
- if (hint === "decrement") {
56
- btnEl = dom.getDecrementTriggerEl(ctx);
57
- }
58
- return btnEl;
59
- },
60
- setupVirtualCursor(ctx) {
61
- if ((0, import_dom_query.isSafari)())
62
- return;
63
- dom.createVirtualCursor(ctx);
64
- return () => {
65
- dom.getCursorEl(ctx)?.remove();
66
- };
67
- },
68
- preventTextSelection(ctx) {
69
- const doc = dom.getDoc(ctx);
70
- const html = doc.documentElement;
71
- const body = doc.body;
72
- body.style.pointerEvents = "none";
73
- html.style.userSelect = "none";
74
- html.style.cursor = "ew-resize";
75
- return () => {
76
- body.style.pointerEvents = "";
77
- html.style.userSelect = "";
78
- html.style.cursor = "";
79
- if (!html.style.length) {
80
- html.removeAttribute("style");
81
- }
82
- if (!body.style.length) {
83
- body.removeAttribute("style");
84
- }
85
- };
86
- },
87
- getMousementValue(ctx, event) {
88
- const x = (0, import_number_utils.roundToDevicePixel)(event.movementX);
89
- const y = (0, import_number_utils.roundToDevicePixel)(event.movementY);
90
- let hint = x > 0 ? "increment" : x < 0 ? "decrement" : null;
91
- if (ctx.isRtl && hint === "increment")
92
- hint = "decrement";
93
- if (ctx.isRtl && hint === "decrement")
94
- hint = "increment";
95
- const point = {
96
- x: ctx.scrubberCursorPoint.x + x,
97
- y: ctx.scrubberCursorPoint.y + y
98
- };
99
- const win = dom.getWin(ctx);
100
- const width = win.innerWidth;
101
- const half = (0, import_number_utils.roundToDevicePixel)(7.5);
102
- point.x = (0, import_number_utils.wrap)(point.x + half, width) - half;
103
- return { hint, point };
104
- },
105
- createVirtualCursor(ctx) {
106
- const doc = dom.getDoc(ctx);
107
- const el = doc.createElement("div");
108
- el.className = "scrubber--cursor";
109
- el.id = dom.getCursorId(ctx);
110
- Object.assign(el.style, {
111
- width: "15px",
112
- height: "15px",
113
- position: "fixed",
114
- pointerEvents: "none",
115
- left: "0px",
116
- top: "0px",
117
- zIndex: import_dom_query.MAX_Z_INDEX,
118
- transform: ctx.scrubberCursorPoint ? `translate3d(${ctx.scrubberCursorPoint.x}px, ${ctx.scrubberCursorPoint.y}px, 0px)` : void 0,
119
- willChange: "transform"
120
- });
121
- el.innerHTML = `
122
- <svg width="46" height="15" style="left: -15.5px; position: absolute; top: 0; filter: drop-shadow(rgba(0, 0, 0, 0.4) 0px 1px 1.1px);">
123
- <g transform="translate(2 3)">
124
- <path fill-rule="evenodd" d="M 15 4.5L 15 2L 11.5 5.5L 15 9L 15 6.5L 31 6.5L 31 9L 34.5 5.5L 31 2L 31 4.5Z" style="stroke-width: 2px; stroke: white;"></path>
125
- <path fill-rule="evenodd" d="M 15 4.5L 15 2L 11.5 5.5L 15 9L 15 6.5L 31 6.5L 31 9L 34.5 5.5L 31 2L 31 4.5Z"></path>
126
- </g>
127
- </svg>`;
128
- doc.body.appendChild(el);
129
- }
130
- });
131
-
132
- // src/number-input.utils.ts
133
- var import_dom_event = require("@zag-js/dom-event");
134
- var import_number_utils2 = require("@zag-js/number-utils");
135
- var utils = {
136
- isValidNumericEvent: (ctx, event) => {
137
- if (event.key == null)
138
- return true;
139
- const isModifier = (0, import_dom_event.isModifiedEvent)(event);
140
- const isSingleKey = event.key.length === 1;
141
- if (isModifier || !isSingleKey)
142
- return true;
143
- return ctx.validateCharacter?.(event.key) ?? utils.isFloatingPoint(event.key);
144
- },
145
- isFloatingPoint: (v) => /^[0-9+\-.]$/.test(v),
146
- sanitize: (ctx, value) => {
147
- return value.split("").filter(ctx.validateCharacter ?? utils.isFloatingPoint).join("");
148
- },
149
- increment: (ctx, step) => {
150
- const value = (0, import_number_utils2.increment)(ctx.value, step ?? ctx.step);
151
- return (0, import_number_utils2.formatDecimal)((0, import_number_utils2.clamp)(value, ctx), ctx);
152
- },
153
- decrement: (ctx, step) => {
154
- const value = (0, import_number_utils2.decrement)(ctx.value, step ?? ctx.step);
155
- return (0, import_number_utils2.formatDecimal)((0, import_number_utils2.clamp)(value, ctx), ctx);
156
- },
157
- clamp: (ctx) => {
158
- return (0, import_number_utils2.formatDecimal)((0, import_number_utils2.clamp)(ctx.value, ctx), ctx);
159
- },
160
- parse: (ctx, value) => {
161
- return ctx.parse?.(value) ?? value;
162
- },
163
- format: (ctx, value) => {
164
- const _val = value.toString();
165
- return ctx.format?.(_val) ?? _val;
166
- },
167
- round: (ctx) => {
168
- return (0, import_number_utils2.formatDecimal)(ctx.value, ctx);
169
- }
170
- };
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');
171
14
 
172
- // src/number-input.machine.ts
173
- var { not, and } = import_core.guards;
15
+ const { not, and } = core.guards;
174
16
  function machine(userContext) {
175
- const ctx = (0, import_utils.compact)(userContext);
176
- return (0, import_core.createMachine)(
17
+ const ctx = utils.compact(userContext);
18
+ return core.createMachine(
177
19
  {
178
20
  id: "number-input",
179
21
  initial: "idle",
@@ -201,10 +43,10 @@ function machine(userContext) {
201
43
  },
202
44
  computed: {
203
45
  isRtl: (ctx2) => ctx2.dir === "rtl",
204
- valueAsNumber: (ctx2) => (0, import_number_utils3.valueOf)(ctx2.value),
205
- isAtMin: (ctx2) => (0, import_number_utils3.isAtMin)(ctx2.value, ctx2),
206
- isAtMax: (ctx2) => (0, import_number_utils3.isAtMax)(ctx2.value, ctx2),
207
- isOutOfRange: (ctx2) => !(0, import_number_utils3.isWithinRange)(ctx2.value, ctx2),
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),
208
50
  isValueEmpty: (ctx2) => ctx2.value === "",
209
51
  canIncrement: (ctx2) => ctx2.allowOverflow || !ctx2.isAtMax,
210
52
  canDecrement: (ctx2) => ctx2.allowOverflow || !ctx2.isAtMin,
@@ -301,7 +143,7 @@ function machine(userContext) {
301
143
  "before:spin": {
302
144
  tags: "focus",
303
145
  activities: "trackButtonDisabled",
304
- entry: (0, import_core.choose)([
146
+ entry: core.choose([
305
147
  { guard: "isIncrementHint", actions: "increment" },
306
148
  { guard: "isDecrementHint", actions: "decrement" }
307
149
  ]),
@@ -378,23 +220,23 @@ function machine(userContext) {
378
220
  },
379
221
  activities: {
380
222
  setupVirtualCursor(ctx2) {
381
- return dom.setupVirtualCursor(ctx2);
223
+ return numberInput_dom.dom.setupVirtualCursor(ctx2);
382
224
  },
383
225
  preventTextSelection(ctx2) {
384
- return dom.preventTextSelection(ctx2);
226
+ return numberInput_dom.dom.preventTextSelection(ctx2);
385
227
  },
386
228
  trackButtonDisabled(ctx2, _evt, { send }) {
387
- const btn = dom.getPressedTriggerEl(ctx2, ctx2.hint);
388
- return (0, import_mutation_observer.observeAttributes)(btn, ["disabled"], () => {
229
+ const btn = numberInput_dom.dom.getPressedTriggerEl(ctx2, ctx2.hint);
230
+ return mutationObserver.observeAttributes(btn, ["disabled"], () => {
389
231
  send("PRESS_UP");
390
232
  });
391
233
  },
392
234
  attachWheelListener(ctx2, _evt, { send }) {
393
- const input = dom.getInputEl(ctx2);
235
+ const input = numberInput_dom.dom.getInputEl(ctx2);
394
236
  if (!input)
395
237
  return;
396
238
  function onWheel(event) {
397
- const isInputFocused = dom.getDoc(ctx2).activeElement === input;
239
+ const isInputFocused = numberInput_dom.dom.getDoc(ctx2).activeElement === input;
398
240
  if (!ctx2.allowMouseWheel || !isInputFocused)
399
241
  return;
400
242
  event.preventDefault();
@@ -405,19 +247,19 @@ function machine(userContext) {
405
247
  send("DECREMENT");
406
248
  }
407
249
  }
408
- return (0, import_dom_event2.addDomEvent)(input, "wheel", onWheel, { passive: false });
250
+ return domEvent.addDomEvent(input, "wheel", onWheel, { passive: false });
409
251
  },
410
252
  activatePointerLock(ctx2) {
411
- if ((0, import_dom_query2.isSafari)())
253
+ if (domQuery.isSafari())
412
254
  return;
413
- return (0, import_dom_event2.requestPointerLock)(dom.getDoc(ctx2));
255
+ return domEvent.requestPointerLock(numberInput_dom.dom.getDoc(ctx2));
414
256
  },
415
257
  trackMousemove(ctx2, _evt, { send }) {
416
- const doc = dom.getDoc(ctx2);
258
+ const doc = numberInput_dom.dom.getDoc(ctx2);
417
259
  function onMousemove(event) {
418
260
  if (!ctx2.scrubberCursorPoint)
419
261
  return;
420
- const value = dom.getMousementValue(ctx2, event);
262
+ const value = numberInput_dom.dom.getMousementValue(ctx2, event);
421
263
  if (!value.hint)
422
264
  return;
423
265
  send({
@@ -429,9 +271,9 @@ function machine(userContext) {
429
271
  function onMouseup() {
430
272
  send("POINTER_UP_SCRUBBER");
431
273
  }
432
- return (0, import_utils.callAll)(
433
- (0, import_dom_event2.addDomEvent)(doc, "mousemove", onMousemove, false),
434
- (0, import_dom_event2.addDomEvent)(doc, "mouseup", onMouseup, false)
274
+ return utils.callAll(
275
+ domEvent.addDomEvent(doc, "mousemove", onMousemove, false),
276
+ domEvent.addDomEvent(doc, "mouseup", onMouseup, false)
435
277
  );
436
278
  }
437
279
  },
@@ -439,26 +281,26 @@ function machine(userContext) {
439
281
  focusInput(ctx2) {
440
282
  if (!ctx2.focusInputOnChange)
441
283
  return;
442
- const input = dom.getInputEl(ctx2);
443
- (0, import_dom_query2.raf)(() => input?.focus());
284
+ const input = numberInput_dom.dom.getInputEl(ctx2);
285
+ domQuery.raf(() => input?.focus());
444
286
  },
445
287
  increment(ctx2, evt) {
446
- ctx2.value = utils.increment(ctx2, evt.step);
288
+ ctx2.value = numberInput_utils.utils.increment(ctx2, evt.step);
447
289
  },
448
290
  decrement(ctx2, evt) {
449
- ctx2.value = utils.decrement(ctx2, evt.step);
291
+ ctx2.value = numberInput_utils.utils.decrement(ctx2, evt.step);
450
292
  },
451
293
  clampValue(ctx2) {
452
- ctx2.value = utils.clamp(ctx2);
294
+ ctx2.value = numberInput_utils.utils.clamp(ctx2);
453
295
  },
454
296
  roundValue(ctx2) {
455
297
  if (ctx2.value !== "") {
456
- ctx2.value = utils.round(ctx2);
298
+ ctx2.value = numberInput_utils.utils.round(ctx2);
457
299
  }
458
300
  },
459
301
  setValue(ctx2, evt) {
460
302
  const value = evt.target?.value ?? evt.value;
461
- ctx2.value = utils.sanitize(ctx2, utils.parse(ctx2, value.toString()));
303
+ ctx2.value = numberInput_utils.utils.sanitize(ctx2, numberInput_utils.utils.parse(ctx2, value.toString()));
462
304
  },
463
305
  clearValue(ctx2) {
464
306
  ctx2.value = "";
@@ -487,11 +329,11 @@ function machine(userContext) {
487
329
  invokeOnFocus(ctx2, evt) {
488
330
  let srcElement = null;
489
331
  if (evt.type === "PRESS_DOWN") {
490
- srcElement = dom.getPressedTriggerEl(ctx2, evt.hint);
332
+ srcElement = numberInput_dom.dom.getPressedTriggerEl(ctx2, evt.hint);
491
333
  } else if (evt.type === "FOCUS") {
492
- srcElement = dom.getInputEl(ctx2);
334
+ srcElement = numberInput_dom.dom.getInputEl(ctx2);
493
335
  } else if (evt.type === "PRESS_DOWN_SCRUBBER") {
494
- srcElement = dom.getScrubberEl(ctx2);
336
+ srcElement = numberInput_dom.dom.getScrubberEl(ctx2);
495
337
  }
496
338
  ctx2.onFocus?.({
497
339
  value: ctx2.value,
@@ -517,11 +359,11 @@ function machine(userContext) {
517
359
  },
518
360
  // sync input value, in event it was set from form libraries via `ref`, `bind:this`, etc.
519
361
  syncInputValue(ctx2) {
520
- const input = dom.getInputEl(ctx2);
362
+ const input = numberInput_dom.dom.getInputEl(ctx2);
521
363
  if (!input || input.value == ctx2.value)
522
364
  return;
523
- const value = utils.parse(ctx2, input.value);
524
- ctx2.value = utils.sanitize(ctx2, value);
365
+ const value = numberInput_utils.utils.parse(ctx2, input.value);
366
+ ctx2.value = numberInput_utils.utils.sanitize(ctx2, value);
525
367
  },
526
368
  setCursorPoint(ctx2, evt) {
527
369
  ctx2.scrubberCursorPoint = evt.point;
@@ -530,21 +372,19 @@ function machine(userContext) {
530
372
  ctx2.scrubberCursorPoint = null;
531
373
  },
532
374
  setVirtualCursorPosition(ctx2) {
533
- const cursor = dom.getCursorEl(ctx2);
375
+ const cursor = numberInput_dom.dom.getCursorEl(ctx2);
534
376
  if (!cursor || !ctx2.scrubberCursorPoint)
535
377
  return;
536
378
  const { x, y } = ctx2.scrubberCursorPoint;
537
379
  cursor.style.transform = `translate3d(${x}px, ${y}px, 0px)`;
538
380
  },
539
381
  dispatchChangeEvent(ctx2) {
540
- const inputEl = dom.getInputEl(ctx2);
541
- (0, import_form_utils.dispatchInputValueEvent)(inputEl, { value: ctx2.formattedValue });
382
+ const inputEl = numberInput_dom.dom.getInputEl(ctx2);
383
+ formUtils.dispatchInputValueEvent(inputEl, { value: ctx2.formattedValue });
542
384
  }
543
385
  }
544
386
  }
545
387
  );
546
388
  }
547
- // Annotate the CommonJS export names for ESM import in node:
548
- 0 && (module.exports = {
549
- machine
550
- });
389
+
390
+ exports.machine = machine;