@zag-js/pin-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,22 @@
1
+ import { PropTypes, NormalizeProps } from '@zag-js/types';
2
+ import { State, Send } from './pin-input.types.js';
3
+ import '@zag-js/core';
4
+
5
+ declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): {
6
+ value: string[];
7
+ valueAsString: string;
8
+ isValueComplete: boolean;
9
+ setValue(value: string[]): void;
10
+ clearValue(): void;
11
+ setValueAtIndex(index: number, value: string): void;
12
+ focus: () => void;
13
+ rootProps: T["element"];
14
+ labelProps: T["label"];
15
+ hiddenInputProps: T["input"];
16
+ controlProps: T["element"];
17
+ getInputProps({ index }: {
18
+ index: number;
19
+ }): T["input"];
20
+ };
21
+
22
+ export { connect };
@@ -0,0 +1,328 @@
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);
19
+
20
+ // src/pin-input.connect.ts
21
+ var pin_input_connect_exports = {};
22
+ __export(pin_input_connect_exports, {
23
+ connect: () => connect
24
+ });
25
+ module.exports = __toCommonJS(pin_input_connect_exports);
26
+
27
+ // ../../utilities/dom/src/attrs.ts
28
+ var dataAttr = (guard) => {
29
+ return guard ? "" : void 0;
30
+ };
31
+ var ariaAttr = (guard) => {
32
+ return guard ? "true" : void 0;
33
+ };
34
+
35
+ // ../../utilities/core/src/warning.ts
36
+ function invariant(...a) {
37
+ const m = a.length === 1 ? a[0] : a[1];
38
+ const c = a.length === 2 ? a[0] : true;
39
+ if (c && process.env.NODE_ENV !== "production") {
40
+ throw new Error(m);
41
+ }
42
+ }
43
+
44
+ // ../../utilities/dom/src/query.ts
45
+ function isDocument(el) {
46
+ return el.nodeType === Node.DOCUMENT_NODE;
47
+ }
48
+ function isWindow(value) {
49
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
50
+ }
51
+ function getDocument(el) {
52
+ var _a;
53
+ if (isWindow(el))
54
+ return el.document;
55
+ if (isDocument(el))
56
+ return el;
57
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
58
+ }
59
+ function defineDomHelpers(helpers) {
60
+ const dom2 = {
61
+ getRootNode: (ctx) => {
62
+ var _a, _b;
63
+ return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
64
+ },
65
+ getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
66
+ getWin: (ctx) => {
67
+ var _a;
68
+ return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
69
+ },
70
+ getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
71
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id),
72
+ createEmitter: (ctx, target) => {
73
+ const win = dom2.getWin(ctx);
74
+ return function emit(evt, detail, options) {
75
+ const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
76
+ const eventName = `zag:${evt}`;
77
+ const init = { bubbles, cancelable, composed, detail };
78
+ const event = new win.CustomEvent(eventName, init);
79
+ target.dispatchEvent(event);
80
+ };
81
+ },
82
+ createListener: (target) => {
83
+ return function listen(evt, handler) {
84
+ const eventName = `zag:${evt}`;
85
+ const listener = (e) => handler(e);
86
+ target.addEventListener(eventName, listener);
87
+ return () => target.removeEventListener(eventName, listener);
88
+ };
89
+ }
90
+ };
91
+ return {
92
+ ...dom2,
93
+ ...helpers
94
+ };
95
+ }
96
+
97
+ // ../../utilities/dom/src/event.ts
98
+ function getNativeEvent(e) {
99
+ var _a;
100
+ return (_a = e.nativeEvent) != null ? _a : e;
101
+ }
102
+ var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
103
+
104
+ // ../../utilities/dom/src/keyboard-event.ts
105
+ var rtlKeyMap = {
106
+ ArrowLeft: "ArrowRight",
107
+ ArrowRight: "ArrowLeft"
108
+ };
109
+ var sameKeyMap = {
110
+ Up: "ArrowUp",
111
+ Down: "ArrowDown",
112
+ Esc: "Escape",
113
+ " ": "Space",
114
+ ",": "Comma",
115
+ Left: "ArrowLeft",
116
+ Right: "ArrowRight"
117
+ };
118
+ function getEventKey(event, options = {}) {
119
+ var _a;
120
+ const { dir = "ltr", orientation = "horizontal" } = options;
121
+ let { key } = event;
122
+ key = (_a = sameKeyMap[key]) != null ? _a : key;
123
+ const isRtl = dir === "rtl" && orientation === "horizontal";
124
+ if (isRtl && key in rtlKeyMap) {
125
+ key = rtlKeyMap[key];
126
+ }
127
+ return key;
128
+ }
129
+
130
+ // ../../utilities/dom/src/nodelist.ts
131
+ function queryAll(root, selector) {
132
+ var _a;
133
+ return Array.from((_a = root == null ? void 0 : root.querySelectorAll(selector)) != null ? _a : []);
134
+ }
135
+
136
+ // ../../utilities/dom/src/visually-hidden.ts
137
+ var visuallyHiddenStyle = {
138
+ border: "0",
139
+ clip: "rect(0 0 0 0)",
140
+ height: "1px",
141
+ margin: "-1px",
142
+ overflow: "hidden",
143
+ padding: "0",
144
+ position: "absolute",
145
+ width: "1px",
146
+ whiteSpace: "nowrap",
147
+ wordWrap: "normal"
148
+ };
149
+
150
+ // src/pin-input.anatomy.ts
151
+ var import_anatomy = require("@zag-js/anatomy");
152
+ var anatomy = (0, import_anatomy.createAnatomy)("pinInput").parts("root", "label", "hiddenInput", "input", "control");
153
+ var parts = anatomy.build();
154
+
155
+ // src/pin-input.dom.ts
156
+ var dom = defineDomHelpers({
157
+ getRootId: (ctx) => {
158
+ var _a, _b;
159
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `pin-input:${ctx.id}`;
160
+ },
161
+ getInputId: (ctx, id) => {
162
+ var _a, _b, _c;
163
+ return (_c = (_b = (_a = ctx.ids) == null ? void 0 : _a.input) == null ? void 0 : _b.call(_a, id)) != null ? _c : `pin-input:${ctx.id}:${id}`;
164
+ },
165
+ getHiddenInputId: (ctx) => {
166
+ var _a, _b;
167
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.hiddenInput) != null ? _b : `pin-input:${ctx.id}:hidden`;
168
+ },
169
+ getLabelId: (ctx) => {
170
+ var _a, _b;
171
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `pin-input:${ctx.id}:label`;
172
+ },
173
+ getControlId: (ctx) => {
174
+ var _a, _b;
175
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.control) != null ? _b : `pin-input:${ctx.id}:control`;
176
+ },
177
+ getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
178
+ getElements: (ctx) => {
179
+ const ownerId = CSS.escape(dom.getRootId(ctx));
180
+ const selector = `input[data-ownedby=${ownerId}]`;
181
+ return queryAll(dom.getRootEl(ctx), selector);
182
+ },
183
+ getFocusedEl: (ctx) => dom.getElements(ctx)[ctx.focusedIndex],
184
+ getFirstInputEl: (ctx) => dom.getElements(ctx)[0],
185
+ getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx))
186
+ });
187
+
188
+ // src/pin-input.connect.ts
189
+ function connect(state, send, normalize) {
190
+ const isValueComplete = state.context.isValueComplete;
191
+ const isInvalid = state.context.invalid;
192
+ const focusedIndex = state.context.focusedIndex;
193
+ const translations = state.context.translations;
194
+ function focus() {
195
+ var _a;
196
+ (_a = dom.getFirstInputEl(state.context)) == null ? void 0 : _a.focus();
197
+ }
198
+ return {
199
+ value: state.context.value,
200
+ valueAsString: state.context.valueAsString,
201
+ isValueComplete,
202
+ setValue(value) {
203
+ if (!Array.isArray(value)) {
204
+ invariant("[pin-input/setValue] value must be an array");
205
+ }
206
+ send({ type: "SET_VALUE", value });
207
+ },
208
+ clearValue() {
209
+ send({ type: "CLEAR_VALUE" });
210
+ },
211
+ setValueAtIndex(index, value) {
212
+ send({ type: "SET_VALUE", value, index });
213
+ },
214
+ focus,
215
+ rootProps: normalize.element({
216
+ dir: state.context.dir,
217
+ ...parts.root.attrs,
218
+ id: dom.getRootId(state.context),
219
+ "data-invalid": dataAttr(isInvalid),
220
+ "data-disabled": dataAttr(state.context.disabled),
221
+ "data-complete": dataAttr(isValueComplete)
222
+ }),
223
+ labelProps: normalize.label({
224
+ ...parts.label.attrs,
225
+ htmlFor: dom.getHiddenInputId(state.context),
226
+ id: dom.getLabelId(state.context),
227
+ "data-invalid": dataAttr(isInvalid),
228
+ "data-disabled": dataAttr(state.context.disabled),
229
+ "data-complete": dataAttr(isValueComplete),
230
+ onClick: (event) => {
231
+ event.preventDefault();
232
+ focus();
233
+ }
234
+ }),
235
+ hiddenInputProps: normalize.input({
236
+ ...parts.hiddenInput.attrs,
237
+ "aria-hidden": true,
238
+ type: "text",
239
+ tabIndex: -1,
240
+ id: dom.getHiddenInputId(state.context),
241
+ name: state.context.name,
242
+ form: state.context.form,
243
+ style: visuallyHiddenStyle,
244
+ maxLength: state.context.valueLength,
245
+ defaultValue: state.context.valueAsString
246
+ }),
247
+ controlProps: normalize.element({
248
+ ...parts.control.attrs,
249
+ id: dom.getControlId(state.context)
250
+ }),
251
+ getInputProps({ index }) {
252
+ const inputType = state.context.type === "numeric" ? "tel" : "text";
253
+ return normalize.input({
254
+ ...parts.input.attrs,
255
+ disabled: state.context.disabled,
256
+ "data-disabled": dataAttr(state.context.disabled),
257
+ "data-complete": dataAttr(isValueComplete),
258
+ id: dom.getInputId(state.context, index.toString()),
259
+ "data-ownedby": dom.getRootId(state.context),
260
+ "aria-label": translations.inputLabel(index, state.context.valueLength),
261
+ inputMode: state.context.otp || state.context.type === "numeric" ? "numeric" : "text",
262
+ "aria-invalid": ariaAttr(isInvalid),
263
+ "data-invalid": dataAttr(isInvalid),
264
+ type: state.context.mask ? "password" : inputType,
265
+ defaultValue: state.context.value[index] || "",
266
+ autoCapitalize: "none",
267
+ autoComplete: state.context.otp ? "one-time-code" : "off",
268
+ placeholder: focusedIndex === index ? "" : state.context.placeholder,
269
+ onChange(event) {
270
+ const evt = getNativeEvent(event);
271
+ const { value } = event.currentTarget;
272
+ if (evt.inputType === "insertFromPaste" || value.length > 2) {
273
+ send({ type: "PASTE", value });
274
+ event.preventDefault();
275
+ return;
276
+ }
277
+ if (evt.inputType === "deleteContentBackward") {
278
+ send("BACKSPACE");
279
+ return;
280
+ }
281
+ send({ type: "INPUT", value });
282
+ },
283
+ onKeyDown(event) {
284
+ const evt = getNativeEvent(event);
285
+ if (evt.isComposing || isModifiedEvent(evt))
286
+ return;
287
+ const keyMap = {
288
+ Backspace() {
289
+ send("BACKSPACE");
290
+ },
291
+ Delete() {
292
+ send("DELETE");
293
+ },
294
+ ArrowLeft() {
295
+ send("ARROW_LEFT");
296
+ },
297
+ ArrowRight() {
298
+ send("ARROW_RIGHT");
299
+ },
300
+ Enter() {
301
+ send("ENTER");
302
+ }
303
+ };
304
+ const key = getEventKey(event, { dir: state.context.dir });
305
+ const exec = keyMap[key];
306
+ if (exec) {
307
+ exec(event);
308
+ event.preventDefault();
309
+ } else {
310
+ if (key === "Tab")
311
+ return;
312
+ send({ type: "KEY_DOWN", value: key, preventDefault: () => event.preventDefault() });
313
+ }
314
+ },
315
+ onFocus() {
316
+ send({ type: "FOCUS", index });
317
+ },
318
+ onBlur() {
319
+ send({ type: "BLUR", index });
320
+ }
321
+ });
322
+ }
323
+ };
324
+ }
325
+ // Annotate the CommonJS export names for ESM import in node:
326
+ 0 && (module.exports = {
327
+ connect
328
+ });
@@ -0,0 +1,9 @@
1
+ import {
2
+ connect
3
+ } from "./chunk-QX3A66VS.mjs";
4
+ import "./chunk-BJXKBZJG.mjs";
5
+ import "./chunk-NT4W6JYX.mjs";
6
+ import "./chunk-OI3DK2PT.mjs";
7
+ export {
8
+ connect
9
+ };
@@ -0,0 +1,38 @@
1
+ import { MachineContext } from './pin-input.types.js';
2
+ import '@zag-js/core';
3
+ import '@zag-js/types';
4
+
5
+ declare const dom: {
6
+ getRootNode: (ctx: {
7
+ getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
8
+ }) => Document | ShadowRoot;
9
+ getDoc: (ctx: {
10
+ getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
11
+ }) => Document;
12
+ getWin: (ctx: {
13
+ getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
14
+ }) => Window & typeof globalThis;
15
+ getActiveElement: (ctx: {
16
+ getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
17
+ }) => HTMLElement | null;
18
+ getById: <T = HTMLElement>(ctx: {
19
+ getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
20
+ }, id: string) => T | null;
21
+ createEmitter: (ctx: {
22
+ getRootNode?: (() => Node | Document | ShadowRoot) | undefined;
23
+ }, target: HTMLElement) => (evt: string, detail: Record<string, any>, options?: EventInit | undefined) => void;
24
+ createListener: (target: HTMLElement) => <T_1 = any>(evt: string, handler: (e: CustomEvent<T_1>) => void) => () => void;
25
+ } & {
26
+ getRootId: (ctx: MachineContext) => string;
27
+ getInputId: (ctx: MachineContext, id: string) => string;
28
+ getHiddenInputId: (ctx: MachineContext) => string;
29
+ getLabelId: (ctx: MachineContext) => string;
30
+ getControlId: (ctx: MachineContext) => string;
31
+ getRootEl: (ctx: MachineContext) => HTMLElement | null;
32
+ getElements: (ctx: MachineContext) => HTMLInputElement[];
33
+ getFocusedEl: (ctx: MachineContext) => HTMLInputElement;
34
+ getFirstInputEl: (ctx: MachineContext) => HTMLInputElement;
35
+ getHiddenInputEl: (ctx: MachineContext) => HTMLInputElement | null;
36
+ };
37
+
38
+ export { dom };
@@ -0,0 +1,121 @@
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);
19
+
20
+ // src/pin-input.dom.ts
21
+ var pin_input_dom_exports = {};
22
+ __export(pin_input_dom_exports, {
23
+ dom: () => dom
24
+ });
25
+ module.exports = __toCommonJS(pin_input_dom_exports);
26
+
27
+ // ../../utilities/dom/src/query.ts
28
+ function isDocument(el) {
29
+ return el.nodeType === Node.DOCUMENT_NODE;
30
+ }
31
+ function isWindow(value) {
32
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
33
+ }
34
+ function getDocument(el) {
35
+ var _a;
36
+ if (isWindow(el))
37
+ return el.document;
38
+ if (isDocument(el))
39
+ return el;
40
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
41
+ }
42
+ function defineDomHelpers(helpers) {
43
+ const dom2 = {
44
+ getRootNode: (ctx) => {
45
+ var _a, _b;
46
+ return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
47
+ },
48
+ getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
49
+ getWin: (ctx) => {
50
+ var _a;
51
+ return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
52
+ },
53
+ getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
54
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id),
55
+ createEmitter: (ctx, target) => {
56
+ const win = dom2.getWin(ctx);
57
+ return function emit(evt, detail, options) {
58
+ const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
59
+ const eventName = `zag:${evt}`;
60
+ const init = { bubbles, cancelable, composed, detail };
61
+ const event = new win.CustomEvent(eventName, init);
62
+ target.dispatchEvent(event);
63
+ };
64
+ },
65
+ createListener: (target) => {
66
+ return function listen(evt, handler) {
67
+ const eventName = `zag:${evt}`;
68
+ const listener = (e) => handler(e);
69
+ target.addEventListener(eventName, listener);
70
+ return () => target.removeEventListener(eventName, listener);
71
+ };
72
+ }
73
+ };
74
+ return {
75
+ ...dom2,
76
+ ...helpers
77
+ };
78
+ }
79
+
80
+ // ../../utilities/dom/src/nodelist.ts
81
+ function queryAll(root, selector) {
82
+ var _a;
83
+ return Array.from((_a = root == null ? void 0 : root.querySelectorAll(selector)) != null ? _a : []);
84
+ }
85
+
86
+ // src/pin-input.dom.ts
87
+ var dom = defineDomHelpers({
88
+ getRootId: (ctx) => {
89
+ var _a, _b;
90
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `pin-input:${ctx.id}`;
91
+ },
92
+ getInputId: (ctx, id) => {
93
+ var _a, _b, _c;
94
+ return (_c = (_b = (_a = ctx.ids) == null ? void 0 : _a.input) == null ? void 0 : _b.call(_a, id)) != null ? _c : `pin-input:${ctx.id}:${id}`;
95
+ },
96
+ getHiddenInputId: (ctx) => {
97
+ var _a, _b;
98
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.hiddenInput) != null ? _b : `pin-input:${ctx.id}:hidden`;
99
+ },
100
+ getLabelId: (ctx) => {
101
+ var _a, _b;
102
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `pin-input:${ctx.id}:label`;
103
+ },
104
+ getControlId: (ctx) => {
105
+ var _a, _b;
106
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.control) != null ? _b : `pin-input:${ctx.id}:control`;
107
+ },
108
+ getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
109
+ getElements: (ctx) => {
110
+ const ownerId = CSS.escape(dom.getRootId(ctx));
111
+ const selector = `input[data-ownedby=${ownerId}]`;
112
+ return queryAll(dom.getRootEl(ctx), selector);
113
+ },
114
+ getFocusedEl: (ctx) => dom.getElements(ctx)[ctx.focusedIndex],
115
+ getFirstInputEl: (ctx) => dom.getElements(ctx)[0],
116
+ getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx))
117
+ });
118
+ // Annotate the CommonJS export names for ESM import in node:
119
+ 0 && (module.exports = {
120
+ dom
121
+ });
@@ -0,0 +1,6 @@
1
+ import {
2
+ dom
3
+ } from "./chunk-OI3DK2PT.mjs";
4
+ export {
5
+ dom
6
+ };
@@ -0,0 +1,7 @@
1
+ import * as _zag_js_core from '@zag-js/core';
2
+ import { UserDefinedContext, MachineContext, MachineState } from './pin-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 };