@zag-js/number-input 0.10.5 → 0.11.1

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,234 +0,0 @@
1
- import { getNativeEvent, getEventStep, isLeftClick, getEventPoint } from '@zag-js/dom-event';
2
- import { dataAttr, ariaAttr } from '@zag-js/dom-query';
3
- import { roundToDevicePixel } from '@zag-js/number-utils';
4
- import { parts } from './number-input.anatomy.mjs';
5
- import { dom } from './number-input.dom.mjs';
6
- import { utils } from './number-input.utils.mjs';
7
-
8
- function connect(state, send, normalize) {
9
- const isFocused = state.hasTag("focus");
10
- const isInvalid = state.context.isOutOfRange || !!state.context.invalid;
11
- const isDisabled = !!state.context.disabled;
12
- const isValueEmpty = state.context.isValueEmpty;
13
- const isIncrementDisabled = isDisabled || !state.context.canIncrement;
14
- const isDecrementDisabled = isDisabled || !state.context.canDecrement;
15
- const translations = state.context.translations;
16
- return {
17
- /**
18
- * Whether the input is focused.
19
- */
20
- isFocused,
21
- /**
22
- * Whether the input is invalid.
23
- */
24
- isInvalid,
25
- /**
26
- * Whether the input value is empty.
27
- */
28
- isValueEmpty,
29
- /**
30
- * The formatted value of the input.
31
- */
32
- value: state.context.formattedValue,
33
- /**
34
- * The value of the input as a number.
35
- */
36
- valueAsNumber: state.context.valueAsNumber,
37
- /**
38
- * Function to set the value of the input.
39
- */
40
- setValue(value) {
41
- send({ type: "SET_VALUE", value: value.toString() });
42
- },
43
- /**
44
- * Function to clear the value of the input.
45
- */
46
- clearValue() {
47
- send("CLEAR_VALUE");
48
- },
49
- /**
50
- * Function to increment the value of the input by the step.
51
- */
52
- increment() {
53
- send("INCREMENT");
54
- },
55
- /**
56
- * Function to decrement the value of the input by the step.
57
- */
58
- decrement() {
59
- send("DECREMENT");
60
- },
61
- /**
62
- * Function to set the value of the input to the max.
63
- */
64
- setToMax() {
65
- send({ type: "SET_VALUE", value: state.context.max });
66
- },
67
- /**
68
- * Function to set the value of the input to the min.
69
- */
70
- setToMin() {
71
- send({ type: "SET_VALUE", value: state.context.min });
72
- },
73
- /**
74
- * Function to focus the input.
75
- */
76
- focus() {
77
- dom.getInputEl(state.context)?.focus();
78
- },
79
- /**
80
- * Function to blur the input.
81
- */
82
- blur() {
83
- dom.getInputEl(state.context)?.blur();
84
- },
85
- rootProps: normalize.element({
86
- id: dom.getRootId(state.context),
87
- ...parts.root.attrs,
88
- "data-disabled": dataAttr(isDisabled)
89
- }),
90
- labelProps: normalize.label({
91
- ...parts.label.attrs,
92
- "data-disabled": dataAttr(isDisabled),
93
- "data-invalid": dataAttr(isInvalid),
94
- id: dom.getLabelId(state.context),
95
- htmlFor: dom.getInputId(state.context)
96
- }),
97
- controlProps: normalize.element({
98
- ...parts.control.attrs,
99
- role: "group",
100
- "aria-disabled": isDisabled,
101
- "data-disabled": dataAttr(isDisabled),
102
- "data-invalid": dataAttr(isInvalid),
103
- "aria-invalid": ariaAttr(state.context.invalid)
104
- }),
105
- inputProps: normalize.input({
106
- ...parts.input.attrs,
107
- name: state.context.name,
108
- form: state.context.form,
109
- id: dom.getInputId(state.context),
110
- role: "spinbutton",
111
- defaultValue: state.context.formattedValue,
112
- pattern: state.context.pattern,
113
- inputMode: state.context.inputMode,
114
- "aria-invalid": ariaAttr(isInvalid),
115
- "data-invalid": dataAttr(isInvalid),
116
- disabled: isDisabled,
117
- "data-disabled": dataAttr(isDisabled),
118
- readOnly: !!state.context.readOnly,
119
- autoComplete: "off",
120
- autoCorrect: "off",
121
- spellCheck: "false",
122
- type: "text",
123
- "aria-roledescription": "numberfield",
124
- "aria-valuemin": state.context.min,
125
- "aria-valuemax": state.context.max,
126
- "aria-valuenow": isNaN(state.context.valueAsNumber) ? void 0 : state.context.valueAsNumber,
127
- "aria-valuetext": state.context.valueText,
128
- onFocus() {
129
- send("FOCUS");
130
- },
131
- onBlur() {
132
- send("BLUR");
133
- },
134
- onChange(event) {
135
- send({ type: "CHANGE", target: event.currentTarget, hint: "set" });
136
- },
137
- onKeyDown(event) {
138
- const evt = getNativeEvent(event);
139
- if (evt.isComposing)
140
- return;
141
- if (!utils.isValidNumericEvent(state.context, event)) {
142
- event.preventDefault();
143
- }
144
- const step = getEventStep(event) * state.context.step;
145
- const keyMap = {
146
- ArrowUp() {
147
- send({ type: "ARROW_UP", step });
148
- },
149
- ArrowDown() {
150
- send({ type: "ARROW_DOWN", step });
151
- },
152
- Home() {
153
- send("HOME");
154
- },
155
- End() {
156
- send("END");
157
- }
158
- };
159
- const exec = keyMap[event.key];
160
- if (exec) {
161
- exec(event);
162
- event.preventDefault();
163
- }
164
- }
165
- }),
166
- decrementTriggerProps: normalize.button({
167
- ...parts.decrementTrigger.attrs,
168
- id: dom.getDecrementTriggerId(state.context),
169
- disabled: isDecrementDisabled,
170
- "data-disabled": dataAttr(isDecrementDisabled),
171
- "aria-label": translations.decrementLabel,
172
- type: "button",
173
- tabIndex: -1,
174
- "aria-controls": dom.getInputId(state.context),
175
- onPointerDown(event) {
176
- if (isDecrementDisabled)
177
- return;
178
- send(isLeftClick(event) ? { type: "PRESS_DOWN", hint: "decrement" } : { type: "FOCUS" });
179
- event.preventDefault();
180
- },
181
- onPointerUp() {
182
- send({ type: "PRESS_UP", hint: "decrement" });
183
- },
184
- onPointerLeave() {
185
- if (isDecrementDisabled)
186
- return;
187
- send({ type: "PRESS_UP", hint: "decrement" });
188
- }
189
- }),
190
- incrementTriggerProps: normalize.button({
191
- ...parts.incrementTrigger.attrs,
192
- id: dom.getIncrementTriggerId(state.context),
193
- disabled: isIncrementDisabled,
194
- "data-disabled": dataAttr(isIncrementDisabled),
195
- "aria-label": translations.incrementLabel,
196
- type: "button",
197
- tabIndex: -1,
198
- "aria-controls": dom.getInputId(state.context),
199
- onPointerDown(event) {
200
- if (isIncrementDisabled)
201
- return;
202
- send(isLeftClick(event) ? { type: "PRESS_DOWN", hint: "increment" } : { type: "FOCUS" });
203
- event.preventDefault();
204
- },
205
- onPointerUp() {
206
- send({ type: "PRESS_UP", hint: "increment" });
207
- },
208
- onPointerLeave() {
209
- send({ type: "PRESS_UP", hint: "increment" });
210
- }
211
- }),
212
- scrubberProps: normalize.element({
213
- ...parts.scrubber.attrs,
214
- "data-disabled": dataAttr(isDisabled),
215
- id: dom.getScrubberId(state.context),
216
- role: "presentation",
217
- onMouseDown(event) {
218
- if (isDisabled)
219
- return;
220
- const evt = getNativeEvent(event);
221
- const point = getEventPoint(evt);
222
- point.x = point.x - roundToDevicePixel(7.5);
223
- point.y = point.y - roundToDevicePixel(7.5);
224
- send({ type: "PRESS_DOWN_SCRUBBER", point });
225
- event.preventDefault();
226
- },
227
- style: {
228
- cursor: isDisabled ? void 0 : "ew-resize"
229
- }
230
- })
231
- };
232
- }
233
-
234
- export { connect };
@@ -1,45 +0,0 @@
1
- import type { MachineContext as Ctx } from "./number-input.types";
2
- export declare const dom: {
3
- getRootNode: (ctx: {
4
- getRootNode?: (() => Node | ShadowRoot | Document) | undefined;
5
- }) => ShadowRoot | Document;
6
- getDoc: (ctx: {
7
- getRootNode?: (() => Node | ShadowRoot | Document) | undefined;
8
- }) => Document;
9
- getWin: (ctx: {
10
- getRootNode?: (() => Node | ShadowRoot | Document) | undefined;
11
- }) => Window & typeof globalThis;
12
- getActiveElement: (ctx: {
13
- getRootNode?: (() => Node | ShadowRoot | Document) | undefined;
14
- }) => HTMLElement | null;
15
- getById: <T extends HTMLElement = HTMLElement>(ctx: {
16
- getRootNode?: (() => Node | ShadowRoot | Document) | undefined;
17
- }, id: string) => T | null;
18
- queryById: <T_1 extends HTMLElement = HTMLElement>(ctx: {
19
- getRootNode?: (() => Node | ShadowRoot | Document) | undefined;
20
- }, id: string) => T_1;
21
- } & {
22
- getRootId: (ctx: Ctx) => string;
23
- getInputId: (ctx: Ctx) => string;
24
- getIncrementTriggerId: (ctx: Ctx) => string;
25
- getDecrementTriggerId: (ctx: Ctx) => string;
26
- getScrubberId: (ctx: Ctx) => string;
27
- getCursorId: (ctx: Ctx) => string;
28
- getLabelId: (ctx: Ctx) => string;
29
- getInputEl: (ctx: Ctx) => HTMLInputElement | null;
30
- getIncrementTriggerEl: (ctx: Ctx) => HTMLButtonElement | null;
31
- getDecrementTriggerEl: (ctx: Ctx) => HTMLButtonElement | null;
32
- getScrubberEl: (ctx: Ctx) => HTMLElement | null;
33
- getCursorEl: (ctx: Ctx) => HTMLElement | null;
34
- getPressedTriggerEl: (ctx: Ctx, hint?: "set" | "increment" | "decrement" | null) => HTMLButtonElement | null;
35
- setupVirtualCursor(ctx: Ctx): (() => void) | undefined;
36
- preventTextSelection(ctx: Ctx): () => void;
37
- getMousementValue(ctx: Ctx, event: MouseEvent): {
38
- hint: string | null;
39
- point: {
40
- x: number;
41
- y: number;
42
- };
43
- };
44
- createVirtualCursor(ctx: Ctx): void;
45
- };
@@ -1,103 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const domQuery = require('@zag-js/dom-query');
6
- const numberUtils = require('@zag-js/number-utils');
7
-
8
- const dom = domQuery.createScope({
9
- getRootId: (ctx) => ctx.ids?.root ?? `number-input:${ctx.id}`,
10
- getInputId: (ctx) => ctx.ids?.input ?? `number-input:${ctx.id}:input`,
11
- getIncrementTriggerId: (ctx) => ctx.ids?.incrementTrigger ?? `number-input:${ctx.id}:inc`,
12
- getDecrementTriggerId: (ctx) => ctx.ids?.decrementTrigger ?? `number-input:${ctx.id}:dec`,
13
- getScrubberId: (ctx) => ctx.ids?.scrubber ?? `number-input:${ctx.id}:scrubber`,
14
- getCursorId: (ctx) => `number-input:${ctx.id}:cursor`,
15
- getLabelId: (ctx) => ctx.ids?.label ?? `number-input:${ctx.id}:label`,
16
- getInputEl: (ctx) => dom.getById(ctx, dom.getInputId(ctx)),
17
- getIncrementTriggerEl: (ctx) => dom.getById(ctx, dom.getIncrementTriggerId(ctx)),
18
- getDecrementTriggerEl: (ctx) => dom.getById(ctx, dom.getDecrementTriggerId(ctx)),
19
- getScrubberEl: (ctx) => dom.getById(ctx, dom.getScrubberId(ctx)),
20
- getCursorEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getCursorId(ctx)),
21
- getPressedTriggerEl: (ctx, hint = ctx.hint) => {
22
- let btnEl = null;
23
- if (hint === "increment") {
24
- btnEl = dom.getIncrementTriggerEl(ctx);
25
- }
26
- if (hint === "decrement") {
27
- btnEl = dom.getDecrementTriggerEl(ctx);
28
- }
29
- return btnEl;
30
- },
31
- setupVirtualCursor(ctx) {
32
- if (domQuery.isSafari())
33
- return;
34
- dom.createVirtualCursor(ctx);
35
- return () => {
36
- dom.getCursorEl(ctx)?.remove();
37
- };
38
- },
39
- preventTextSelection(ctx) {
40
- const doc = dom.getDoc(ctx);
41
- const html = doc.documentElement;
42
- const body = doc.body;
43
- body.style.pointerEvents = "none";
44
- html.style.userSelect = "none";
45
- html.style.cursor = "ew-resize";
46
- return () => {
47
- body.style.pointerEvents = "";
48
- html.style.userSelect = "";
49
- html.style.cursor = "";
50
- if (!html.style.length) {
51
- html.removeAttribute("style");
52
- }
53
- if (!body.style.length) {
54
- body.removeAttribute("style");
55
- }
56
- };
57
- },
58
- getMousementValue(ctx, event) {
59
- const x = numberUtils.roundToDevicePixel(event.movementX);
60
- const y = numberUtils.roundToDevicePixel(event.movementY);
61
- let hint = x > 0 ? "increment" : x < 0 ? "decrement" : null;
62
- if (ctx.isRtl && hint === "increment")
63
- hint = "decrement";
64
- if (ctx.isRtl && hint === "decrement")
65
- hint = "increment";
66
- const point = {
67
- x: ctx.scrubberCursorPoint.x + x,
68
- y: ctx.scrubberCursorPoint.y + y
69
- };
70
- const win = dom.getWin(ctx);
71
- const width = win.innerWidth;
72
- const half = numberUtils.roundToDevicePixel(7.5);
73
- point.x = numberUtils.wrap(point.x + half, width) - half;
74
- return { hint, point };
75
- },
76
- createVirtualCursor(ctx) {
77
- const doc = dom.getDoc(ctx);
78
- const el = doc.createElement("div");
79
- el.className = "scrubber--cursor";
80
- el.id = dom.getCursorId(ctx);
81
- Object.assign(el.style, {
82
- width: "15px",
83
- height: "15px",
84
- position: "fixed",
85
- pointerEvents: "none",
86
- left: "0px",
87
- top: "0px",
88
- zIndex: domQuery.MAX_Z_INDEX,
89
- transform: ctx.scrubberCursorPoint ? `translate3d(${ctx.scrubberCursorPoint.x}px, ${ctx.scrubberCursorPoint.y}px, 0px)` : void 0,
90
- willChange: "transform"
91
- });
92
- el.innerHTML = `
93
- <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);">
94
- <g transform="translate(2 3)">
95
- <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>
96
- <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>
97
- </g>
98
- </svg>`;
99
- doc.body.appendChild(el);
100
- }
101
- });
102
-
103
- exports.dom = dom;
@@ -1,99 +0,0 @@
1
- import { createScope, isSafari, MAX_Z_INDEX } from '@zag-js/dom-query';
2
- import { roundToDevicePixel, wrap } from '@zag-js/number-utils';
3
-
4
- const dom = createScope({
5
- getRootId: (ctx) => ctx.ids?.root ?? `number-input:${ctx.id}`,
6
- getInputId: (ctx) => ctx.ids?.input ?? `number-input:${ctx.id}:input`,
7
- getIncrementTriggerId: (ctx) => ctx.ids?.incrementTrigger ?? `number-input:${ctx.id}:inc`,
8
- getDecrementTriggerId: (ctx) => ctx.ids?.decrementTrigger ?? `number-input:${ctx.id}:dec`,
9
- getScrubberId: (ctx) => ctx.ids?.scrubber ?? `number-input:${ctx.id}:scrubber`,
10
- getCursorId: (ctx) => `number-input:${ctx.id}:cursor`,
11
- getLabelId: (ctx) => ctx.ids?.label ?? `number-input:${ctx.id}:label`,
12
- getInputEl: (ctx) => dom.getById(ctx, dom.getInputId(ctx)),
13
- getIncrementTriggerEl: (ctx) => dom.getById(ctx, dom.getIncrementTriggerId(ctx)),
14
- getDecrementTriggerEl: (ctx) => dom.getById(ctx, dom.getDecrementTriggerId(ctx)),
15
- getScrubberEl: (ctx) => dom.getById(ctx, dom.getScrubberId(ctx)),
16
- getCursorEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getCursorId(ctx)),
17
- getPressedTriggerEl: (ctx, hint = ctx.hint) => {
18
- let btnEl = null;
19
- if (hint === "increment") {
20
- btnEl = dom.getIncrementTriggerEl(ctx);
21
- }
22
- if (hint === "decrement") {
23
- btnEl = dom.getDecrementTriggerEl(ctx);
24
- }
25
- return btnEl;
26
- },
27
- setupVirtualCursor(ctx) {
28
- if (isSafari())
29
- return;
30
- dom.createVirtualCursor(ctx);
31
- return () => {
32
- dom.getCursorEl(ctx)?.remove();
33
- };
34
- },
35
- preventTextSelection(ctx) {
36
- const doc = dom.getDoc(ctx);
37
- const html = doc.documentElement;
38
- const body = doc.body;
39
- body.style.pointerEvents = "none";
40
- html.style.userSelect = "none";
41
- html.style.cursor = "ew-resize";
42
- return () => {
43
- body.style.pointerEvents = "";
44
- html.style.userSelect = "";
45
- html.style.cursor = "";
46
- if (!html.style.length) {
47
- html.removeAttribute("style");
48
- }
49
- if (!body.style.length) {
50
- body.removeAttribute("style");
51
- }
52
- };
53
- },
54
- getMousementValue(ctx, event) {
55
- const x = roundToDevicePixel(event.movementX);
56
- const y = roundToDevicePixel(event.movementY);
57
- let hint = x > 0 ? "increment" : x < 0 ? "decrement" : null;
58
- if (ctx.isRtl && hint === "increment")
59
- hint = "decrement";
60
- if (ctx.isRtl && hint === "decrement")
61
- hint = "increment";
62
- const point = {
63
- x: ctx.scrubberCursorPoint.x + x,
64
- y: ctx.scrubberCursorPoint.y + y
65
- };
66
- const win = dom.getWin(ctx);
67
- const width = win.innerWidth;
68
- const half = roundToDevicePixel(7.5);
69
- point.x = wrap(point.x + half, width) - half;
70
- return { hint, point };
71
- },
72
- createVirtualCursor(ctx) {
73
- const doc = dom.getDoc(ctx);
74
- const el = doc.createElement("div");
75
- el.className = "scrubber--cursor";
76
- el.id = dom.getCursorId(ctx);
77
- Object.assign(el.style, {
78
- width: "15px",
79
- height: "15px",
80
- position: "fixed",
81
- pointerEvents: "none",
82
- left: "0px",
83
- top: "0px",
84
- zIndex: MAX_Z_INDEX,
85
- transform: ctx.scrubberCursorPoint ? `translate3d(${ctx.scrubberCursorPoint.x}px, ${ctx.scrubberCursorPoint.y}px, 0px)` : void 0,
86
- willChange: "transform"
87
- });
88
- el.innerHTML = `
89
- <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);">
90
- <g transform="translate(2 3)">
91
- <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>
92
- <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>
93
- </g>
94
- </svg>`;
95
- doc.body.appendChild(el);
96
- }
97
- });
98
-
99
- export { dom };
@@ -1,3 +0,0 @@
1
- import type { Machine, StateMachine } from '@zag-js/core';
2
- import type { MachineContext, MachineState, UserDefinedContext } from "./number-input.types";
3
- export declare function machine(userContext: UserDefinedContext): Machine<MachineContext, MachineState, StateMachine.AnyEventObject>;