@purpurds/password-field 6.4.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 (43) hide show
  1. package/dist/LICENSE.txt +92 -0
  2. package/dist/constants.d.ts +10 -0
  3. package/dist/constants.d.ts.map +1 -0
  4. package/dist/main-B6vgbdqW.mjs +1002 -0
  5. package/dist/main-B6vgbdqW.mjs.map +1 -0
  6. package/dist/main-CqBhSgIE.js +2 -0
  7. package/dist/main-CqBhSgIE.js.map +1 -0
  8. package/dist/metadata.js +9 -0
  9. package/dist/password-field-BwuKVABM.mjs +1557 -0
  10. package/dist/password-field-BwuKVABM.mjs.map +1 -0
  11. package/dist/password-field-CE2wn3y4.js +58 -0
  12. package/dist/password-field-CE2wn3y4.js.map +1 -0
  13. package/dist/password-field.cjs.js +2 -0
  14. package/dist/password-field.cjs.js.map +1 -0
  15. package/dist/password-field.d.ts +27 -0
  16. package/dist/password-field.d.ts.map +1 -0
  17. package/dist/password-field.es.js +8 -0
  18. package/dist/password-field.es.js.map +1 -0
  19. package/dist/password-strength-indicator.d.ts +11 -0
  20. package/dist/password-strength-indicator.d.ts.map +1 -0
  21. package/dist/password-strength.d.ts +13 -0
  22. package/dist/password-strength.d.ts.map +1 -0
  23. package/dist/styles.css +1 -0
  24. package/dist/types.d.ts +7 -0
  25. package/dist/types.d.ts.map +1 -0
  26. package/dist/use-password-strength.d.ts +10 -0
  27. package/dist/use-password-strength.d.ts.map +1 -0
  28. package/package.json +69 -0
  29. package/src/constants.ts +10 -0
  30. package/src/global.d.ts +4 -0
  31. package/src/password-field.module.scss +5 -0
  32. package/src/password-field.stories.tsx +273 -0
  33. package/src/password-field.story.css +23 -0
  34. package/src/password-field.test.tsx +69 -0
  35. package/src/password-field.tsx +128 -0
  36. package/src/password-strength-indicator.module.scss +19 -0
  37. package/src/password-strength-indicator.test.tsx +46 -0
  38. package/src/password-strength-indicator.tsx +38 -0
  39. package/src/password-strength.module.scss +30 -0
  40. package/src/password-strength.test.tsx +52 -0
  41. package/src/password-strength.tsx +79 -0
  42. package/src/types.ts +7 -0
  43. package/src/use-password-strength.ts +61 -0
@@ -0,0 +1,79 @@
1
+ import React, { ForwardedRef, forwardRef } from "react";
2
+ import { Paragraph } from "@purpurds/paragraph";
3
+ import c from "classnames/bind";
4
+
5
+ import styles from "./password-strength.module.scss";
6
+ import { PasswordStrengthIndicator } from "./password-strength-indicator";
7
+ import { usePasswordStrength } from "./use-password-strength";
8
+ const cx = c.bind(styles);
9
+
10
+ export type PasswordStrengthProps = {
11
+ ["data-testid"]?: string;
12
+ className?: string;
13
+ value: string;
14
+ label: string;
15
+ weakText: string;
16
+ mediumText: string;
17
+ strongText: string;
18
+ };
19
+
20
+ const rootClassName = "purpur-password-strength";
21
+
22
+ export const PasswordStrength = forwardRef(
23
+ (
24
+ {
25
+ ["data-testid"]: dataTestId = "password-strength",
26
+ className,
27
+ value,
28
+ label,
29
+ weakText,
30
+ mediumText,
31
+ strongText,
32
+ ...props
33
+ }: PasswordStrengthProps,
34
+ ref: ForwardedRef<HTMLDivElement>
35
+ ) => {
36
+ const classes = cx([className, rootClassName]);
37
+ const { score, passwordStrength, currentPasswordStrengthText } = usePasswordStrength(
38
+ value,
39
+ weakText,
40
+ mediumText,
41
+ strongText
42
+ );
43
+
44
+ return (
45
+ <div data-testid={dataTestId} className={classes} ref={ref} {...props}>
46
+ <div className={cx(`${rootClassName}__indicator-container`)}>
47
+ <PasswordStrengthIndicator
48
+ active={!!passwordStrength}
49
+ passwordStrength={passwordStrength}
50
+ />
51
+ <PasswordStrengthIndicator
52
+ active={!!passwordStrength && score > 1}
53
+ passwordStrength={passwordStrength}
54
+ />
55
+ <PasswordStrengthIndicator
56
+ active={!!passwordStrength && score > 2}
57
+ passwordStrength={passwordStrength}
58
+ />
59
+ </div>
60
+ <Paragraph
61
+ aria-live="polite"
62
+ data-testid={`${dataTestId}-text`}
63
+ className={cx([
64
+ `${rootClassName}__password-strength-text`,
65
+ { [`${rootClassName}__password-strength-text--${passwordStrength}`]: passwordStrength },
66
+ ])}
67
+ >
68
+ <span className={cx(`${rootClassName}__password-strength-label`)}>
69
+ {label}
70
+ {currentPasswordStrengthText ? ": " : ""}
71
+ </span>
72
+ {currentPasswordStrengthText}
73
+ </Paragraph>
74
+ </div>
75
+ );
76
+ }
77
+ );
78
+
79
+ PasswordStrength.displayName = "PasswordStrength";
package/src/types.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { FIELD_TYPE, PASSWORD_STRENGTH } from "./constants";
2
+
3
+ export type PasswordStrengthValue = (typeof PASSWORD_STRENGTH)[keyof typeof PASSWORD_STRENGTH];
4
+ export type FieldType = (typeof FIELD_TYPE)[keyof typeof FIELD_TYPE];
5
+ export type PasswordStrengthMapValue = [PasswordStrengthValue, string];
6
+
7
+ export type PasswordScore = 0 | 1 | 2 | 3 | 4;
@@ -0,0 +1,61 @@
1
+ import { useEffect, useMemo, useState } from "react";
2
+
3
+ import { PASSWORD_STRENGTH } from "./constants";
4
+ import {
5
+ type PasswordScore,
6
+ type PasswordStrengthMapValue,
7
+ type PasswordStrengthValue,
8
+ } from "./types";
9
+
10
+ type UsePasswordStrengthHook = {
11
+ score: number;
12
+ passwordStrength: PasswordStrengthValue | undefined;
13
+ currentPasswordStrengthText: string | undefined;
14
+ };
15
+
16
+ export const usePasswordStrength = (
17
+ password: string,
18
+ weakText: string,
19
+ mediumText: string,
20
+ strongText: string
21
+ ): UsePasswordStrengthHook => {
22
+ const [score, setScore] = useState<PasswordScore>(0);
23
+ const [passwordStrength, setPasswordStrength] = useState<PasswordStrengthValue | undefined>();
24
+ const [currentPasswordStrengthText, setCurrentPasswordStrengthText] = useState<
25
+ string | undefined
26
+ >(undefined);
27
+
28
+ const passwordStrengthMap: Record<PasswordScore, PasswordStrengthMapValue> = useMemo(
29
+ () => ({
30
+ 0: [PASSWORD_STRENGTH.WEAK, weakText],
31
+ 1: [PASSWORD_STRENGTH.WEAK, weakText],
32
+ 2: [PASSWORD_STRENGTH.MEDIUM, mediumText],
33
+ 3: [PASSWORD_STRENGTH.STRONG, strongText],
34
+ 4: [PASSWORD_STRENGTH.STRONG, strongText],
35
+ }),
36
+ [weakText, mediumText, strongText]
37
+ );
38
+
39
+ useEffect(() => {
40
+ (async () => {
41
+ if (!password) {
42
+ setScore(0);
43
+ setPasswordStrength(undefined);
44
+ setCurrentPasswordStrengthText(undefined);
45
+ } else {
46
+ const { default: zxcvbn } = await import("zxcvbn");
47
+ const { score } = zxcvbn(password);
48
+ const [passwordStrength, passwordStrengthText] = passwordStrengthMap[score];
49
+ setScore(score);
50
+ setPasswordStrength(passwordStrength);
51
+ setCurrentPasswordStrengthText(passwordStrengthText);
52
+ }
53
+ })();
54
+ }, [password, passwordStrengthMap]);
55
+
56
+ return {
57
+ score,
58
+ passwordStrength,
59
+ currentPasswordStrengthText,
60
+ };
61
+ };