@zag-js/number-input 0.1.11 → 0.1.12

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.js CHANGED
@@ -43,6 +43,22 @@ __export(src_exports, {
43
43
  module.exports = __toCommonJS(src_exports);
44
44
 
45
45
  // ../../utilities/dom/dist/index.mjs
46
+ var __defProp2 = Object.defineProperty;
47
+ var __getOwnPropSymbols2 = Object.getOwnPropertySymbols;
48
+ var __hasOwnProp2 = Object.prototype.hasOwnProperty;
49
+ var __propIsEnum2 = Object.prototype.propertyIsEnumerable;
50
+ var __defNormalProp2 = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
51
+ var __spreadValues2 = (a, b) => {
52
+ for (var prop in b || (b = {}))
53
+ if (__hasOwnProp2.call(b, prop))
54
+ __defNormalProp2(a, prop, b[prop]);
55
+ if (__getOwnPropSymbols2)
56
+ for (var prop of __getOwnPropSymbols2(b)) {
57
+ if (__propIsEnum2.call(b, prop))
58
+ __defNormalProp2(a, prop, b[prop]);
59
+ }
60
+ return a;
61
+ };
46
62
  var dataAttr = (guard) => {
47
63
  return guard ? "" : void 0;
48
64
  };
@@ -75,6 +91,36 @@ var isMac = () => pt(/^Mac/) && !isTouchDevice;
75
91
  var isSafari = () => isApple() && vn(/apple/i);
76
92
  var isApple = () => pt(/mac|iphone|ipad|ipod/i);
77
93
  var isIos = () => isApple() && !isMac();
94
+ function isDocument(el) {
95
+ return el.nodeType === Node.DOCUMENT_NODE;
96
+ }
97
+ function isWindow(value) {
98
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
99
+ }
100
+ function getDocument(el) {
101
+ var _a;
102
+ if (isWindow(el))
103
+ return el.document;
104
+ if (isDocument(el))
105
+ return el;
106
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
107
+ }
108
+ function defineDomHelpers(helpers) {
109
+ const dom2 = {
110
+ getRootNode: (ctx) => {
111
+ var _a, _b;
112
+ return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
113
+ },
114
+ getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
115
+ getWin: (ctx) => {
116
+ var _a;
117
+ return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
118
+ },
119
+ getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
120
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
121
+ };
122
+ return __spreadValues2(__spreadValues2({}, dom2), helpers);
123
+ }
78
124
  function getNativeEvent(e) {
79
125
  var _a;
80
126
  return (_a = e.nativeEvent) != null ? _a : e;
@@ -241,48 +287,36 @@ function decimalOperation(a, op, b) {
241
287
  var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
242
288
 
243
289
  // src/number-input.dom.ts
244
- var dom = {
245
- getDoc: (ctx) => {
246
- var _a;
247
- return (_a = ctx.doc) != null ? _a : document;
248
- },
249
- getWin: (ctx) => {
250
- var _a;
251
- return (_a = dom.getDoc(ctx).defaultView) != null ? _a : window;
252
- },
253
- getRootNode: (ctx) => {
254
- var _a;
255
- return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
256
- },
290
+ var dom = defineDomHelpers({
257
291
  getRootId: (ctx) => {
258
292
  var _a, _b;
259
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `number-input:${ctx.uid}`;
293
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `number-input:${ctx.id}`;
260
294
  },
261
295
  getInputId: (ctx) => {
262
296
  var _a, _b;
263
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.input) != null ? _b : `number-input:${ctx.uid}:input`;
297
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.input) != null ? _b : `number-input:${ctx.id}:input`;
264
298
  },
265
299
  getIncButtonId: (ctx) => {
266
300
  var _a, _b;
267
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.incBtn) != null ? _b : `number-input:${ctx.uid}:inc-btn`;
301
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.incBtn) != null ? _b : `number-input:${ctx.id}:inc-btn`;
268
302
  },
269
303
  getDecButtonId: (ctx) => {
270
304
  var _a, _b;
271
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.decBtn) != null ? _b : `number-input:${ctx.uid}:dec-btn`;
305
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.decBtn) != null ? _b : `number-input:${ctx.id}:dec-btn`;
272
306
  },
273
307
  getScrubberId: (ctx) => {
274
308
  var _a, _b;
275
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.scrubber) != null ? _b : `number-input:${ctx.uid}:scrubber`;
309
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.scrubber) != null ? _b : `number-input:${ctx.id}:scrubber`;
276
310
  },
277
- getCursorId: (ctx) => `number-input:${ctx.uid}:cursor`,
311
+ getCursorId: (ctx) => `number-input:${ctx.id}:cursor`,
278
312
  getLabelId: (ctx) => {
279
313
  var _a, _b;
280
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `number-input:${ctx.uid}:label`;
314
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `number-input:${ctx.id}:label`;
281
315
  },
282
- getInputEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getInputId(ctx)),
283
- getIncButtonEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getIncButtonId(ctx)),
284
- getDecButtonEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getDecButtonId(ctx)),
285
- getScrubberEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getScrubberId(ctx)),
316
+ getInputEl: (ctx) => dom.getById(ctx, dom.getInputId(ctx)),
317
+ getIncButtonEl: (ctx) => dom.getById(ctx, dom.getIncButtonId(ctx)),
318
+ getDecButtonEl: (ctx) => dom.getById(ctx, dom.getDecButtonId(ctx)),
319
+ getScrubberEl: (ctx) => dom.getById(ctx, dom.getScrubberId(ctx)),
286
320
  getCursorEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getCursorId(ctx)),
287
321
  getMousementValue(ctx, event) {
288
322
  const x = roundToDevicePixel(event.movementX);
@@ -327,7 +361,7 @@ var dom = {
327
361
  </svg>`;
328
362
  doc.body.appendChild(el);
329
363
  }
330
- };
364
+ });
331
365
 
332
366
  // src/number-input.utils.ts
333
367
  var utils = {
@@ -573,12 +607,11 @@ var callAll2 = (...fns) => (...a) => {
573
607
 
574
608
  // src/number-input.machine.ts
575
609
  var { not, and } = import_core.guards;
576
- function machine(ctx = {}) {
610
+ function machine(ctx) {
577
611
  return (0, import_core.createMachine)({
578
612
  id: "number-input",
579
613
  initial: "unknown",
580
614
  context: __spreadProps(__spreadValues({
581
- uid: "",
582
615
  dir: "ltr",
583
616
  focusInputOnChange: true,
584
617
  clampValueOnBlur: true,
@@ -639,7 +672,7 @@ function machine(ctx = {}) {
639
672
  on: {
640
673
  SETUP: {
641
674
  target: "idle",
642
- actions: ["setupDocument", "syncInputValue"]
675
+ actions: "syncInputValue"
643
676
  }
644
677
  }
645
678
  },
@@ -845,13 +878,6 @@ function machine(ctx = {}) {
845
878
  }
846
879
  },
847
880
  actions: {
848
- setupDocument: (ctx2, evt) => {
849
- if (evt.doc)
850
- ctx2.doc = (0, import_core.ref)(evt.doc);
851
- if (evt.root)
852
- ctx2.rootNode = (0, import_core.ref)(evt.root);
853
- ctx2.uid = evt.id;
854
- },
855
881
  focusInput(ctx2) {
856
882
  if (!ctx2.focusInputOnChange)
857
883
  return;
package/dist/index.mjs CHANGED
@@ -19,6 +19,22 @@ var __spreadValues = (a, b) => {
19
19
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
20
 
21
21
  // ../../utilities/dom/dist/index.mjs
22
+ var __defProp2 = Object.defineProperty;
23
+ var __getOwnPropSymbols2 = Object.getOwnPropertySymbols;
24
+ var __hasOwnProp2 = Object.prototype.hasOwnProperty;
25
+ var __propIsEnum2 = Object.prototype.propertyIsEnumerable;
26
+ var __defNormalProp2 = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
27
+ var __spreadValues2 = (a, b) => {
28
+ for (var prop in b || (b = {}))
29
+ if (__hasOwnProp2.call(b, prop))
30
+ __defNormalProp2(a, prop, b[prop]);
31
+ if (__getOwnPropSymbols2)
32
+ for (var prop of __getOwnPropSymbols2(b)) {
33
+ if (__propIsEnum2.call(b, prop))
34
+ __defNormalProp2(a, prop, b[prop]);
35
+ }
36
+ return a;
37
+ };
22
38
  var dataAttr = (guard) => {
23
39
  return guard ? "" : void 0;
24
40
  };
@@ -51,6 +67,36 @@ var isMac = () => pt(/^Mac/) && !isTouchDevice;
51
67
  var isSafari = () => isApple() && vn(/apple/i);
52
68
  var isApple = () => pt(/mac|iphone|ipad|ipod/i);
53
69
  var isIos = () => isApple() && !isMac();
70
+ function isDocument(el) {
71
+ return el.nodeType === Node.DOCUMENT_NODE;
72
+ }
73
+ function isWindow(value) {
74
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
75
+ }
76
+ function getDocument(el) {
77
+ var _a;
78
+ if (isWindow(el))
79
+ return el.document;
80
+ if (isDocument(el))
81
+ return el;
82
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
83
+ }
84
+ function defineDomHelpers(helpers) {
85
+ const dom2 = {
86
+ getRootNode: (ctx) => {
87
+ var _a, _b;
88
+ return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
89
+ },
90
+ getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
91
+ getWin: (ctx) => {
92
+ var _a;
93
+ return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
94
+ },
95
+ getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
96
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
97
+ };
98
+ return __spreadValues2(__spreadValues2({}, dom2), helpers);
99
+ }
54
100
  function getNativeEvent(e) {
55
101
  var _a;
56
102
  return (_a = e.nativeEvent) != null ? _a : e;
@@ -217,48 +263,36 @@ function decimalOperation(a, op, b) {
217
263
  var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
218
264
 
219
265
  // src/number-input.dom.ts
220
- var dom = {
221
- getDoc: (ctx) => {
222
- var _a;
223
- return (_a = ctx.doc) != null ? _a : document;
224
- },
225
- getWin: (ctx) => {
226
- var _a;
227
- return (_a = dom.getDoc(ctx).defaultView) != null ? _a : window;
228
- },
229
- getRootNode: (ctx) => {
230
- var _a;
231
- return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx);
232
- },
266
+ var dom = defineDomHelpers({
233
267
  getRootId: (ctx) => {
234
268
  var _a, _b;
235
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `number-input:${ctx.uid}`;
269
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `number-input:${ctx.id}`;
236
270
  },
237
271
  getInputId: (ctx) => {
238
272
  var _a, _b;
239
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.input) != null ? _b : `number-input:${ctx.uid}:input`;
273
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.input) != null ? _b : `number-input:${ctx.id}:input`;
240
274
  },
241
275
  getIncButtonId: (ctx) => {
242
276
  var _a, _b;
243
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.incBtn) != null ? _b : `number-input:${ctx.uid}:inc-btn`;
277
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.incBtn) != null ? _b : `number-input:${ctx.id}:inc-btn`;
244
278
  },
245
279
  getDecButtonId: (ctx) => {
246
280
  var _a, _b;
247
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.decBtn) != null ? _b : `number-input:${ctx.uid}:dec-btn`;
281
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.decBtn) != null ? _b : `number-input:${ctx.id}:dec-btn`;
248
282
  },
249
283
  getScrubberId: (ctx) => {
250
284
  var _a, _b;
251
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.scrubber) != null ? _b : `number-input:${ctx.uid}:scrubber`;
285
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.scrubber) != null ? _b : `number-input:${ctx.id}:scrubber`;
252
286
  },
253
- getCursorId: (ctx) => `number-input:${ctx.uid}:cursor`,
287
+ getCursorId: (ctx) => `number-input:${ctx.id}:cursor`,
254
288
  getLabelId: (ctx) => {
255
289
  var _a, _b;
256
- return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `number-input:${ctx.uid}:label`;
290
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `number-input:${ctx.id}:label`;
257
291
  },
258
- getInputEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getInputId(ctx)),
259
- getIncButtonEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getIncButtonId(ctx)),
260
- getDecButtonEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getDecButtonId(ctx)),
261
- getScrubberEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getScrubberId(ctx)),
292
+ getInputEl: (ctx) => dom.getById(ctx, dom.getInputId(ctx)),
293
+ getIncButtonEl: (ctx) => dom.getById(ctx, dom.getIncButtonId(ctx)),
294
+ getDecButtonEl: (ctx) => dom.getById(ctx, dom.getDecButtonId(ctx)),
295
+ getScrubberEl: (ctx) => dom.getById(ctx, dom.getScrubberId(ctx)),
262
296
  getCursorEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getCursorId(ctx)),
263
297
  getMousementValue(ctx, event) {
264
298
  const x = roundToDevicePixel(event.movementX);
@@ -303,7 +337,7 @@ var dom = {
303
337
  </svg>`;
304
338
  doc.body.appendChild(el);
305
339
  }
306
- };
340
+ });
307
341
 
308
342
  // src/number-input.utils.ts
309
343
  var utils = {
@@ -538,7 +572,7 @@ function connect(state, send, normalize) {
538
572
  }
539
573
 
540
574
  // src/number-input.machine.ts
541
- import { choose, createMachine, guards, ref } from "@zag-js/core";
575
+ import { choose, createMachine, guards } from "@zag-js/core";
542
576
 
543
577
  // ../../utilities/core/dist/index.mjs
544
578
  var callAll2 = (...fns) => (...a) => {
@@ -549,12 +583,11 @@ var callAll2 = (...fns) => (...a) => {
549
583
 
550
584
  // src/number-input.machine.ts
551
585
  var { not, and } = guards;
552
- function machine(ctx = {}) {
586
+ function machine(ctx) {
553
587
  return createMachine({
554
588
  id: "number-input",
555
589
  initial: "unknown",
556
590
  context: __spreadProps(__spreadValues({
557
- uid: "",
558
591
  dir: "ltr",
559
592
  focusInputOnChange: true,
560
593
  clampValueOnBlur: true,
@@ -615,7 +648,7 @@ function machine(ctx = {}) {
615
648
  on: {
616
649
  SETUP: {
617
650
  target: "idle",
618
- actions: ["setupDocument", "syncInputValue"]
651
+ actions: "syncInputValue"
619
652
  }
620
653
  }
621
654
  },
@@ -821,13 +854,6 @@ function machine(ctx = {}) {
821
854
  }
822
855
  },
823
856
  actions: {
824
- setupDocument: (ctx2, evt) => {
825
- if (evt.doc)
826
- ctx2.doc = ref(evt.doc);
827
- if (evt.root)
828
- ctx2.rootNode = ref(evt.root);
829
- ctx2.uid = evt.id;
830
- },
831
857
  focusInput(ctx2) {
832
858
  if (!ctx2.focusInputOnChange)
833
859
  return;
@@ -1,8 +1,21 @@
1
1
  import type { MachineContext as Ctx } from "./number-input.types";
2
2
  export declare const dom: {
3
- getDoc: (ctx: Ctx) => Document;
4
- getWin: (ctx: Ctx) => Window & typeof globalThis;
5
- getRootNode: (ctx: Ctx) => Document | ShadowRoot;
3
+ getRootNode: (ctx: {
4
+ getRootNode?: () => Node | Document | ShadowRoot;
5
+ }) => Document | ShadowRoot;
6
+ getDoc: (ctx: {
7
+ getRootNode?: () => Node | Document | ShadowRoot;
8
+ }) => Document;
9
+ getWin: (ctx: {
10
+ getRootNode?: () => Node | Document | ShadowRoot;
11
+ }) => Window & typeof globalThis;
12
+ getActiveElement: (ctx: {
13
+ getRootNode?: () => Node | Document | ShadowRoot;
14
+ }) => HTMLElement;
15
+ getById: <T_1 = HTMLElement>(ctx: {
16
+ getRootNode?: () => Node | Document | ShadowRoot;
17
+ }, id: string) => T_1;
18
+ } & {
6
19
  getRootId: (ctx: Ctx) => string;
7
20
  getInputId: (ctx: Ctx) => string;
8
21
  getIncButtonId: (ctx: Ctx) => string;
@@ -10,7 +23,7 @@ export declare const dom: {
10
23
  getScrubberId: (ctx: Ctx) => string;
11
24
  getCursorId: (ctx: Ctx) => string;
12
25
  getLabelId: (ctx: Ctx) => string;
13
- getInputEl: (ctx: Ctx) => HTMLInputElement;
26
+ getInputEl: (ctx: Ctx) => HTMLElement;
14
27
  getIncButtonEl: (ctx: Ctx) => HTMLButtonElement;
15
28
  getDecButtonEl: (ctx: Ctx) => HTMLButtonElement;
16
29
  getScrubberEl: (ctx: Ctx) => HTMLElement;
@@ -1,2 +1,2 @@
1
1
  import type { MachineContext, MachineState, UserDefinedContext } from "./number-input.types";
2
- export declare function machine(ctx?: UserDefinedContext): import("@zag-js/core").Machine<MachineContext, MachineState, import("@zag-js/core").StateMachine.AnyEventObject>;
2
+ export declare function machine(ctx: UserDefinedContext): import("@zag-js/core").Machine<MachineContext, MachineState, import("@zag-js/core").StateMachine.AnyEventObject>;
@@ -1,6 +1,5 @@
1
1
  import type { StateMachine as S } from "@zag-js/core";
2
- import type { FormatDecimalOptions } from "@zag-js/number-utils";
3
- import type { Context, DirectionProperty } from "@zag-js/types";
2
+ import type { CommonProperties, Context, DirectionProperty, RequiredBy } from "@zag-js/types";
4
3
  declare type ValidityState = "rangeUnderflow" | "rangeOverflow";
5
4
  declare type ElementIds = Partial<{
6
5
  root: string;
@@ -25,7 +24,7 @@ declare type IntlMessages = {
25
24
  */
26
25
  decrementLabel: string;
27
26
  };
28
- declare type PublicContext = DirectionProperty & FormatDecimalOptions & {
27
+ declare type PublicContext = DirectionProperty & CommonProperties & {
29
28
  /**
30
29
  * The ids of the elements in the number input. Useful for composition.
31
30
  */
@@ -126,8 +125,16 @@ declare type PublicContext = DirectionProperty & FormatDecimalOptions & {
126
125
  value: string;
127
126
  valueAsNumber: number;
128
127
  }) => void;
128
+ /**
129
+ * The minimum number of fraction digits to use. Possible values are from 0 to 20
130
+ */
131
+ minFractionDigits?: number;
132
+ /**
133
+ * The maximum number of fraction digits to use. Possible values are from 0 to 20;
134
+ */
135
+ maxFractionDigits?: number;
129
136
  };
130
- export declare type UserDefinedContext = Partial<PublicContext>;
137
+ export declare type UserDefinedContext = RequiredBy<PublicContext, "id">;
131
138
  declare type ComputedContext = Readonly<{
132
139
  /**
133
140
  * @computed
@@ -1,3 +1,4 @@
1
+ import type { JSX } from "@zag-js/types";
1
2
  import type { MachineContext as Ctx } from "./number-input.types";
2
3
  export declare const utils: {
3
4
  isValidNumericEvent: (ctx: Ctx, event: JSX.KeyboardEvent) => boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/number-input",
3
- "version": "0.1.11",
3
+ "version": "0.1.12",
4
4
  "description": "Core logic for the number-input widget implemented as a state machine",
5
5
  "keywords": [
6
6
  "js",
@@ -29,10 +29,12 @@
29
29
  "url": "https://github.com/chakra-ui/zag/issues"
30
30
  },
31
31
  "dependencies": {
32
- "@zag-js/core": "0.1.7",
33
- "@zag-js/dom-utils": "0.1.6",
32
+ "@zag-js/core": "0.1.8",
33
+ "@zag-js/types": "0.2.2"
34
+ },
35
+ "devDependencies": {
36
+ "@zag-js/dom-utils": "0.1.7",
34
37
  "@zag-js/number-utils": "0.1.2",
35
- "@zag-js/types": "0.2.1",
36
38
  "@zag-js/utils": "0.1.2"
37
39
  },
38
40
  "scripts": {