# Animated Password Input for UI SDK (/docs/components/password-input)

The text comes into focus out of a blur when revealed, and requirements tick as they are met.

```tsx title="example.tsx"
import * as React from "react"

import { Field, FieldDescription, FieldLabel } from "@/components/keyframery/field"
import {
  PasswordInput,
  PasswordInputCapsLock,
  PasswordInputControl,
  PasswordInputToggle,
} from "@/components/keyframery/password-input"

/* Types a value one character at a time. */
function typeOut(into: string, text: string, step = 90) {
  return Array.from(text, (_, i) => ({ type: text.slice(0, i + 1), into, wait: step }))
}

const field = 'form > [data-slot="field"]:nth-child(2)'

const rules = [
  { label: "8 characters", test: (v: string) => v.length >= 8 },
  { label: "A number", test: (v: string) => /\d/.test(v) },
  { label: "A symbol", test: (v: string) => /[^A-Za-z0-9\s]/.test(v) },
]

function Rule({ label, met }: { label: string; met: boolean }) {
  return (
    <li
      className={
        "flex items-center gap-1.5 text-[12px] leading-4 transition-colors duration-150 motion-reduce:transition-none " +
        (met ? "text-foreground" : "text-muted-foreground")
      }
    >
      <span className="relative grid size-3.5 place-items-center">
        <span
          className={
            "absolute size-1 rounded-full bg-muted-foreground/50 transition-[opacity,scale] duration-150 motion-reduce:transition-none " +
            (met ? "scale-50 opacity-0" : "")
          }
        />
        <svg
          viewBox="0 0 16 16"
          fill="none"
          stroke="oklch(0.62 0.16 150)"
          strokeWidth="2"
          strokeLinecap="round"
          strokeLinejoin="round"
          aria-hidden="true"
          className="size-3.5"
        >
          <path
            d="m3.5 8.5 3 3 6-7"
            pathLength={1}
            strokeDasharray={1}
            strokeDashoffset={met ? 0 : 1}
            className="transition-[stroke-dashoffset] duration-300 ease-[cubic-bezier(0.22,1,0.36,1)] motion-reduce:transition-none"
          />
        </svg>
      </span>
      {label}
    </li>
  )
}

export default function PasswordInputDemo() {
  const [password, setPassword] = React.useState("")

  return (
    <form className="flex w-[340px] flex-col gap-5" onSubmit={(event) => event.preventDefault()}>
      <div>
        <h3 className="text-[15px] font-semibold tracking-tight">Create your account</h3>
        <p className="mt-1 text-[13px] text-muted-foreground">One login for every trip you plan.</p>
      </div>
      <Field name="password">
        <FieldLabel>Password</FieldLabel>
        <PasswordInput autoComplete="new-password" value={password} onValueChange={(value) => setPassword(value)}>
          <PasswordInputControl placeholder="At least 8 characters" />
          <PasswordInputCapsLock />
          <PasswordInputToggle aria-label="Show password" />
        </PasswordInput>
        <ul className="mt-0.5 flex gap-4">
          {rules.map((rule) => (
            <Rule key={rule.label} label={rule.label} met={rule.test(password)} />
          ))}
        </ul>
      </Field>
      <Field name="wifi">
        <FieldLabel>Guest Wi-Fi</FieldLabel>
        <PasswordInput readOnly defaultValue="hollowfarm-2024">
          <PasswordInputControl />
          <PasswordInputToggle aria-label="Show password" />
        </PasswordInput>
        <FieldDescription>Network “HollowFarm-Guest”, from the note on the fridge.</FieldDescription>
      </Field>
      <Field name="old" disabled>
        <FieldLabel>Current password</FieldLabel>
        <PasswordInput defaultValue="hunter-two">
          <PasswordInputControl />
          <PasswordInputToggle aria-label="Show password" />
        </PasswordInput>
      </Field>
    </form>
  )
}
```

## Installation [#installation]

```bash
npx shadcn@latest add @keyframery/password-input
```

This adds `components/keyframery/password-input.tsx` and its primitive, `components/keyframery/primitives/password-input.tsx`, to your project, and installs `@ui-sdk/react`. The motion tokens are added to your global CSS the first time you install a component.

## Usage [#usage]

```tsx
import {
  PasswordInput,
  PasswordInputCapsLock,
  PasswordInputControl,
  PasswordInputToggle,
} from "@/components/keyframery/password-input"
```

The parts and props are the same as UI SDK's Password Input. For every prop, see the [UI SDK password input reference](https://ui-sdk.dev/docs/components/password-input).

## What moves [#what-moves]

| Move     | What you see                                           |
| -------- | ------------------------------------------------------ |
| **Fade** | The text comes into focus out of a blur when revealed. |
| **Pop**  | The eye icons swap with a pop.                         |
| **Draw** | Requirement ticks draw themselves as each rule is met. |

Timing and springiness follow the [motion style](/docs/motion-styles) you set, so this component moves like the rest of your app.

## Swap the import [#swap-the-import]

Already using UI SDK's password input with your own styles? Keep your markup and change one line. The primitive adds motion and nothing else.

```tsx
// Before
import { … } from "@ui-sdk/react/password-input"

// After: same parts, same props, now animated
import { … } from "@/components/keyframery/primitives/password-input"
```

## Parts [#parts]

The finished component exports 4 parts:

`PasswordInput`, `PasswordInputCapsLock`, `PasswordInputControl`, `PasswordInputToggle`

## Accessibility [#accessibility]

Keyboard, focus and screen-reader behaviour come from UI SDK and are not changed. With reduced motion turned on, nothing in the password input moves: changes happen at once or with a short fade. More in [Accessibility](/docs/accessibility).

## Source [#source]

The code that lands in your project. It is yours to change.

**components/keyframery/password-input.tsx**

```tsx
"use client"

/* Styled password input: the animated primitive plus the Keyframery look. */

import * as React from "react"

import {
  PasswordInput as PasswordInputPrimitive,
  PasswordInputControl as PasswordInputControlPrimitive,
  PasswordInputToggle as PasswordInputTogglePrimitive,
  createPasswordInputController,
} from "@/components/keyframery/primitives/password-input"
import { cn } from "@/lib/utils"

/* The root is the field: the native input, a Caps Lock mark and the toggle sit inside one box.
   The hairline's colour is one variable, so hover, focus and invalid each change a single value. */
function PasswordInput({ className, ...props }: React.ComponentProps<typeof PasswordInputPrimitive>) {
  return (
    <PasswordInputPrimitive
      className={cn(
        "group/password flex h-9 w-full min-w-0 cursor-text items-center gap-1 rounded-[10px] bg-background ps-3 pe-1 text-[13px] text-muted-foreground",
        "outline-0 outline-ring",
        "[--field-line:rgb(0_0_0/0.09)] dark:[--field-line:rgb(255_255_255/0.1)]",
        "shadow-[0_0_0_1px_var(--field-line),0_1px_2px_rgb(0_0_0/0.05)] dark:shadow-[0_0_0_1px_var(--field-line)]",
        "hover:[--field-line:rgb(0_0_0/0.16)] dark:hover:[--field-line:rgb(255_255_255/0.18)]",
        "has-[input:focus-visible]:outline-[3px] has-[input:focus-visible]:outline-ring/30 has-[input:focus-visible]:[--field-line:var(--ring)] dark:has-[input:focus-visible]:[--field-line:var(--ring)]",
        "has-aria-invalid:outline-destructive has-aria-invalid:has-[input:focus-visible]:outline-destructive/30 has-aria-invalid:[--field-line:var(--destructive)] dark:has-aria-invalid:[--field-line:var(--destructive)]",
        "data-readonly:bg-muted/50",
        "data-disabled:cursor-not-allowed data-disabled:bg-muted data-disabled:[--field-line:rgb(0_0_0/0.06)] dark:data-disabled:[--field-line:rgb(255_255_255/0.06)]",
        className,
      )}
      {...props}
    />
  )
}

function PasswordInputControl({
  className,
  ...props
}: React.ComponentProps<typeof PasswordInputControlPrimitive>) {
  return (
    <PasswordInputControlPrimitive
      className={cn(
        "h-full min-w-0 flex-1 bg-transparent text-[13px] text-foreground outline-none select-text placeholder:text-muted-foreground/70",
        "data-disabled:cursor-not-allowed data-disabled:text-muted-foreground",
        className,
      )}
      {...props}
    />
  )
}

const icon = {
  viewBox: "0 0 16 16",
  fill: "none",
  stroke: "currentColor",
  strokeWidth: "1.5",
  strokeLinecap: "round",
  strokeLinejoin: "round",
  "aria-hidden": true,
} as const

function EyeIcon() {
  return (
    <svg {...icon} className="size-4">
      <path d="M1.75 8S4 3.75 8 3.75 14.25 8 14.25 8 12 12.25 8 12.25 1.75 8 1.75 8Z" />
      <circle cx="8" cy="8" r="2" />
    </svg>
  )
}

function EyeOffIcon() {
  return (
    <svg {...icon} className="size-4">
      <path d="M6.6 3.9A6 6 0 0 1 8 3.75C12 3.75 14.25 8 14.25 8a11 11 0 0 1-1.7 2.3M4.3 4.9C2.6 6 1.75 8 1.75 8S4 12.25 8 12.25a6 6 0 0 0 2.9-.75" />
      <path d="M6.6 6.6a2 2 0 0 0 2.8 2.8" />
      <path d="m2.5 2.5 11 11" />
    </svg>
  )
}

/* One icon per state, so the primitive pops each one in as it changes. */
function PasswordInputToggle({
  className,
  children,
  ...props
}: React.ComponentProps<typeof PasswordInputTogglePrimitive>) {
  return (
    <PasswordInputTogglePrimitive
      className={cn(
        "grid size-7 shrink-0 cursor-pointer place-items-center rounded-[7px] text-muted-foreground outline-none",
        "hover:bg-foreground/[0.06] hover:text-foreground aria-pressed:text-foreground",
        "focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
        "data-disabled:pointer-events-none data-disabled:opacity-50",
        "[&_svg]:shrink-0",
        className,
      )}
      {...props}
    >
      {children ?? ((state) => (state.visible ? <EyeOffIcon /> : <EyeIcon />))}
    </PasswordInputTogglePrimitive>
  )
}

/* Place inside PasswordInput, before the toggle. It is out of the page until Caps Lock
   is on while the field has focus, then it pops in from its starting style. */
function PasswordInputCapsLock({ className, ...props }: React.ComponentProps<"span">) {
  return (
    <span
      data-slot="password-input-caps-lock"
      title="Caps Lock is on"
      className={cn(
        "hidden size-5 shrink-0 place-items-center rounded-[6px] bg-muted text-foreground",
        "group-data-capslock/password:grid",
        "[transition:opacity_calc(200ms*var(--kf-speed,1))_var(--ease-house),scale_calc(380ms*var(--kf-speed,1))_var(--ease-pop),display_calc(380ms*var(--kf-speed,1))] transition-discrete",
        "starting:scale-50 starting:opacity-0 motion-reduce:transition-none!",
        className,
      )}
      {...props}
    >
      <svg {...icon} className="size-3.5">
        <path d="M8 2.75 3.25 8h2.5v2.25h4.5V8h2.5L8 2.75Z" />
        <path d="M5.75 13h4.5" />
      </svg>
      <span className="sr-only">Caps Lock is on</span>
    </span>
  )
}

export {
  PasswordInput,
  PasswordInputCapsLock,
  PasswordInputControl,
  PasswordInputToggle,
  createPasswordInputController,
}
```

**components/keyframery/primitives/password-input.tsx**

```tsx
"use client"

/* Animated drop-in for `@ui-sdk/react/password-input`: same parts, same props. Swap the import and keep
   your code. This file adds motion only. Colours, radii, shadows and spacing stay yours. */

import {
  PasswordInput as PasswordInputRoot,
  PasswordInputControl as PasswordInputControlPart,
  PasswordInputToggle as PasswordInputTogglePart,
} from "@ui-sdk/react/password-input"
import * as React from "react"

import { cn } from "@/lib/utils"

export * from "@ui-sdk/react/password-input"

const keyframes =
  "@keyframes kf-password-reveal{from{filter:blur(5px);opacity:.35;letter-spacing:.12em}to{filter:none;opacity:1;letter-spacing:normal}}"

/* The root is usually the field's box: its focus ring is the outline, which closes in from
   10px out on a spring, easing from its resting colour to its focus colour. Give it width and colour with `has-[input:focus-visible]:outline-…`. */
function PasswordInput({ className, ...props }: React.ComponentProps<typeof PasswordInputRoot>) {
  return (
    <PasswordInputRoot
      className={cn(
        "outline-offset-[10px] has-[input:focus-visible]:outline-offset-0",
        "[transition:outline-offset_calc(160ms*var(--kf-speed,1))_var(--ease-house),outline-width_calc(160ms*var(--kf-speed,1))_var(--ease-house),outline-color_0s_linear_calc(160ms*var(--kf-speed,1)),box-shadow_calc(150ms*var(--kf-speed,1))_var(--ease-house),background-color_calc(150ms*var(--kf-speed,1))_var(--ease-house)] has-[input:focus-visible]:[transition:outline-offset_calc(900ms*var(--kf-speed,1))_var(--ease-spring),outline-width_calc(900ms*var(--kf-speed,1))_var(--ease-spring),outline-color_calc(700ms*var(--kf-speed,1))_var(--ease-house),box-shadow_calc(150ms*var(--kf-speed,1))_var(--ease-house),background-color_calc(150ms*var(--kf-speed,1))_var(--ease-house)]",
        "motion-reduce:[transition:none]!",
        className,
      )}
      {...props}
    />
  )
}

/* When the password is revealed, the text comes into focus: a short blur-to-clear. */
function PasswordInputControl({
  className,
  ...props
}: React.ComponentProps<typeof PasswordInputControlPart>) {
  return (
    <>
      <style href="kf-password-reveal" precedence="default">
        {keyframes}
      </style>
      <PasswordInputControlPart
        className={cn(
          "data-visible:animate-[kf-password-reveal_calc(420ms*var(--kf-speed,1))_var(--ease-house)] motion-reduce:animate-none!",
          className,
        )}
        {...props}
      />
    </>
  )
}

/* The toggle presses with a pop release. Whatever icon it shows pops in when it changes
   (render a different icon for each state, as the UI SDK examples do): it grows from 40%
   with a quarter turn on `ease-pop`. */
function PasswordInputToggle({
  className,
  ...props
}: React.ComponentProps<typeof PasswordInputTogglePart>) {
  return (
    <PasswordInputTogglePart
      className={cn(
        "[transition:scale_calc(260ms*var(--kf-speed,1))_var(--ease-pop),color_calc(150ms*var(--kf-speed,1)),background-color_calc(150ms*var(--kf-speed,1))] active:scale-[0.86]",
        "[&_svg]:[transition:scale_calc(440ms*var(--kf-speed,1))_var(--ease-pop),rotate_calc(440ms*var(--kf-speed,1))_var(--ease-pop),opacity_calc(200ms*var(--kf-speed,1))_var(--ease-house)]",
        "[&_svg]:starting:scale-[0.4] [&_svg]:starting:-rotate-90 [&_svg]:starting:opacity-0",
        "motion-reduce:[transition:none]! motion-reduce:[&_svg]:[transition:none]",
        className,
      )}
      {...props}
    />
  )
}

export { PasswordInput, PasswordInputControl, PasswordInputToggle }
```

## Questions

### How does the animated Password Input for UI SDK move?

Fade: The text comes into focus out of a blur when revealed. Pop: The eye icons swap with a pop. Draw: Requirement ticks draw themselves as each rule is met.

### Do I need Motion or Framer Motion for the animated Password Input?

No. Every move is plain CSS, so there is nothing to install besides the component and UI SDK.

### Can I keep my own styling and only take the motion?

Yes. Import from `@/components/keyframery/primitives/password-input` instead. It has the same parts and props as `@ui-sdk/react/password-input` and adds motion only.

### What happens to the Password Input when someone turns on reduced motion?

Nothing moves. State changes happen at once, or with a short fade.
