@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
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { CSSProperties } from 'react';
|
|
2
|
+
/** Containing block for the input overlay. */
|
|
3
|
+
export declare const ROOT_STYLE: CSSProperties;
|
|
4
|
+
/**
|
|
5
|
+
* The input covers the whole row, so a press anywhere on it (slots, gaps, padding) focuses the
|
|
6
|
+
* real field, and the platform keeps ownership of typing, the long-press menu and SMS autofill.
|
|
7
|
+
*/
|
|
8
|
+
export declare const INPUT_STYLE: CSSProperties;
|
|
9
|
+
/**
|
|
10
|
+
* Slots and separators never take pointer events, so a press always reaches the input, even when
|
|
11
|
+
* custom styles stack a slot above it (`position` plus `z-index`).
|
|
12
|
+
*/
|
|
13
|
+
export declare const PASSIVE_STYLE: CSSProperties;
|
|
14
|
+
/**
|
|
15
|
+
* Writes functional styles through the CSSOM when the element has no inline declarations at all.
|
|
16
|
+
* A strict Content Security Policy (`style-src` without `'unsafe-inline'`) blocks the `style`
|
|
17
|
+
* attributes of server-rendered markup, and React does not re-apply styles when it hydrates;
|
|
18
|
+
* CSSOM writes are not subject to that policy. An element that has declarations got them from
|
|
19
|
+
* React, consumer overrides included, so it is left alone. Values are strings or unitless
|
|
20
|
+
* numbers (`0`, `1`).
|
|
21
|
+
*/
|
|
22
|
+
export declare const restoreStyle: (element: HTMLElement, style: CSSProperties) => void;
|
package/dist/styles.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { CSSProperties } from 'react';
|
|
2
|
+
/** Containing block for the input overlay. */
|
|
3
|
+
export declare const ROOT_STYLE: CSSProperties;
|
|
4
|
+
/**
|
|
5
|
+
* The input covers the whole row, so a press anywhere on it (slots, gaps, padding) focuses the
|
|
6
|
+
* real field, and the platform keeps ownership of typing, the long-press menu and SMS autofill.
|
|
7
|
+
*/
|
|
8
|
+
export declare const INPUT_STYLE: CSSProperties;
|
|
9
|
+
/**
|
|
10
|
+
* Slots and separators never take pointer events, so a press always reaches the input, even when
|
|
11
|
+
* custom styles stack a slot above it (`position` plus `z-index`).
|
|
12
|
+
*/
|
|
13
|
+
export declare const PASSIVE_STYLE: CSSProperties;
|
|
14
|
+
/**
|
|
15
|
+
* Writes functional styles through the CSSOM when the element has no inline declarations at all.
|
|
16
|
+
* A strict Content Security Policy (`style-src` without `'unsafe-inline'`) blocks the `style`
|
|
17
|
+
* attributes of server-rendered markup, and React does not re-apply styles when it hydrates;
|
|
18
|
+
* CSSOM writes are not subject to that policy. An element that has declarations got them from
|
|
19
|
+
* React, consumer overrides included, so it is left alone. Values are strings or unitless
|
|
20
|
+
* numbers (`0`, `1`).
|
|
21
|
+
*/
|
|
22
|
+
export declare const restoreStyle: (element: HTMLElement, style: CSSProperties) => void;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { type OtpController, type OtpControllerOptions, type OtpSlot, type OtpSnapshot } from '@r5n/otp-core';
|
|
2
|
+
import { type AttachOptions } from '@r5n/otp-core/dom';
|
|
3
|
+
import { type CSSProperties, type MouseEvent } from 'react';
|
|
4
|
+
/** `autocomplete` token of the input. */
|
|
5
|
+
export type OtpAutoComplete = NonNullable<AttachOptions['autoComplete']>;
|
|
6
|
+
/** Virtual keyboard hint (`inputmode`) of the input. */
|
|
7
|
+
export type OtpInputMode = NonNullable<AttachOptions['inputMode']>;
|
|
8
|
+
/**
|
|
9
|
+
* Layout direction of the slots, as in `@r5n/otp-react-native`. The value is always in logical
|
|
10
|
+
* order; only the row is mirrored.
|
|
11
|
+
* - `'ltr'` (default): codes read left to right, also on right-to-left pages.
|
|
12
|
+
* - `'rtl'`: the first character is in the right-most slot.
|
|
13
|
+
* - `'auto'`: follows the page (the root has no `dir` and inherits it).
|
|
14
|
+
*/
|
|
15
|
+
export type OtpInputDirection = 'ltr' | 'rtl' | 'auto';
|
|
16
|
+
/** Options of {@link useOtp}: the controller options plus input behaviour. */
|
|
17
|
+
export interface UseOtpOptions extends OtpControllerOptions {
|
|
18
|
+
/** `'one-time-code'` (default) lets iOS Safari and Chrome on Android offer SMS codes. */
|
|
19
|
+
autoComplete?: OtpAutoComplete | undefined;
|
|
20
|
+
/** Virtual keyboard. Default: `'numeric'` for the numeric charset, otherwise `'text'`. */
|
|
21
|
+
inputMode?: OtpInputMode | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Right after `onComplete`, submit the input's form (`input.form`, which honours the `form`
|
|
24
|
+
* attribute) with `requestSubmit()`, so validation and `submit` handlers run (a temporary
|
|
25
|
+
* submit button where `requestSubmit` is missing, as in Safari before 16). Enter does not submit
|
|
26
|
+
* the code that was just auto-submitted again; it does once the value changes. Default `false`.
|
|
27
|
+
*/
|
|
28
|
+
autoSubmit?: boolean | undefined;
|
|
29
|
+
/** Default `'ltr'`: see {@link OtpInputDirection}. */
|
|
30
|
+
direction?: OtpInputDirection | undefined;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Ref object holding the `<input>`. Declared structurally, so the exported type does not depend on
|
|
34
|
+
* how `@types/react` defines `RefObject`; this shape is accepted by the `ref` of an `<input>`.
|
|
35
|
+
*/
|
|
36
|
+
export interface OtpInputElementRef {
|
|
37
|
+
current: HTMLInputElement | null;
|
|
38
|
+
}
|
|
39
|
+
/** Props to spread on the element that contains the slots and the input. */
|
|
40
|
+
export interface OtpRootProps {
|
|
41
|
+
readonly 'data-otp-root': '';
|
|
42
|
+
/** `'ltr'` or `'rtl'` from `direction`; absent for `'auto'`, so the page direction applies. */
|
|
43
|
+
readonly dir: 'ltr' | 'rtl' | undefined;
|
|
44
|
+
readonly 'data-complete': '' | undefined;
|
|
45
|
+
readonly style: CSSProperties;
|
|
46
|
+
/** Moves the focus to the input when a part of the root outside the input is pressed. */
|
|
47
|
+
readonly onMouseDown: (event: MouseEvent<HTMLElement>) => void;
|
|
48
|
+
}
|
|
49
|
+
/** Props to spread on the `<input>`. React never renders its value: the binder mirrors it. */
|
|
50
|
+
export interface OtpInputElementProps {
|
|
51
|
+
readonly ref: OtpInputElementRef;
|
|
52
|
+
readonly type: 'text';
|
|
53
|
+
readonly autoComplete: OtpAutoComplete;
|
|
54
|
+
readonly inputMode: OtpInputMode;
|
|
55
|
+
readonly 'data-otp-input': '';
|
|
56
|
+
/** Stacks the input transparently over its positioned parent; see `OtpInput`. */
|
|
57
|
+
readonly style: CSSProperties;
|
|
58
|
+
}
|
|
59
|
+
/** Result of {@link useOtp}. */
|
|
60
|
+
export interface UseOtpResult {
|
|
61
|
+
/** The store. Its identity changes only when React re-runs effects of a kept component. */
|
|
62
|
+
readonly controller: OtpController;
|
|
63
|
+
readonly snapshot: OtpSnapshot;
|
|
64
|
+
/** `snapshot.slots`: one render state per cell. */
|
|
65
|
+
readonly slots: readonly OtpSlot[];
|
|
66
|
+
readonly inputRef: OtpInputElementRef;
|
|
67
|
+
readonly rootProps: OtpRootProps;
|
|
68
|
+
readonly inputProps: OtpInputElementProps;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Focuses the input with the caret after the last character, where typing continues the code.
|
|
72
|
+
* @internal
|
|
73
|
+
*/
|
|
74
|
+
export declare const focusAtEnd: (input: HTMLInputElement, controller: OtpController) => void;
|
|
75
|
+
/**
|
|
76
|
+
* Headless one-time code input: owns the controller and binds it to your `<input>`.
|
|
77
|
+
* Spread `rootProps` on a container and `inputProps` on the input, and render `slots` however you
|
|
78
|
+
* like. The input must be rendered whenever the component that calls the hook is mounted.
|
|
79
|
+
*
|
|
80
|
+
* Controlled (`value` + `onChange`) and uncontrolled (`defaultValue`) use work as in React
|
|
81
|
+
* inputs. Props reach the controller after every commit, never during render, so callbacks may
|
|
82
|
+
* change identity freely and `onChange` may set state. When controlled, set `value` synchronously
|
|
83
|
+
* in `onChange`: an edit the next render does not confirm is reverted, and `onComplete` (with
|
|
84
|
+
* `autoSubmit`) fires only once the parent's `value` confirms the complete code.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```tsx
|
|
88
|
+
* const otp = useOtp({ length: 6, onComplete: verify });
|
|
89
|
+
* return (
|
|
90
|
+
* <div {...otp.rootProps} className="flex gap-2">
|
|
91
|
+
* {otp.slots.map((slot) => (
|
|
92
|
+
* <div key={slot.index} aria-hidden="true" className={slot.isActive ? 'ring' : ''}>
|
|
93
|
+
* {slot.char}
|
|
94
|
+
* </div>
|
|
95
|
+
* ))}
|
|
96
|
+
* <input {...otp.inputProps} aria-label="Verification code" />
|
|
97
|
+
* </div>
|
|
98
|
+
* );
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export declare function useOtp(options: UseOtpOptions): UseOtpResult;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { type OtpController, type OtpControllerOptions, type OtpSlot, type OtpSnapshot } from '@r5n/otp-core';
|
|
2
|
+
import { type AttachOptions } from '@r5n/otp-core/dom';
|
|
3
|
+
import { type CSSProperties, type MouseEvent } from 'react';
|
|
4
|
+
/** `autocomplete` token of the input. */
|
|
5
|
+
export type OtpAutoComplete = NonNullable<AttachOptions['autoComplete']>;
|
|
6
|
+
/** Virtual keyboard hint (`inputmode`) of the input. */
|
|
7
|
+
export type OtpInputMode = NonNullable<AttachOptions['inputMode']>;
|
|
8
|
+
/**
|
|
9
|
+
* Layout direction of the slots, as in `@r5n/otp-react-native`. The value is always in logical
|
|
10
|
+
* order; only the row is mirrored.
|
|
11
|
+
* - `'ltr'` (default): codes read left to right, also on right-to-left pages.
|
|
12
|
+
* - `'rtl'`: the first character is in the right-most slot.
|
|
13
|
+
* - `'auto'`: follows the page (the root has no `dir` and inherits it).
|
|
14
|
+
*/
|
|
15
|
+
export type OtpInputDirection = 'ltr' | 'rtl' | 'auto';
|
|
16
|
+
/** Options of {@link useOtp}: the controller options plus input behaviour. */
|
|
17
|
+
export interface UseOtpOptions extends OtpControllerOptions {
|
|
18
|
+
/** `'one-time-code'` (default) lets iOS Safari and Chrome on Android offer SMS codes. */
|
|
19
|
+
autoComplete?: OtpAutoComplete | undefined;
|
|
20
|
+
/** Virtual keyboard. Default: `'numeric'` for the numeric charset, otherwise `'text'`. */
|
|
21
|
+
inputMode?: OtpInputMode | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Right after `onComplete`, submit the input's form (`input.form`, which honours the `form`
|
|
24
|
+
* attribute) with `requestSubmit()`, so validation and `submit` handlers run (a temporary
|
|
25
|
+
* submit button where `requestSubmit` is missing, as in Safari before 16). Enter does not submit
|
|
26
|
+
* the code that was just auto-submitted again; it does once the value changes. Default `false`.
|
|
27
|
+
*/
|
|
28
|
+
autoSubmit?: boolean | undefined;
|
|
29
|
+
/** Default `'ltr'`: see {@link OtpInputDirection}. */
|
|
30
|
+
direction?: OtpInputDirection | undefined;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Ref object holding the `<input>`. Declared structurally, so the exported type does not depend on
|
|
34
|
+
* how `@types/react` defines `RefObject`; this shape is accepted by the `ref` of an `<input>`.
|
|
35
|
+
*/
|
|
36
|
+
export interface OtpInputElementRef {
|
|
37
|
+
current: HTMLInputElement | null;
|
|
38
|
+
}
|
|
39
|
+
/** Props to spread on the element that contains the slots and the input. */
|
|
40
|
+
export interface OtpRootProps {
|
|
41
|
+
readonly 'data-otp-root': '';
|
|
42
|
+
/** `'ltr'` or `'rtl'` from `direction`; absent for `'auto'`, so the page direction applies. */
|
|
43
|
+
readonly dir: 'ltr' | 'rtl' | undefined;
|
|
44
|
+
readonly 'data-complete': '' | undefined;
|
|
45
|
+
readonly style: CSSProperties;
|
|
46
|
+
/** Moves the focus to the input when a part of the root outside the input is pressed. */
|
|
47
|
+
readonly onMouseDown: (event: MouseEvent<HTMLElement>) => void;
|
|
48
|
+
}
|
|
49
|
+
/** Props to spread on the `<input>`. React never renders its value: the binder mirrors it. */
|
|
50
|
+
export interface OtpInputElementProps {
|
|
51
|
+
readonly ref: OtpInputElementRef;
|
|
52
|
+
readonly type: 'text';
|
|
53
|
+
readonly autoComplete: OtpAutoComplete;
|
|
54
|
+
readonly inputMode: OtpInputMode;
|
|
55
|
+
readonly 'data-otp-input': '';
|
|
56
|
+
/** Stacks the input transparently over its positioned parent; see `OtpInput`. */
|
|
57
|
+
readonly style: CSSProperties;
|
|
58
|
+
}
|
|
59
|
+
/** Result of {@link useOtp}. */
|
|
60
|
+
export interface UseOtpResult {
|
|
61
|
+
/** The store. Its identity changes only when React re-runs effects of a kept component. */
|
|
62
|
+
readonly controller: OtpController;
|
|
63
|
+
readonly snapshot: OtpSnapshot;
|
|
64
|
+
/** `snapshot.slots`: one render state per cell. */
|
|
65
|
+
readonly slots: readonly OtpSlot[];
|
|
66
|
+
readonly inputRef: OtpInputElementRef;
|
|
67
|
+
readonly rootProps: OtpRootProps;
|
|
68
|
+
readonly inputProps: OtpInputElementProps;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Focuses the input with the caret after the last character, where typing continues the code.
|
|
72
|
+
* @internal
|
|
73
|
+
*/
|
|
74
|
+
export declare const focusAtEnd: (input: HTMLInputElement, controller: OtpController) => void;
|
|
75
|
+
/**
|
|
76
|
+
* Headless one-time code input: owns the controller and binds it to your `<input>`.
|
|
77
|
+
* Spread `rootProps` on a container and `inputProps` on the input, and render `slots` however you
|
|
78
|
+
* like. The input must be rendered whenever the component that calls the hook is mounted.
|
|
79
|
+
*
|
|
80
|
+
* Controlled (`value` + `onChange`) and uncontrolled (`defaultValue`) use work as in React
|
|
81
|
+
* inputs. Props reach the controller after every commit, never during render, so callbacks may
|
|
82
|
+
* change identity freely and `onChange` may set state. When controlled, set `value` synchronously
|
|
83
|
+
* in `onChange`: an edit the next render does not confirm is reverted, and `onComplete` (with
|
|
84
|
+
* `autoSubmit`) fires only once the parent's `value` confirms the complete code.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```tsx
|
|
88
|
+
* const otp = useOtp({ length: 6, onComplete: verify });
|
|
89
|
+
* return (
|
|
90
|
+
* <div {...otp.rootProps} className="flex gap-2">
|
|
91
|
+
* {otp.slots.map((slot) => (
|
|
92
|
+
* <div key={slot.index} aria-hidden="true" className={slot.isActive ? 'ring' : ''}>
|
|
93
|
+
* {slot.char}
|
|
94
|
+
* </div>
|
|
95
|
+
* ))}
|
|
96
|
+
* <input {...otp.inputProps} aria-label="Verification code" />
|
|
97
|
+
* </div>
|
|
98
|
+
* );
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
export declare function useOtp(options: UseOtpOptions): UseOtpResult;
|
package/package.json
CHANGED
|
@@ -1,6 +1,97 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@r5n/otp-react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Headless one-time code input for React DOM: one real input, any number of visual slots, with autofill and paste.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"otp",
|
|
7
|
+
"otp-input",
|
|
8
|
+
"one-time-password",
|
|
9
|
+
"one-time-code",
|
|
10
|
+
"verification-code",
|
|
11
|
+
"pin",
|
|
12
|
+
"autofill",
|
|
13
|
+
"react",
|
|
14
|
+
"headless"
|
|
15
|
+
],
|
|
16
|
+
"homepage": "https://github.com/r5n-labs/react-native-otp-inputs/tree/develop/packages/otp-react#readme",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/r5n-labs/react-native-otp-inputs/issues"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/r5n-labs/react-native-otp-inputs.git",
|
|
23
|
+
"directory": "packages/otp-react"
|
|
24
|
+
},
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"author": "Damian Sznajder <damsznajder@gmail.com>",
|
|
27
|
+
"contributors": [
|
|
28
|
+
"Michal Kantor <kantorm96@gmail.com>"
|
|
29
|
+
],
|
|
30
|
+
"r5n": {
|
|
31
|
+
"product": "otp-inputs",
|
|
32
|
+
"kind": "react",
|
|
33
|
+
"size": [
|
|
34
|
+
{
|
|
35
|
+
"name": "with @r5n/otp-core",
|
|
36
|
+
"entry": "dist/index.js",
|
|
37
|
+
"include": [
|
|
38
|
+
"@r5n/otp-core"
|
|
39
|
+
],
|
|
40
|
+
"limit": "8.4 kB"
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
"name": "alone",
|
|
44
|
+
"entry": "dist/index.js",
|
|
45
|
+
"limit": "2.9 kB"
|
|
46
|
+
}
|
|
47
|
+
]
|
|
48
|
+
},
|
|
49
|
+
"sideEffects": false,
|
|
50
|
+
"type": "module",
|
|
51
|
+
"exports": {
|
|
52
|
+
".": {
|
|
53
|
+
"import": {
|
|
54
|
+
"types": "./dist/index.d.ts",
|
|
55
|
+
"default": "./dist/index.js"
|
|
56
|
+
},
|
|
57
|
+
"require": {
|
|
58
|
+
"types": "./dist/index.d.cts",
|
|
59
|
+
"default": "./dist/index.cjs"
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
"./package.json": "./package.json"
|
|
63
|
+
},
|
|
64
|
+
"main": "./dist/index.cjs",
|
|
65
|
+
"module": "./dist/index.js",
|
|
66
|
+
"types": "./dist/index.d.cts",
|
|
67
|
+
"files": [
|
|
68
|
+
"dist",
|
|
69
|
+
"CHANGELOG.md"
|
|
70
|
+
],
|
|
71
|
+
"scripts": {
|
|
72
|
+
"build": "bun ../../tools/build.ts",
|
|
73
|
+
"check": "bun run --bun tsc -p tsconfig.json",
|
|
74
|
+
"test": "bun test",
|
|
75
|
+
"test:coverage": "bun test --coverage"
|
|
76
|
+
},
|
|
77
|
+
"dependencies": {
|
|
78
|
+
"@r5n/otp-core": "^0.1.0"
|
|
79
|
+
},
|
|
80
|
+
"peerDependencies": {
|
|
81
|
+
"react": ">=19"
|
|
82
|
+
},
|
|
83
|
+
"devDependencies": {
|
|
84
|
+
"@happy-dom/global-registrator": "^21.0.4",
|
|
85
|
+
"@testing-library/dom": "^10.4.2",
|
|
86
|
+
"@testing-library/react": "^16.3.3",
|
|
87
|
+
"@testing-library/user-event": "^14.6.14",
|
|
88
|
+
"@types/bun": "^1.4.5",
|
|
89
|
+
"@types/react": "^19.3.3",
|
|
90
|
+
"@types/react-dom": "^19.3.0",
|
|
91
|
+
"react": "19.3.0",
|
|
92
|
+
"react-dom": "19.3.0"
|
|
93
|
+
},
|
|
94
|
+
"publishConfig": {
|
|
95
|
+
"access": "public"
|
|
96
|
+
}
|
|
97
|
+
}
|