@zag-js/number-input 0.2.5 → 0.2.7

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.
@@ -0,0 +1,169 @@
1
+ // ../../utilities/dom/src/platform.ts
2
+ var isDom = () => typeof window !== "undefined";
3
+ function getPlatform() {
4
+ var _a;
5
+ const agent = navigator.userAgentData;
6
+ return (_a = agent == null ? void 0 : agent.platform) != null ? _a : navigator.platform;
7
+ }
8
+ var pt = (v) => isDom() && v.test(getPlatform());
9
+ var vn = (v) => isDom() && v.test(navigator.vendor);
10
+ var isSafari = () => isApple() && vn(/apple/i);
11
+ var isApple = () => pt(/mac|iphone|ipad|ipod/i);
12
+
13
+ // ../../utilities/dom/src/query.ts
14
+ function isDocument(el) {
15
+ return el.nodeType === Node.DOCUMENT_NODE;
16
+ }
17
+ function isWindow(value) {
18
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
19
+ }
20
+ function getDocument(el) {
21
+ var _a;
22
+ if (isWindow(el))
23
+ return el.document;
24
+ if (isDocument(el))
25
+ return el;
26
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
27
+ }
28
+ function getWindow(el) {
29
+ var _a;
30
+ return (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
31
+ }
32
+ function defineDomHelpers(helpers) {
33
+ const dom = {
34
+ getRootNode: (ctx) => {
35
+ var _a, _b;
36
+ return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
37
+ },
38
+ getDoc: (ctx) => getDocument(dom.getRootNode(ctx)),
39
+ getWin: (ctx) => {
40
+ var _a;
41
+ return (_a = dom.getDoc(ctx).defaultView) != null ? _a : window;
42
+ },
43
+ getActiveElement: (ctx) => dom.getDoc(ctx).activeElement,
44
+ getById: (ctx, id) => dom.getRootNode(ctx).getElementById(id),
45
+ createEmitter: (ctx, target) => {
46
+ const win = dom.getWin(ctx);
47
+ return function emit(evt, detail, options) {
48
+ const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
49
+ const eventName = `zag:${evt}`;
50
+ const init = { bubbles, cancelable, composed, detail };
51
+ const event = new win.CustomEvent(eventName, init);
52
+ target.dispatchEvent(event);
53
+ };
54
+ },
55
+ createListener: (target) => {
56
+ return function listen(evt, handler) {
57
+ const eventName = `zag:${evt}`;
58
+ const listener = (e) => handler(e);
59
+ target.addEventListener(eventName, listener);
60
+ return () => target.removeEventListener(eventName, listener);
61
+ };
62
+ }
63
+ };
64
+ return {
65
+ ...dom,
66
+ ...helpers
67
+ };
68
+ }
69
+
70
+ // ../../utilities/core/src/guard.ts
71
+ var isArray = (v) => Array.isArray(v);
72
+ var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
73
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
74
+
75
+ // ../../utilities/dom/src/event.ts
76
+ function getNativeEvent(e) {
77
+ var _a;
78
+ return (_a = e.nativeEvent) != null ? _a : e;
79
+ }
80
+ var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
81
+ var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
82
+ var isLeftClick = (v) => v.button === 0;
83
+ var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
84
+
85
+ // ../../utilities/number/src/number.ts
86
+ function wrap(num, max) {
87
+ return (num % max + max) % max;
88
+ }
89
+ function roundToDevicePixel(num) {
90
+ if (typeof window === "undefined")
91
+ return Math.round(num);
92
+ const dp = window.devicePixelRatio;
93
+ return Math.floor(num * dp + 0.5) / dp;
94
+ }
95
+ function clamp(v, o) {
96
+ return Math.min(Math.max(valueOf(v), o.min), o.max);
97
+ }
98
+ function countDecimals(value) {
99
+ if (!Number.isFinite(value))
100
+ return 0;
101
+ let e = 1, p = 0;
102
+ while (Math.round(value * e) / e !== value) {
103
+ e *= 10;
104
+ p += 1;
105
+ }
106
+ return p;
107
+ }
108
+ var increment = (v, s) => decimalOperation(valueOf(v), "+", s);
109
+ var decrement = (v, s) => decimalOperation(valueOf(v), "-", s);
110
+ function valueOf(v) {
111
+ if (typeof v === "number")
112
+ return v;
113
+ const num = parseFloat(v.toString().replace(/[^\w.-]+/g, ""));
114
+ return !Number.isNaN(num) ? num : 0;
115
+ }
116
+ function formatDecimal(v, o) {
117
+ return new Intl.NumberFormat("en-US", {
118
+ useGrouping: false,
119
+ style: "decimal",
120
+ minimumFractionDigits: o.minFractionDigits,
121
+ maximumFractionDigits: o.maxFractionDigits
122
+ }).format(valueOf(v));
123
+ }
124
+ function isAtMax(v, o) {
125
+ const val = valueOf(v);
126
+ return val >= o.max;
127
+ }
128
+ function isAtMin(v, o) {
129
+ const val = valueOf(v);
130
+ return val <= o.min;
131
+ }
132
+ function isWithinRange(v, o) {
133
+ const val = valueOf(v);
134
+ return val >= o.min && val <= o.max;
135
+ }
136
+ function decimalOperation(a, op, b) {
137
+ let result = op === "+" ? a + b : a - b;
138
+ if (a % 1 !== 0 || b % 1 !== 0) {
139
+ const multiplier = 10 ** Math.max(countDecimals(a), countDecimals(b));
140
+ a = Math.round(a * multiplier);
141
+ b = Math.round(b * multiplier);
142
+ result = op === "+" ? a + b : a - b;
143
+ result /= multiplier;
144
+ }
145
+ return result;
146
+ }
147
+
148
+ export {
149
+ isObject,
150
+ hasProp,
151
+ isSafari,
152
+ getWindow,
153
+ defineDomHelpers,
154
+ getNativeEvent,
155
+ supportsPointerEvent,
156
+ isTouchEvent,
157
+ isLeftClick,
158
+ isModifiedEvent,
159
+ wrap,
160
+ roundToDevicePixel,
161
+ clamp,
162
+ increment,
163
+ decrement,
164
+ valueOf,
165
+ formatDecimal,
166
+ isAtMax,
167
+ isAtMin,
168
+ isWithinRange
169
+ };
@@ -0,0 +1,53 @@
1
+ import {
2
+ clamp,
3
+ decrement,
4
+ formatDecimal,
5
+ increment,
6
+ isModifiedEvent
7
+ } from "./chunk-DDNK5RLW.mjs";
8
+
9
+ // src/number-input.utils.ts
10
+ var utils = {
11
+ isValidNumericEvent: (ctx, event) => {
12
+ var _a, _b;
13
+ if (event.key == null)
14
+ return true;
15
+ const isModifier = isModifiedEvent(event);
16
+ const isSingleKey = event.key.length === 1;
17
+ if (isModifier || !isSingleKey)
18
+ return true;
19
+ return (_b = (_a = ctx.validateCharacter) == null ? void 0 : _a.call(ctx, event.key)) != null ? _b : utils.isFloatingPoint(event.key);
20
+ },
21
+ isFloatingPoint: (v) => /^[0-9+\-.]$/.test(v),
22
+ sanitize: (ctx, value) => {
23
+ var _a;
24
+ return value.split("").filter((_a = ctx.validateCharacter) != null ? _a : utils.isFloatingPoint).join("");
25
+ },
26
+ increment: (ctx, step) => {
27
+ const value = increment(ctx.value, step != null ? step : ctx.step);
28
+ return formatDecimal(clamp(value, ctx), ctx);
29
+ },
30
+ decrement: (ctx, step) => {
31
+ const value = decrement(ctx.value, step != null ? step : ctx.step);
32
+ return formatDecimal(clamp(value, ctx), ctx);
33
+ },
34
+ clamp: (ctx) => {
35
+ return formatDecimal(clamp(ctx.value, ctx), ctx);
36
+ },
37
+ parse: (ctx, value) => {
38
+ var _a, _b;
39
+ return (_b = (_a = ctx.parse) == null ? void 0 : _a.call(ctx, value)) != null ? _b : value;
40
+ },
41
+ format: (ctx, value) => {
42
+ var _a, _b;
43
+ const _val = value.toString();
44
+ return (_b = (_a = ctx.format) == null ? void 0 : _a.call(ctx, _val)) != null ? _b : _val;
45
+ },
46
+ round: (ctx) => {
47
+ return formatDecimal(ctx.value, ctx);
48
+ }
49
+ };
50
+
51
+ export {
52
+ utils
53
+ };
@@ -0,0 +1,246 @@
1
+ import {
2
+ parts
3
+ } from "./chunk-XHRILSH3.mjs";
4
+ import {
5
+ dom
6
+ } from "./chunk-2SIS62IB.mjs";
7
+ import {
8
+ utils
9
+ } from "./chunk-GWZPUXGU.mjs";
10
+ import {
11
+ getNativeEvent,
12
+ isLeftClick,
13
+ isTouchEvent,
14
+ roundToDevicePixel
15
+ } from "./chunk-DDNK5RLW.mjs";
16
+
17
+ // ../../utilities/dom/src/attrs.ts
18
+ var dataAttr = (guard) => {
19
+ return guard ? "" : void 0;
20
+ };
21
+ var ariaAttr = (guard) => {
22
+ return guard ? "true" : void 0;
23
+ };
24
+
25
+ // ../../utilities/dom/src/get-event-point.ts
26
+ var fallback = {
27
+ pageX: 0,
28
+ pageY: 0,
29
+ clientX: 0,
30
+ clientY: 0
31
+ };
32
+ function getEventPoint(event, type = "page") {
33
+ var _a, _b;
34
+ const point = isTouchEvent(event) ? (_b = (_a = event.touches[0]) != null ? _a : event.changedTouches[0]) != null ? _b : fallback : event;
35
+ return { x: point[`${type}X`], y: point[`${type}Y`] };
36
+ }
37
+
38
+ // ../../utilities/dom/src/keyboard-event.ts
39
+ var PAGE_KEYS = /* @__PURE__ */ new Set(["PageUp", "PageDown"]);
40
+ var ARROW_KEYS = /* @__PURE__ */ new Set(["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
41
+ function getEventStep(event) {
42
+ if (event.ctrlKey || event.metaKey) {
43
+ return 0.1;
44
+ } else {
45
+ const isPageKey = PAGE_KEYS.has(event.key);
46
+ const isSkipKey = isPageKey || event.shiftKey && ARROW_KEYS.has(event.key);
47
+ return isSkipKey ? 10 : 1;
48
+ }
49
+ }
50
+
51
+ // src/number-input.connect.ts
52
+ function connect(state, send, normalize) {
53
+ const isFocused = state.hasTag("focus");
54
+ const isInvalid = state.context.isOutOfRange || !!state.context.invalid;
55
+ const isDisabled = !!state.context.disabled;
56
+ const isValueEmpty = state.context.isValueEmpty;
57
+ const isIncrementDisabled = isDisabled || !state.context.canIncrement;
58
+ const isDecrementDisabled = isDisabled || !state.context.canDecrement;
59
+ const translations = state.context.translations;
60
+ return {
61
+ isFocused,
62
+ isInvalid,
63
+ isValueEmpty,
64
+ value: state.context.formattedValue,
65
+ valueAsNumber: state.context.valueAsNumber,
66
+ setValue(value) {
67
+ send({ type: "SET_VALUE", value: value.toString() });
68
+ },
69
+ clearValue() {
70
+ send("CLEAR_VALUE");
71
+ },
72
+ increment() {
73
+ send("INCREMENT");
74
+ },
75
+ decrement() {
76
+ send("DECREMENT");
77
+ },
78
+ setToMax() {
79
+ send({ type: "SET_VALUE", value: state.context.max });
80
+ },
81
+ setToMin() {
82
+ send({ type: "SET_VALUE", value: state.context.min });
83
+ },
84
+ focus() {
85
+ var _a;
86
+ (_a = dom.getInputEl(state.context)) == null ? void 0 : _a.focus();
87
+ },
88
+ blur() {
89
+ var _a;
90
+ (_a = dom.getInputEl(state.context)) == null ? void 0 : _a.blur();
91
+ },
92
+ rootProps: normalize.element({
93
+ id: dom.getRootId(state.context),
94
+ ...parts.root.attrs,
95
+ "data-disabled": dataAttr(isDisabled)
96
+ }),
97
+ labelProps: normalize.label({
98
+ ...parts.label.attrs,
99
+ "data-disabled": dataAttr(isDisabled),
100
+ "data-invalid": dataAttr(isInvalid),
101
+ id: dom.getLabelId(state.context),
102
+ htmlFor: dom.getInputId(state.context)
103
+ }),
104
+ controlProps: normalize.element({
105
+ ...parts.control.attrs,
106
+ role: "group",
107
+ "aria-disabled": isDisabled,
108
+ "data-disabled": dataAttr(isDisabled),
109
+ "data-invalid": dataAttr(isInvalid),
110
+ "aria-invalid": ariaAttr(state.context.invalid)
111
+ }),
112
+ inputProps: normalize.input({
113
+ ...parts.input.attrs,
114
+ name: state.context.name,
115
+ form: state.context.form,
116
+ id: dom.getInputId(state.context),
117
+ role: "spinbutton",
118
+ defaultValue: state.context.formattedValue,
119
+ pattern: state.context.pattern,
120
+ inputMode: state.context.inputMode,
121
+ "aria-invalid": isInvalid || void 0,
122
+ "data-invalid": dataAttr(isInvalid),
123
+ disabled: isDisabled,
124
+ "data-disabled": dataAttr(isDisabled),
125
+ readOnly: !!state.context.readOnly,
126
+ autoComplete: "off",
127
+ autoCorrect: "off",
128
+ spellCheck: "false",
129
+ type: "text",
130
+ "aria-roledescription": "numberfield",
131
+ "aria-valuemin": state.context.min,
132
+ "aria-valuemax": state.context.max,
133
+ "aria-valuenow": isNaN(state.context.valueAsNumber) ? void 0 : state.context.valueAsNumber,
134
+ "aria-valuetext": state.context.valueText,
135
+ onFocus() {
136
+ send("FOCUS");
137
+ },
138
+ onBlur() {
139
+ send("BLUR");
140
+ },
141
+ onChange(event) {
142
+ const evt = getNativeEvent(event);
143
+ if (evt.isComposing)
144
+ return;
145
+ send({ type: "CHANGE", target: event.currentTarget, hint: "set" });
146
+ },
147
+ onKeyDown(event) {
148
+ const evt = getNativeEvent(event);
149
+ if (evt.isComposing)
150
+ return;
151
+ if (!utils.isValidNumericEvent(state.context, event)) {
152
+ event.preventDefault();
153
+ }
154
+ const step = getEventStep(event) * state.context.step;
155
+ const keyMap = {
156
+ ArrowUp() {
157
+ send({ type: "ARROW_UP", step });
158
+ },
159
+ ArrowDown() {
160
+ send({ type: "ARROW_DOWN", step });
161
+ },
162
+ Home() {
163
+ send("HOME");
164
+ },
165
+ End() {
166
+ send("END");
167
+ }
168
+ };
169
+ const exec = keyMap[event.key];
170
+ if (exec) {
171
+ exec(event);
172
+ event.preventDefault();
173
+ }
174
+ }
175
+ }),
176
+ decrementTriggerProps: normalize.button({
177
+ ...parts.decrementTrigger.attrs,
178
+ id: dom.getDecrementTriggerId(state.context),
179
+ disabled: isDecrementDisabled,
180
+ "data-disabled": dataAttr(isDecrementDisabled),
181
+ "aria-label": translations.decrementLabel,
182
+ type: "button",
183
+ tabIndex: -1,
184
+ "aria-controls": dom.getInputId(state.context),
185
+ onPointerDown(event) {
186
+ if (isDecrementDisabled)
187
+ return;
188
+ send(isLeftClick(event) ? { type: "PRESS_DOWN", hint: "decrement" } : { type: "FOCUS" });
189
+ event.preventDefault();
190
+ },
191
+ onPointerUp() {
192
+ send({ type: "PRESS_UP", hint: "decrement" });
193
+ },
194
+ onPointerLeave() {
195
+ if (isDecrementDisabled)
196
+ return;
197
+ send({ type: "PRESS_UP", hint: "decrement" });
198
+ }
199
+ }),
200
+ incrementTriggerProps: normalize.button({
201
+ ...parts.incrementTrigger.attrs,
202
+ id: dom.getIncrementTriggerId(state.context),
203
+ disabled: isIncrementDisabled,
204
+ "data-disabled": dataAttr(isIncrementDisabled),
205
+ "aria-label": translations.incrementLabel,
206
+ type: "button",
207
+ tabIndex: -1,
208
+ "aria-controls": dom.getInputId(state.context),
209
+ onPointerDown(event) {
210
+ if (isIncrementDisabled)
211
+ return;
212
+ send(isLeftClick(event) ? { type: "PRESS_DOWN", hint: "increment" } : { type: "FOCUS" });
213
+ event.preventDefault();
214
+ },
215
+ onPointerUp() {
216
+ send({ type: "PRESS_UP", hint: "increment" });
217
+ },
218
+ onPointerLeave() {
219
+ send({ type: "PRESS_UP", hint: "increment" });
220
+ }
221
+ }),
222
+ scrubberProps: normalize.element({
223
+ ...parts.scrubber.attrs,
224
+ "data-disabled": dataAttr(isDisabled),
225
+ id: dom.getScrubberId(state.context),
226
+ role: "presentation",
227
+ onMouseDown(event) {
228
+ if (isDisabled)
229
+ return;
230
+ const evt = getNativeEvent(event);
231
+ event.preventDefault();
232
+ const point = getEventPoint(evt);
233
+ point.x = point.x - roundToDevicePixel(7.5);
234
+ point.y = point.y - roundToDevicePixel(7.5);
235
+ send({ type: "PRESS_DOWN_SCRUBBER", point });
236
+ },
237
+ style: {
238
+ cursor: isDisabled ? void 0 : "ew-resize"
239
+ }
240
+ })
241
+ };
242
+ }
243
+
244
+ export {
245
+ connect
246
+ };
@@ -0,0 +1,17 @@
1
+ // src/number-input.anatomy.ts
2
+ import { createAnatomy } from "@zag-js/anatomy";
3
+ var anatomy = createAnatomy("numberInput").parts(
4
+ "root",
5
+ "label",
6
+ "input",
7
+ "control",
8
+ "incrementTrigger",
9
+ "decrementTrigger",
10
+ "scrubber"
11
+ );
12
+ var parts = anatomy.build();
13
+
14
+ export {
15
+ anatomy,
16
+ parts
17
+ };