-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathPasswordStrength.tsx
More file actions
308 lines (287 loc) · 8.22 KB
/
PasswordStrength.tsx
File metadata and controls
308 lines (287 loc) · 8.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import React, { useEffect, useRef, useState } from 'react';
import {
Text,
View,
StyleSheet,
SafeAreaView,
TextInput,
Pressable,
} from 'react-native';
import Animated, {
useAnimatedStyle,
useSharedValue,
withRepeat,
withSpring,
withTiming,
} from 'react-native-reanimated';
const MAX_LENGTH = 8;
const PAD = 12;
const PASS_COLORS = [
'lightgrey',
'red',
'orange',
'rosybrown',
'darkseagreen',
'royalblue',
];
const PASS_VALID_INTIAIL = {
lowercase: false,
uppercase: false,
numeric: false,
specialChar: false,
minChar: false,
};
function checkpasswordStrength(value: string) {
var strength = 0;
let validity = { ...PASS_VALID_INTIAIL };
if (value.length >= 8) {
strength += 1;
validity.minChar = true;
}
if (value.match(/[a-z]+/)) {
strength += 1;
validity.lowercase = true;
}
if (value.match(/[A-Z]+/)) {
strength += 1;
validity.uppercase = true;
}
if (value.match(/[0-9]+/)) {
strength += 1;
validity.numeric = true;
}
if (value.match(/[$@#&!]+/)) {
strength += 1;
validity.specialChar = true;
}
return { strength, validity };
}
const PasswordStrength = () => {
const [password, setPassword] = useState('');
const [passwordStrength, setPasswordStrength] = useState({
strength: 0,
validity: { ...PASS_VALID_INTIAIL },
});
const [isInputFocused, setInputFocused] = useState(false);
const [inputDimension, setInputDimension] = useState({ width: 0, height: 0 });
const [caretPosition, setCaretPosition] = useState({ start: 0, end: 0 });
const inputRef = useRef<TextInput>(null);
const cursorOpacity = useSharedValue(1);
const cursorX = useSharedValue(0);
const tickState = useSharedValue(0);
useEffect(() => {
if (isInputFocused) {
cursorOpacity.value = withRepeat(withTiming(0, { duration: 1000 }), -1);
tickState.value = 0;
} else {
cursorOpacity.value = password.length > 0 ? 1 : 0;
}
return () => {
cursorOpacity.value = 1;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [password, isInputFocused]);
const cursorStyle = useAnimatedStyle(
() => {
let caretWidth = caretPosition.start < password.length ? 2 : 4;
let caretPositionX = cursorX.value;
if (!isInputFocused && password.length > 0) {
caretWidth = inputDimension.height;
caretPositionX = inputDimension.width - inputDimension.height;
} else {
// Getting some incorrect ~3 width with no text, so setting it 0 here
if (password.length === 0 || caretPosition.start === 0) {
caretPositionX = 0;
}
}
return {
width: withTiming(caretWidth),
// left: cursorX.value,
transform: [
{
translateX: withTiming(
caretPositionX,
{ duration: 250 },
finished => {
if (finished && !isInputFocused) {
tickState.value = 1;
}
},
),
},
],
opacity: cursorOpacity.value,
};
},
// , [cursorX, cursorOpacity]
);
const tickAnim = useAnimatedStyle(() => {
return {
opacity: withSpring(tickState.value),
transform: [{ scale: withSpring(tickState.value) }],
};
});
const caretFillStyle = useAnimatedStyle(() => {
const fillPercent = (password.length / MAX_LENGTH) * 100;
const caretFillHeight = (fillPercent / 100) * inputDimension.height;
return {
height: !isInputFocused ? inputDimension.height : caretFillHeight,
backgroundColor: !isInputFocused
? passwordStrength.strength < 5
? 'red'
: 'royalblue'
: PASS_COLORS[passwordStrength.strength],
};
});
const renderErrText = (
error: string,
valid: keyof typeof passwordStrength.validity,
) => {
const color =
isInputFocused || password.length === 0
? 'grey'
: passwordStrength.validity[valid]
? 'royalblue'
: 'red';
return <Text style={{ color: color, fontWeight: '600' }}>{error}</Text>;
};
return (
<SafeAreaView>
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.fieldTitle}>Password</Text>
<Text style={{ color: 'darkgrey' }}>
{password.length}/{MAX_LENGTH}
</Text>
</View>
<View>
<TextInput
style={styles.nameInput}
// autoFocus
// react-native can't seem to show/hide caret on real time properly :(
// caretHidden={caretPosition.start === name.length || name.length < MAX_LENGTH}
caretHidden
autoCapitalize="none"
// secureTextEntry
clearTextOnFocus={false}
value={password}
onChangeText={value => {
setPassword(value);
setPasswordStrength(checkpasswordStrength(value));
}}
onSelectionChange={e => setCaretPosition(e.nativeEvent.selection)}
onFocus={() => setInputFocused(true)}
onBlur={() => setInputFocused(false)}
onLayout={event => {
const layout = event.nativeEvent.layout;
const H_PAD = PAD * 2;
setInputDimension({
width: layout.width - H_PAD,
height: layout.height - H_PAD,
});
}}
ref={inputRef}
/>
<Text style={{ fontSize: 11, color: 'grey', marginTop: 4 }}>
Password must contain at least{' '}
{renderErrText('one small', 'lowercase')}
{' & '}
{renderErrText('one capital', 'uppercase')}
{' alphabet, '}
{renderErrText('one numeric digit', 'numeric')}
{', '}
{renderErrText('one Special character', 'specialChar')}
{' and be at least '}
{renderErrText(`${MAX_LENGTH} characters long`, 'minChar')}
</Text>
<Animated.View
style={[
styles.cursor,
{ height: inputDimension.height },
cursorStyle,
]}
>
<Animated.View style={[styles.tickContainer, caretFillStyle]}>
<Animated.Text style={[styles.tickUnicode, tickAnim]}>
{passwordStrength.strength === 5 ? '\u2713' : '\u2717'}
</Animated.Text>
</Animated.View>
</Animated.View>
</View>
<Pressable
style={({ pressed }) => [
styles.submitBtn,
{ opacity: pressed ? 0.4 : 1 },
]}
onPress={() => inputRef.current?.blur()}
>
<Text style={{ color: 'white', fontWeight: '700' }}>Submit</Text>
</Pressable>
</View>
<View style={{ position: 'absolute', opacity: 0 }} pointerEvents="none">
<TextInput
// secureTextEntry
value={password.substring(0, caretPosition.start)}
onLayout={event => {
// Update cursor position, shouldn't extend input total width
cursorX.value = Math.min(
event.nativeEvent.layout.width,
inputDimension.width,
);
}}
/>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
gap: 8,
},
header: {
flexDirection: 'row',
justifyContent: 'space-between',
paddingHorizontal: 16,
},
fieldTitle: {
color: 'grey',
fontWeight: '600',
},
nameInput: {
backgroundColor: 'white',
borderWidth: 0.5,
borderColor: 'lightgrey',
padding: PAD,
borderRadius: 12,
},
cursor: {
position: 'absolute',
backgroundColor: 'lightgrey',
borderRadius: 16,
margin: PAD,
overflow: 'hidden',
justifyContent: 'flex-end',
borderWidth: StyleSheet.hairlineWidth,
borderColor: 'lightgrey',
},
tickContainer: {
width: '100%',
backgroundColor: 'royalblue',
justifyContent: 'center',
alignItems: 'center',
},
tickUnicode: {
color: 'white',
fontSize: 11,
fontWeight: 'bold',
},
submitBtn: {
backgroundColor: 'royalblue',
padding: 12,
alignItems: 'center',
justifyContent: 'center',
borderRadius: 8,
marginTop: 8,
},
});
export default PasswordStrength;