@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,9 +1,234 @@
1
- import {
2
- connect
3
- } from "./chunk-5ENKYWE3.mjs";
4
- import "./chunk-XHRILSH3.mjs";
5
- import "./chunk-QYY4CWRS.mjs";
6
- import "./chunk-AXXDGYUW.mjs";
7
- export {
8
- connect
9
- };
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,8 +1,5 @@
1
- import { MachineContext } from './number-input.types.js';
2
- import '@zag-js/core';
3
- import '@zag-js/types';
4
-
5
- declare const dom: {
1
+ import type { MachineContext as Ctx } from "./number-input.types";
2
+ export declare const dom: {
6
3
  getRootNode: (ctx: {
7
4
  getRootNode?: (() => Document | Node | ShadowRoot) | undefined;
8
5
  }) => Document | ShadowRoot;
@@ -22,29 +19,27 @@ declare const dom: {
22
19
  getRootNode?: (() => Document | Node | ShadowRoot) | undefined;
23
20
  }, id: string) => T_1;
24
21
  } & {
25
- getRootId: (ctx: MachineContext) => string;
26
- getInputId: (ctx: MachineContext) => string;
27
- getIncrementTriggerId: (ctx: MachineContext) => string;
28
- getDecrementTriggerId: (ctx: MachineContext) => string;
29
- getScrubberId: (ctx: MachineContext) => string;
30
- getCursorId: (ctx: MachineContext) => string;
31
- getLabelId: (ctx: MachineContext) => string;
32
- getInputEl: (ctx: MachineContext) => HTMLInputElement | null;
33
- getIncrementTriggerEl: (ctx: MachineContext) => HTMLButtonElement | null;
34
- getDecrementTriggerEl: (ctx: MachineContext) => HTMLButtonElement | null;
35
- getScrubberEl: (ctx: MachineContext) => HTMLElement | null;
36
- getCursorEl: (ctx: MachineContext) => HTMLElement | null;
37
- getPressedTriggerEl: (ctx: MachineContext, hint?: "set" | "increment" | "decrement" | null) => HTMLButtonElement | null;
38
- setupVirtualCursor(ctx: MachineContext): (() => void) | undefined;
39
- preventTextSelection(ctx: MachineContext): () => void;
40
- getMousementValue(ctx: MachineContext, event: MouseEvent): {
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): {
41
38
  hint: string | null;
42
39
  point: {
43
40
  x: number;
44
41
  y: number;
45
42
  };
46
43
  };
47
- createVirtualCursor(ctx: MachineContext): void;
44
+ createVirtualCursor(ctx: Ctx): void;
48
45
  };
49
-
50
- export { dom };
@@ -1,31 +1,11 @@
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.dom.ts
21
- var number_input_dom_exports = {};
22
- __export(number_input_dom_exports, {
23
- dom: () => dom
24
- });
25
- module.exports = __toCommonJS(number_input_dom_exports);
26
- var import_dom_query = require("@zag-js/dom-query");
27
- var import_number_utils = require("@zag-js/number-utils");
28
- var dom = (0, import_dom_query.createScope)({
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({
29
9
  getRootId: (ctx) => ctx.ids?.root ?? `number-input:${ctx.id}`,
30
10
  getInputId: (ctx) => ctx.ids?.input ?? `number-input:${ctx.id}:input`,
31
11
  getIncrementTriggerId: (ctx) => ctx.ids?.incrementTrigger ?? `number-input:${ctx.id}:inc`,
@@ -49,7 +29,7 @@ var dom = (0, import_dom_query.createScope)({
49
29
  return btnEl;
50
30
  },
51
31
  setupVirtualCursor(ctx) {
52
- if ((0, import_dom_query.isSafari)())
32
+ if (domQuery.isSafari())
53
33
  return;
54
34
  dom.createVirtualCursor(ctx);
55
35
  return () => {
@@ -76,8 +56,8 @@ var dom = (0, import_dom_query.createScope)({
76
56
  };
77
57
  },
78
58
  getMousementValue(ctx, event) {
79
- const x = (0, import_number_utils.roundToDevicePixel)(event.movementX);
80
- const y = (0, import_number_utils.roundToDevicePixel)(event.movementY);
59
+ const x = numberUtils.roundToDevicePixel(event.movementX);
60
+ const y = numberUtils.roundToDevicePixel(event.movementY);
81
61
  let hint = x > 0 ? "increment" : x < 0 ? "decrement" : null;
82
62
  if (ctx.isRtl && hint === "increment")
83
63
  hint = "decrement";
@@ -89,8 +69,8 @@ var dom = (0, import_dom_query.createScope)({
89
69
  };
90
70
  const win = dom.getWin(ctx);
91
71
  const width = win.innerWidth;
92
- const half = (0, import_number_utils.roundToDevicePixel)(7.5);
93
- point.x = (0, import_number_utils.wrap)(point.x + half, width) - half;
72
+ const half = numberUtils.roundToDevicePixel(7.5);
73
+ point.x = numberUtils.wrap(point.x + half, width) - half;
94
74
  return { hint, point };
95
75
  },
96
76
  createVirtualCursor(ctx) {
@@ -105,7 +85,7 @@ var dom = (0, import_dom_query.createScope)({
105
85
  pointerEvents: "none",
106
86
  left: "0px",
107
87
  top: "0px",
108
- zIndex: import_dom_query.MAX_Z_INDEX,
88
+ zIndex: domQuery.MAX_Z_INDEX,
109
89
  transform: ctx.scrubberCursorPoint ? `translate3d(${ctx.scrubberCursorPoint.x}px, ${ctx.scrubberCursorPoint.y}px, 0px)` : void 0,
110
90
  willChange: "transform"
111
91
  });
@@ -119,7 +99,5 @@ var dom = (0, import_dom_query.createScope)({
119
99
  doc.body.appendChild(el);
120
100
  }
121
101
  });
122
- // Annotate the CommonJS export names for ESM import in node:
123
- 0 && (module.exports = {
124
- dom
125
- });
102
+
103
+ exports.dom = dom;
@@ -1,6 +1,99 @@
1
- import {
2
- dom
3
- } from "./chunk-QYY4CWRS.mjs";
4
- export {
5
- dom
6
- };
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,7 +1,3 @@
1
- import * as _zag_js_core from '@zag-js/core';
2
- import { UserDefinedContext, MachineContext, MachineState } from './number-input.types.js';
3
- import '@zag-js/types';
4
-
5
- declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
6
-
7
- export { machine };
1
+ import { 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>;