Skip to content
Browse documentation

A password field with an eye that turns into eye-off, a four-step strength meter that fills and names itself, and a Caps Lock warning.

Used forSign-in formsSign-up and resetAccount settingsAPI secrets

Install with the shadcn CLI

$ npx shadcn@latest add @hairline/password-input

Older shadcn CLI, or a network that blocks ui.shadcn.com? Install by URL: shadcn add https://hairlineui.com/r/password-input.json. More

Used in a block

All blocks

Templates

Need the whole page?

65 Next.js landing pages, each designed from scratch. Try any of them live; one is $39, all of them $159, and 8 are free.

Where it’s used

  • Sign-in forms

    Show the password without losing the caret.

  • Sign-up and reset

    A meter that tells people how strong the new password is.

  • Account settings

    Change-password fields with a Caps Lock warning.

  • API secrets

    Masked tokens you can peek at.

What it handles for you

Most peek buttons take focus from the field, so the keyboard closes on phones and the caret jumps. This one never takes focus.

Instead of wiring upshow/hide togglestrength meterCaps Lock detection
  1. Peek without losing your place

    The toggle never takes focus, so the caret and the phone keyboard stay.

  2. A meter that fills

    Four segments fill left to right with a 50ms stagger; the word morphs through TextMorph.

  3. Bring your own scoring

    Pass a function (zxcvbn, your API) instead of the built-in length-and-variety score.

  4. Caps Lock note

    Read from the key events; it folds in under the field and away on blur.

Overview

Show/hide toggles usually swap icons, steal focus and move the caret. Here the eye turns into eye-off without leaving the field, the strength meter fills segment by segment as the word morphs from Weak to Strong, and a Caps Lock note folds in while you type with it on.

States

hidden
Dots, with the eye at the end.
shown
The eye turns into eye-off (rotate morph, 480ms); focus stays in the field.
strength
Segments fill (420ms each, 50ms apart) and turn red, amber or green; the word morphs.
caps lock
"Caps Lock is on" folds in where the hint goes.

Usage

usage.tsx
import { PasswordInput } from "@/components/password-input";<PasswordInput label="Password" /><PasswordInput label="New password" strength autoComplete="new-password" />

Depends on lucide-react, installed for you by the CLI along with the keyframes it needs.

Works with

Props

strength= false
boolean | (value) => 0–4
Show the meter; pass a function to score it yourself.
strengthLabels= ["Weak","Fair","Good","Strong"]
[string, string, string, string]
Words for scores 1 to 4.
shown / defaultShown / onShownChange= — / false
boolean
Whether the password shows as text, controlled or not; onShownChange fires when the eye is pressed.
...Input props
InputProps
label, hint, error, size, onValueChange and every native prop.

Accessibility and motion

  • Shares field.tsx with every text control: one surface, one focus ring, one way of showing hints and errors.
  • The meter is announced politely as it changes; the eye is a toggle button with aria-pressed.
  • scorePassword is exported if you want the same score elsewhere.

Requirements

React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

The CLI also installs Icon morph, Text morph.

Source

"use client";import * as React from "react";import { Eye, EyeOff } from "lucide-react";import { IconMorph } from "./icon-morph";import { Input, type InputProps } from "./input";import { TextMorph } from "./text-morph";import { EASE_OUT, INTERACTION_FOCUS, INTERACTION_PRESS, useInteractionReducedMotion } from "./ui-interaction-motion";/* ───────────────────────────────────────────────────────── * PASSWORD INPUT: show, measure, warn * *   show     the eye turns into eye-off (rotate morph) and the *            dots become letters; focus stays in the field *   measure  with `strength`, four segments fill left to right *            as the score climbs and the word morphs *            (Weak → Fair → Good → Strong) *   warn     Caps Lock on while typing folds a note in under it * ───────────────────────────────────────────────────────── */export type PasswordScore = 0 | 1 | 2 | 3 | 4;export interface PasswordInputProps extends Omit<InputProps, "type" | "trailing" | "clearable"> {  /** Show the four-segment meter. Pass a function to score it yourself (e.g. zxcvbn). */  strength?: boolean | ((value: string) => PasswordScore);  /** Words for scores 1 to 4. */  strengthLabels?: [string, string, string, string];  /** Whether the password shows as text, controlled or not. */  shown?: boolean;  defaultShown?: boolean;  onShownChange?: (shown: boolean) => void;}/** A plain score: length and variety. Swap in zxcvbn for real checks. */export function scorePassword(value: string): PasswordScore {  if (!value) return 0;  let score = 0;  if (value.length >= 8) score++;  if (value.length >= 12) score++;  if (/[a-z]/.test(value) && /[A-Z]/.test(value)) score++;  if (/\d/.test(value) && /[^A-Za-z0-9]/.test(value)) score++;  return Math.max(1, Math.min(4, score)) as PasswordScore;}const TONES = ["bg-red-500", "bg-amber-500", "bg-emerald-500", "bg-emerald-500"];const TEXT = ["text-red-600 dark:text-red-400", "text-amber-600 dark:text-amber-400", "text-emerald-600 dark:text-emerald-400", "text-emerald-600 dark:text-emerald-400"];export function PasswordInput({  strength,  strengthLabels = ["Weak", "Fair", "Good", "Strong"],  hint,  value,  defaultValue,  onValueChange,  onKeyDown,  onKeyUp,  onBlur,  size = "md",  shown: shownProp,  defaultShown = false,  onShownChange,  ...props}: PasswordInputProps) {  const reduced = useInteractionReducedMotion();  const [ownShown, setOwnShown] = React.useState(defaultShown);  const shown = shownProp ?? ownShown;  const [text, setText] = React.useState(String(defaultValue ?? ""));  const [caps, setCaps] = React.useState(false);  const current = value !== undefined ? String(value) : text;  const score: PasswordScore = !strength || !current ? 0 : typeof strength === "function" ? strength(current) : scorePassword(current);  const readCaps = (e: React.KeyboardEvent<HTMLInputElement>) => setCaps(e.getModifierState?.("CapsLock") ?? false);  return (    <div className="flex w-full flex-col">      <Input        {...props}        size={size}        type={shown ? "text" : "password"}        autoComplete={props.autoComplete ?? "current-password"}        value={value}        defaultValue={defaultValue}        hint={caps ? "Caps Lock is on" : hint}        onValueChange={(v) => {          if (value === undefined) setText(v);          onValueChange?.(v);        }}        onKeyDown={(e) => {          readCaps(e);          onKeyDown?.(e);        }}        onKeyUp={(e) => {          readCaps(e);          onKeyUp?.(e);        }}        onBlur={(e) => {          setCaps(false);          onBlur?.(e);        }}        trailing={          <button            type="button"            aria-label={shown ? "Hide password" : "Show password"}            aria-pressed={shown}            // Keep focus (and the caret) in the field.            onPointerDown={(e) => e.preventDefault()}            onClick={() => {              if (shownProp === undefined) setOwnShown(!shown);              onShownChange?.(!shown);            }}            className={`flex items-center justify-center rounded-full text-muted-foreground cursor-pointer hover:bg-accent hover:text-foreground ${size === "sm" ? "size-5" : "size-7"} ${INTERACTION_PRESS} ${INTERACTION_FOCUS}`}          >            <IconMorph size={14} mode="rotate" active={shown} from={<Eye />} to={<EyeOff />} />          </button>        }      />      {strength && (        <div className="flex items-center gap-3 px-3 pt-2" aria-live="polite">          <div className="flex flex-1 gap-1" aria-hidden="true">            {[1, 2, 3, 4].map((step) => (              <span key={step} className="relative h-1 flex-1 overflow-hidden rounded-full bg-muted">                <span                  className={`absolute inset-0 origin-left rounded-full ${TONES[Math.max(0, score - 1)]}`}                  style={{                    transform: score >= step ? "scaleX(1)" : "scaleX(0)",                    transition: reduced ? "none" : `transform 420ms ${EASE_OUT} ${score >= step ? (step - 1) * 50 : 0}ms, background-color 300ms`,                  }}                />              </span>            ))}          </div>          <span className={`w-12 text-right text-[12px] font-medium ${score ? TEXT[score - 1] : "text-muted-foreground"}`}>            <TextMorph>{score ? strengthLabels[score - 1] : " "}</TextMorph>          </span>        </div>      )}    </div>  );}export default PasswordInput;

Registry item: /r/password-input.json