Skip to content
Molecules

Password input

A new password with a strength meter and rules.

Variants

2 more takes on the password input, each a single file you install on its own by its id.

aui-039Strength dial

Four arcs light up as it gets stronger.

$ pnpm dlx asta-ui@latest add password-input-aui-039

Or with the shadcn CLI: pnpm dlx shadcn@latest add https://ui.astabyte.com/r/password-input-aui-039.json

aui-040Rule tiles

Rule tiles that turn blue as you meet them.

$ pnpm dlx asta-ui@latest add password-input-aui-040

Or with the shadcn CLI: pnpm dlx shadcn@latest add https://ui.astabyte.com/r/password-input-aui-040.json

Installation

  1. Run the CLI

    Run pnpm dlx asta-ui@latest init once, then this. The file lands in components/astabyte/ and the colour tokens go into your CSS.

    $ pnpm dlx asta-ui@latest add password-input

    Or with the shadcn CLI: pnpm dlx shadcn@latest add https://ui.astabyte.com/r/password-input.json

  2. Or install the package

    Don’t want the file? Import from asta-ui/password-input and update with pnpm update asta-ui. Add the stylesheet once. More.

    $ pnpm add asta-ui
    import { PasswordField } from "asta-ui/password-input";
  3. Or copy it by hand

    Copy this file. Keep the paths, or fix the @/ imports. Then add the tokens from globals.css.

    "use client";
    
    import { useId, useState, type InputHTMLAttributes } from "react";
    
    const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" ");
    
    export interface PasswordRule {
      label: string;
      test: (value: string) => boolean;
    }
    
    export const DEFAULT_PASSWORD_RULES: PasswordRule[] = [
      { label: "At least 8 characters", test: (v) => v.length >= 8 },
      { label: "One uppercase letter", test: (v) => /[A-Z]/.test(v) },
      { label: "One number", test: (v) => /\d/.test(v) },
      { label: "One symbol", test: (v) => /[^A-Za-z0-9]/.test(v) },
    ];
    
    const WORDS = ["Weak", "Weak", "Fair", "Good", "Strong"];
    
    /** How many rules a password passes, and the matching word. */
    export function passwordStrength(value: string, rules: PasswordRule[] = DEFAULT_PASSWORD_RULES) {
      const passed = rules.map((r) => r.test(value));
      const score = passed.filter(Boolean).length;
      const ratio = rules.length ? score / rules.length : 0;
      const word = !value ? "" : WORDS[Math.round(ratio * 4)];
      return { score, max: rules.length, passed, word, strong: score === rules.length };
    }
    
    export interface PasswordFieldProps extends Omit<InputHTMLAttributes<HTMLInputElement>, "type" | "value" | "defaultValue" | "onChange" | "size"> {
      value?: string;
      defaultValue?: string;
      onChange?: (value: string, strength: ReturnType<typeof passwordStrength>) => void;
      label?: string;
      rules?: PasswordRule[];
      /** Show the segmented strength meter. */
      meter?: boolean;
      /** Show the rule checklist under the meter. */
      checklist?: boolean;
      className?: string;
    }
    
    function Eye({ hidden }: { hidden: boolean }) {
      return (
        <svg viewBox="0 0 24 24" className="h-[18px] w-[18px]" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden>
          <path d="M2.5 12c3-5.5 16-5.5 19 0-3 5.5-16 5.5-19 0Z" />
          <circle
            cx="12" cy="12" r="2.75"
            className="origin-center transition-transform duration-300 ease-[cubic-bezier(.16,1,.3,1)] [transform-box:fill-box] motion-reduce:transition-none"
            style={{ transform: hidden ? "scale(.55)" : "scale(1)" }}
          />
          <path
            d="m4.5 4.5 15 15"
            pathLength={1}
            strokeDasharray={1}
            strokeDashoffset={hidden ? 0 : 1}
            className="transition-[stroke-dashoffset] duration-300 ease-[cubic-bezier(.16,1,.3,1)] motion-reduce:transition-none"
          />
        </svg>
      );
    }
    
    function Tick({ on }: { on: boolean }) {
      return (
        <span
          className={cx(
            "relative flex h-4 w-4 shrink-0 items-center justify-center rounded-full transition-[background-color,box-shadow] duration-300",
            on ? "bg-brand text-on-brand" : "shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_20%,transparent)]",
          )}
        >
          <svg viewBox="0 0 24 24" className="h-2.5 w-2.5" fill="none" stroke="currentColor" strokeWidth="3.6" strokeLinecap="round" strokeLinejoin="round" aria-hidden>
            <path
              d="m5 12.5 4.5 4.5L19 7.5"
              pathLength={1}
              strokeDasharray={1}
              strokeDashoffset={on ? 0 : 1}
              className="transition-[stroke-dashoffset] delay-100 duration-[400ms] ease-[cubic-bezier(.16,1,.3,1)] motion-reduce:transition-none"
            />
          </svg>
        </span>
      );
    }
    
    /**
     * A new-password field. The eye morphs when you reveal the text, the meter segments fill as more rules pass
     * (they turn blue once every rule passes) and each rule ticks itself off. Rules are plain `{ label, test }`.
     */
    export function PasswordField({
      value,
      defaultValue = "",
      onChange,
      label = "Password",
      rules = DEFAULT_PASSWORD_RULES,
      meter = true,
      checklist = true,
      className,
      id,
      autoComplete = "new-password",
      ...rest
    }: PasswordFieldProps) {
      const uid = useId();
      const inputId = id ?? `${uid}-input`;
      const [inner, setInner] = useState(defaultValue);
      const [shown, setShown] = useState(false);
      const current = value ?? inner;
      const s = passwordStrength(current, rules);
    
      return (
        <div className={cx("flex w-full flex-col gap-2", className)}>
          <label htmlFor={inputId} className="text-[13px] font-medium text-ink">
            {label}
          </label>
          <div
            className={cx(
              "flex h-10 items-center gap-2 rounded-lg bg-paper pl-3 pr-1 transition-shadow duration-200",
              "shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_14%,transparent)] hover:shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_24%,transparent)]",
              "focus-within:!shadow-[inset_0_0_0_1px_var(--color-brand),0_0_0_4px_color-mix(in_srgb,var(--color-brand)_16%,transparent)]",
            )}
          >
            <input
              name="password"
              {...rest}
              id={inputId}
              type={shown ? "text" : "password"}
              value={current}
              autoComplete={autoComplete}
              spellCheck={false}
              autoCapitalize="off"
              aria-describedby={checklist || meter ? `${uid}-help` : undefined}
              onChange={(e) => {
                const v = e.target.value;
                if (value === undefined) setInner(v);
                onChange?.(v, passwordStrength(v, rules));
              }}
              className="h-full min-w-0 flex-1 bg-transparent text-[16px] sm:text-[14px] tracking-[0.01em] text-ink outline-none placeholder:text-mute"
            />
            <button
              type="button"
              aria-label={shown ? "Hide password" : "Show password"}
              aria-pressed={shown}
              aria-controls={inputId}
              onPointerDown={(e) => e.preventDefault()}
              onClick={() => setShown((v) => !v)}
              className="flex h-8 w-8 shrink-0 items-center justify-center rounded-md text-mute outline-none transition-colors duration-200 hover:text-ink focus-visible:ring-2 focus-visible:ring-brand/50"
            >
              <Eye hidden={!shown} />
            </button>
          </div>
    
          {(meter || checklist) && (
            <div id={`${uid}-help`} className="flex flex-col gap-3 pt-1">
              {meter && (
                <div className="flex items-center gap-3">
                  <div aria-hidden className="flex flex-1 gap-1.5">
                    {rules.map((r, i) => (
                      <span key={r.label} className="relative h-1 flex-1 overflow-hidden rounded-full bg-[color-mix(in_srgb,var(--color-ink)_10%,transparent)]">
                        <span
                          className={cx(
                            "absolute inset-0 origin-left rounded-full transition-[transform,background-color] duration-500 ease-[cubic-bezier(.16,1,.3,1)] motion-reduce:transition-none",
                            s.strong ? "bg-brand" : "bg-ink",
                          )}
                          style={{ transform: `scaleX(${i < s.score ? 1 : 0})`, transitionDelay: `${i * 40}ms` }}
                        />
                      </span>
                    ))}
                  </div>
                  <span aria-live="polite" className={cx("w-12 text-right text-[12px] tabular-nums transition-colors duration-300", s.strong ? "text-brand" : "text-mute")}>
                    {s.word && <span className="sr-only">Strength: </span>}
                    {s.word}
                  </span>
                </div>
              )}
              {checklist && (
                <ul className="grid gap-1.5 sm:grid-cols-2">
                  {rules.map((r, i) => (
                    <li key={r.label} className={cx("flex items-center gap-2 text-[12.5px] transition-colors duration-300", s.passed[i] ? "text-ink" : "text-mute")}>
                      <Tick on={s.passed[i]} />
                      {r.label}
                      <span className="sr-only">{s.passed[i] ? ", done" : ", not yet"}</span>
                    </li>
                  ))}
                </ul>
              )}
            </div>
          )}
        </div>
      );
    }
    
    export default PasswordField;
    
  4. Or ask your AI agent

    Paste this into Claude Code, Cursor, Codex, v0 or Lovable. Fill in the last line. More in Prompts.

    Add the Astabyte UI Password input component to my project.
    
    A new password with a strength meter and rules.
    It includes: PasswordField, show/hide, strength meter, checklist, custom rules, passwordStrength.
    
    Install it:
    ```sh
    pnpm dlx asta-ui@latest add password-input --radix
    ```
    Or with the shadcn CLI:
    ```sh
    pnpm dlx shadcn@latest add https://ui.astabyte.com/r/password-input.json
    ```
    If the project isn't set up yet, run `pnpm dlx asta-ui@latest init --radix` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4).
    
    Import it like this:
    ```tsx
    import { Button } from "@/components/astabyte/button";
    import { PasswordField } from "@/components/astabyte/password-input";
    ```
    
    Usage example and props: https://ui.astabyte.com/password-input
    Full catalogue for context: https://ui.astabyte.com/llms.txt
    
    Rules:
    - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS.
    - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback.
    - Keep keyboard, focus and ARIA behaviour.
    - Use fake data and fake brand names in examples.
    
    Use it to: [describe where it goes and what it should do]
  5. Or use plain HTML + CSS

    Not on React? Use it from Django, Rails, Laravel, Go html/template, htmx or a static page. Load asta.css and asta.js once (the head.html tab), then paste the markup. The ui.astabyte.com/cdn link is temporary: pin a version from jsDelivr for production. More in HTML + CSS.

    <!-- Needs asta.js for the reveal toggle, meter and rules. -->
    <form style="display:flex;flex-direction:column;align-items:flex-start;gap:16px"><div style="width:20rem;max-width:100%"><div data-asta-password data-strength="0" class="asta-password"><label for="pw" class="asta-password-label">Password</label><div class="asta-password-box"><input id="pw" name="password" type="password" placeholder="" autocomplete="new-password" spellcheck="false" autocapitalize="off" aria-describedby="pw-help" data-asta-password-input class="asta-password-input"> <button type="button" aria-label="Show password" aria-pressed="false" aria-controls="pw" data-asta-password-toggle class="asta-password-toggle"><svg viewBox="0 0 20 20"  fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2.5 10s2.7-5 7.5-5 7.5 5 7.5 5-2.7 5-7.5 5-7.5-5-7.5-5Z"></path> <circle cx="10" cy="10" r="2.2" class="asta-password-pupil"></circle> <path d="M4 3.5 16 16.5" pathLength="1" class="asta-pw-slash"></path></svg></button></div><div id="pw-help" class="asta-password-help"><div class="asta-password-meter"><div aria-hidden="true" class="asta-password-bars"><span class="asta-password-bar"><span data-asta-password-seg class="asta-password-seg" style="transition-delay:0ms;"></span></span><span class="asta-password-bar"><span data-asta-password-seg class="asta-password-seg" style="transition-delay:40ms;"></span></span><span class="asta-password-bar"><span data-asta-password-seg class="asta-password-seg" style="transition-delay:80ms;"></span></span><span class="asta-password-bar"><span data-asta-password-seg class="asta-password-seg" style="transition-delay:120ms;"></span></span></div><span aria-live="polite" data-asta-password-word class="asta-password-word"></span></div><ul class="asta-password-rules"><li data-asta-password-rule="^[\s\S]{8,}$" class="asta-password-rule"><span class="asta-password-tick"><svg viewBox="0 0 16 16"  fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m3.5 8.5 3 3 6-7" pathLength="1" class="asta-pw-tick"></path></svg></span> At least 8 characters <span class="asta-sr-only" data-asta-password-said>, not yet</span></li><li data-asta-password-rule="[A-Z]" class="asta-password-rule"><span class="asta-password-tick"><svg viewBox="0 0 16 16"  fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m3.5 8.5 3 3 6-7" pathLength="1" class="asta-pw-tick"></path></svg></span> One uppercase letter <span class="asta-sr-only" data-asta-password-said>, not yet</span></li><li data-asta-password-rule="\d" class="asta-password-rule"><span class="asta-password-tick"><svg viewBox="0 0 16 16"  fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m3.5 8.5 3 3 6-7" pathLength="1" class="asta-pw-tick"></path></svg></span> One number <span class="asta-sr-only" data-asta-password-said>, not yet</span></li><li data-asta-password-rule="[^A-Za-z0-9]" class="asta-password-rule"><span class="asta-password-tick"><svg viewBox="0 0 16 16"  fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m3.5 8.5 3 3 6-7" pathLength="1" class="asta-pw-tick"></path></svg></span> One symbol <span class="asta-sr-only" data-asta-password-said>, not yet</span></li></ul></div></div></div></form>

Usage

The demo above, as code.

"use client";

import { useState } from "react";
import { Button } from "@/components/astabyte/button";
import { PasswordField } from "@/components/astabyte/password-input";

export default function PasswordInputDemo() {
  const [strong, setStrong] = useState(false);

  return (
    <div className="flex min-h-[420px] w-full items-center justify-center p-6 sm:p-10">
      <form onSubmit={(e) => e.preventDefault()} className="flex w-full max-w-sm flex-col gap-6 rounded-xl bg-paper-2 p-5">
        <div>
          <p className="text-[15px] font-medium tracking-[-0.01em] text-ink">Choose a new password</p>
          <p className="mt-1 text-[13px] text-mute">You will use it to sign in on every device.</p>
        </div>
        <PasswordField label="New password" placeholder="Type a password…" onChange={(_, s) => setStrong(s.strong)} />
        <Button type="submit" disabled={!strong}>
          Update password
        </Button>
      </form>
    </div>
  );
}