diff --git a/components/input/react/password-strength-meter/README.md b/components/input/react/password-strength-meter/README.md new file mode 100644 index 0000000..01bdc77 --- /dev/null +++ b/components/input/react/password-strength-meter/README.md @@ -0,0 +1,30 @@ +# PasswordStrengthMeter + +Modern and responsive password strength meter built with React, Tailwind CSS. It evaluates password strength based on length, numbers, symbols, and uppercase characters. + +## Features + +* Real-time password strength detection +* Strength indicator (Weak, Moderate, Strong, Very Strong) +* Fully responsive and reusable component + + +## Usage + +```jsx +import PasswordStrengthMeter from "./PasswordStrengthMeter"; + +export default function App() { + return ( +
+ console.log(value, strength)} /> +
+ ); +} +``` + +## Props + +| Prop Name | Type | Default | Description | +| ----------- | -------- | ------------ | ---------------------------------------------- | +| `onChange` | `func` | `() => {}` | Callback triggered whenever password changes. | diff --git a/components/input/react/password-strength-meter/component.json b/components/input/react/password-strength-meter/component.json new file mode 100644 index 0000000..9767e5b --- /dev/null +++ b/components/input/react/password-strength-meter/component.json @@ -0,0 +1,17 @@ +{ + "name": "password-strength-meter", + "framework": "react", + "category": "input", + "tags": ["password", "input", "strength", "validation", "security"], + "author": "Sachin K.C.", + "license": "MIT", + "version": "1.0.0", + "preview": "Password strength meter component with requirement checklist, and show/hide toggle for secure password creation.", + "props": [ + { + "name": "onChange", + "type": "function", + "description": "Callback function that receives password value and strength label (Weak, Fair, Good, Strong)" + } + ] +} diff --git a/components/input/react/password-strength-meter/password-strength-meter.jsx b/components/input/react/password-strength-meter/password-strength-meter.jsx new file mode 100644 index 0000000..291baa8 --- /dev/null +++ b/components/input/react/password-strength-meter/password-strength-meter.jsx @@ -0,0 +1,122 @@ +"use client"; +import React, { useState } from "react"; +import { AiOutlineEye, AiOutlineEyeInvisible } from "react-icons/ai"; +import { IoCheckmark, IoClose } from "react-icons/io5"; + +const PasswordStrengthMeter = ({ onChange }) => { + const [password, setPassword] = useState(""); + const [showPassword, setShowPassword] = useState(false); + const [strength, setStrength] = useState({ label: "", score: 0 }); + + const requirements = [ + { id: 1, text: "At least 8 characters", test: (val) => val.length >= 8 }, + { id: 2, text: "One uppercase letter", test: (val) => /[A-Z]/.test(val) }, + { id: 3, text: "One number", test: (val) => /[0-9]/.test(val) }, + { id: 4, text: "One special character", test: (val) => /[^A-Za-z0-9]/.test(val) }, + ]; + + const evaluate = (val) => { + if (!val) return { label: "", score: 0, color: "" }; + + let score = 0; + requirements.forEach((req) => { + if (req.test(val)) score++; + }); + + const strengthMap = { + 1: { label: "Weak", color: "text-red-600", bgColor: "bg-red-50", borderColor: "border-red-200" }, + 2: { label: "Fair", color: "text-orange-600", bgColor: "bg-orange-50", borderColor: "border-orange-200" }, + 3: { label: "Good", color: "text-blue-600", bgColor: "bg-blue-50", borderColor: "border-blue-200" }, + 4: { label: "Strong", color: "text-green-600", bgColor: "bg-green-50", borderColor: "border-green-200" }, + }; + + return { ...strengthMap[score], score }; + }; + + const handleChange = (e) => { + const val = e.target.value; + setPassword(val); + const s = evaluate(val); + setStrength(s); + if (onChange) onChange(val, s.label); + }; + + return ( +
+
+
+ +
+ + +
+
+ + {password && ( +
+
+
+ + Password strength + + + {strength.label} + +
+
+ +
+

+ Password must contain: +

+
+ {requirements.map((req) => { + const isMet = req.test(password); + return ( +
+
+ {isMet ? : } +
+ + {req.text} + +
+ ); + })} +
+
+
+ )} +
+
+ ); +}; + +export default PasswordStrengthMeter; \ No newline at end of file