|
| 1 | +import React, { useMemo } from 'react'; |
| 2 | +import { useTheme } from '../../contexts/ThemeContext'; |
| 3 | +import { THEMES } from '../../constants'; |
| 4 | +import { Check } from 'lucide-react'; |
| 5 | + |
| 6 | +interface PasswordStrengthProps { |
| 7 | + password?: string; |
| 8 | +} |
| 9 | + |
| 10 | +export const PasswordStrength: React.FC<PasswordStrengthProps> = ({ password = '' }) => { |
| 11 | + const { style } = useTheme(); |
| 12 | + |
| 13 | + const { score, label, metCriteria } = useMemo(() => { |
| 14 | + let s = 0; |
| 15 | + const criteria = { |
| 16 | + length: password.length >= 6, |
| 17 | + hasUpper: /[A-Z]/.test(password), |
| 18 | + hasLower: /[a-z]/.test(password), |
| 19 | + hasNumber: /[0-9]/.test(password), |
| 20 | + hasSpecial: /[^A-Za-z0-9]/.test(password), |
| 21 | + }; |
| 22 | + |
| 23 | + let l = 'Weak'; |
| 24 | + |
| 25 | + if (password.length > 0) { |
| 26 | + if (password.length < 6) { |
| 27 | + s = 1; // Very Weak |
| 28 | + l = 'Too Short'; |
| 29 | + } else { |
| 30 | + s = 1; |
| 31 | + // Bonus for length |
| 32 | + if (criteria.length) s++; |
| 33 | + |
| 34 | + // Bonus for complexity |
| 35 | + const complexity = (criteria.hasUpper ? 1 : 0) + |
| 36 | + (criteria.hasLower ? 1 : 0) + |
| 37 | + (criteria.hasNumber ? 1 : 0) + |
| 38 | + (criteria.hasSpecial ? 1 : 0); |
| 39 | + |
| 40 | + if (complexity >= 2) s++; |
| 41 | + if (complexity >= 4) s++; // Max bonus |
| 42 | + |
| 43 | + // Adjust label based on final score |
| 44 | + if (s >= 4) l = 'Strong'; |
| 45 | + else if (s === 3) l = 'Good'; |
| 46 | + else if (s === 2) l = 'Fair'; |
| 47 | + else l = 'Weak'; |
| 48 | + } |
| 49 | + } else { |
| 50 | + l = ''; |
| 51 | + } |
| 52 | + |
| 53 | + // Normalize score 0-4 |
| 54 | + if (s > 4) s = 4; |
| 55 | + |
| 56 | + return { score: s, label: l, metCriteria: criteria }; |
| 57 | + }, [password]); |
| 58 | + |
| 59 | + const getColor = (s: number) => { |
| 60 | + switch (s) { |
| 61 | + case 1: return 'bg-red-500'; |
| 62 | + case 2: return 'bg-orange-500'; |
| 63 | + case 3: return 'bg-yellow-500'; |
| 64 | + case 4: return 'bg-green-500'; |
| 65 | + default: return 'bg-gray-200 dark:bg-zinc-700'; |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + const isNeo = style === THEMES.NEOBRUTALISM; |
| 70 | + |
| 71 | + if (!password) return null; |
| 72 | + |
| 73 | + return ( |
| 74 | + <div className="w-full flex flex-col gap-2 mt-2" role="region" aria-label="Password strength indicator"> |
| 75 | + {/* Strength Bar */} |
| 76 | + <div className="flex w-full gap-1 h-2"> |
| 77 | + {[1, 2, 3, 4].map((level) => ( |
| 78 | + <div |
| 79 | + key={level} |
| 80 | + className={`flex-1 h-full transition-all duration-300 ${ |
| 81 | + score >= level |
| 82 | + ? getColor(score) |
| 83 | + : (isNeo ? 'bg-gray-200 dark:bg-zinc-800' : 'bg-white/10') |
| 84 | + } ${isNeo ? 'border-2 border-black' : 'rounded-full'}`} |
| 85 | + /> |
| 86 | + ))} |
| 87 | + </div> |
| 88 | + |
| 89 | + {/* Label and Criteria */} |
| 90 | + <div className="flex justify-between items-center h-4"> |
| 91 | + <span className={`text-xs font-bold transition-colors duration-300 ${ |
| 92 | + score === 4 ? 'text-green-600 dark:text-green-400' : |
| 93 | + score === 3 ? 'text-yellow-600 dark:text-yellow-400' : |
| 94 | + score === 2 ? 'text-orange-600 dark:text-orange-400' : |
| 95 | + 'text-red-600 dark:text-red-400' |
| 96 | + } ${isNeo ? 'uppercase tracking-wider' : ''}`}> |
| 97 | + {label} |
| 98 | + </span> |
| 99 | + </div> |
| 100 | + |
| 101 | + <div className="grid grid-cols-2 gap-y-1 gap-x-4 text-[10px] text-gray-500 dark:text-gray-400 transition-opacity duration-300"> |
| 102 | + <div className={`flex items-center gap-1.5 ${metCriteria.length ? 'text-green-600 dark:text-green-400 font-bold' : ''}`}> |
| 103 | + {metCriteria.length ? <Check size={12} strokeWidth={3} /> : <div className="w-3" />} |
| 104 | + 6+ characters |
| 105 | + </div> |
| 106 | + <div className={`flex items-center gap-1.5 ${(metCriteria.hasUpper && metCriteria.hasLower) ? 'text-green-600 dark:text-green-400 font-bold' : ''}`}> |
| 107 | + {(metCriteria.hasUpper && metCriteria.hasLower) ? <Check size={12} strokeWidth={3} /> : <div className="w-3" />} |
| 108 | + Mixed case |
| 109 | + </div> |
| 110 | + <div className={`flex items-center gap-1.5 ${metCriteria.hasNumber ? 'text-green-600 dark:text-green-400 font-bold' : ''}`}> |
| 111 | + {metCriteria.hasNumber ? <Check size={12} strokeWidth={3} /> : <div className="w-3" />} |
| 112 | + Number |
| 113 | + </div> |
| 114 | + <div className={`flex items-center gap-1.5 ${metCriteria.hasSpecial ? 'text-green-600 dark:text-green-400 font-bold' : ''}`}> |
| 115 | + {metCriteria.hasSpecial ? <Check size={12} strokeWidth={3} /> : <div className="w-3" />} |
| 116 | + Symbol |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + |
| 120 | + {/* Hidden live region for accessibility */} |
| 121 | + <output className="sr-only" aria-live="polite"> |
| 122 | + Password strength: {label}. |
| 123 | + {score < 4 && label ? "Add more characters, numbers, or symbols to strengthen." : ""} |
| 124 | + </output> |
| 125 | + </div> |
| 126 | + ); |
| 127 | +}; |
0 commit comments