@r5n/otp-react 0.0.0-stage → 0.1.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.
- package/CHANGELOG.md +20 -0
- package/LICENSE +21 -0
- package/README.md +178 -2
- package/dist/index.cjs +394 -0
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +366 -0
- package/dist/otp-input.d.cts +153 -0
- package/dist/otp-input.d.ts +153 -0
- package/dist/styles.d.cts +22 -0
- package/dist/styles.d.ts +22 -0
- package/dist/use-otp.d.cts +101 -0
- package/dist/use-otp.d.ts +101 -0
- package/package.json +95 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# @r5n/otp-react
|
|
2
|
+
|
|
3
|
+
## 0.1.0 (2026-12-03)
|
|
4
|
+
|
|
5
|
+
### 🪨 First release: a headless one-time code input for React DOM (React 19 or newer), built on `@r5n/otp-core`.
|
|
6
|
+
|
|
7
|
+
<details>
|
|
8
|
+
<summary>Description</summary>
|
|
9
|
+
|
|
10
|
+
- `OtpInput` renders one real `<input>` over any number of visual slots, so browser autofill (`autocomplete="one-time-code"`), paste, selection and screen readers work as they do on a single field. `ref.input` gives the native element.
|
|
11
|
+
- `useOtp` hook and a `render` prop for custom slots. Slots expose their state through `data-*` attributes, and the only built-in styles are those needed for the input overlay.
|
|
12
|
+
- Controlled or uncontrolled value with `onChange` and `onComplete`. In controlled mode, `onComplete` (and `autoSubmit`) waits for the parent's next `value`: it fires with the code if the parent accepts it, once with the parent's own complete value if the parent normalises it in `onChange` (upper-casing, stripping spaces), and not at all if the parent reverts or reports an incomplete value.
|
|
13
|
+
- `direction` (`'ltr'` by default, `'rtl'` or `'auto'` to follow the page) and `errorMessage`, an alert linked to the input while `error` is set.
|
|
14
|
+
- `mask` hides the characters on screen only; the input stays `type="text"`, so password managers leave it alone. `type="password"` is an opt-in that hides them from assistive technology too.
|
|
15
|
+
- Form integration through `name`, `autoSubmit` and `form.reset()`. Server-side rendering without hydration mismatches keeps text typed before hydration, and works under a strict style CSP. Safe under React StrictMode.
|
|
16
|
+
- ESM and CommonJS builds with type declarations, marked `"use client"`. The published JavaScript is ES2021.
|
|
17
|
+
</details>
|
|
18
|
+
|
|
19
|
+
### Dependency updates
|
|
20
|
+
- `@r5n/otp-core` 0.0.0 → 0.1.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Damian Sznajder
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,179 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @r5n/otp-react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
One-time code input for React DOM. One real `<input>` sits invisibly over the visual slots, so the
|
|
4
|
+
browser keeps typing, deletion, selection, the context menu, paste and SMS autofill
|
|
5
|
+
(`autocomplete="one-time-code"`). The component only draws the value. It ships no CSS: you style
|
|
6
|
+
data attributes. Behaviour comes from
|
|
7
|
+
[`@r5n/otp-core`](https://www.npmjs.com/package/@r5n/otp-core).
|
|
8
|
+
|
|
9
|
+
- Digits only by default; letters, alphanumeric codes or your own `RegExp` on request.
|
|
10
|
+
- "Your code is 123-456" pasted or autofilled becomes `123456`.
|
|
11
|
+
- `onComplete` once per completed code; `autoSubmit` submits the form, once.
|
|
12
|
+
- One accessible text field, named by your `<label>`; slots hidden from assistive technology; an
|
|
13
|
+
`errorMessage` announced as an alert.
|
|
14
|
+
- React 19 and later, StrictMode, server rendering and hydration (also under a strict Content
|
|
15
|
+
Security Policy), Next.js App Router (`'use client'` is kept in the build).
|
|
16
|
+
|
|
17
|
+
<p align="center">
|
|
18
|
+
<img src="https://raw.githubusercontent.com/r5n-labs/react-native-otp-inputs/develop/docs/assets/react-web.gif" alt="Six slots in two groups of three fill one by one in a browser, then Verified appears" width="60%" />
|
|
19
|
+
</p>
|
|
20
|
+
|
|
21
|
+
<p align="center"><sub>Chromium. The component and the stylesheet are in the <a href="https://github.com/r5n-labs/react-native-otp-inputs/blob/develop/docs/otp-inputs/examples.md#verify-an-email">examples</a>.</sub></p>
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
npm install @r5n/otp-react
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
The only peer dependency is `react` 19 or later.
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
import { OtpInput } from '@r5n/otp-react';
|
|
35
|
+
|
|
36
|
+
export function VerifyForm() {
|
|
37
|
+
return (
|
|
38
|
+
<form method="post" action="/verify">
|
|
39
|
+
<label htmlFor="code">Verification code</label>
|
|
40
|
+
<OtpInput
|
|
41
|
+
id="code"
|
|
42
|
+
length={6}
|
|
43
|
+
groups={[3, 3]}
|
|
44
|
+
renderSeparator={() => '-'}
|
|
45
|
+
name="code"
|
|
46
|
+
autoSubmit
|
|
47
|
+
/>
|
|
48
|
+
<button type="submit">Verify</button>
|
|
49
|
+
</form>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`autoSubmit` submits the form when the code is complete (Enter does not submit it a second time).
|
|
55
|
+
If you verify in JavaScript instead, use `onComplete` without `autoSubmit`, and allow one request
|
|
56
|
+
at a time: a corrected code completes again.
|
|
57
|
+
|
|
58
|
+
```css
|
|
59
|
+
[data-otp-root] {
|
|
60
|
+
display: flex;
|
|
61
|
+
gap: 0.5rem;
|
|
62
|
+
}
|
|
63
|
+
[data-otp-slot] {
|
|
64
|
+
display: grid;
|
|
65
|
+
place-items: center;
|
|
66
|
+
width: 2.75rem;
|
|
67
|
+
height: 3.25rem;
|
|
68
|
+
border: 1px solid #8e8e93;
|
|
69
|
+
border-radius: 0.5rem;
|
|
70
|
+
font:
|
|
71
|
+
600 1.5rem / 1 ui-monospace,
|
|
72
|
+
monospace;
|
|
73
|
+
}
|
|
74
|
+
[data-otp-slot][data-active] {
|
|
75
|
+
outline: 2px solid #2563eb;
|
|
76
|
+
outline-offset: -1px;
|
|
77
|
+
}
|
|
78
|
+
[data-otp-root][data-error] [data-otp-slot] {
|
|
79
|
+
border-color: #dc2626;
|
|
80
|
+
}
|
|
81
|
+
[data-otp-input]::selection {
|
|
82
|
+
background: transparent;
|
|
83
|
+
}
|
|
84
|
+
[data-otp-input]:autofill {
|
|
85
|
+
-webkit-text-fill-color: transparent;
|
|
86
|
+
transition: background-color 9999s;
|
|
87
|
+
}
|
|
88
|
+
@media (forced-colors: active) {
|
|
89
|
+
[data-otp-slot][data-active] {
|
|
90
|
+
outline-color: Highlight;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
The active slot is marked with an `outline`, which forced-colours mode (Windows High Contrast)
|
|
96
|
+
keeps. For a blinking fake caret, add a `@media (prefers-reduced-motion: reduce)` rule that stops
|
|
97
|
+
the animation; see
|
|
98
|
+
[a fake caret](https://github.com/r5n-labs/react-native-otp-inputs/blob/develop/docs/otp-inputs/react/styling.md#a-fake-caret).
|
|
99
|
+
|
|
100
|
+
Controlled with an error and reset:
|
|
101
|
+
|
|
102
|
+
```tsx
|
|
103
|
+
import { OtpInput } from '@r5n/otp-react';
|
|
104
|
+
import { useState } from 'react';
|
|
105
|
+
|
|
106
|
+
export function ControlledCode({ verify }: { verify: (code: string) => Promise<boolean> }) {
|
|
107
|
+
const [code, setCode] = useState('');
|
|
108
|
+
const [error, setError] = useState(false);
|
|
109
|
+
return (
|
|
110
|
+
<OtpInput
|
|
111
|
+
length={6}
|
|
112
|
+
value={code}
|
|
113
|
+
error={error}
|
|
114
|
+
errorMessage="That code is not right. Try again."
|
|
115
|
+
onChange={(next) => {
|
|
116
|
+
setCode(next);
|
|
117
|
+
if (next !== '') setError(false);
|
|
118
|
+
}}
|
|
119
|
+
onComplete={async (complete) => {
|
|
120
|
+
if (await verify(complete)) return;
|
|
121
|
+
setError(true);
|
|
122
|
+
setCode('');
|
|
123
|
+
}}
|
|
124
|
+
/>
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Set `value` synchronously in `onChange`, as here. When the user completes the code, `onComplete`
|
|
130
|
+
fires once your state has accepted it: with the same value, or with your normalised value (for
|
|
131
|
+
example upper-cased). It does not fire if your state keeps its previous value. A value you set by
|
|
132
|
+
yourself fires no callbacks.
|
|
133
|
+
For asynchronous state (timers, transitions, stores), use an uncontrolled input with
|
|
134
|
+
`ref.setValue()`. `error` sets `aria-invalid` on the input and `data-error` on the root;
|
|
135
|
+
`errorMessage` is shown in a `role="alert"` element after the root while `error` is set.
|
|
136
|
+
|
|
137
|
+
## Props
|
|
138
|
+
|
|
139
|
+
| Prop | Default | Purpose |
|
|
140
|
+
| --------------------------------------------------------- | --------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
|
|
141
|
+
| `length` | required | Number of characters, 1 to 32. |
|
|
142
|
+
| `charset` | `'numeric'` | `'alphanumeric'`, `'alphabetic'`, `'any'` or a one-character `RegExp`. |
|
|
143
|
+
| `casing` | `'preserve'` | `'upper'` or `'lower'`. |
|
|
144
|
+
| `groups` | `[length]` | Visual groups such as `[3, 3]`, with `renderSeparator`. |
|
|
145
|
+
| `value`, `defaultValue`, `onChange`, `onComplete` | | Value and callbacks. |
|
|
146
|
+
| `name`, `form`, `required`, `autoSubmit` | | Form integration; `autoSubmit` calls `requestSubmit()` after `onComplete` (a temporary submit button in Safari before 16). |
|
|
147
|
+
| `autoComplete`, `inputMode` | `'one-time-code'`, from `charset` | Autofill token and virtual keyboard. |
|
|
148
|
+
| `direction` | `'ltr'` | `'rtl'`, or `'auto'` to follow the page. |
|
|
149
|
+
| `mask`, `placeholder`, `error`, `errorMessage` | | Display states. `mask` is visual only; `type="password"` also protects it from screen readers. |
|
|
150
|
+
| `type` | `'text'` | `'password'` hides the value from screen readers too, but browsers then treat the code as a password. |
|
|
151
|
+
| `aria-label`, `aria-labelledby`, `aria-describedby`, `id` | "One-time code, 6 digits" | Accessible name and description. A `<label>` replaces the default name. |
|
|
152
|
+
| `disabled`, `readOnly`, `autoFocus`, `onFocus`, `onBlur` | | As for any input. |
|
|
153
|
+
| `className`, `style`, `slotClassName` | | Root and slot classes. |
|
|
154
|
+
| `render`, `renderSeparator`, `renderCaret` | | Custom slot content, separators and a fake caret. |
|
|
155
|
+
| `passwordManagers` | `'allow'` | `'ignore'` asks 1Password, LastPass, Bitwarden and Dashlane to skip the field. |
|
|
156
|
+
| `inputProps` | | Extra attributes for the `<input>`, except those the component owns. |
|
|
157
|
+
|
|
158
|
+
Ref: `focus()`, `blur()`, `clear()`, `setValue(text)` (fires the callbacks; accepts an SMS body and
|
|
159
|
+
ignores text without a code), `getValue()`, `select()`, and `input`, the native element. Headless:
|
|
160
|
+
`useOtp(options)` returns `rootProps`, `inputProps`, `slots`, `snapshot`, `inputRef` and
|
|
161
|
+
`controller`.
|
|
162
|
+
|
|
163
|
+
## Documentation
|
|
164
|
+
|
|
165
|
+
- [Getting started](https://github.com/r5n-labs/react-native-otp-inputs/blob/develop/docs/otp-inputs/react/getting-started.md)
|
|
166
|
+
- [API reference](https://github.com/r5n-labs/react-native-otp-inputs/blob/develop/docs/otp-inputs/react/api.md)
|
|
167
|
+
- [Styling](https://github.com/r5n-labs/react-native-otp-inputs/blob/develop/docs/otp-inputs/react/styling.md):
|
|
168
|
+
data attributes, plain CSS, Tailwind, a fake caret
|
|
169
|
+
- [Forms](https://github.com/r5n-labs/react-native-otp-inputs/blob/develop/docs/otp-inputs/react/forms.md):
|
|
170
|
+
native forms, react-hook-form, Formik, server actions
|
|
171
|
+
- [Server rendering and frameworks](https://github.com/r5n-labs/react-native-otp-inputs/blob/develop/docs/otp-inputs/react/ssr-and-frameworks.md)
|
|
172
|
+
- [All documentation](https://github.com/r5n-labs/react-native-otp-inputs/blob/develop/docs/otp-inputs/README.md)
|
|
173
|
+
|
|
174
|
+
For React Native, use
|
|
175
|
+
[`@r5n/otp-react-native`](https://www.npmjs.com/package/@r5n/otp-react-native).
|
|
176
|
+
|
|
177
|
+
## Licence
|
|
178
|
+
|
|
179
|
+
[MIT](https://github.com/r5n-labs/react-native-otp-inputs/blob/develop/packages/otp-react/LICENSE)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
function __accessProp(key) {
|
|
8
|
+
return this[key];
|
|
9
|
+
}
|
|
10
|
+
var __toCommonJS = (from) => {
|
|
11
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
12
|
+
if (entry)
|
|
13
|
+
return entry;
|
|
14
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
15
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
+
for (var key of __getOwnPropNames(from))
|
|
17
|
+
if (!__hasOwnProp.call(entry, key))
|
|
18
|
+
__defProp(entry, key, {
|
|
19
|
+
get: __accessProp.bind(from, key),
|
|
20
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
__moduleCache.set(from, entry);
|
|
24
|
+
return entry;
|
|
25
|
+
};
|
|
26
|
+
var __moduleCache;
|
|
27
|
+
var __returnValue = (v) => v;
|
|
28
|
+
function __exportSetter(name, newValue) {
|
|
29
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
30
|
+
}
|
|
31
|
+
var __export = (target, all) => {
|
|
32
|
+
for (var name in all)
|
|
33
|
+
__defProp(target, name, {
|
|
34
|
+
get: all[name],
|
|
35
|
+
enumerable: true,
|
|
36
|
+
configurable: true,
|
|
37
|
+
set: __exportSetter.bind(all, name)
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/index.ts
|
|
42
|
+
var exports_src = {};
|
|
43
|
+
__export(exports_src, {
|
|
44
|
+
OtpInput: () => OtpInput,
|
|
45
|
+
useOtp: () => useOtp
|
|
46
|
+
});
|
|
47
|
+
module.exports = __toCommonJS(exports_src);
|
|
48
|
+
|
|
49
|
+
// src/otp-input.tsx
|
|
50
|
+
var import_react2 = require("react");
|
|
51
|
+
|
|
52
|
+
// src/styles.ts
|
|
53
|
+
var ROOT_STYLE = { position: "relative" };
|
|
54
|
+
var INPUT_STYLE = {
|
|
55
|
+
position: "absolute",
|
|
56
|
+
inset: 0,
|
|
57
|
+
width: "100%",
|
|
58
|
+
height: "100%",
|
|
59
|
+
margin: 0,
|
|
60
|
+
padding: 0,
|
|
61
|
+
border: 0,
|
|
62
|
+
outline: "none",
|
|
63
|
+
boxShadow: "none",
|
|
64
|
+
boxSizing: "border-box",
|
|
65
|
+
appearance: "none",
|
|
66
|
+
background: "transparent",
|
|
67
|
+
opacity: 1,
|
|
68
|
+
color: "transparent",
|
|
69
|
+
WebkitTextFillColor: "transparent",
|
|
70
|
+
caretColor: "transparent",
|
|
71
|
+
forcedColorAdjust: "none",
|
|
72
|
+
fontSize: "16px",
|
|
73
|
+
fontFamily: "monospace",
|
|
74
|
+
lineHeight: 1,
|
|
75
|
+
textAlign: "start",
|
|
76
|
+
letterSpacing: "-0.5em"
|
|
77
|
+
};
|
|
78
|
+
var PASSIVE_STYLE = { pointerEvents: "none" };
|
|
79
|
+
var toCssName = (name) => name.replace(/[A-Z]/g, (upper) => `-${upper.toLowerCase()}`);
|
|
80
|
+
var restoreStyle = (element, style) => {
|
|
81
|
+
if (element.style.length > 0)
|
|
82
|
+
return;
|
|
83
|
+
for (const [name, value] of Object.entries(style)) {
|
|
84
|
+
element.style.setProperty(toCssName(name), String(value));
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// src/use-otp.ts
|
|
89
|
+
var import_otp_core = require("@r5n/otp-core");
|
|
90
|
+
var import_dom = require("@r5n/otp-core/dom");
|
|
91
|
+
var import_react = require("react");
|
|
92
|
+
var defaultInputMode = (charset) => charset === undefined || charset === "numeric" ? "numeric" : "text";
|
|
93
|
+
var submitForm = (form) => {
|
|
94
|
+
if (typeof form.requestSubmit === "function") {
|
|
95
|
+
form.requestSubmit();
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const button = form.ownerDocument.createElement("button");
|
|
99
|
+
button.type = "submit";
|
|
100
|
+
button.hidden = true;
|
|
101
|
+
form.append(button);
|
|
102
|
+
button.click();
|
|
103
|
+
button.remove();
|
|
104
|
+
};
|
|
105
|
+
var focusAtEnd = (input, controller) => {
|
|
106
|
+
input.focus();
|
|
107
|
+
const end = input.value.length;
|
|
108
|
+
input.setSelectionRange(end, end);
|
|
109
|
+
const { length } = controller.getSnapshot().options;
|
|
110
|
+
controller.setSelection({ start: length, end: length });
|
|
111
|
+
};
|
|
112
|
+
function useOtp(options) {
|
|
113
|
+
const {
|
|
114
|
+
length,
|
|
115
|
+
charset,
|
|
116
|
+
casing,
|
|
117
|
+
groups,
|
|
118
|
+
value,
|
|
119
|
+
defaultValue,
|
|
120
|
+
onChange,
|
|
121
|
+
onComplete,
|
|
122
|
+
autoComplete = "one-time-code",
|
|
123
|
+
inputMode = defaultInputMode(charset),
|
|
124
|
+
autoSubmit = false,
|
|
125
|
+
direction = "ltr"
|
|
126
|
+
} = options;
|
|
127
|
+
const inputRef = import_react.useRef(null);
|
|
128
|
+
const submitted = import_react.useRef(null);
|
|
129
|
+
const controllerOptions = {
|
|
130
|
+
length,
|
|
131
|
+
charset,
|
|
132
|
+
casing,
|
|
133
|
+
groups,
|
|
134
|
+
value,
|
|
135
|
+
defaultValue,
|
|
136
|
+
onChange,
|
|
137
|
+
onComplete: autoSubmit ? (code) => {
|
|
138
|
+
onComplete?.(code);
|
|
139
|
+
const form = inputRef.current?.form;
|
|
140
|
+
if (!form)
|
|
141
|
+
return;
|
|
142
|
+
submitted.current = code;
|
|
143
|
+
submitForm(form);
|
|
144
|
+
} : onComplete
|
|
145
|
+
};
|
|
146
|
+
const latest = import_react.useRef(controllerOptions);
|
|
147
|
+
const [controller, setController] = import_react.useState(() => import_otp_core.createOtpController(controllerOptions));
|
|
148
|
+
const destroyed = import_react.useRef(null);
|
|
149
|
+
const snapshot = import_react.useSyncExternalStore(controller.subscribe, controller.getSnapshot, controller.getSnapshot);
|
|
150
|
+
import_react.useLayoutEffect(() => {
|
|
151
|
+
latest.current = controllerOptions;
|
|
152
|
+
controller.update(controllerOptions);
|
|
153
|
+
});
|
|
154
|
+
import_react.useLayoutEffect(() => {
|
|
155
|
+
const input = inputRef.current;
|
|
156
|
+
if (!input || destroyed.current === controller)
|
|
157
|
+
return;
|
|
158
|
+
restoreStyle(input, INPUT_STYLE);
|
|
159
|
+
const root = input.closest("[data-otp-root]");
|
|
160
|
+
if (root)
|
|
161
|
+
restoreStyle(root, ROOT_STYLE);
|
|
162
|
+
const onKeyDown = (event) => {
|
|
163
|
+
if (event.key !== "Enter" || event.isComposing)
|
|
164
|
+
return;
|
|
165
|
+
if (submitted.current === controller.getSnapshot().value)
|
|
166
|
+
event.preventDefault();
|
|
167
|
+
};
|
|
168
|
+
input.addEventListener("keydown", onKeyDown);
|
|
169
|
+
const detach = import_dom.attachOtpInput(input, controller, { autoComplete, inputMode });
|
|
170
|
+
return () => {
|
|
171
|
+
input.removeEventListener("keydown", onKeyDown);
|
|
172
|
+
detach();
|
|
173
|
+
};
|
|
174
|
+
}, [controller, autoComplete, inputMode]);
|
|
175
|
+
import_react.useLayoutEffect(() => {
|
|
176
|
+
if (destroyed.current === controller) {
|
|
177
|
+
const { value: last } = controller.getSnapshot();
|
|
178
|
+
setController(import_otp_core.createOtpController({ ...latest.current, defaultValue: last }));
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
return () => {
|
|
182
|
+
destroyed.current = controller;
|
|
183
|
+
controller.destroy();
|
|
184
|
+
};
|
|
185
|
+
}, [controller]);
|
|
186
|
+
return {
|
|
187
|
+
controller,
|
|
188
|
+
snapshot,
|
|
189
|
+
slots: snapshot.slots,
|
|
190
|
+
inputRef,
|
|
191
|
+
rootProps: {
|
|
192
|
+
"data-otp-root": "",
|
|
193
|
+
dir: direction === "auto" ? undefined : direction,
|
|
194
|
+
"data-complete": snapshot.isComplete ? "" : undefined,
|
|
195
|
+
style: ROOT_STYLE,
|
|
196
|
+
onMouseDown: (event) => {
|
|
197
|
+
const input = inputRef.current;
|
|
198
|
+
if (!input || input.disabled || event.button !== 0 || event.target === input)
|
|
199
|
+
return;
|
|
200
|
+
event.preventDefault();
|
|
201
|
+
focusAtEnd(input, controller);
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
inputProps: {
|
|
205
|
+
ref: inputRef,
|
|
206
|
+
type: "text",
|
|
207
|
+
autoComplete,
|
|
208
|
+
inputMode,
|
|
209
|
+
"data-otp-input": "",
|
|
210
|
+
style: INPUT_STYLE
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// src/otp-input.tsx
|
|
216
|
+
var jsx_runtime = require("react/jsx-runtime");
|
|
217
|
+
var MASK_GLYPH = "•";
|
|
218
|
+
var PASSWORD_MANAGER_IGNORE = {
|
|
219
|
+
"data-1p-ignore": "true",
|
|
220
|
+
"data-lpignore": "true",
|
|
221
|
+
"data-bwignore": "true",
|
|
222
|
+
"data-form-type": "other"
|
|
223
|
+
};
|
|
224
|
+
var PASSIVE_SELECTOR = "[data-otp-slot], [data-otp-separator]";
|
|
225
|
+
var flag = (on) => on ? "" : undefined;
|
|
226
|
+
var defaultLabel = (length, charset) => {
|
|
227
|
+
const unit = charset === "numeric" ? "digit" : charset === "alphabetic" ? "letter" : "character";
|
|
228
|
+
return `One-time code, ${length} ${unit}${length === 1 ? "" : "s"}`;
|
|
229
|
+
};
|
|
230
|
+
var OtpInput = import_react2.forwardRef(function OtpInput(props, ref) {
|
|
231
|
+
const {
|
|
232
|
+
autoFocus,
|
|
233
|
+
disabled,
|
|
234
|
+
readOnly,
|
|
235
|
+
name,
|
|
236
|
+
form,
|
|
237
|
+
required,
|
|
238
|
+
mask,
|
|
239
|
+
type = "text",
|
|
240
|
+
placeholder,
|
|
241
|
+
error,
|
|
242
|
+
errorMessage,
|
|
243
|
+
"aria-invalid": ariaInvalid,
|
|
244
|
+
"aria-label": ariaLabel,
|
|
245
|
+
"aria-labelledby": ariaLabelledBy,
|
|
246
|
+
"aria-describedby": ariaDescribedBy,
|
|
247
|
+
id,
|
|
248
|
+
className,
|
|
249
|
+
style,
|
|
250
|
+
slotClassName,
|
|
251
|
+
render,
|
|
252
|
+
renderSeparator,
|
|
253
|
+
renderCaret,
|
|
254
|
+
passwordManagers = "allow",
|
|
255
|
+
inputProps,
|
|
256
|
+
onFocus,
|
|
257
|
+
onBlur,
|
|
258
|
+
...options
|
|
259
|
+
} = props;
|
|
260
|
+
const {
|
|
261
|
+
controller,
|
|
262
|
+
snapshot,
|
|
263
|
+
inputRef,
|
|
264
|
+
rootProps,
|
|
265
|
+
inputProps: ownInputProps
|
|
266
|
+
} = useOtp(options);
|
|
267
|
+
const errorId = import_react2.useId();
|
|
268
|
+
const [labelled, setLabelled] = import_react2.useState(false);
|
|
269
|
+
const [resetValue] = import_react2.useState(options.defaultValue);
|
|
270
|
+
import_react2.useLayoutEffect(() => {
|
|
271
|
+
const input = inputRef.current;
|
|
272
|
+
const { labels } = input;
|
|
273
|
+
const hasLabel = labels !== null && labels.length > 0;
|
|
274
|
+
if (hasLabel !== labelled)
|
|
275
|
+
setLabelled(hasLabel);
|
|
276
|
+
const root = input.parentElement;
|
|
277
|
+
for (const element of root.querySelectorAll(PASSIVE_SELECTOR)) {
|
|
278
|
+
restoreStyle(element, PASSIVE_STYLE);
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
import_react2.useImperativeHandle(ref, () => ({
|
|
282
|
+
focus: () => {
|
|
283
|
+
if (inputRef.current)
|
|
284
|
+
focusAtEnd(inputRef.current, controller);
|
|
285
|
+
},
|
|
286
|
+
blur: () => inputRef.current?.blur(),
|
|
287
|
+
clear: () => controller.clear(),
|
|
288
|
+
setValue: (value) => controller.setValue(value),
|
|
289
|
+
getValue: () => controller.getSnapshot().value,
|
|
290
|
+
select: () => {
|
|
291
|
+
const input = inputRef.current;
|
|
292
|
+
if (!input)
|
|
293
|
+
return;
|
|
294
|
+
input.focus();
|
|
295
|
+
input.select();
|
|
296
|
+
controller.setSelection({ start: 0, end: controller.getSnapshot().options.length });
|
|
297
|
+
},
|
|
298
|
+
get input() {
|
|
299
|
+
return inputRef.current;
|
|
300
|
+
}
|
|
301
|
+
}), [controller, inputRef]);
|
|
302
|
+
const { length, charset, groups } = snapshot.options;
|
|
303
|
+
const { selection, isFocused, isComplete } = snapshot;
|
|
304
|
+
const glyph = mask === true ? MASK_GLYPH : typeof mask === "string" && mask !== "" ? mask : undefined;
|
|
305
|
+
const placeholders = placeholder === undefined ? [] : Array.from(placeholder);
|
|
306
|
+
const isError = error === true || ariaInvalid !== undefined && ariaInvalid !== false && ariaInvalid !== "false";
|
|
307
|
+
const { style: inputStyle, ...nativeInputProps } = inputProps ?? {};
|
|
308
|
+
const showsError = isError && errorMessage !== undefined && errorMessage !== null;
|
|
309
|
+
const describedBy = [ariaDescribedBy, showsError ? errorId : undefined].filter(Boolean).join(" ") || undefined;
|
|
310
|
+
const root = /* @__PURE__ */ jsx_runtime.jsxs("div", {
|
|
311
|
+
...rootProps,
|
|
312
|
+
"data-disabled": flag(disabled === true),
|
|
313
|
+
"data-error": flag(isError),
|
|
314
|
+
className,
|
|
315
|
+
style: style === undefined ? rootProps.style : { ...rootProps.style, ...style },
|
|
316
|
+
children: [
|
|
317
|
+
snapshot.slots.map((slot) => {
|
|
318
|
+
const fallback = placeholder === undefined ? undefined : placeholders.length === length ? placeholders[slot.index] : placeholder;
|
|
319
|
+
const state = {
|
|
320
|
+
display: slot.isFilled ? glyph ?? slot.char : fallback ?? "",
|
|
321
|
+
isPlaceholder: !slot.isFilled && fallback !== undefined,
|
|
322
|
+
hasCaret: slot.isActive && !slot.isFilled && selection.start === selection.end && !readOnly,
|
|
323
|
+
isFocused,
|
|
324
|
+
isComplete,
|
|
325
|
+
isDisabled: disabled === true,
|
|
326
|
+
isError
|
|
327
|
+
};
|
|
328
|
+
return /* @__PURE__ */ jsx_runtime.jsxs(import_react2.Fragment, {
|
|
329
|
+
children: [
|
|
330
|
+
/* @__PURE__ */ jsx_runtime.jsxs("div", {
|
|
331
|
+
"data-otp-slot": "",
|
|
332
|
+
"data-index": slot.index,
|
|
333
|
+
"data-active": flag(slot.isActive),
|
|
334
|
+
"data-filled": flag(slot.isFilled),
|
|
335
|
+
"data-caret": flag(state.hasCaret),
|
|
336
|
+
"data-placeholder": flag(state.isPlaceholder),
|
|
337
|
+
"data-first-in-group": flag(slot.isFirstInGroup),
|
|
338
|
+
"data-last-in-group": flag(slot.isLastInGroup),
|
|
339
|
+
"aria-hidden": "true",
|
|
340
|
+
className: slotClassName,
|
|
341
|
+
style: PASSIVE_STYLE,
|
|
342
|
+
children: [
|
|
343
|
+
render ? render(slot, state) : state.display,
|
|
344
|
+
state.hasCaret && renderCaret ? renderCaret() : null
|
|
345
|
+
]
|
|
346
|
+
}),
|
|
347
|
+
renderSeparator && slot.isLastInGroup && slot.groupIndex < groups.length - 1 ? /* @__PURE__ */ jsx_runtime.jsx("div", {
|
|
348
|
+
"data-otp-separator": "",
|
|
349
|
+
"aria-hidden": "true",
|
|
350
|
+
style: PASSIVE_STYLE,
|
|
351
|
+
children: renderSeparator(slot.groupIndex)
|
|
352
|
+
}) : null
|
|
353
|
+
]
|
|
354
|
+
}, slot.index);
|
|
355
|
+
}),
|
|
356
|
+
/* @__PURE__ */ jsx_runtime.jsx("input", {
|
|
357
|
+
...ownInputProps,
|
|
358
|
+
type,
|
|
359
|
+
defaultValue: resetValue,
|
|
360
|
+
id,
|
|
361
|
+
name,
|
|
362
|
+
form,
|
|
363
|
+
required,
|
|
364
|
+
disabled,
|
|
365
|
+
readOnly,
|
|
366
|
+
autoFocus,
|
|
367
|
+
"aria-label": ariaLabel ?? (ariaLabelledBy === undefined && !labelled ? defaultLabel(length, charset) : undefined),
|
|
368
|
+
"aria-labelledby": ariaLabelledBy,
|
|
369
|
+
"aria-describedby": describedBy,
|
|
370
|
+
"aria-invalid": ariaInvalid ?? (error === true ? true : undefined),
|
|
371
|
+
onFocus,
|
|
372
|
+
onBlur,
|
|
373
|
+
...passwordManagers === "ignore" ? PASSWORD_MANAGER_IGNORE : undefined,
|
|
374
|
+
...nativeInputProps,
|
|
375
|
+
style: {
|
|
376
|
+
...ownInputProps.style,
|
|
377
|
+
cursor: disabled === true ? "default" : "text",
|
|
378
|
+
...inputStyle
|
|
379
|
+
}
|
|
380
|
+
})
|
|
381
|
+
]
|
|
382
|
+
});
|
|
383
|
+
return errorMessage === undefined ? root : /* @__PURE__ */ jsx_runtime.jsxs(jsx_runtime.Fragment, {
|
|
384
|
+
children: [
|
|
385
|
+
root,
|
|
386
|
+
/* @__PURE__ */ jsx_runtime.jsx("div", {
|
|
387
|
+
id: errorId,
|
|
388
|
+
"data-otp-error-message": "",
|
|
389
|
+
role: "alert",
|
|
390
|
+
children: showsError ? errorMessage : null
|
|
391
|
+
})
|
|
392
|
+
]
|
|
393
|
+
});
|
|
394
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@r5n/otp-react`: one-time code input for React DOM, built on `@r5n/otp-core`.
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
export type { OtpCasing, OtpCharset, OtpController, OtpSelection, OtpSlot, OtpSnapshot, } from '@r5n/otp-core';
|
|
7
|
+
export { OtpInput, type OtpInputProps, type OtpInputRef, type OtpNativeInputProps, type OtpSlotState, } from './otp-input.cjs';
|
|
8
|
+
export { type OtpAutoComplete, type OtpInputDirection, type OtpInputElementProps, type OtpInputElementRef, type OtpInputMode, type OtpRootProps, type UseOtpOptions, type UseOtpResult, useOtp, } from './use-otp.cjs';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@r5n/otp-react`: one-time code input for React DOM, built on `@r5n/otp-core`.
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
export type { OtpCasing, OtpCharset, OtpController, OtpSelection, OtpSlot, OtpSnapshot, } from '@r5n/otp-core';
|
|
7
|
+
export { OtpInput, type OtpInputProps, type OtpInputRef, type OtpNativeInputProps, type OtpSlotState, } from './otp-input.js';
|
|
8
|
+
export { type OtpAutoComplete, type OtpInputDirection, type OtpInputElementProps, type OtpInputElementRef, type OtpInputMode, type OtpRootProps, type UseOtpOptions, type UseOtpResult, useOtp, } from './use-otp.js';
|