Files
WRNexusJS/packages/ui/components/StrongPassword.wrn
2026-08-01 01:09:58 +05:30

219 lines
8.8 KiB
Plaintext

component StrongPassword {
outputs {
input(payload: { value: string; score: number; maximumScore: number; percent: number; level: string })
change(payload: { value: string; score: number; maximumScore: number; percent: number; level: string })
strength(payload: { value: string; score: number; maximumScore: number; percent: number; level: string })
}
props {
size: string = "default"
color: string = "primary"
label: string = "Password"
name: string = "password"
value: string = ""
placeholder: string = "Create a strong password"
autocomplete: string = "new-password"
minLength: number = 8
specialCharactersSet: string = "!@#$%^&*()_+-=[]{}|;:,.<>?"
requireLowercase: boolean = true
requireUppercase: boolean = true
requireNumber: boolean = true
requireSpecialCharacter: boolean = true
showRequirements: boolean = true
presentation: string = "inline"
hintText: string = "Use a unique password you do not use elsewhere."
emptyLabel: string = "Enter a password"
weakLabel: string = "Weak"
fairLabel: string = "Fair"
goodLabel: string = "Good"
strongLabel: string = "Strong"
disabled: boolean = false
readonly: boolean = false
required: boolean = false
invalid: boolean = false
validationMessage: string = ""
class: string = ""
}
state password = value
state detailsOpen: boolean = false
functions {
shared function hasMinimumLength() {
return password.length >= Number(minLength)
}
shared function hasLowercase() {
return password !== password.toUpperCase()
}
shared function hasUppercase() {
return password !== password.toLowerCase()
}
shared function hasNumber() {
return containsCharacterAt("0123456789", 0)
}
shared function hasSpecialCharacter() {
return containsCharacterAt(specialCharactersSet, 0)
}
shared function containsCharacterAt(characters, index) {
if (index >= characters.length) {
return false
}
if (password.includes(characters[index])) {
return true
}
return containsCharacterAt(characters, index + 1)
}
shared function score() {
return (hasMinimumLength() ? 1 : 0) + (requireLowercase && hasLowercase() ? 1 : 0) + (requireUppercase && hasUppercase() ? 1 : 0) + (requireNumber && hasNumber() ? 1 : 0) + (requireSpecialCharacter && hasSpecialCharacter() ? 1 : 0)
}
shared function maximumScore() {
return 1 + (requireLowercase ? 1 : 0) + (requireUppercase ? 1 : 0) + (requireNumber ? 1 : 0) + (requireSpecialCharacter ? 1 : 0)
}
shared function strengthPercent() {
return Math.round(score() / maximumScore() * 100)
}
shared function strengthLevel() {
if (!password) {
return "empty"
}
if (strengthPercent() <= 25) {
return "weak"
}
if (strengthPercent() <= 50) {
return "fair"
}
if (strengthPercent() < 100) {
return "good"
}
return "strong"
}
shared function strengthLabel() {
if (strengthLevel() === "weak") {
return weakLabel
}
if (strengthLevel() === "fair") {
return fairLabel
}
if (strengthLevel() === "good") {
return goodLabel
}
if (strengthLevel() === "strong") {
return strongLabel
}
return emptyLabel
}
}
view {
<div
class="wire-next wire-next--color-{color} wire-next--size-{size} wire-next--strong-password wire-next--strong-password-{presentation} {invalid ? 'wire-next--invalid' : ''} {disabled ? 'wire-next--disabled' : ''} {class}"
data-wrn-strong-password
data-strength="{password ? strengthLevel() : 'empty'}"
data-score="{password ? score() : 0}"
>
<label for="{name}-strong-password">{label}</label>
<div class="wire-next__strong-password-anchor">
<input
{...attrs}
id="{name}-strong-password"
type="password"
name="{name}"
value="{password}"
placeholder="{placeholder}"
autocomplete="{autocomplete}"
minlength="{minLength}"
disabled="{disabled}"
readonly="{readonly}"
required="{required}"
aria-invalid="{invalid ? 'true' : 'false'}"
aria-describedby="{validationMessage ? name + '-validation' : hintText ? name + '-hint' : ''}"
@focus="detailsOpen = true"
@blur="detailsOpen = false"
@input="password = event.target.value; output.input({ value: password, score: score(), maximumScore: maximumScore(), percent: strengthPercent(), level: strengthLevel() }); output.strength({ value: password, score: score(), maximumScore: maximumScore(), percent: strengthPercent(), level: strengthLevel() })"
@change="output.change({ value: password, score: score(), maximumScore: maximumScore(), percent: strengthPercent(), level: strengthLevel() })"
/>
{#if presentation === "popover"}
<div
class="wire-next__strong-password-popover"
data-open="{detailsOpen ? 'true' : 'false'}"
role="status"
>
<div class="wire-next__strong-password-popover-arrow" aria-hidden="true"></div>
<div class="wire-next__strong-password-details">
<div class="wire-next__strong-password-summary">
<span>Password strength</span>
<strong>{password ? strengthLabel() : emptyLabel}</strong>
</div>
<div
class="wire-next__strong-password-meter"
role="progressbar"
aria-label="Password strength"
aria-valuemin="0"
aria-valuemax="100"
aria-valuenow="{password ? strengthPercent() : 0}"
>
<span style="width: {password ? strengthPercent() : 0}%"></span>
</div>
{#if showRequirements}
<ul class="wire-next__strong-password-requirements">
<li data-met="{password && hasMinimumLength() ? 'true' : 'false'}">
<span aria-hidden="true"></span>At least {minLength} characters
</li>
{#if requireLowercase}<li data-met="{password && hasLowercase() ? 'true' : 'false'}"><span aria-hidden="true"></span>One lowercase letter</li>{/if}
{#if requireUppercase}<li data-met="{password && hasUppercase() ? 'true' : 'false'}"><span aria-hidden="true"></span>One uppercase letter</li>{/if}
{#if requireNumber}<li data-met="{password && hasNumber() ? 'true' : 'false'}"><span aria-hidden="true"></span>One number</li>{/if}
{#if requireSpecialCharacter}<li data-met="{password && hasSpecialCharacter() ? 'true' : 'false'}"><span aria-hidden="true"></span>One special character: {specialCharactersSet}</li>{/if}
</ul>
{/if}
{#if hintText}<small id="{name}-hint">{hintText}</small>{/if}
</div>
</div>
{/if}
</div>
{#if presentation !== "popover"}
<div class="wire-next__strong-password-details">
<div class="wire-next__strong-password-summary">
<span>Password strength</span>
<strong>{password ? strengthLabel() : emptyLabel}</strong>
</div>
<div
class="wire-next__strong-password-meter"
role="progressbar"
aria-label="Password strength"
aria-valuemin="0"
aria-valuemax="100"
aria-valuenow="{password ? strengthPercent() : 0}"
>
<span style="width: {password ? strengthPercent() : 0}%"></span>
</div>
{#if showRequirements}
<ul class="wire-next__strong-password-requirements">
<li data-met="{password && hasMinimumLength() ? 'true' : 'false'}"><span aria-hidden="true"></span>At least {minLength} characters</li>
{#if requireLowercase}<li data-met="{password && hasLowercase() ? 'true' : 'false'}"><span aria-hidden="true"></span>One lowercase letter</li>{/if}
{#if requireUppercase}<li data-met="{password && hasUppercase() ? 'true' : 'false'}"><span aria-hidden="true"></span>One uppercase letter</li>{/if}
{#if requireNumber}<li data-met="{password && hasNumber() ? 'true' : 'false'}"><span aria-hidden="true"></span>One number</li>{/if}
{#if requireSpecialCharacter}<li data-met="{password && hasSpecialCharacter() ? 'true' : 'false'}"><span aria-hidden="true"></span>One special character: {specialCharactersSet}</li>{/if}
</ul>
{/if}
{#if hintText}<small id="{name}-hint">{hintText}</small>{/if}
</div>
{/if}
<small id="{name}-validation" class="wire-next__validation" data-error="{name}">{validationMessage}</small>
</div>
}
}