@nanoporetech-digital/components 8.7.1 → 8.8.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.
Files changed (145) hide show
  1. package/dist/cjs/{fade-NeUskjyB.js → fade-DBKqk3wh.js} +1 -1
  2. package/dist/cjs/{fullscreen-D5cgtwoy.js → fullscreen-lcdCtrG6.js} +1 -1
  3. package/dist/cjs/index-DGttnXif.js +4 -0
  4. package/dist/cjs/{lazyload-Dm1NcTJ1.js → lazyload-BjSSEx9f.js} +1 -1
  5. package/dist/cjs/loader.cjs.js +1 -1
  6. package/dist/cjs/nano-avatar_5.cjs.entry.js +7 -7
  7. package/dist/cjs/nano-collapsible-comparison.cjs.entry.js +1 -1
  8. package/dist/cjs/nano-components.cjs.js +1 -1
  9. package/dist/cjs/nano-icon-button_2.cjs.entry.js +2 -2
  10. package/dist/cjs/nano-input-otp.cjs.entry.js +710 -0
  11. package/dist/cjs/nano-intersection-observe.cjs.entry.js +1 -1
  12. package/dist/cjs/nano-masked-overflow.cjs.entry.js +3 -3
  13. package/dist/cjs/nano-menu-drawer.cjs.entry.js +2 -2
  14. package/dist/cjs/nano-more-less.cjs.entry.js +2 -2
  15. package/dist/cjs/nano-rating.cjs.entry.js +4 -4
  16. package/dist/cjs/nano-resize-observe.cjs.entry.js +2 -2
  17. package/dist/cjs/nano-slide.cjs.entry.js +1 -1
  18. package/dist/cjs/{nano-slides-ycRcUf0g.js → nano-slides-CryCLfsK.js} +7 -7
  19. package/dist/cjs/nano-slides.cjs.entry.js +1 -1
  20. package/dist/cjs/nano-sortable.cjs.entry.js +1 -1
  21. package/dist/cjs/nano-step-accordion.cjs.entry.js +1 -1
  22. package/dist/cjs/nano-step-breadcrumb.cjs.entry.js +2 -2
  23. package/dist/cjs/nano-sticker.cjs.entry.js +2 -2
  24. package/dist/cjs/nano-tab-content.cjs.entry.js +2 -2
  25. package/dist/cjs/nano-tab.cjs.entry.js +2 -2
  26. package/dist/cjs/nano-table.cjs.entry.js +2 -2
  27. package/dist/cjs/{page-dots--38yVfTZ.js → page-dots-DR1fLjRi.js} +1 -1
  28. package/dist/collection/collection-manifest.json +1 -0
  29. package/dist/collection/components/breadcrumb/breadcrumb.js +1 -1
  30. package/dist/collection/components/collapsible-comparison/collapsible-comparison.js +1 -1
  31. package/dist/collection/components/cta/cta.js +3 -3
  32. package/dist/collection/components/input-otp/input-otp-interface.js +1 -0
  33. package/dist/collection/components/input-otp/input-otp.css +184 -0
  34. package/dist/collection/components/input-otp/input-otp.js +1044 -0
  35. package/dist/collection/components/intersection-observe/intersection-observe.js +1 -1
  36. package/dist/collection/components/masked-overflow/masked-overflow.js +3 -3
  37. package/dist/collection/components/menu-drawer/menu-drawer.js +2 -2
  38. package/dist/collection/components/more-less/more-less.js +2 -2
  39. package/dist/collection/components/rating/rating.js +4 -4
  40. package/dist/collection/components/resize-observe/resize-observe.js +2 -2
  41. package/dist/collection/components/select/select.js +7 -7
  42. package/dist/collection/components/slides/slide.js +1 -1
  43. package/dist/collection/components/slides/slides.js +3 -3
  44. package/dist/collection/components/sortable/sortable.js +1 -1
  45. package/dist/collection/components/step-accordion/step-accordion.js +1 -1
  46. package/dist/collection/components/step-breadcrumb/step-breadcrumb.js +2 -2
  47. package/dist/collection/components/sticker/sticker.js +2 -2
  48. package/dist/collection/components/table/table.js +2 -2
  49. package/dist/collection/components/tabs/tab-content.js +2 -2
  50. package/dist/collection/components/tabs/tab.js +2 -2
  51. package/dist/collection/components/tooltip/tooltip.js +2 -2
  52. package/dist/collection/types/shared.js +0 -1
  53. package/dist/components/cta.js +1 -1
  54. package/dist/components/masked-overflow.js +3 -3
  55. package/dist/components/nano-collapsible-comparison.js +1 -1
  56. package/dist/components/nano-input-otp.d.ts +11 -0
  57. package/dist/components/nano-input-otp.js +749 -0
  58. package/dist/components/nano-intersection-observe.js +1 -1
  59. package/dist/components/nano-menu-drawer.js +2 -2
  60. package/dist/components/nano-more-less.js +2 -2
  61. package/dist/components/nano-rating.js +4 -4
  62. package/dist/components/nano-slide.js +1 -1
  63. package/dist/components/nano-sortable.js +1 -1
  64. package/dist/components/nano-step-accordion.js +1 -1
  65. package/dist/components/nano-step-breadcrumb.js +2 -2
  66. package/dist/components/nano-tab-content.js +2 -2
  67. package/dist/components/nano-tab.js +2 -2
  68. package/dist/components/nano-table.js +2 -2
  69. package/dist/components/resize-observe.js +2 -2
  70. package/dist/components/select.js +7 -7
  71. package/dist/components/slides.js +3 -3
  72. package/dist/components/sticker.js +2 -2
  73. package/dist/components/tooltip.js +2 -2
  74. package/dist/esm/{fade-BA6b-Fk9.js → fade-D980mOvm.js} +1 -1
  75. package/dist/esm/{fullscreen-De_yN4cg.js → fullscreen-CjInlYb9.js} +1 -1
  76. package/dist/esm/index-BM3Om9WE.js +4 -0
  77. package/dist/esm/{lazyload-Cl8k3tPs.js → lazyload-B9B4e3ZI.js} +1 -1
  78. package/dist/esm/loader.js +1 -1
  79. package/dist/esm/nano-avatar_5.entry.js +7 -7
  80. package/dist/esm/nano-collapsible-comparison.entry.js +1 -1
  81. package/dist/esm/nano-components.js +1 -1
  82. package/dist/esm/nano-icon-button_2.entry.js +2 -2
  83. package/dist/esm/nano-input-otp.entry.js +708 -0
  84. package/dist/esm/nano-intersection-observe.entry.js +1 -1
  85. package/dist/esm/nano-masked-overflow.entry.js +3 -3
  86. package/dist/esm/nano-menu-drawer.entry.js +2 -2
  87. package/dist/esm/nano-more-less.entry.js +2 -2
  88. package/dist/esm/nano-rating.entry.js +4 -4
  89. package/dist/esm/nano-resize-observe.entry.js +2 -2
  90. package/dist/esm/nano-slide.entry.js +1 -1
  91. package/dist/esm/{nano-slides-GrbOydfa.js → nano-slides-xbY6lXX9.js} +7 -7
  92. package/dist/esm/nano-slides.entry.js +1 -1
  93. package/dist/esm/nano-sortable.entry.js +1 -1
  94. package/dist/esm/nano-step-accordion.entry.js +1 -1
  95. package/dist/esm/nano-step-breadcrumb.entry.js +2 -2
  96. package/dist/esm/nano-sticker.entry.js +2 -2
  97. package/dist/esm/nano-tab-content.entry.js +2 -2
  98. package/dist/esm/nano-tab.entry.js +2 -2
  99. package/dist/esm/nano-table.entry.js +2 -2
  100. package/dist/esm/{page-dots-CSf81OuJ.js → page-dots-CCfERISy.js} +1 -1
  101. package/dist/nano-components/{fade-BA6b-Fk9.js → fade-D980mOvm.js} +1 -1
  102. package/dist/nano-components/{fullscreen-De_yN4cg.js → fullscreen-CjInlYb9.js} +1 -1
  103. package/dist/nano-components/lazyload-B9B4e3ZI.js +4 -0
  104. package/dist/nano-components/nano-avatar_5.entry.js +1 -1
  105. package/dist/nano-components/nano-collapsible-comparison.entry.js +1 -1
  106. package/dist/nano-components/nano-components.esm.js +1 -1
  107. package/dist/nano-components/nano-icon-button_2.entry.js +1 -1
  108. package/dist/nano-components/nano-input-otp.entry.js +4 -0
  109. package/dist/nano-components/nano-intersection-observe.entry.js +1 -1
  110. package/dist/nano-components/nano-masked-overflow.entry.js +1 -1
  111. package/dist/nano-components/nano-menu-drawer.entry.js +1 -1
  112. package/dist/nano-components/nano-more-less.entry.js +1 -1
  113. package/dist/nano-components/nano-rating.entry.js +1 -1
  114. package/dist/nano-components/nano-resize-observe.entry.js +1 -1
  115. package/dist/nano-components/nano-slide.entry.js +1 -1
  116. package/dist/nano-components/{nano-slides-GrbOydfa.js → nano-slides-xbY6lXX9.js} +3 -3
  117. package/dist/nano-components/nano-slides.entry.js +1 -1
  118. package/dist/nano-components/nano-sortable.entry.js +1 -1
  119. package/dist/nano-components/nano-step-accordion.entry.js +1 -1
  120. package/dist/nano-components/nano-step-breadcrumb.entry.js +1 -1
  121. package/dist/nano-components/nano-sticker.entry.js +1 -1
  122. package/dist/nano-components/nano-tab-content.entry.js +1 -1
  123. package/dist/nano-components/nano-tab.entry.js +1 -1
  124. package/dist/nano-components/nano-table.entry.js +1 -1
  125. package/dist/nano-components/{page-dots-CSf81OuJ.js → page-dots-CCfERISy.js} +1 -1
  126. package/dist/types/components/cta/cta.d.ts +1 -1
  127. package/dist/types/components/input-otp/input-otp-interface.d.ts +24 -0
  128. package/dist/types/components/input-otp/input-otp.d.ts +281 -0
  129. package/dist/types/components.d.ts +181 -2
  130. package/docs-json.json +618 -11
  131. package/docs-vscode.json +80 -9
  132. package/hydrate/index.js +781 -45
  133. package/hydrate/index.mjs +781 -45
  134. package/package.json +2 -2
  135. package/dist/nano-components/lazyload-Cl8k3tPs.js +0 -4
  136. /package/dist/types/builds/{FKs_t1C_ → sxrVAURy}/0/Digital/nano-components/packages/components/.stencil/plugins/stencil/vue-output/generate-vue-component.d.ts +0 -0
  137. /package/dist/types/builds/{FKs_t1C_ → sxrVAURy}/0/Digital/nano-components/packages/components/.stencil/plugins/stencil/vue-output/index.d.ts +0 -0
  138. /package/dist/types/builds/{FKs_t1C_ → sxrVAURy}/0/Digital/nano-components/packages/components/.stencil/plugins/stencil/vue-output/output-vue.d.ts +0 -0
  139. /package/dist/types/builds/{FKs_t1C_ → sxrVAURy}/0/Digital/nano-components/packages/components/.stencil/plugins/stencil/vue-output/plugin.d.ts +0 -0
  140. /package/dist/types/builds/{FKs_t1C_ → sxrVAURy}/0/Digital/nano-components/packages/components/.stencil/plugins/stencil/vue-output/types.d.ts +0 -0
  141. /package/dist/types/builds/{FKs_t1C_ → sxrVAURy}/0/Digital/nano-components/packages/components/.stencil/plugins/stencil/vue-output/utils.d.ts +0 -0
  142. /package/dist/types/builds/{FKs_t1C_ → sxrVAURy}/0/Digital/nano-components/packages/components/.stencil/stencil.config.d.ts +0 -0
  143. /package/dist/types/builds/{FKs_t1C_ → sxrVAURy}/0/Digital/nano-components/packages/components/.stencil/stencil.config.prod.d.ts +0 -0
  144. /package/dist/types/builds/{FKs_t1C_ → sxrVAURy}/0/Digital/nano-components/packages/components/.stencil/testing/mocks/intersection-observer.d.ts +0 -0
  145. /package/dist/types/builds/{FKs_t1C_ → sxrVAURy}/0/Digital/nano-components/packages/components/.stencil/wdio.conf.d.ts +0 -0
@@ -0,0 +1,708 @@
1
+ /*!
2
+ * Custom elements for Nanopore-Digital Web applications
3
+ */
4
+ import { r as registerInstance, c as createEvent, g as getElement, h, F as Fragment, a as Host } from './index-BM3Om9WE.js';
5
+
6
+ const inputOtpCss = ".sc-nano-input-otp-h,*.sc-nano-input-otp,*.sc-nano-input-otp::before,*.sc-nano-input-otp::after{box-sizing:border-box}[hidden].sc-nano-input-otp{display:none !important}@media (prefers-reduced-motion: reduce){.sc-nano-input-otp-h,*.sc-nano-input-otp,*.sc-nano-input-otp::before,*.sc-nano-input-otp::after{animation-duration:0.01ms !important;animation-iteration-count:1 !important;transition-duration:0.01ms !important;scroll-behavior:auto !important}}.sc-nano-input-otp-h,*.sc-nano-input-otp,*.sc-nano-input-otp::before,*.sc-nano-input-otp::after{box-sizing:border-box}[hidden].sc-nano-input-otp{display:none !important}@media (prefers-reduced-motion: reduce){.sc-nano-input-otp-h,*.sc-nano-input-otp,*.sc-nano-input-otp::before,*.sc-nano-input-otp::after{animation-duration:0.01ms !important;animation-iteration-count:1 !important;transition-duration:0.01ms !important;scroll-behavior:auto !important}}.sc-nano-input-otp-h{--margin-top:0;--margin-end:0;--margin-bottom:0;--margin-start:0;--padding-top:var(--nano-spacing-md);--padding-end:0;--padding-bottom:var(--nano-spacing-xl);--padding-start:0;--color:initial;--min-width:40px;--width:40px;--height:50px;--gap:8px;--separator-width:7px;--separator-height:1px;--separator-border-radius:0;--separator-color:var(--nano-color-primary-1000);--highlight-color-valid:var(--nano-color-success-1100);--highlight-color-invalid:var(--nano-color-warning-1100);--border-radius:8px;--border-color:var(--nano-color-neutral-700);--border-width:1px;display:block;position:relative}.input-otp-group.sc-nano-input-otp{margin:var(--margin-top) var(--margin-end) var(--margin-bottom) var(--margin-start);padding:var(--padding-top) var(--padding-end) var(--padding-bottom) var(--padding-start);display:flex;align-items:center;justify-content:center;gap:var(--gap)}.native-wrapper.sc-nano-input-otp{display:flex;align-items:center;justify-content:center;min-inline-size:var(--min-width)}.native-input.sc-nano-input-otp{border-radius:var(--border-radius);inline-size:var(--width);min-inline-size:inherit;block-size:var(--height);border-width:var(--border-width);border-style:solid;border-color:var(--border-color);background:var(--background);color:var(--color);font-size:inherit;text-align:center;appearance:none}.input-otp-description.sc-nano-input-otp{color:currentcolor;font-size:var(--nano-font-size-xs);line-height:var(--nano-line-height-normal);text-align:center;position:relative;inset-inline-start:-24px}.input-otp-description-hidden.sc-nano-input-otp{display:none}.input-otp-separator.sc-nano-input-otp{border-radius:var(--separator-border-radius);flex-shrink:0;inline-size:var(--separator-width);block-size:var(--separator-height);background:var(--separator-color)}.input-otp-disabled.sc-nano-input-otp-h{--color:var(--nano-color-neutral-700);opacity:0.5}.input-otp-disabled.sc-nano-input-otp-h,.input-otp-disabled.sc-nano-input-otp-h .native-input.sc-nano-input-otp:disabled{cursor:not-allowed}.has-focus.sc-nano-input-otp-h .native-input.sc-nano-input-otp:focus{outline:var(--nano-focus-ring)}.nano-invalid.sc-nano-input-otp-h .native-input.sc-nano-input-otp{border-color:var(--highlight-color-invalid)}.nano-valid.sc-nano-input-otp-h .native-input.sc-nano-input-otp{border-color:var(--highlight-color-valid)}.input-otp-icon.sc-nano-input-otp{inline-size:24px;block-size:24px;display:flex;align-items:center}.input-otp-icon--valid.sc-nano-input-otp{color:var(--nano-color-success-1100)}.input-otp-icon--invalid.sc-nano-input-otp{color:var(--nano-color-warning-1100)}";
7
+
8
+ const InputOTP = class {
9
+ constructor(hostRef) {
10
+ registerInstance(this, hostRef);
11
+ this.nanoInput = createEvent(this, "nanoInput", 7);
12
+ this.nanoChange = createEvent(this, "nanoChange", 7);
13
+ this.nanoComplete = createEvent(this, "nanoComplete", 7);
14
+ this.nanoBlur = createEvent(this, "nanoBlur", 7);
15
+ this.nanoFocus = createEvent(this, "nanoFocus", 7);
16
+ }
17
+ inputRefs = [];
18
+ inputId = `nano-input-otp-${inputIds++}`;
19
+ /**
20
+ * Stores the initial value of the input when it receives focus.
21
+ * Used to determine if the value changed during the focus session
22
+ * to avoid emitting unnecessary change events on blur.
23
+ */
24
+ focusedValue;
25
+ /**
26
+ * Tracks whether the user is navigating through input boxes using keyboard navigation
27
+ * (arrow keys, tab) versus mouse clicks. This is used to determine the appropriate
28
+ * focus behavior when an input box is focused.
29
+ */
30
+ isKeyboardNavigation = false;
31
+ get el() { return getElement(this); }
32
+ inputValues = [];
33
+ hasFocus = false;
34
+ previousInputValues = [];
35
+ invalid = false;
36
+ valid = false;
37
+ /**
38
+ * If `true`, the user cannot interact with the input.
39
+ */
40
+ disabled = false;
41
+ /**
42
+ * A hint to the browser for which keyboard to display.
43
+ * Possible values: `"none"`, `"text"`, `"tel"`, `"url"`,
44
+ * `"email"`, `"numeric"`, `"decimal"`, and `"search"`.
45
+ *
46
+ * For numbers (type="number"): "numeric"
47
+ * For text (type="text"): "text"
48
+ */
49
+ inputmode;
50
+ /**
51
+ * The number of input boxes to display.
52
+ */
53
+ length = 6;
54
+ /**
55
+ * A regex pattern string for allowed characters. Defaults based on type.
56
+ *
57
+ * For numbers (`type="number"`): `"[\p{N}]"`
58
+ * For text (`type="text"`): `"[\p{L}\p{N}]"`
59
+ */
60
+ pattern;
61
+ /**
62
+ * If `true`, the user cannot modify the value.
63
+ */
64
+ readonly = false;
65
+ /**
66
+ * The type of input allowed in the input boxes.
67
+ */
68
+ type = 'number';
69
+ /**
70
+ * The value of the input group.
71
+ */
72
+ value = '';
73
+ /**
74
+ * If `true`, allows the input to be completely filled with invalid values.
75
+ * By default, if any input box contains an invalid value, the entire input group
76
+ * is marked as invalid.
77
+ * Default is `false`.
78
+ */
79
+ allowInvalid = false;
80
+ /**
81
+ * If `true`, shows a validity icon when all input boxes are filled.
82
+ * A check icon is shown if all values are valid, and an X icon is shown if any value is invalid.
83
+ * This does not affect the `invalid` or `valid` states of the input group.
84
+ * Default is `false`.
85
+ * */
86
+ showValidity = false;
87
+ /**
88
+ * The `nanoInput` event is fired each time the user modifies the input's value.
89
+ * Unlike the `nanoChange` event, the `nanoInput` event is fired for each alteration
90
+ * to the input's value. This typically happens for each keystroke as the user types.
91
+ *
92
+ * For elements that accept text input (`type=text`, `type=tel`, etc.), the interface
93
+ * is [`InputEvent`](https://developer.mozilla.org/en-US/docs/Web/API/InputEvent); for others,
94
+ * the interface is [`Event`](https://developer.mozilla.org/en-US/docs/Web/API/Event). If
95
+ * the input is cleared on edit, the type is `null`.
96
+ */
97
+ nanoInput;
98
+ /**
99
+ * The `nanoChange` event is fired when the user modifies the input's value.
100
+ * Unlike the `nanoInput` event, the `nanoChange` event is only fired when changes
101
+ * are committed, not as the user types.
102
+ *
103
+ * The `nanoChange` event fires when the `<nano-input-otp>` component loses
104
+ * focus after its value has changed.
105
+ *
106
+ * This event will not emit when programmatically setting the `value` property.
107
+ */
108
+ nanoChange;
109
+ /**
110
+ * Emitted when all input boxes have been filled with valid values.
111
+ */
112
+ nanoComplete;
113
+ /**
114
+ * Emitted when the input group loses focus.
115
+ */
116
+ nanoBlur;
117
+ /**
118
+ * Emitted when the input group has focus.
119
+ */
120
+ nanoFocus;
121
+ /**
122
+ * Sets focus to an input box.
123
+ * @param index - The index of the input box to focus (0-based).
124
+ * If provided and the input box has a value, the input box at that index will be focused.
125
+ * Otherwise, the first empty input box or the last input if all are filled will be focused.
126
+ */
127
+ async setFocus(index) {
128
+ if (typeof index === 'number') {
129
+ const validIndex = Math.max(0, Math.min(index, this.length - 1));
130
+ this.inputRefs[validIndex]?.focus();
131
+ }
132
+ else {
133
+ const tabbableIndex = this.getTabbableIndex();
134
+ this.inputRefs[tabbableIndex]?.focus();
135
+ }
136
+ }
137
+ valueChanged() {
138
+ this.initializeValues();
139
+ this.updateTabIndexes();
140
+ this.computeValidity();
141
+ }
142
+ componentWillLoad() {
143
+ this.initializeValues();
144
+ this.computeValidity();
145
+ }
146
+ componentDidLoad() {
147
+ this.updateTabIndexes();
148
+ }
149
+ /**
150
+ * Get the regex pattern for allowed characters.
151
+ * If a pattern is provided, use it to create a regex pattern
152
+ * Otherwise, use the default regex pattern based on type
153
+ */
154
+ get validKeyPattern() {
155
+ return new RegExp(`^${this.getPattern()}$`, 'u');
156
+ }
157
+ get specialCharsPattern() {
158
+ return new RegExp(`^[\\p{S}\\p{P}]$`, 'u');
159
+ }
160
+ /**
161
+ * Gets the string pattern to pass to the input element
162
+ * and use in the regex for allowed characters.
163
+ * @returns {string} The regex pattern string for allowed characters.
164
+ */
165
+ getPattern() {
166
+ const { pattern, type } = this;
167
+ if (pattern) {
168
+ return pattern;
169
+ }
170
+ return type === 'number' ? '[\\p{N}]' : '[\\p{L}\\p{N}]';
171
+ }
172
+ /**
173
+ * Get the default value for inputmode.
174
+ * If inputmode is provided, use it.
175
+ * Otherwise, use the default inputmode based on type
176
+ * @returns {string} The inputmode to use for the input boxes.
177
+ */
178
+ getInputmode() {
179
+ const { inputmode } = this;
180
+ if (inputmode) {
181
+ return inputmode;
182
+ }
183
+ if (this.type == 'number') {
184
+ return 'numeric';
185
+ }
186
+ else {
187
+ return 'text';
188
+ }
189
+ }
190
+ /**
191
+ * Initializes the input values array based on the current value prop.
192
+ * This splits the value into individual characters and validates them against
193
+ * the allowed pattern. The values are then used as the values in the native
194
+ * input boxes and the value of the input group is updated.
195
+ */
196
+ initializeValues() {
197
+ // Clear all input values
198
+ this.inputValues = Array(this.length).fill('');
199
+ // If the value is null, undefined, or an empty string, return
200
+ if (this.value == null || String(this.value).length === 0) {
201
+ return;
202
+ }
203
+ // Split the value into individual characters and validate
204
+ // them against the allowed pattern if invalid characters are allowed, else just put them in
205
+ const chars = String(this.value).split('').slice(0, this.length);
206
+ chars.forEach((char, index) => {
207
+ if (this.allowInvalid && !this.specialCharsPattern.test(char)) {
208
+ this.inputValues[index] = char;
209
+ }
210
+ else if (this.validKeyPattern.test(char)) {
211
+ this.inputValues[index] = char;
212
+ }
213
+ });
214
+ // Update the value without emitting events
215
+ this.value = this.inputValues.join('');
216
+ this.previousInputValues = [...this.inputValues];
217
+ }
218
+ /**
219
+ * Returns `true` if the document or host element
220
+ * has a `dir` set to `rtl`. The host value will always
221
+ * take priority over the root document value.
222
+ * @returns {boolean} True if RTL direction is set, false otherwise.
223
+ */
224
+ isRTL = (hostEl) => {
225
+ if (hostEl) {
226
+ if (hostEl.dir !== '') {
227
+ return hostEl.dir.toLowerCase() === 'rtl';
228
+ }
229
+ }
230
+ return document?.dir.toLowerCase() === 'rtl';
231
+ };
232
+ /**
233
+ * Updates the value of the input group.
234
+ * This updates the value of the input group and emits an `nanoChange` event.
235
+ * If all of the input boxes are filled, it emits an `nanoComplete` event.
236
+ */
237
+ updateValue(event) {
238
+ const { inputValues, length } = this;
239
+ const newValue = this.getValidChars(inputValues.join('')).join('');
240
+ this.value = newValue;
241
+ this.computeValidity();
242
+ this.emitNanoInput(event);
243
+ if (newValue.length === length) {
244
+ this.nanoComplete.emit({ value: newValue, valid: this.valid });
245
+ }
246
+ }
247
+ /**
248
+ * Emits an `nanoChange` event.
249
+ * This API should be called for user committed changes.
250
+ * This API should not be used for external value changes.
251
+ */
252
+ emitNanoChange(event) {
253
+ const { value } = this;
254
+ // Checks for both null and undefined values
255
+ const newValue = value == null ? value : value.toString();
256
+ this.computeValidity();
257
+ this.nanoChange.emit({
258
+ value: newValue?.toString(),
259
+ event,
260
+ valid: this.valid,
261
+ });
262
+ }
263
+ /**
264
+ * Emits an `nanoInput` event.
265
+ * This is used to emit the input value when the user types,
266
+ * backspaces, or pastes.
267
+ */
268
+ emitNanoInput(event) {
269
+ const { value } = this;
270
+ // Checks for both null and undefined values
271
+ const newValue = value == null ? value : value.toString();
272
+ this.computeValidity();
273
+ this.nanoInput.emit({
274
+ value: newValue?.toString(),
275
+ event,
276
+ valid: this.valid,
277
+ });
278
+ }
279
+ /**
280
+ * Handles the focus behavior for the input OTP component.
281
+ *
282
+ * Focus behavior:
283
+ * 1. Keyboard navigation: Allow normal focus movement
284
+ * 2. Mouse click:
285
+ * - If clicked box has value: Focus that box
286
+ * - If clicked box is empty: Focus first empty box
287
+ *
288
+ * Emits the `nanoFocus` event when the input group gains focus.
289
+ * @returns {(event: FocusEvent) => void} Event handler for focus events.
290
+ */
291
+ onFocus = (index) => (event) => {
292
+ const { inputRefs } = this;
293
+ // Only emit nanoFocus and set the focusedValue when the
294
+ // component first gains focus
295
+ if (!this.hasFocus) {
296
+ this.nanoFocus.emit(event);
297
+ this.focusedValue = this.value;
298
+ }
299
+ this.hasFocus = true;
300
+ let finalIndex = index;
301
+ if (!this.isKeyboardNavigation) {
302
+ // If the clicked box has a value, focus it
303
+ // Otherwise focus the first empty box
304
+ const targetIndex = this.inputValues[index]
305
+ ? index
306
+ : this.getFirstEmptyIndex();
307
+ finalIndex = targetIndex === -1 ? this.length - 1 : targetIndex;
308
+ // Focus the target box
309
+ this.inputRefs[finalIndex]?.focus();
310
+ }
311
+ // Update tabIndexes to match the focused box
312
+ inputRefs.forEach((input, i) => {
313
+ input.tabIndex = i === finalIndex ? 0 : -1;
314
+ });
315
+ // Reset the keyboard navigation flag
316
+ this.isKeyboardNavigation = false;
317
+ };
318
+ /**
319
+ * Handles the blur behavior for the input OTP component.
320
+ * Emits the `nanoBlur` event when the input group loses focus.
321
+ */
322
+ onBlur = (event) => {
323
+ const { inputRefs } = this;
324
+ const relatedTarget = event.relatedTarget;
325
+ // Do not emit blur if we're moving to another input box in the same component
326
+ const isInternalFocus = relatedTarget != null &&
327
+ inputRefs.includes(relatedTarget);
328
+ if (!isInternalFocus) {
329
+ this.hasFocus = false;
330
+ // Reset tabIndexes when focus leaves the component
331
+ this.updateTabIndexes();
332
+ // Always emit nanoBlur when focus leaves the component
333
+ this.nanoBlur.emit(event);
334
+ // Only emit nanoChange if the value has actually changed
335
+ if (this.focusedValue !== this.value) {
336
+ this.emitNanoChange(event);
337
+ }
338
+ }
339
+ };
340
+ /**
341
+ * Focuses the next input box.
342
+ */
343
+ focusNext(currentIndex) {
344
+ const { inputRefs, length } = this;
345
+ if (currentIndex < length - 1) {
346
+ inputRefs[currentIndex + 1]?.focus();
347
+ }
348
+ }
349
+ /**
350
+ * Focuses the previous input box.
351
+ */
352
+ focusPrevious(currentIndex) {
353
+ const { inputRefs } = this;
354
+ if (currentIndex > 0) {
355
+ inputRefs[currentIndex - 1]?.focus();
356
+ }
357
+ }
358
+ /**
359
+ * Searches through the input values and returns the index
360
+ * of the first empty input.
361
+ * Returns -1 if all inputs are filled.
362
+ * @returns {number} The index of the first empty input, or -1 if all are filled.
363
+ */
364
+ getFirstEmptyIndex() {
365
+ const { inputValues, length } = this;
366
+ // Create an array of the same length as the input OTP
367
+ // and fill it with the input values
368
+ const values = Array.from({ length }, (_, i) => inputValues[i] || '');
369
+ return values.findIndex((value) => !value || value === '') ?? -1;
370
+ }
371
+ /**
372
+ * Returns the index of the input that should be tabbed to.
373
+ * If all inputs are filled, returns the last input's index.
374
+ * Otherwise, returns the index of the first empty input.
375
+ * @returns {number} The index to tab to.
376
+ */
377
+ getTabbableIndex() {
378
+ const { length } = this;
379
+ const firstEmptyIndex = this.getFirstEmptyIndex();
380
+ return firstEmptyIndex === -1 ? length - 1 : firstEmptyIndex;
381
+ }
382
+ /**
383
+ * Updates the tabIndexes for the input boxes.
384
+ * This is used to ensure that the correct input is
385
+ * focused when the user navigates using the tab key.
386
+ */
387
+ updateTabIndexes() {
388
+ const { inputRefs, inputValues, length } = this;
389
+ // Find first empty index after any filled boxes
390
+ let firstEmptyIndex = -1;
391
+ for (let i = 0; i < length; i++) {
392
+ if (!inputValues[i] || inputValues[i] === '') {
393
+ firstEmptyIndex = i;
394
+ break;
395
+ }
396
+ }
397
+ // Update tabIndex and aria-hidden for all inputs
398
+ inputRefs.forEach((input, index) => {
399
+ const shouldBeTabbable = firstEmptyIndex === -1
400
+ ? index === length - 1
401
+ : firstEmptyIndex === index;
402
+ input.tabIndex = shouldBeTabbable ? 0 : -1;
403
+ // If the input is empty and not the first empty input,
404
+ // it should be hidden from screen readers.
405
+ const isEmpty = !inputValues[index] || inputValues[index] === '';
406
+ input.setAttribute('aria-hidden', isEmpty && !shouldBeTabbable ? 'true' : 'false');
407
+ });
408
+ }
409
+ /**
410
+ * Handles keyboard navigation for the OTP component.
411
+ *
412
+ * Navigation:
413
+ * - Backspace: Clears current input and moves to previous box if empty
414
+ * - Arrow Left/Right: Moves focus between input boxes
415
+ * - Tab: Allows normal tab navigation between components
416
+ * @returns {(event: KeyboardEvent) => void} Event handler for keydown events.
417
+ */
418
+ onKeyDown = (index) => (event) => {
419
+ const { length } = this;
420
+ const rtl = this.isRTL(this.el);
421
+ const input = event.target;
422
+ // Meta shortcuts are used to copy, paste, and select text
423
+ // We don't want to handle these keys here
424
+ const metaShortcuts = ['a', 'c', 'v', 'x', 'r', 'z', 'y'];
425
+ const isTextSelection = input.selectionStart !== input.selectionEnd;
426
+ // Return if the key is a meta shortcut or the input value
427
+ // text is selected and let the onPaste / onInput handler manage it
428
+ if (isTextSelection ||
429
+ ((event.metaKey || event.ctrlKey) &&
430
+ metaShortcuts.includes(event.key.toLowerCase()))) {
431
+ return;
432
+ }
433
+ if (event.key === 'Backspace') {
434
+ if (this.inputValues[index]) {
435
+ // Shift all values to the right of the current index left by one
436
+ for (let i = index; i < length - 1; i++) {
437
+ this.inputValues[i] = this.inputValues[i + 1];
438
+ }
439
+ // Clear the last box
440
+ this.inputValues[length - 1] = '';
441
+ // Update all inputRefs to match inputValues
442
+ for (let i = 0; i < length; i++) {
443
+ this.inputRefs[i].value = this.inputValues[i] || '';
444
+ }
445
+ this.updateValue(event);
446
+ event.preventDefault();
447
+ }
448
+ else if (!this.inputValues[index] && index > 0) {
449
+ // If current input is empty, move to previous input
450
+ this.focusPrevious(index);
451
+ }
452
+ }
453
+ else if (event.key === 'ArrowLeft' || event.key === 'ArrowRight') {
454
+ this.isKeyboardNavigation = true;
455
+ event.preventDefault();
456
+ const isLeft = event.key === 'ArrowLeft';
457
+ const shouldMoveNext = (isLeft && rtl) || (!isLeft && !rtl);
458
+ // Only allow moving to the next input if the current has a value
459
+ if (shouldMoveNext) {
460
+ if (this.inputValues[index] && index < length - 1) {
461
+ this.focusNext(index);
462
+ }
463
+ }
464
+ else {
465
+ this.focusPrevious(index);
466
+ }
467
+ }
468
+ else if (event.key === 'Tab') {
469
+ this.isKeyboardNavigation = true;
470
+ // Let all tab events proceed normally
471
+ return;
472
+ }
473
+ };
474
+ /**
475
+ * Processes all input scenarios for each input box.
476
+ *
477
+ * This function manages:
478
+ * 1. Autofill handling
479
+ * 2. Input validation
480
+ * 3. Full selection replacement or typing in an empty box
481
+ * 4. Inserting in the middle with available space (shifting)
482
+ * 5. Single character replacement
483
+ * @returns {(event: InputEvent) => void} Event handler for input events.
484
+ */
485
+ onInput = (index) => (event) => {
486
+ const { length, validKeyPattern, allowInvalid } = this;
487
+ const input = event.target;
488
+ const value = input.value;
489
+ const previousValue = this.previousInputValues[index] || '';
490
+ // 1. Autofill handling
491
+ // If the length of the value increases by more than 1 from the previous
492
+ // value, treat this as autofill. This is to prevent the case where the
493
+ // user is typing a single character into an input box containing a value
494
+ // as that will trigger this function with a value length of 2 characters.
495
+ const isAutofill = value.length - previousValue.length > 1;
496
+ if (isAutofill) {
497
+ // Distribute valid characters across input boxes
498
+ const validChars = this.getValidChars(value);
499
+ // If there are no valid characters coming from the
500
+ // autofill, all input refs have to be cleared after the
501
+ // browser has finished the autofill behavior
502
+ if (validChars.length === 0) {
503
+ requestAnimationFrame(() => {
504
+ this.inputRefs.forEach((input) => {
505
+ input.value = '';
506
+ });
507
+ });
508
+ }
509
+ for (let i = 0; i < length; i++) {
510
+ this.inputValues[i] = validChars[i] || '';
511
+ this.inputRefs[i].value = validChars[i] || '';
512
+ }
513
+ this.updateValue(event);
514
+ // Focus the first empty input box or the last input box if all boxes
515
+ // are filled after a small delay to ensure the input boxes have been
516
+ // updated before moving the focus
517
+ setTimeout(() => {
518
+ const nextIndex = validChars.length < length ? validChars.length : length - 1;
519
+ this.inputRefs[nextIndex]?.focus();
520
+ }, 20);
521
+ this.previousInputValues = [...this.inputValues];
522
+ return;
523
+ }
524
+ // 2. Input validation
525
+ // If the character entered is invalid (does not match the pattern),
526
+ // restore the previous value and exit
527
+ if ((!allowInvalid &&
528
+ value.length > 0 &&
529
+ !validKeyPattern.test(value[value.length - 1])) ||
530
+ (allowInvalid && this.specialCharsPattern.test(value[value.length - 1]))) {
531
+ input.value = this.inputValues[index] || '';
532
+ this.previousInputValues = [...this.inputValues];
533
+ return;
534
+ }
535
+ // 3. Full selection replacement or typing in an empty box
536
+ // If the user selects all text in the input box and types, or if the
537
+ // input box is empty, replace only this input box. If the box is empty,
538
+ // move to the next box, otherwise stay focused on this box.
539
+ const isAllSelected = input.selectionStart === 0 && input.selectionEnd === value.length;
540
+ const isEmpty = !this.inputValues[index];
541
+ if (isAllSelected || isEmpty) {
542
+ this.inputValues[index] = value;
543
+ input.value = value;
544
+ this.updateValue(event);
545
+ this.focusNext(index);
546
+ this.previousInputValues = [...this.inputValues];
547
+ return;
548
+ }
549
+ // 4. Inserting in the middle with available space (shifting)
550
+ // If typing in a filled input box and there are empty boxes at the end,
551
+ // shift all values starting at the current box to the right, and insert
552
+ // the new character at the current box.
553
+ const hasAvailableBoxAtEnd = this.inputValues[this.inputValues.length - 1] === '';
554
+ if (this.inputValues[index] && hasAvailableBoxAtEnd && value.length === 2) {
555
+ // Get the inserted character (from event or by diffing value/previousValue)
556
+ let newChar = event.data;
557
+ if (!newChar) {
558
+ newChar =
559
+ value.split('').find((c, i) => c !== previousValue[i]) ||
560
+ value[value.length - 1];
561
+ }
562
+ // Validate the new character before shifting
563
+ if (!allowInvalid && !validKeyPattern.test(newChar)) {
564
+ input.value = this.inputValues[index] || '';
565
+ this.previousInputValues = [...this.inputValues];
566
+ return;
567
+ }
568
+ // Shift values right from the end to the insertion point
569
+ for (let i = this.inputValues.length - 1; i > index; i--) {
570
+ this.inputValues[i] = this.inputValues[i - 1];
571
+ this.inputRefs[i].value = this.inputValues[i] || '';
572
+ }
573
+ this.inputValues[index] = newChar;
574
+ this.inputRefs[index].value = newChar;
575
+ this.updateValue(event);
576
+ this.previousInputValues = [...this.inputValues];
577
+ return;
578
+ }
579
+ // 5. Single character replacement
580
+ // Handles replacing a single character in a box containing a value based
581
+ // on the cursor position. We need the cursor position to determine which
582
+ // character was the last character typed. For example, if the user types "2"
583
+ // in an input box with the cursor at the beginning of the value of "6",
584
+ // the value will be "26", but we want to grab the "2" as the last character
585
+ // typed.
586
+ const cursorPos = input.selectionStart ?? value.length;
587
+ const newCharIndex = cursorPos - 1;
588
+ const newChar = value[newCharIndex] ?? value[0];
589
+ // Check if the new character is valid before updating the value
590
+ if (!allowInvalid && !validKeyPattern.test(newChar)) {
591
+ input.value = this.inputValues[index] || '';
592
+ this.previousInputValues = [...this.inputValues];
593
+ return;
594
+ }
595
+ this.inputValues[index] = newChar;
596
+ input.value = newChar;
597
+ this.updateValue(event);
598
+ this.previousInputValues = [...this.inputValues];
599
+ };
600
+ /**
601
+ * Handles pasting text into the input OTP component.
602
+ * This function prevents the default paste behavior and
603
+ * validates the pasted text against the allowed pattern.
604
+ * It then updates the value of the input group and focuses
605
+ * the next empty input after pasting.
606
+ */
607
+ onPaste = (event) => {
608
+ const { inputRefs, length } = this;
609
+ event.preventDefault();
610
+ const pastedText = event.clipboardData?.getData('text');
611
+ // If there is no pasted text, still emit the input change event
612
+ // because this is how the native input element behaves
613
+ // but return early because there is nothing to paste.
614
+ if (!pastedText) {
615
+ this.emitNanoInput(event);
616
+ return;
617
+ }
618
+ const validChars = this.getValidChars(pastedText);
619
+ // Always paste starting at the first box
620
+ validChars.forEach((char, index) => {
621
+ if (index < length) {
622
+ this.inputRefs[index].value = char;
623
+ this.inputValues[index] = char;
624
+ }
625
+ });
626
+ // Update the value so that all input boxes are updated
627
+ this.value = validChars.join('');
628
+ this.updateValue(event);
629
+ // Focus the next empty input after pasting
630
+ // If all boxes are filled, focus the last input
631
+ const nextEmptyIndex = validChars.length < length ? validChars.length : length - 1;
632
+ inputRefs[nextEmptyIndex]?.focus();
633
+ };
634
+ /**
635
+ * Determines if a separator should be shown for a given index by:
636
+ * - Checking if the total length is even (no separator for odd lengths)
637
+ * - If even, showing a separator after the middle index
638
+ *
639
+ * @param index - The index to check for a separator.
640
+ * @returns {boolean} True if a separator should be shown after the given index, false otherwise.
641
+ */
642
+ showSeparator(index) {
643
+ const { length } = this;
644
+ if (length < 3 || length % 2 !== 0)
645
+ return false;
646
+ return index === length / 2 - 1;
647
+ }
648
+ /**
649
+ * Filters the input string to only include valid characters based on the
650
+ * `validKeyPattern` regex. If `allowInvalid` is true, it will also
651
+ * exclude special characters. The resulting array is sliced to the
652
+ * maximum length of the input OTP.
653
+ * @param str - The input string to filter.
654
+ * @returns {string[]} An array of valid characters.
655
+ */
656
+ getValidChars(str) {
657
+ const { allowInvalid, validKeyPattern, length } = this;
658
+ const allChars = str.split('');
659
+ return (allowInvalid
660
+ ? allChars.filter((char) => char.trim() !== '' && !this.specialCharsPattern.test(char))
661
+ : allChars.filter((char) => char.trim() !== '' && validKeyPattern.test(char))).slice(0, length);
662
+ }
663
+ /**
664
+ * Computes the validity of the input group based on the current input values.
665
+ * Sets the following `invalid` and `valid` states:
666
+ * - `invalid` is true if `allowInvalid` is true and any input value is invalid.
667
+ * - `valid` is true if all input boxes are filled with valid values.
668
+ */
669
+ computeValidity() {
670
+ const { inputValues } = this;
671
+ this.invalid =
672
+ this.allowInvalid &&
673
+ this.inputValues.some((val) => val !== '' && !this.validKeyPattern.test(val));
674
+ this.valid =
675
+ this.inputValues.length === this.length &&
676
+ this.inputValues.every((val) => val !== '' && this.validKeyPattern.test(val));
677
+ inputValues.some((v) => v !== '' && !this.validKeyPattern.test(v));
678
+ }
679
+ render() {
680
+ const { disabled, el, hasFocus, inputId, inputRefs, inputValues, length, readonly, invalid, valid, } = this;
681
+ const inputmode = this.getInputmode();
682
+ const tabbableIndex = this.getTabbableIndex();
683
+ const pattern = this.getPattern();
684
+ const hasDescription = el.querySelector('.input-otp-description')?.textContent?.trim() !== '';
685
+ return (h(Host, { key: 'f397a0775d2d961ccc9013206fae72a8d0ad90ae', class: {
686
+ 'has-focus': hasFocus,
687
+ 'input-otp-disabled': disabled,
688
+ 'input-otp-readonly': readonly,
689
+ 'nano-invalid': invalid,
690
+ 'nano-valid': valid,
691
+ } }, h("div", { key: 'add5fe905af87f4c9b87d29c66134ede52ff2b6b', role: "group", "aria-label": "One-time password input", class: "input-otp-group" }, Array.from({ length }).map((_, index) => (h(Fragment, null, h("div", { class: "native-wrapper" }, h("input", { class: "native-input", id: `${inputId}-${index}`, "aria-label": `Input ${index + 1} of ${length}`, type: "text", inputmode: inputmode, pattern: pattern, disabled: disabled, readOnly: readonly, tabIndex: index === tabbableIndex ? 0 : -1, value: inputValues[index] || '', autocomplete: "one-time-code", ref: (el) => (inputRefs[index] = el), onInput: this.onInput(index), onBlur: this.onBlur, onFocus: this.onFocus(index), onKeyDown: this.onKeyDown(index), onPaste: this.onPaste })), this.showSeparator(index) && h("div", { class: "input-otp-separator" })))), this.showValidity && (h("div", { key: '71d38d8f343be559d187f1c5710a64733cf7ceaa', class: {
692
+ 'input-otp-icon': true,
693
+ 'input-otp-icon--valid': valid,
694
+ 'input-otp-icon--invalid': invalid,
695
+ } }, h("slot", { key: '14529ecc57a6268932bc1485f0a425bb6cb45c51', name: "validity-icon" }, h("nano-icon", { key: '65669a6b4ca76011b721e76fb68b7f9bfeb87092', hidden: !valid, name: "light/circle-check", class: "input-otp-valid-icon", "aria-label": "Valid input" }))))), h("div", { key: '3e677ab8517765759a0cc4a0aa6f0fec58767c90', class: {
696
+ 'input-otp-description': true,
697
+ 'input-otp-description-hidden': !hasDescription,
698
+ 'input-otp-description-inset': this.showValidity,
699
+ } }, h("slot", { key: 'e875d5c5d7ed8f804b9866618b7ed5e5211dd7b1' }))));
700
+ }
701
+ static get watchers() { return {
702
+ "value": ["valueChanged"]
703
+ }; }
704
+ };
705
+ let inputIds = 0;
706
+ InputOTP.style = inputOtpCss;
707
+
708
+ export { InputOTP as nano_input_otp };