@pwd-meter/react 2.0.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/dist/index.cjs ADDED
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ PasswordInput: () => PasswordInput,
24
+ PasswordStrengthMeter: () => PasswordStrengthMeter,
25
+ usePasswordGenerator: () => usePasswordGenerator,
26
+ usePasswordStrength: () => usePasswordStrength
27
+ });
28
+ module.exports = __toCommonJS(index_exports);
29
+
30
+ // src/PasswordStrengthMeter.tsx
31
+ var import_core2 = require("@pwd-meter/core");
32
+ var import_react2 = require("react");
33
+
34
+ // src/usePasswordStrength.ts
35
+ var import_react = require("react");
36
+ var import_core = require("@pwd-meter/core");
37
+ function usePasswordStrength(password, options = {}) {
38
+ const { checkPwned = false, debounceMs = 400, hibp } = options;
39
+ const base = (0, import_react.useMemo)(() => (0, import_core.analyzePassword)(password), [password]);
40
+ const [result, setResult] = (0, import_react.useState)(base);
41
+ (0, import_react.useEffect)(() => {
42
+ if (!checkPwned || !password) {
43
+ setResult(base);
44
+ return;
45
+ }
46
+ let cancelled = false;
47
+ setResult({ ...base, pwnedCheckPending: true });
48
+ const timer = window.setTimeout(async () => {
49
+ const next = await (0, import_core.analyzePasswordAsync)(password, { checkPwned: true, hibp });
50
+ if (!cancelled) {
51
+ setResult(next);
52
+ }
53
+ }, debounceMs);
54
+ return () => {
55
+ cancelled = true;
56
+ window.clearTimeout(timer);
57
+ };
58
+ }, [password, base, checkPwned, debounceMs, hibp]);
59
+ return result;
60
+ }
61
+ function usePasswordGenerator(defaultOptions) {
62
+ return (0, import_react.useMemo)(
63
+ () => ({
64
+ generate: (options) => (0, import_core.generateSecurePassword)({ ...defaultOptions, ...options })
65
+ }),
66
+ [defaultOptions]
67
+ );
68
+ }
69
+
70
+ // src/PasswordStrengthMeter.tsx
71
+ var import_jsx_runtime = require("react/jsx-runtime");
72
+ function PasswordStrengthMeter({
73
+ password,
74
+ result: externalResult,
75
+ showSuggestions = true,
76
+ showChecks = false,
77
+ checkPwned = false,
78
+ hibpOptions,
79
+ className = ""
80
+ }) {
81
+ const ownsAnalysis = externalResult === void 0;
82
+ const internalResult = usePasswordStrength(ownsAnalysis ? password : "", {
83
+ checkPwned: ownsAnalysis ? checkPwned : false,
84
+ hibp: ownsAnalysis ? hibpOptions : void 0
85
+ });
86
+ const result = externalResult ?? internalResult;
87
+ const width = password ? `${(result.score + 1) / 5 * 100}%` : "0%";
88
+ const color = (0, import_core2.getStrengthColor)(result.label);
89
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: `ps-meter ${className}`.trim(), "aria-live": "polite", children: [
90
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "ps-meter__track", "aria-hidden": "true", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "ps-meter__bar", style: { width, backgroundColor: color } }) }),
91
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { className: "ps-meter__message", children: (0, import_core2.getStrengthMessage)(result) }),
92
+ result.crackTimeDisplay && password && !result.isPwned && !result.pwnedCheckPending && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", { className: "ps-meter__meta", children: [
93
+ "Estimated crack time: ",
94
+ result.crackTimeDisplay
95
+ ] }),
96
+ showChecks && password && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("ul", { className: "ps-meter__checks", children: [
97
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("li", { "data-pass": result.checks.length, children: "At least 12 characters" }),
98
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("li", { "data-pass": result.checks.lowercase, children: "Lowercase letter" }),
99
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("li", { "data-pass": result.checks.uppercase, children: "Uppercase letter" }),
100
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("li", { "data-pass": result.checks.number, children: "Number" }),
101
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("li", { "data-pass": result.checks.symbol, children: "Symbol" })
102
+ ] }),
103
+ showSuggestions && (result.feedback.warning || result.feedback.suggestions.length > 0) && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "ps-meter__feedback", children: [
104
+ result.feedback.warning && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { className: "ps-meter__warning", children: result.feedback.warning }),
105
+ result.feedback.suggestions.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("ul", { children: result.feedback.suggestions.map((item) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("li", { children: item }, item)) })
106
+ ] })
107
+ ] });
108
+ }
109
+ function PasswordInput({
110
+ value,
111
+ defaultValue = "",
112
+ onChange,
113
+ placeholder = "Enter password",
114
+ showMeter = true,
115
+ showGenerate = true,
116
+ checkPwned = false,
117
+ hibpOptions,
118
+ generateOptions,
119
+ className = "",
120
+ inputClassName = ""
121
+ }) {
122
+ const [internalValue, setInternalValue] = (0, import_react2.useState)(defaultValue);
123
+ const currentValue = value ?? internalValue;
124
+ const setValue = (next) => {
125
+ if (value === void 0) setInternalValue(next);
126
+ onChange?.(next);
127
+ };
128
+ const result = usePasswordStrength(currentValue, { checkPwned, hibp: hibpOptions });
129
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: `ps-input ${className}`.trim(), children: [
130
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "ps-input__row", children: [
131
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
132
+ "input",
133
+ {
134
+ type: "password",
135
+ className: `ps-input__field ${inputClassName}`.trim(),
136
+ value: currentValue,
137
+ placeholder,
138
+ onChange: (event) => setValue(event.target.value),
139
+ "aria-describedby": "password-strength-message"
140
+ }
141
+ ),
142
+ showGenerate && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
143
+ "button",
144
+ {
145
+ type: "button",
146
+ className: "ps-input__generate",
147
+ onClick: () => setValue((0, import_core2.generateSecurePassword)(generateOptions)),
148
+ children: "Generate"
149
+ }
150
+ )
151
+ ] }),
152
+ showMeter && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PasswordStrengthMeter, { password: currentValue, result }),
153
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { id: "password-strength-message", className: "ps-sr-only", children: (0, import_core2.getStrengthMessage)(result) })
154
+ ] });
155
+ }
156
+ // Annotate the CommonJS export names for ESM import in node:
157
+ 0 && (module.exports = {
158
+ PasswordInput,
159
+ PasswordStrengthMeter,
160
+ usePasswordGenerator,
161
+ usePasswordStrength
162
+ });
@@ -0,0 +1,38 @@
1
+ import * as react from 'react';
2
+ import { AnalyzePasswordOptions, GeneratePasswordOptions, PasswordStrengthResult } from '@pwd-meter/core';
3
+ export { AnalyzePasswordOptions, GeneratePasswordOptions, HibpOptions, PasswordStrengthResult, PwnedPasswordResult, StrengthLabel } from '@pwd-meter/core';
4
+
5
+ type UsePasswordStrengthOptions = AnalyzePasswordOptions & {
6
+ debounceMs?: number;
7
+ };
8
+ declare function usePasswordStrength(password: string, options?: UsePasswordStrengthOptions): PasswordStrengthResult;
9
+ declare function usePasswordGenerator(defaultOptions?: GeneratePasswordOptions): {
10
+ generate: (options?: GeneratePasswordOptions) => string;
11
+ };
12
+
13
+ type PasswordStrengthMeterProps = {
14
+ password: string;
15
+ result?: PasswordStrengthResult;
16
+ showSuggestions?: boolean;
17
+ showChecks?: boolean;
18
+ checkPwned?: boolean;
19
+ hibpOptions?: UsePasswordStrengthOptions["hibp"];
20
+ className?: string;
21
+ };
22
+ declare function PasswordStrengthMeter({ password, result: externalResult, showSuggestions, showChecks, checkPwned, hibpOptions, className, }: PasswordStrengthMeterProps): react.JSX.Element;
23
+ type PasswordInputProps = {
24
+ value?: string;
25
+ defaultValue?: string;
26
+ onChange?: (value: string) => void;
27
+ placeholder?: string;
28
+ showMeter?: boolean;
29
+ showGenerate?: boolean;
30
+ checkPwned?: boolean;
31
+ hibpOptions?: UsePasswordStrengthOptions["hibp"];
32
+ generateOptions?: GeneratePasswordOptions;
33
+ className?: string;
34
+ inputClassName?: string;
35
+ };
36
+ declare function PasswordInput({ value, defaultValue, onChange, placeholder, showMeter, showGenerate, checkPwned, hibpOptions, generateOptions, className, inputClassName, }: PasswordInputProps): react.JSX.Element;
37
+
38
+ export { PasswordInput, type PasswordInputProps, PasswordStrengthMeter, type PasswordStrengthMeterProps, type UsePasswordStrengthOptions, usePasswordGenerator, usePasswordStrength };
@@ -0,0 +1,38 @@
1
+ import * as react from 'react';
2
+ import { AnalyzePasswordOptions, GeneratePasswordOptions, PasswordStrengthResult } from '@pwd-meter/core';
3
+ export { AnalyzePasswordOptions, GeneratePasswordOptions, HibpOptions, PasswordStrengthResult, PwnedPasswordResult, StrengthLabel } from '@pwd-meter/core';
4
+
5
+ type UsePasswordStrengthOptions = AnalyzePasswordOptions & {
6
+ debounceMs?: number;
7
+ };
8
+ declare function usePasswordStrength(password: string, options?: UsePasswordStrengthOptions): PasswordStrengthResult;
9
+ declare function usePasswordGenerator(defaultOptions?: GeneratePasswordOptions): {
10
+ generate: (options?: GeneratePasswordOptions) => string;
11
+ };
12
+
13
+ type PasswordStrengthMeterProps = {
14
+ password: string;
15
+ result?: PasswordStrengthResult;
16
+ showSuggestions?: boolean;
17
+ showChecks?: boolean;
18
+ checkPwned?: boolean;
19
+ hibpOptions?: UsePasswordStrengthOptions["hibp"];
20
+ className?: string;
21
+ };
22
+ declare function PasswordStrengthMeter({ password, result: externalResult, showSuggestions, showChecks, checkPwned, hibpOptions, className, }: PasswordStrengthMeterProps): react.JSX.Element;
23
+ type PasswordInputProps = {
24
+ value?: string;
25
+ defaultValue?: string;
26
+ onChange?: (value: string) => void;
27
+ placeholder?: string;
28
+ showMeter?: boolean;
29
+ showGenerate?: boolean;
30
+ checkPwned?: boolean;
31
+ hibpOptions?: UsePasswordStrengthOptions["hibp"];
32
+ generateOptions?: GeneratePasswordOptions;
33
+ className?: string;
34
+ inputClassName?: string;
35
+ };
36
+ declare function PasswordInput({ value, defaultValue, onChange, placeholder, showMeter, showGenerate, checkPwned, hibpOptions, generateOptions, className, inputClassName, }: PasswordInputProps): react.JSX.Element;
37
+
38
+ export { PasswordInput, type PasswordInputProps, PasswordStrengthMeter, type PasswordStrengthMeterProps, type UsePasswordStrengthOptions, usePasswordGenerator, usePasswordStrength };
package/dist/index.js ADDED
@@ -0,0 +1,141 @@
1
+ // src/PasswordStrengthMeter.tsx
2
+ import {
3
+ generateSecurePassword as generateSecurePassword2,
4
+ getStrengthColor,
5
+ getStrengthMessage
6
+ } from "@pwd-meter/core";
7
+ import { useState as useState2 } from "react";
8
+
9
+ // src/usePasswordStrength.ts
10
+ import { useEffect, useMemo, useState } from "react";
11
+ import {
12
+ analyzePassword,
13
+ analyzePasswordAsync,
14
+ checkPwnedPassword,
15
+ generateSecurePassword
16
+ } from "@pwd-meter/core";
17
+ function usePasswordStrength(password, options = {}) {
18
+ const { checkPwned = false, debounceMs = 400, hibp } = options;
19
+ const base = useMemo(() => analyzePassword(password), [password]);
20
+ const [result, setResult] = useState(base);
21
+ useEffect(() => {
22
+ if (!checkPwned || !password) {
23
+ setResult(base);
24
+ return;
25
+ }
26
+ let cancelled = false;
27
+ setResult({ ...base, pwnedCheckPending: true });
28
+ const timer = window.setTimeout(async () => {
29
+ const next = await analyzePasswordAsync(password, { checkPwned: true, hibp });
30
+ if (!cancelled) {
31
+ setResult(next);
32
+ }
33
+ }, debounceMs);
34
+ return () => {
35
+ cancelled = true;
36
+ window.clearTimeout(timer);
37
+ };
38
+ }, [password, base, checkPwned, debounceMs, hibp]);
39
+ return result;
40
+ }
41
+ function usePasswordGenerator(defaultOptions) {
42
+ return useMemo(
43
+ () => ({
44
+ generate: (options) => generateSecurePassword({ ...defaultOptions, ...options })
45
+ }),
46
+ [defaultOptions]
47
+ );
48
+ }
49
+
50
+ // src/PasswordStrengthMeter.tsx
51
+ import { jsx, jsxs } from "react/jsx-runtime";
52
+ function PasswordStrengthMeter({
53
+ password,
54
+ result: externalResult,
55
+ showSuggestions = true,
56
+ showChecks = false,
57
+ checkPwned = false,
58
+ hibpOptions,
59
+ className = ""
60
+ }) {
61
+ const ownsAnalysis = externalResult === void 0;
62
+ const internalResult = usePasswordStrength(ownsAnalysis ? password : "", {
63
+ checkPwned: ownsAnalysis ? checkPwned : false,
64
+ hibp: ownsAnalysis ? hibpOptions : void 0
65
+ });
66
+ const result = externalResult ?? internalResult;
67
+ const width = password ? `${(result.score + 1) / 5 * 100}%` : "0%";
68
+ const color = getStrengthColor(result.label);
69
+ return /* @__PURE__ */ jsxs("div", { className: `ps-meter ${className}`.trim(), "aria-live": "polite", children: [
70
+ /* @__PURE__ */ jsx("div", { className: "ps-meter__track", "aria-hidden": "true", children: /* @__PURE__ */ jsx("div", { className: "ps-meter__bar", style: { width, backgroundColor: color } }) }),
71
+ /* @__PURE__ */ jsx("p", { className: "ps-meter__message", children: getStrengthMessage(result) }),
72
+ result.crackTimeDisplay && password && !result.isPwned && !result.pwnedCheckPending && /* @__PURE__ */ jsxs("p", { className: "ps-meter__meta", children: [
73
+ "Estimated crack time: ",
74
+ result.crackTimeDisplay
75
+ ] }),
76
+ showChecks && password && /* @__PURE__ */ jsxs("ul", { className: "ps-meter__checks", children: [
77
+ /* @__PURE__ */ jsx("li", { "data-pass": result.checks.length, children: "At least 12 characters" }),
78
+ /* @__PURE__ */ jsx("li", { "data-pass": result.checks.lowercase, children: "Lowercase letter" }),
79
+ /* @__PURE__ */ jsx("li", { "data-pass": result.checks.uppercase, children: "Uppercase letter" }),
80
+ /* @__PURE__ */ jsx("li", { "data-pass": result.checks.number, children: "Number" }),
81
+ /* @__PURE__ */ jsx("li", { "data-pass": result.checks.symbol, children: "Symbol" })
82
+ ] }),
83
+ showSuggestions && (result.feedback.warning || result.feedback.suggestions.length > 0) && /* @__PURE__ */ jsxs("div", { className: "ps-meter__feedback", children: [
84
+ result.feedback.warning && /* @__PURE__ */ jsx("p", { className: "ps-meter__warning", children: result.feedback.warning }),
85
+ result.feedback.suggestions.length > 0 && /* @__PURE__ */ jsx("ul", { children: result.feedback.suggestions.map((item) => /* @__PURE__ */ jsx("li", { children: item }, item)) })
86
+ ] })
87
+ ] });
88
+ }
89
+ function PasswordInput({
90
+ value,
91
+ defaultValue = "",
92
+ onChange,
93
+ placeholder = "Enter password",
94
+ showMeter = true,
95
+ showGenerate = true,
96
+ checkPwned = false,
97
+ hibpOptions,
98
+ generateOptions,
99
+ className = "",
100
+ inputClassName = ""
101
+ }) {
102
+ const [internalValue, setInternalValue] = useState2(defaultValue);
103
+ const currentValue = value ?? internalValue;
104
+ const setValue = (next) => {
105
+ if (value === void 0) setInternalValue(next);
106
+ onChange?.(next);
107
+ };
108
+ const result = usePasswordStrength(currentValue, { checkPwned, hibp: hibpOptions });
109
+ return /* @__PURE__ */ jsxs("div", { className: `ps-input ${className}`.trim(), children: [
110
+ /* @__PURE__ */ jsxs("div", { className: "ps-input__row", children: [
111
+ /* @__PURE__ */ jsx(
112
+ "input",
113
+ {
114
+ type: "password",
115
+ className: `ps-input__field ${inputClassName}`.trim(),
116
+ value: currentValue,
117
+ placeholder,
118
+ onChange: (event) => setValue(event.target.value),
119
+ "aria-describedby": "password-strength-message"
120
+ }
121
+ ),
122
+ showGenerate && /* @__PURE__ */ jsx(
123
+ "button",
124
+ {
125
+ type: "button",
126
+ className: "ps-input__generate",
127
+ onClick: () => setValue(generateSecurePassword2(generateOptions)),
128
+ children: "Generate"
129
+ }
130
+ )
131
+ ] }),
132
+ showMeter && /* @__PURE__ */ jsx(PasswordStrengthMeter, { password: currentValue, result }),
133
+ /* @__PURE__ */ jsx("span", { id: "password-strength-message", className: "ps-sr-only", children: getStrengthMessage(result) })
134
+ ] });
135
+ }
136
+ export {
137
+ PasswordInput,
138
+ PasswordStrengthMeter,
139
+ usePasswordGenerator,
140
+ usePasswordStrength
141
+ };
@@ -0,0 +1,82 @@
1
+ .ps-meter,
2
+ .ps-input {
3
+ font-family: Inter, ui-sans-serif, system-ui, sans-serif;
4
+ color: #0f172a;
5
+ }
6
+
7
+ .ps-input__row {
8
+ display: flex;
9
+ gap: 8px;
10
+ }
11
+
12
+ .ps-input__field {
13
+ flex: 1;
14
+ min-height: 42px;
15
+ border: 1px solid #cbd5e1;
16
+ border-radius: 8px;
17
+ padding: 0 12px;
18
+ }
19
+
20
+ .ps-input__generate {
21
+ min-height: 42px;
22
+ border: 0;
23
+ border-radius: 8px;
24
+ padding: 0 14px;
25
+ color: #fff;
26
+ background: #2563eb;
27
+ cursor: pointer;
28
+ }
29
+
30
+ .ps-meter {
31
+ margin-top: 10px;
32
+ }
33
+
34
+ .ps-meter__track {
35
+ height: 8px;
36
+ border-radius: 999px;
37
+ background: #e2e8f0;
38
+ overflow: hidden;
39
+ }
40
+
41
+ .ps-meter__bar {
42
+ height: 100%;
43
+ border-radius: inherit;
44
+ transition: width 0.2s ease, background-color 0.2s ease;
45
+ }
46
+
47
+ .ps-meter__message {
48
+ margin: 8px 0 0;
49
+ font-size: 0.92rem;
50
+ font-weight: 600;
51
+ }
52
+
53
+ .ps-meter__meta,
54
+ .ps-meter__warning,
55
+ .ps-meter__feedback {
56
+ margin: 6px 0 0;
57
+ color: #475569;
58
+ font-size: 0.86rem;
59
+ }
60
+
61
+ .ps-meter__checks {
62
+ margin: 8px 0 0;
63
+ padding-left: 18px;
64
+ color: #64748b;
65
+ font-size: 0.84rem;
66
+ }
67
+
68
+ .ps-meter__checks li[data-pass="true"] {
69
+ color: #15803d;
70
+ }
71
+
72
+ .ps-sr-only {
73
+ position: absolute;
74
+ width: 1px;
75
+ height: 1px;
76
+ padding: 0;
77
+ margin: -1px;
78
+ overflow: hidden;
79
+ clip: rect(0, 0, 0, 0);
80
+ white-space: nowrap;
81
+ border: 0;
82
+ }
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@pwd-meter/react",
3
+ "version": "2.0.0",
4
+ "description": "React hook and UI components for password strength checking.",
5
+ "license": "MIT",
6
+ "author": "Alen Joy <alenjoy333@gmail.com>",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/alenjoy333/Password-Strength-Checker.git",
10
+ "directory": "packages/react"
11
+ },
12
+ "keywords": ["password", "react", "hook", "security"],
13
+ "type": "module",
14
+ "main": "./dist/index.cjs",
15
+ "module": "./dist/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "import": "./dist/index.js",
21
+ "require": "./dist/index.cjs"
22
+ },
23
+ "./styles.css": "./dist/styles.css"
24
+ },
25
+ "files": ["dist"],
26
+ "sideEffects": ["**/*.css"],
27
+ "scripts": {
28
+ "build": "tsup src/index.ts --format esm,cjs --dts --clean && cp src/styles.css dist/styles.css",
29
+ "clean": "rm -rf dist"
30
+ },
31
+ "dependencies": {
32
+ "@pwd-meter/core": "2.0.0"
33
+ },
34
+ "peerDependencies": {
35
+ "react": ">=18"
36
+ },
37
+ "devDependencies": {
38
+ "@types/react": "^19.1.8",
39
+ "react": "^19.1.0",
40
+ "tsup": "^8.5.0",
41
+ "typescript": "^5.8.3"
42
+ }
43
+ }