Skip to content

Password Strength

密码强度指示器。基于 usePasswordStrength 按长度与字符多样性划分 tooWeak / weak / medium / strong,并以分段轨道与可选文案展示结果。 设计上可作为 PasswordInput 的**父组件**:将输入放在默认插槽中,由外层传入 password 驱动强度展示。二者**互不引用**,安装与使用均需单独添加;password-strength 不会拉取 password-input。

Installation

pnpm dlx @weme-ui/weme-ui add password-strength
# 若与 PasswordInput 组合使用,需另行安装:
pnpm dlx @weme-ui/weme-ui add password-input

Usage

*
Too weak
Vueexamples/usage.vueExpand codeHide code
<script lang="ts" setup>
import * as v from 'valibot'
import Button from '../../button/button.vue'
import FormField from '../../form-field/form-field.vue'
import Form from '../../form/form.vue'
import PasswordInput from '../../password-input/password-input.vue'
import PasswordStrength from '../password-strength.vue'

const schema = v.object({
  password: v.pipe(v.string('密码不能为空'), v.minLength(8, '至少 8 位')),
})

const defaultValues = {
  password: '',
}

function onFormSubmit(values: { password: string }) {
  // eslint-disable-next-line no-alert
  alert(JSON.stringify(values))
}
</script>

<template>
  <Form
    class="max-w-sm w-full"
    :default-values="defaultValues"
    :schema="schema"
    @form-submit="onFormSubmit"
  >
    <FormField v-slot="{ field }" name="password" label="Password" required>
      <PasswordStrength :password="String(field.state.value ?? '')" show-message>
        <PasswordInput
          :model-value="field.state.value"
          placeholder="Enter password"
          toggle-visibility
          @blur="field.api.handleBlur"
          @update:model-value="field.api.handleChange"
        />
      </PasswordStrength>
    </FormField>
    <Button type="submit"> Submit </Button>
  </Form>
</template>

Examples

Sizes

Medium
Medium
Medium
Vueexamples/sizes.vueExpand codeHide code
<script lang="ts" setup>
import PasswordStrength from '../password-strength.vue'

const sample = 'Abcdefgh1!'
</script>

<template>
  <div class="flex flex-col gap-4 max-w-sm w-full">
    <PasswordStrength :password="sample" size="sm" show-message />
    <PasswordStrength :password="sample" size="md" show-message />
    <PasswordStrength :password="sample" size="lg" show-message />
  </div>
</template>

Messages

一般
Vueexamples/messages.vueExpand codeHide code
<script lang="ts" setup>
import PasswordStrength from '../password-strength.vue'
</script>

<template>
  <div class="max-w-sm w-full">
    <PasswordStrength
      password="Abcdefgh1!@"
      show-message
      :messages="{
        tooWeak: '太弱',
        weak: '较弱',
        medium: '一般',
        strong: '很强',
      }"
    />
  </div>
</template>

Show Message

Medium
Vueexamples/show-message.vueExpand codeHide code
<script lang="ts" setup>
import PasswordStrength from '../password-strength.vue'
</script>

<template>
  <div class="flex flex-col gap-4 max-w-sm w-full">
    <PasswordStrength password="Abcdefgh1!" show-message />
    <PasswordStrength password="Abcdefgh1!" />
  </div>
</template>

Disabled

Vueexamples/disabled.vueExpand codeHide code
<script lang="ts" setup>
import PasswordStrength from '../password-strength.vue'
</script>

<template>
  <div class="max-w-sm w-full">
    <PasswordStrength disabled password="Abcdefgh1!" show-message />
  </div>
</template>

Unstyled

Medium
Vueexamples/unstyled.vueExpand codeHide code
<script lang="ts" setup>
import PasswordStrength from '../password-strength.vue'
</script>

<template>
  <div class="max-w-sm w-full">
    <PasswordStrength
      unstyled
      password="Abcdefgh1!"
      show-message
      class="flex flex-col gap-1 w-full"
      :ui="{
        track: 'flex gap-1 h-1.5 w-full',
        indicator: 'flex-1 rounded-sm bg-muted',
        message: 'text-xs text-subtle',
      }"
    />
  </div>
</template>

Props

PropTypeDefault
asany'div'
根节点渲染为的元素或组件(Reka UI Primitive)
any
'div'
asChildbooleanfalse
是否合并到子元素(Reka UI Primitive)
boolean
false
passwordstring-
待评估的密码字符串
string
-
optionsUsePasswordStrengthOptions['options']-
覆盖 weak / medium / strong 阈值(不可覆盖 tooWeak);默认 weak 长度 ≥ 8 且多样性 ≥ 2,medium ≥ 10 / 3,strong ≥ 12 / 4
UsePasswordStrengthOptions['options']
-
messagesUsePasswordStrengthOptions['messages']-
覆盖各档位文案;默认 Too weak / Weak / Medium / Strong
UsePasswordStrengthOptions['messages']
-
showMessageboolean-
是否显示强度文案
boolean
-
size'sm' | 'md' | 'lg''md'
尺寸
'sm' | 'md' | 'lg'
'md'
disabledboolean-
为 true 时不渲染轨道与文案;根节点设置 data-disabled
boolean
-
unstyledboolean-
移除样式变体类,仅保留结构
boolean
-
classany-
根节点额外 class
any
-
uiPartial<PasswordStrengthStyleSlots>-
覆盖 slots 样式(root / track / indicator / message)
Partial<PasswordStrengthStyleSlots>
-

Slots

SlotDescription
default根节点内、轨道之前的自定义内容;典型用法是放入 PasswordInput(需另行安装)
根节点内、轨道之前的自定义内容;典型用法是放入 PasswordInput(需另行安装)
message仅 showMessage 为真且未 disabled 时渲染;作用域参数 { message: string, length: number };未传时回退到 message 文本
仅 showMessage 为真且未 disabled 时渲染;作用域参数 { message: string, length: number };未传时回退到 message 文本

CSS Variables

可在 *.style.ts 或 class 中使用,例如 bg-password-strength-medium。

CSS VariableValue
--password-strength-medium-colorsuccess.7
success.7
--password-strength-strong-colorsuccess.9
success.9
--password-strength-too-weak-colorerror.9
error.9
--password-strength-weak-colorerror.7
error.7

error.9 / success.7 等为语义色别名(色板阶),详见 Tokens。

Accessibility

基于 Reka UI Primitive。根节点设置 data-slot="password-strength",禁用时设置 data-disabled。强度轨道为 role="progressbar";各分段为 role="presentation",并以 aria-valuenow 标记档位键名(tooWeak / weak / medium / strong)。开启 showMessage 时,文案容器为 role="status",并以 aria-label 暴露当前强度文案。

Files

  • src/components/password-strength/password-strength.vue
  • src/components/password-strength/password-strength.props.ts
  • src/components/password-strength/password-strength.style.ts