@zag-js/pin-input 0.10.5 → 0.11.0

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,169 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const domEvent = require('@zag-js/dom-event');
6
- const domQuery = require('@zag-js/dom-query');
7
- const utils = require('@zag-js/utils');
8
- const visuallyHidden = require('@zag-js/visually-hidden');
9
- const pinInput_anatomy = require('./pin-input.anatomy.js');
10
- const pinInput_dom = require('./pin-input.dom.js');
11
-
12
- function connect(state, send, normalize) {
13
- const isValueComplete = state.context.isValueComplete;
14
- const isInvalid = state.context.invalid;
15
- const focusedIndex = state.context.focusedIndex;
16
- const translations = state.context.translations;
17
- function focus() {
18
- pinInput_dom.dom.getFirstInputEl(state.context)?.focus();
19
- }
20
- return {
21
- /**
22
- * The value of the input as an array of strings.
23
- */
24
- value: state.context.value,
25
- /**
26
- * The value of the input as a string.
27
- */
28
- valueAsString: state.context.valueAsString,
29
- /**
30
- * Whether all inputs are filled.
31
- */
32
- isValueComplete,
33
- /**
34
- * Function to set the value of the inputs.
35
- */
36
- setValue(value) {
37
- if (!Array.isArray(value)) {
38
- utils.invariant("[pin-input/setValue] value must be an array");
39
- }
40
- send({ type: "SET_VALUE", value });
41
- },
42
- /**
43
- * Function to clear the value of the inputs.
44
- */
45
- clearValue() {
46
- send({ type: "CLEAR_VALUE" });
47
- },
48
- /**
49
- * Function to set the value of the input at a specific index.
50
- */
51
- setValueAtIndex(index, value) {
52
- send({ type: "SET_VALUE", value, index });
53
- },
54
- /**
55
- * Function to focus the pin-input. This will focus the first input.
56
- */
57
- focus,
58
- rootProps: normalize.element({
59
- dir: state.context.dir,
60
- ...pinInput_anatomy.parts.root.attrs,
61
- id: pinInput_dom.dom.getRootId(state.context),
62
- "data-invalid": domQuery.dataAttr(isInvalid),
63
- "data-disabled": domQuery.dataAttr(state.context.disabled),
64
- "data-complete": domQuery.dataAttr(isValueComplete)
65
- }),
66
- labelProps: normalize.label({
67
- ...pinInput_anatomy.parts.label.attrs,
68
- htmlFor: pinInput_dom.dom.getHiddenInputId(state.context),
69
- id: pinInput_dom.dom.getLabelId(state.context),
70
- "data-invalid": domQuery.dataAttr(isInvalid),
71
- "data-disabled": domQuery.dataAttr(state.context.disabled),
72
- "data-complete": domQuery.dataAttr(isValueComplete),
73
- onClick: (event) => {
74
- event.preventDefault();
75
- focus();
76
- }
77
- }),
78
- hiddenInputProps: normalize.input({
79
- ...pinInput_anatomy.parts.hiddenInput.attrs,
80
- "aria-hidden": true,
81
- type: "text",
82
- tabIndex: -1,
83
- id: pinInput_dom.dom.getHiddenInputId(state.context),
84
- name: state.context.name,
85
- form: state.context.form,
86
- style: visuallyHidden.visuallyHiddenStyle,
87
- maxLength: state.context.valueLength,
88
- defaultValue: state.context.valueAsString
89
- }),
90
- controlProps: normalize.element({
91
- ...pinInput_anatomy.parts.control.attrs,
92
- id: pinInput_dom.dom.getControlId(state.context)
93
- }),
94
- getInputProps({ index }) {
95
- const inputType = state.context.type === "numeric" ? "tel" : "text";
96
- return normalize.input({
97
- ...pinInput_anatomy.parts.input.attrs,
98
- disabled: state.context.disabled,
99
- "data-disabled": domQuery.dataAttr(state.context.disabled),
100
- "data-complete": domQuery.dataAttr(isValueComplete),
101
- id: pinInput_dom.dom.getInputId(state.context, index.toString()),
102
- "data-ownedby": pinInput_dom.dom.getRootId(state.context),
103
- "aria-label": translations.inputLabel(index, state.context.valueLength),
104
- inputMode: state.context.otp || state.context.type === "numeric" ? "numeric" : "text",
105
- "aria-invalid": domQuery.ariaAttr(isInvalid),
106
- "data-invalid": domQuery.dataAttr(isInvalid),
107
- type: state.context.mask ? "password" : inputType,
108
- defaultValue: state.context.value[index] || "",
109
- autoCapitalize: "none",
110
- autoComplete: state.context.otp ? "one-time-code" : "off",
111
- placeholder: focusedIndex === index ? "" : state.context.placeholder,
112
- onChange(event) {
113
- const evt = domEvent.getNativeEvent(event);
114
- const { value } = event.currentTarget;
115
- if (evt.inputType === "insertFromPaste" || value.length > 2) {
116
- send({ type: "PASTE", value });
117
- event.preventDefault();
118
- return;
119
- }
120
- if (evt.inputType === "deleteContentBackward") {
121
- send("BACKSPACE");
122
- return;
123
- }
124
- send({ type: "INPUT", value, index });
125
- },
126
- onKeyDown(event) {
127
- const evt = domEvent.getNativeEvent(event);
128
- if (evt.isComposing || domEvent.isModifiedEvent(evt))
129
- return;
130
- const keyMap = {
131
- Backspace() {
132
- send("BACKSPACE");
133
- },
134
- Delete() {
135
- send("DELETE");
136
- },
137
- ArrowLeft() {
138
- send("ARROW_LEFT");
139
- },
140
- ArrowRight() {
141
- send("ARROW_RIGHT");
142
- },
143
- Enter() {
144
- send("ENTER");
145
- }
146
- };
147
- const key = domEvent.getEventKey(event, { dir: state.context.dir });
148
- const exec = keyMap[key];
149
- if (exec) {
150
- exec(event);
151
- event.preventDefault();
152
- } else {
153
- if (key === "Tab")
154
- return;
155
- send({ type: "KEY_DOWN", value: key, preventDefault: () => event.preventDefault() });
156
- }
157
- },
158
- onFocus() {
159
- send({ type: "FOCUS", index });
160
- },
161
- onBlur() {
162
- send({ type: "BLUR", index });
163
- }
164
- });
165
- }
166
- };
167
- }
168
-
169
- exports.connect = connect;
@@ -1,165 +0,0 @@
1
- import { getNativeEvent, isModifiedEvent, getEventKey } from '@zag-js/dom-event';
2
- import { dataAttr, ariaAttr } from '@zag-js/dom-query';
3
- import { invariant } from '@zag-js/utils';
4
- import { visuallyHiddenStyle } from '@zag-js/visually-hidden';
5
- import { parts } from './pin-input.anatomy.mjs';
6
- import { dom } from './pin-input.dom.mjs';
7
-
8
- function connect(state, send, normalize) {
9
- const isValueComplete = state.context.isValueComplete;
10
- const isInvalid = state.context.invalid;
11
- const focusedIndex = state.context.focusedIndex;
12
- const translations = state.context.translations;
13
- function focus() {
14
- dom.getFirstInputEl(state.context)?.focus();
15
- }
16
- return {
17
- /**
18
- * The value of the input as an array of strings.
19
- */
20
- value: state.context.value,
21
- /**
22
- * The value of the input as a string.
23
- */
24
- valueAsString: state.context.valueAsString,
25
- /**
26
- * Whether all inputs are filled.
27
- */
28
- isValueComplete,
29
- /**
30
- * Function to set the value of the inputs.
31
- */
32
- setValue(value) {
33
- if (!Array.isArray(value)) {
34
- invariant("[pin-input/setValue] value must be an array");
35
- }
36
- send({ type: "SET_VALUE", value });
37
- },
38
- /**
39
- * Function to clear the value of the inputs.
40
- */
41
- clearValue() {
42
- send({ type: "CLEAR_VALUE" });
43
- },
44
- /**
45
- * Function to set the value of the input at a specific index.
46
- */
47
- setValueAtIndex(index, value) {
48
- send({ type: "SET_VALUE", value, index });
49
- },
50
- /**
51
- * Function to focus the pin-input. This will focus the first input.
52
- */
53
- focus,
54
- rootProps: normalize.element({
55
- dir: state.context.dir,
56
- ...parts.root.attrs,
57
- id: dom.getRootId(state.context),
58
- "data-invalid": dataAttr(isInvalid),
59
- "data-disabled": dataAttr(state.context.disabled),
60
- "data-complete": dataAttr(isValueComplete)
61
- }),
62
- labelProps: normalize.label({
63
- ...parts.label.attrs,
64
- htmlFor: dom.getHiddenInputId(state.context),
65
- id: dom.getLabelId(state.context),
66
- "data-invalid": dataAttr(isInvalid),
67
- "data-disabled": dataAttr(state.context.disabled),
68
- "data-complete": dataAttr(isValueComplete),
69
- onClick: (event) => {
70
- event.preventDefault();
71
- focus();
72
- }
73
- }),
74
- hiddenInputProps: normalize.input({
75
- ...parts.hiddenInput.attrs,
76
- "aria-hidden": true,
77
- type: "text",
78
- tabIndex: -1,
79
- id: dom.getHiddenInputId(state.context),
80
- name: state.context.name,
81
- form: state.context.form,
82
- style: visuallyHiddenStyle,
83
- maxLength: state.context.valueLength,
84
- defaultValue: state.context.valueAsString
85
- }),
86
- controlProps: normalize.element({
87
- ...parts.control.attrs,
88
- id: dom.getControlId(state.context)
89
- }),
90
- getInputProps({ index }) {
91
- const inputType = state.context.type === "numeric" ? "tel" : "text";
92
- return normalize.input({
93
- ...parts.input.attrs,
94
- disabled: state.context.disabled,
95
- "data-disabled": dataAttr(state.context.disabled),
96
- "data-complete": dataAttr(isValueComplete),
97
- id: dom.getInputId(state.context, index.toString()),
98
- "data-ownedby": dom.getRootId(state.context),
99
- "aria-label": translations.inputLabel(index, state.context.valueLength),
100
- inputMode: state.context.otp || state.context.type === "numeric" ? "numeric" : "text",
101
- "aria-invalid": ariaAttr(isInvalid),
102
- "data-invalid": dataAttr(isInvalid),
103
- type: state.context.mask ? "password" : inputType,
104
- defaultValue: state.context.value[index] || "",
105
- autoCapitalize: "none",
106
- autoComplete: state.context.otp ? "one-time-code" : "off",
107
- placeholder: focusedIndex === index ? "" : state.context.placeholder,
108
- onChange(event) {
109
- const evt = getNativeEvent(event);
110
- const { value } = event.currentTarget;
111
- if (evt.inputType === "insertFromPaste" || value.length > 2) {
112
- send({ type: "PASTE", value });
113
- event.preventDefault();
114
- return;
115
- }
116
- if (evt.inputType === "deleteContentBackward") {
117
- send("BACKSPACE");
118
- return;
119
- }
120
- send({ type: "INPUT", value, index });
121
- },
122
- onKeyDown(event) {
123
- const evt = getNativeEvent(event);
124
- if (evt.isComposing || isModifiedEvent(evt))
125
- return;
126
- const keyMap = {
127
- Backspace() {
128
- send("BACKSPACE");
129
- },
130
- Delete() {
131
- send("DELETE");
132
- },
133
- ArrowLeft() {
134
- send("ARROW_LEFT");
135
- },
136
- ArrowRight() {
137
- send("ARROW_RIGHT");
138
- },
139
- Enter() {
140
- send("ENTER");
141
- }
142
- };
143
- const key = getEventKey(event, { dir: state.context.dir });
144
- const exec = keyMap[key];
145
- if (exec) {
146
- exec(event);
147
- event.preventDefault();
148
- } else {
149
- if (key === "Tab")
150
- return;
151
- send({ type: "KEY_DOWN", value: key, preventDefault: () => event.preventDefault() });
152
- }
153
- },
154
- onFocus() {
155
- send({ type: "FOCUS", index });
156
- },
157
- onBlur() {
158
- send({ type: "BLUR", index });
159
- }
160
- });
161
- }
162
- };
163
- }
164
-
165
- export { connect };
@@ -1,33 +0,0 @@
1
- import type { MachineContext as Ctx } from "./pin-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, id: string) => string;
24
- getHiddenInputId: (ctx: Ctx) => string;
25
- getLabelId: (ctx: Ctx) => string;
26
- getControlId: (ctx: Ctx) => string;
27
- getRootEl: (ctx: Ctx) => HTMLElement | null;
28
- getElements: (ctx: Ctx) => HTMLInputElement[];
29
- getInputEl: (ctx: Ctx, id: string) => HTMLInputElement | null;
30
- getFocusedInputEl: (ctx: Ctx) => HTMLInputElement;
31
- getFirstInputEl: (ctx: Ctx) => HTMLInputElement;
32
- getHiddenInputEl: (ctx: Ctx) => HTMLInputElement | null;
33
- };
@@ -1,25 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const domQuery = require('@zag-js/dom-query');
6
-
7
- const dom = domQuery.createScope({
8
- getRootId: (ctx) => ctx.ids?.root ?? `pin-input:${ctx.id}`,
9
- getInputId: (ctx, id) => ctx.ids?.input?.(id) ?? `pin-input:${ctx.id}:${id}`,
10
- getHiddenInputId: (ctx) => ctx.ids?.hiddenInput ?? `pin-input:${ctx.id}:hidden`,
11
- getLabelId: (ctx) => ctx.ids?.label ?? `pin-input:${ctx.id}:label`,
12
- getControlId: (ctx) => ctx.ids?.control ?? `pin-input:${ctx.id}:control`,
13
- getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
14
- getElements: (ctx) => {
15
- const ownerId = CSS.escape(dom.getRootId(ctx));
16
- const selector = `input[data-ownedby=${ownerId}]`;
17
- return domQuery.queryAll(dom.getRootEl(ctx), selector);
18
- },
19
- getInputEl: (ctx, id) => dom.getById(ctx, dom.getInputId(ctx, id)),
20
- getFocusedInputEl: (ctx) => dom.getElements(ctx)[ctx.focusedIndex],
21
- getFirstInputEl: (ctx) => dom.getElements(ctx)[0],
22
- getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx))
23
- });
24
-
25
- exports.dom = dom;
@@ -1,21 +0,0 @@
1
- import { createScope, queryAll } from '@zag-js/dom-query';
2
-
3
- const dom = createScope({
4
- getRootId: (ctx) => ctx.ids?.root ?? `pin-input:${ctx.id}`,
5
- getInputId: (ctx, id) => ctx.ids?.input?.(id) ?? `pin-input:${ctx.id}:${id}`,
6
- getHiddenInputId: (ctx) => ctx.ids?.hiddenInput ?? `pin-input:${ctx.id}:hidden`,
7
- getLabelId: (ctx) => ctx.ids?.label ?? `pin-input:${ctx.id}:label`,
8
- getControlId: (ctx) => ctx.ids?.control ?? `pin-input:${ctx.id}:control`,
9
- getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
10
- getElements: (ctx) => {
11
- const ownerId = CSS.escape(dom.getRootId(ctx));
12
- const selector = `input[data-ownedby=${ownerId}]`;
13
- return queryAll(dom.getRootEl(ctx), selector);
14
- },
15
- getInputEl: (ctx, id) => dom.getById(ctx, dom.getInputId(ctx, id)),
16
- getFocusedInputEl: (ctx) => dom.getElements(ctx)[ctx.focusedIndex],
17
- getFirstInputEl: (ctx) => dom.getElements(ctx)[0],
18
- getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx))
19
- });
20
-
21
- export { dom };
@@ -1,3 +0,0 @@
1
- import type { Machine, StateMachine } from '@zag-js/core';
2
- import type { MachineContext, MachineState, UserDefinedContext } from "./pin-input.types";
3
- export declare function machine(userContext: UserDefinedContext): Machine<MachineContext, MachineState, StateMachine.AnyEventObject>;