@asaddevworld/code-input 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/LICENSE +20 -0
- package/README.md +105 -2
- package/lib/module/Cell.js +128 -0
- package/lib/module/Cell.js.map +1 -0
- package/lib/module/CodeInput.js +157 -0
- package/lib/module/CodeInput.js.map +1 -0
- package/lib/module/PinPad.js +222 -0
- package/lib/module/PinPad.js.map +1 -0
- package/lib/module/index.js +5 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/types.js +4 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/useShake.js +35 -0
- package/lib/module/useShake.js.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/Cell.d.ts +15 -0
- package/lib/typescript/src/Cell.d.ts.map +1 -0
- package/lib/typescript/src/CodeInput.d.ts +3 -0
- package/lib/typescript/src/CodeInput.d.ts.map +1 -0
- package/lib/typescript/src/PinPad.d.ts +3 -0
- package/lib/typescript/src/PinPad.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +4 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts +54 -0
- package/lib/typescript/src/types.d.ts.map +1 -0
- package/lib/typescript/src/useShake.d.ts +10 -0
- package/lib/typescript/src/useShake.d.ts.map +1 -0
- package/package.json +160 -4
- package/src/Cell.tsx +152 -0
- package/src/CodeInput.tsx +187 -0
- package/src/PinPad.tsx +241 -0
- package/src/index.tsx +8 -0
- package/src/types.ts +57 -0
- package/src/useShake.ts +48 -0
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useCallback,
|
|
3
|
+
useEffect,
|
|
4
|
+
useRef,
|
|
5
|
+
useState,
|
|
6
|
+
type ComponentRef,
|
|
7
|
+
} from 'react';
|
|
8
|
+
import {
|
|
9
|
+
Platform,
|
|
10
|
+
Pressable,
|
|
11
|
+
StyleSheet,
|
|
12
|
+
TextInput,
|
|
13
|
+
type TextInputKeyPressEvent,
|
|
14
|
+
} from 'react-native';
|
|
15
|
+
import Animated from 'react-native-reanimated';
|
|
16
|
+
import { Cell } from './Cell';
|
|
17
|
+
import type { CodeInputProps } from './types';
|
|
18
|
+
import { useShake } from './useShake';
|
|
19
|
+
|
|
20
|
+
function sanitize(text: string, length: number, numeric: boolean): string {
|
|
21
|
+
const cleaned = numeric ? text.replace(/\D/g, '') : text.replace(/\s/g, '');
|
|
22
|
+
return cleaned.slice(0, length);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function CodeInput({
|
|
26
|
+
length = 6,
|
|
27
|
+
value,
|
|
28
|
+
defaultValue = '',
|
|
29
|
+
onChange,
|
|
30
|
+
onComplete,
|
|
31
|
+
masked = false,
|
|
32
|
+
error = false,
|
|
33
|
+
onErrorCleared,
|
|
34
|
+
autoFocus = false,
|
|
35
|
+
keyboardType = 'number-pad',
|
|
36
|
+
disabled = false,
|
|
37
|
+
haptics,
|
|
38
|
+
cellStyle,
|
|
39
|
+
activeCellStyle,
|
|
40
|
+
filledCellStyle,
|
|
41
|
+
errorCellStyle,
|
|
42
|
+
cellTextStyle,
|
|
43
|
+
gap = 10,
|
|
44
|
+
style,
|
|
45
|
+
accessibilityLabel,
|
|
46
|
+
testID,
|
|
47
|
+
}: CodeInputProps) {
|
|
48
|
+
const numeric = keyboardType === 'number-pad';
|
|
49
|
+
const [inner, setInner] = useState(() =>
|
|
50
|
+
sanitize(defaultValue, length, numeric)
|
|
51
|
+
);
|
|
52
|
+
const code = value !== undefined ? sanitize(value, length, numeric) : inner;
|
|
53
|
+
const [focused, setFocused] = useState(autoFocus);
|
|
54
|
+
const [showError, setShowError] = useState(false);
|
|
55
|
+
const inputRef = useRef<ComponentRef<typeof TextInput>>(null);
|
|
56
|
+
const completedFor = useRef<string | null>(null);
|
|
57
|
+
const { shake, style: shakeStyle } = useShake();
|
|
58
|
+
|
|
59
|
+
const update = useCallback(
|
|
60
|
+
(next: string) => {
|
|
61
|
+
const cleaned = sanitize(next, length, numeric);
|
|
62
|
+
if (cleaned.length > code.length) {
|
|
63
|
+
haptics?.('tick');
|
|
64
|
+
}
|
|
65
|
+
if (value === undefined) {
|
|
66
|
+
setInner(cleaned);
|
|
67
|
+
}
|
|
68
|
+
onChange?.(cleaned);
|
|
69
|
+
},
|
|
70
|
+
[code.length, haptics, length, numeric, onChange, value]
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
if (code.length === length && completedFor.current !== code) {
|
|
75
|
+
completedFor.current = code;
|
|
76
|
+
haptics?.('success');
|
|
77
|
+
onComplete?.(code);
|
|
78
|
+
}
|
|
79
|
+
if (code.length < length) {
|
|
80
|
+
completedFor.current = null;
|
|
81
|
+
}
|
|
82
|
+
}, [code, length, onComplete, haptics]);
|
|
83
|
+
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
if (!error) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
setShowError(true);
|
|
89
|
+
haptics?.('error');
|
|
90
|
+
shake(() => {
|
|
91
|
+
setShowError(false);
|
|
92
|
+
if (value === undefined) {
|
|
93
|
+
setInner('');
|
|
94
|
+
}
|
|
95
|
+
onChange?.('');
|
|
96
|
+
onErrorCleared?.();
|
|
97
|
+
});
|
|
98
|
+
// Run once per error pulse.
|
|
99
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
100
|
+
}, [error]);
|
|
101
|
+
|
|
102
|
+
const focus = useCallback(() => {
|
|
103
|
+
if (!disabled) {
|
|
104
|
+
inputRef.current?.focus();
|
|
105
|
+
}
|
|
106
|
+
}, [disabled]);
|
|
107
|
+
|
|
108
|
+
const onKeyPress = useCallback(
|
|
109
|
+
(e: TextInputKeyPressEvent) => {
|
|
110
|
+
// Android number pads do not always report a changed text on backspace of an empty field.
|
|
111
|
+
if (
|
|
112
|
+
e.nativeEvent.key === 'Backspace' &&
|
|
113
|
+
code.length > 0 &&
|
|
114
|
+
Platform.OS === 'android'
|
|
115
|
+
) {
|
|
116
|
+
update(code.slice(0, -1));
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
[code, update]
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
const activeIndex = Math.min(code.length, length - 1);
|
|
123
|
+
|
|
124
|
+
return (
|
|
125
|
+
<Pressable
|
|
126
|
+
onPress={focus}
|
|
127
|
+
style={style}
|
|
128
|
+
accessible
|
|
129
|
+
accessibilityRole="none"
|
|
130
|
+
accessibilityLabel={
|
|
131
|
+
accessibilityLabel ??
|
|
132
|
+
`Verification code, ${code.length} of ${length} ${numeric ? 'digits' : 'characters'} entered`
|
|
133
|
+
}
|
|
134
|
+
testID={testID}
|
|
135
|
+
>
|
|
136
|
+
<Animated.View style={[styles.row, { gap }, shakeStyle]}>
|
|
137
|
+
{Array.from({ length }, (_, i) => (
|
|
138
|
+
<Cell
|
|
139
|
+
key={i}
|
|
140
|
+
char={code[i] ?? ''}
|
|
141
|
+
masked={masked}
|
|
142
|
+
active={focused && i === activeIndex && !showError}
|
|
143
|
+
error={showError}
|
|
144
|
+
style={cellStyle}
|
|
145
|
+
activeStyle={activeCellStyle}
|
|
146
|
+
filledStyle={filledCellStyle}
|
|
147
|
+
errorStyle={errorCellStyle}
|
|
148
|
+
textStyle={cellTextStyle}
|
|
149
|
+
/>
|
|
150
|
+
))}
|
|
151
|
+
</Animated.View>
|
|
152
|
+
<TextInput
|
|
153
|
+
ref={inputRef}
|
|
154
|
+
value={code}
|
|
155
|
+
onChangeText={update}
|
|
156
|
+
onKeyPress={onKeyPress}
|
|
157
|
+
onFocus={() => setFocused(true)}
|
|
158
|
+
onBlur={() => setFocused(false)}
|
|
159
|
+
autoFocus={autoFocus}
|
|
160
|
+
editable={!disabled}
|
|
161
|
+
maxLength={length}
|
|
162
|
+
keyboardType={numeric ? 'number-pad' : 'default'}
|
|
163
|
+
autoCapitalize="characters"
|
|
164
|
+
autoCorrect={false}
|
|
165
|
+
textContentType="oneTimeCode"
|
|
166
|
+
autoComplete={Platform.OS === 'android' ? 'sms-otp' : 'one-time-code'}
|
|
167
|
+
caretHidden
|
|
168
|
+
style={styles.hidden}
|
|
169
|
+
accessibilityElementsHidden
|
|
170
|
+
importantForAccessibility="no-hide-descendants"
|
|
171
|
+
testID={testID ? `${testID}-input` : undefined}
|
|
172
|
+
/>
|
|
173
|
+
</Pressable>
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const styles = StyleSheet.create({
|
|
178
|
+
row: { flexDirection: 'row', justifyContent: 'center' },
|
|
179
|
+
hidden: {
|
|
180
|
+
position: 'absolute',
|
|
181
|
+
opacity: 0,
|
|
182
|
+
width: 1,
|
|
183
|
+
height: 1,
|
|
184
|
+
left: 0,
|
|
185
|
+
top: 0,
|
|
186
|
+
},
|
|
187
|
+
});
|
package/src/PinPad.tsx
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { Pressable, StyleSheet, Text, View } from 'react-native';
|
|
3
|
+
import Animated, {
|
|
4
|
+
ReduceMotion,
|
|
5
|
+
useAnimatedStyle,
|
|
6
|
+
useSharedValue,
|
|
7
|
+
withRepeat,
|
|
8
|
+
withSequence,
|
|
9
|
+
withSpring,
|
|
10
|
+
withTiming,
|
|
11
|
+
} from 'react-native-reanimated';
|
|
12
|
+
import type { PinPadProps } from './types';
|
|
13
|
+
import { useShake } from './useShake';
|
|
14
|
+
|
|
15
|
+
const KEYS = [
|
|
16
|
+
'1',
|
|
17
|
+
'2',
|
|
18
|
+
'3',
|
|
19
|
+
'4',
|
|
20
|
+
'5',
|
|
21
|
+
'6',
|
|
22
|
+
'7',
|
|
23
|
+
'8',
|
|
24
|
+
'9',
|
|
25
|
+
'bio',
|
|
26
|
+
'0',
|
|
27
|
+
'del',
|
|
28
|
+
] as const;
|
|
29
|
+
|
|
30
|
+
export function PinPad({
|
|
31
|
+
length = 4,
|
|
32
|
+
onComplete,
|
|
33
|
+
biometricLabel,
|
|
34
|
+
onBiometric,
|
|
35
|
+
haptics,
|
|
36
|
+
keySize = 72,
|
|
37
|
+
keyStyle,
|
|
38
|
+
keyTextStyle,
|
|
39
|
+
dotColor = '#F3F4F6',
|
|
40
|
+
errorColor = '#FF6B6B',
|
|
41
|
+
style,
|
|
42
|
+
testID,
|
|
43
|
+
}: PinPadProps) {
|
|
44
|
+
const [pin, setPin] = useState('');
|
|
45
|
+
const [pending, setPending] = useState(false);
|
|
46
|
+
const [wrong, setWrong] = useState(false);
|
|
47
|
+
const { shake, style: shakeStyle } = useShake();
|
|
48
|
+
const pulse = useSharedValue(1);
|
|
49
|
+
const busy = useRef(false);
|
|
50
|
+
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
if (pending) {
|
|
53
|
+
pulse.value = withRepeat(
|
|
54
|
+
withSequence(
|
|
55
|
+
withTiming(0.4, { duration: 350, reduceMotion: ReduceMotion.System }),
|
|
56
|
+
withTiming(1, { duration: 350, reduceMotion: ReduceMotion.System })
|
|
57
|
+
),
|
|
58
|
+
-1,
|
|
59
|
+
false
|
|
60
|
+
);
|
|
61
|
+
} else {
|
|
62
|
+
pulse.value = withTiming(1, {
|
|
63
|
+
duration: 120,
|
|
64
|
+
reduceMotion: ReduceMotion.System,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}, [pending, pulse]);
|
|
68
|
+
|
|
69
|
+
const dotsStyle = useAnimatedStyle(() => ({ opacity: pulse.value }));
|
|
70
|
+
|
|
71
|
+
const submit = useCallback(
|
|
72
|
+
async (candidate: string) => {
|
|
73
|
+
busy.current = true;
|
|
74
|
+
setPending(true);
|
|
75
|
+
let ok = false;
|
|
76
|
+
try {
|
|
77
|
+
ok = await onComplete(candidate);
|
|
78
|
+
} finally {
|
|
79
|
+
setPending(false);
|
|
80
|
+
}
|
|
81
|
+
if (ok) {
|
|
82
|
+
haptics?.('success');
|
|
83
|
+
busy.current = false;
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
setWrong(true);
|
|
87
|
+
haptics?.('error');
|
|
88
|
+
shake(() => {
|
|
89
|
+
setWrong(false);
|
|
90
|
+
setPin('');
|
|
91
|
+
busy.current = false;
|
|
92
|
+
});
|
|
93
|
+
},
|
|
94
|
+
[haptics, onComplete, shake]
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
const press = useCallback(
|
|
98
|
+
(key: (typeof KEYS)[number]) => {
|
|
99
|
+
if (busy.current) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
if (key === 'bio') {
|
|
103
|
+
onBiometric?.();
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (key === 'del') {
|
|
107
|
+
setPin((p) => p.slice(0, -1));
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
haptics?.('tick');
|
|
111
|
+
setPin((p) => {
|
|
112
|
+
if (p.length >= length) {
|
|
113
|
+
return p;
|
|
114
|
+
}
|
|
115
|
+
const next = p + key;
|
|
116
|
+
if (next.length === length) {
|
|
117
|
+
void submit(next);
|
|
118
|
+
}
|
|
119
|
+
return next;
|
|
120
|
+
});
|
|
121
|
+
},
|
|
122
|
+
[haptics, length, onBiometric, submit]
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
return (
|
|
126
|
+
<View style={[styles.root, style]} testID={testID}>
|
|
127
|
+
<Animated.View
|
|
128
|
+
style={[styles.dots, shakeStyle, dotsStyle]}
|
|
129
|
+
accessibilityLabel={`${pin.length} of ${length} digits entered`}
|
|
130
|
+
>
|
|
131
|
+
{Array.from({ length }, (_, i) => (
|
|
132
|
+
<View
|
|
133
|
+
key={i}
|
|
134
|
+
style={[
|
|
135
|
+
styles.dot,
|
|
136
|
+
{ borderColor: wrong ? errorColor : dotColor },
|
|
137
|
+
i < pin.length && {
|
|
138
|
+
backgroundColor: wrong ? errorColor : dotColor,
|
|
139
|
+
},
|
|
140
|
+
]}
|
|
141
|
+
/>
|
|
142
|
+
))}
|
|
143
|
+
</Animated.View>
|
|
144
|
+
<View style={[styles.grid, { width: keySize * 3 + 24 * 2 }]}>
|
|
145
|
+
{KEYS.map((key) => {
|
|
146
|
+
const blank = key === 'bio' && !biometricLabel;
|
|
147
|
+
const label =
|
|
148
|
+
key === 'bio' ? (biometricLabel ?? '') : key === 'del' ? '⌫' : key;
|
|
149
|
+
return (
|
|
150
|
+
<Key
|
|
151
|
+
key={key}
|
|
152
|
+
label={label}
|
|
153
|
+
size={keySize}
|
|
154
|
+
blank={blank}
|
|
155
|
+
small={key === 'bio' || key === 'del'}
|
|
156
|
+
onPress={() => press(key)}
|
|
157
|
+
style={keyStyle}
|
|
158
|
+
textStyle={keyTextStyle}
|
|
159
|
+
accessibilityLabel={
|
|
160
|
+
key === 'del' ? 'Delete' : key === 'bio' ? biometricLabel : key
|
|
161
|
+
}
|
|
162
|
+
/>
|
|
163
|
+
);
|
|
164
|
+
})}
|
|
165
|
+
</View>
|
|
166
|
+
</View>
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function Key({
|
|
171
|
+
label,
|
|
172
|
+
size,
|
|
173
|
+
blank,
|
|
174
|
+
small,
|
|
175
|
+
onPress,
|
|
176
|
+
style,
|
|
177
|
+
textStyle,
|
|
178
|
+
accessibilityLabel,
|
|
179
|
+
}: {
|
|
180
|
+
label: string;
|
|
181
|
+
size: number;
|
|
182
|
+
blank: boolean;
|
|
183
|
+
small: boolean;
|
|
184
|
+
onPress: () => void;
|
|
185
|
+
style?: PinPadProps['keyStyle'];
|
|
186
|
+
textStyle?: PinPadProps['keyTextStyle'];
|
|
187
|
+
accessibilityLabel?: string;
|
|
188
|
+
}) {
|
|
189
|
+
const scale = useSharedValue(1);
|
|
190
|
+
const animated = useAnimatedStyle(() => ({
|
|
191
|
+
transform: [{ scale: scale.value }],
|
|
192
|
+
}));
|
|
193
|
+
if (blank) {
|
|
194
|
+
return <View style={{ width: size, height: size }} />;
|
|
195
|
+
}
|
|
196
|
+
return (
|
|
197
|
+
<Pressable
|
|
198
|
+
onPress={onPress}
|
|
199
|
+
onPressIn={() => {
|
|
200
|
+
scale.value = withSpring(0.9, { damping: 14, stiffness: 400 });
|
|
201
|
+
}}
|
|
202
|
+
onPressOut={() => {
|
|
203
|
+
scale.value = withSpring(1, { damping: 14, stiffness: 400 });
|
|
204
|
+
}}
|
|
205
|
+
accessibilityRole="button"
|
|
206
|
+
accessibilityLabel={accessibilityLabel}
|
|
207
|
+
>
|
|
208
|
+
<Animated.View
|
|
209
|
+
style={[
|
|
210
|
+
styles.key,
|
|
211
|
+
{ width: size, height: size, borderRadius: size / 2 },
|
|
212
|
+
style,
|
|
213
|
+
animated,
|
|
214
|
+
]}
|
|
215
|
+
>
|
|
216
|
+
<Text style={[styles.keyText, small && styles.keyTextSmall, textStyle]}>
|
|
217
|
+
{label}
|
|
218
|
+
</Text>
|
|
219
|
+
</Animated.View>
|
|
220
|
+
</Pressable>
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const styles = StyleSheet.create({
|
|
225
|
+
root: { alignItems: 'center', gap: 32 },
|
|
226
|
+
dots: { flexDirection: 'row', gap: 16 },
|
|
227
|
+
dot: { width: 14, height: 14, borderRadius: 7, borderWidth: 1.5 },
|
|
228
|
+
grid: {
|
|
229
|
+
flexDirection: 'row',
|
|
230
|
+
flexWrap: 'wrap',
|
|
231
|
+
justifyContent: 'space-between',
|
|
232
|
+
rowGap: 18,
|
|
233
|
+
},
|
|
234
|
+
key: {
|
|
235
|
+
backgroundColor: 'rgba(128,128,128,0.18)',
|
|
236
|
+
alignItems: 'center',
|
|
237
|
+
justifyContent: 'center',
|
|
238
|
+
},
|
|
239
|
+
keyText: { fontSize: 30, fontWeight: '500', color: '#F3F4F6' },
|
|
240
|
+
keyTextSmall: { fontSize: 16, fontWeight: '600' },
|
|
241
|
+
});
|
package/src/index.tsx
ADDED
package/src/types.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { StyleProp, TextStyle, ViewStyle } from 'react-native';
|
|
2
|
+
|
|
3
|
+
export type HapticEvent = 'tick' | 'error' | 'success';
|
|
4
|
+
|
|
5
|
+
export type HapticsCallback = (event: HapticEvent) => void;
|
|
6
|
+
|
|
7
|
+
export interface CodeInputProps {
|
|
8
|
+
/** Number of cells. Default 6. */
|
|
9
|
+
length?: number;
|
|
10
|
+
/** Controlled value. Digits only unless `keyboardType` is 'default'. */
|
|
11
|
+
value?: string;
|
|
12
|
+
defaultValue?: string;
|
|
13
|
+
onChange?: (code: string) => void;
|
|
14
|
+
/** Fires once when every cell is filled. */
|
|
15
|
+
onComplete?: (code: string) => void;
|
|
16
|
+
/** Show dots instead of characters. Default false. */
|
|
17
|
+
masked?: boolean;
|
|
18
|
+
/** True plays the shake, shows the error state, then clears the value. */
|
|
19
|
+
error?: boolean;
|
|
20
|
+
/** Called after the error shake has cleared the value. */
|
|
21
|
+
onErrorCleared?: () => void;
|
|
22
|
+
autoFocus?: boolean;
|
|
23
|
+
/** 'number-pad' (default) or 'default' for alphanumeric codes. */
|
|
24
|
+
keyboardType?: 'number-pad' | 'default';
|
|
25
|
+
/** Disable input. */
|
|
26
|
+
disabled?: boolean;
|
|
27
|
+
haptics?: HapticsCallback;
|
|
28
|
+
cellStyle?: StyleProp<ViewStyle>;
|
|
29
|
+
activeCellStyle?: StyleProp<ViewStyle>;
|
|
30
|
+
filledCellStyle?: StyleProp<ViewStyle>;
|
|
31
|
+
errorCellStyle?: StyleProp<ViewStyle>;
|
|
32
|
+
cellTextStyle?: StyleProp<TextStyle>;
|
|
33
|
+
/** Space between cells. Default 10. */
|
|
34
|
+
gap?: number;
|
|
35
|
+
style?: StyleProp<ViewStyle>;
|
|
36
|
+
accessibilityLabel?: string;
|
|
37
|
+
testID?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface PinPadProps {
|
|
41
|
+
/** Number of digits. Default 4. */
|
|
42
|
+
length?: number;
|
|
43
|
+
/** Return false (or a promise of false) to shake and clear. */
|
|
44
|
+
onComplete: (pin: string) => boolean | Promise<boolean>;
|
|
45
|
+
/** Label of the bottom-left key, for example "Face ID". Omit to leave it blank. */
|
|
46
|
+
biometricLabel?: string;
|
|
47
|
+
onBiometric?: () => void;
|
|
48
|
+
haptics?: HapticsCallback;
|
|
49
|
+
/** Key diameter. Default 72. */
|
|
50
|
+
keySize?: number;
|
|
51
|
+
keyStyle?: StyleProp<ViewStyle>;
|
|
52
|
+
keyTextStyle?: StyleProp<TextStyle>;
|
|
53
|
+
dotColor?: string;
|
|
54
|
+
errorColor?: string;
|
|
55
|
+
style?: StyleProp<ViewStyle>;
|
|
56
|
+
testID?: string;
|
|
57
|
+
}
|
package/src/useShake.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
ReduceMotion,
|
|
4
|
+
runOnJS,
|
|
5
|
+
useAnimatedStyle,
|
|
6
|
+
useSharedValue,
|
|
7
|
+
withSequence,
|
|
8
|
+
withTiming,
|
|
9
|
+
} from 'react-native-reanimated';
|
|
10
|
+
|
|
11
|
+
const AMPLITUDE = 8;
|
|
12
|
+
const STEP = 55;
|
|
13
|
+
|
|
14
|
+
/** Horizontal shake: four cycles, then a callback on the JS thread. */
|
|
15
|
+
export function useShake() {
|
|
16
|
+
const offset = useSharedValue(0);
|
|
17
|
+
|
|
18
|
+
const shake = useCallback(
|
|
19
|
+
(onDone?: () => void) => {
|
|
20
|
+
const t = (to: number) =>
|
|
21
|
+
withTiming(to, { duration: STEP, reduceMotion: ReduceMotion.System });
|
|
22
|
+
offset.value = withSequence(
|
|
23
|
+
t(-AMPLITUDE),
|
|
24
|
+
t(AMPLITUDE),
|
|
25
|
+
t(-AMPLITUDE),
|
|
26
|
+
t(AMPLITUDE),
|
|
27
|
+
t(-AMPLITUDE / 2),
|
|
28
|
+
t(AMPLITUDE / 2),
|
|
29
|
+
withTiming(
|
|
30
|
+
0,
|
|
31
|
+
{ duration: STEP, reduceMotion: ReduceMotion.System },
|
|
32
|
+
() => {
|
|
33
|
+
if (onDone) {
|
|
34
|
+
runOnJS(onDone)();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
)
|
|
38
|
+
);
|
|
39
|
+
},
|
|
40
|
+
[offset]
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const style = useAnimatedStyle(() => ({
|
|
44
|
+
transform: [{ translateX: offset.value }],
|
|
45
|
+
}));
|
|
46
|
+
|
|
47
|
+
return { shake, style };
|
|
48
|
+
}
|