# Password input

> A native password field with a keyboard-accessible visibility toggle.

- Kind: ui · Category: Forms · Interactivity: script
- Install: `npx shadcn@latest add @haze/password-input`
- File: `src/components/haze/ui/password-input.astro`
- npm dependencies: @lucide/astro
- Registry dependencies: @haze/utils
- Docs page: https://hazeui.com/ui/password-input

## Use when
- People need to check what they have typed into a password field
- The field has a visible or otherwise accessible label

## Avoid when
- The value is ordinary text, email or search; use input
- No visibility control is wanted; use input with type password

## Alternatives
- [input](https://hazeui.com/ui/input.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` |  | yes | Unique input ID referenced by the field's label. |
| name | `string` | id | no | Name submitted with the form. |
| placeholder | `string` |  | no | Optional example text; not a replacement for a label. |
| autocomplete | `string` | "current-password" | no | Autofill hint; use new-password when creating a password. |
| required | `boolean` |  | no | Marks the native input required. |
| disabled | `boolean` |  | no | Disables the field and visibility control. |
| toggleLabel | `string` | "Show password" | no | Stable accessible name for the visibility toggle. |
| class | `string` |  | no | Additional classes on the input. |
| ...attributes | `HTML input attributes` |  | no | Other attributes are forwarded to the native input. |

## Accessibility
- The caller must supply a label associated with the input ID.
- The reveal button uses a stable name and pressed state; without JavaScript it is hidden and the input remains usable as a password field.
- Key Tab, Enter or Space: Focus the visibility button and toggle the password display.

## Agent prompts

### Install and use

Install the Haze UI component "Password input" with `npx shadcn@latest add @haze/password-input`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "https://hazeui.com/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import PasswordInput from "@/components/haze/ui/password-input.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: https://hazeui.com/ui/password-input.md

### Customize with real content

Adapt the Haze password-input ui for our product. Replace the demo values of these props with our real content: id, name, placeholder, autocomplete, required, disabled, toggleLabel, class, ...attributes. Keep the layout, the heading level rules and these accessibility details: The caller must supply a label associated with the input ID. The reveal button uses a stable name and pressed state; without JavaScript it is hidden and the input remains usable as a password field. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using password-input, check it fits: use it when people need to check what they have typed into a password field; the field has a visible or otherwise accessible label. Avoid it when the value is ordinary text, email or search; use input; no visibility control is wanted; use input with type password. Alternatives: @haze/input.

## Example requests that should lead here
- Add a labelled password field with an accessible show-password button

## Source

```astro
---
import { Eye, EyeOff } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";

interface Props {
  id: string;
  name?: string;
  placeholder?: string;
  autocomplete?: string;
  required?: boolean;
  disabled?: boolean;
  toggleLabel?: string;
  class?: string;
  /** Other native input attributes, such as aria-describedby, aria-invalid or value. */
  [key: string]: unknown;
}

const {
  id,
  name = id,
  placeholder,
  autocomplete = "current-password",
  required,
  disabled,
  toggleLabel = "Show password",
  class: className,
  ...rest
} = Astro.props;
---

<div data-haze-password-input class="relative w-full min-w-0">
  <input
    {...rest}
    id={id}
    name={name}
    type="password"
    placeholder={placeholder}
    autocomplete={autocomplete}
    required={required}
    disabled={disabled}
    data-password-field
    class={cn(
      "h-10 w-full min-w-0 rounded-lg border border-frame-input bg-field pr-12 pl-3 text-sm tracking-[-0.01em] text-foreground placeholder:text-muted-foreground",
      "transition-[border-color,background-color,box-shadow] duration-150 ease-snappy hover:border-frame-foreground",
      "focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/24 focus-visible:outline-hidden",
      "disabled:cursor-not-allowed disabled:bg-muted disabled:opacity-50 disabled:hover:border-frame-input aria-invalid:border-destructive! aria-invalid:focus-visible:ring-destructive/24",
      className,
    )}
  />
  <button
    type="button"
    hidden
    disabled={disabled}
    aria-label={toggleLabel}
    aria-controls={id}
    aria-pressed="false"
    data-password-toggle
    class="group absolute top-1/2 right-1 hidden size-7 -translate-y-1/2 cursor-pointer items-center justify-center rounded-md text-muted-foreground transition-[background-color,color,scale] duration-150 ease-snappy hover:bg-accent hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring active:scale-90 active:duration-75 disabled:cursor-not-allowed disabled:opacity-50"
  >
    <Eye aria-hidden="true" class="size-4 group-aria-pressed:hidden" />
    <EyeOff aria-hidden="true" class="hidden size-4 group-aria-pressed:block group-aria-pressed:animate-pop" />
  </button>
</div>

<script>
  function enhance(root: HTMLElement) {
    if (root.hasAttribute("data-enhanced")) return;
    const input = root.querySelector<HTMLInputElement>("[data-password-field]");
    const toggle = root.querySelector<HTMLButtonElement>("[data-password-toggle]");
    if (!input || !toggle) return;
    root.setAttribute("data-enhanced", "");
    toggle.hidden = false;
    toggle.classList.remove("hidden");
    toggle.classList.add("inline-flex");
    toggle.addEventListener("click", () => {
      if (input.disabled) return;
      const show = input.type === "password";
      input.type = show ? "text" : "password";
      toggle.setAttribute("aria-pressed", String(show));
    });
  }

  const init = () => document.querySelectorAll<HTMLElement>("[data-haze-password-input]").forEach(enhance);
  init();
  if (!document.documentElement.hasAttribute("data-haze-password-input-listener")) {
    document.documentElement.setAttribute("data-haze-password-input-listener", "");
    document.addEventListener("astro:page-load", init);
  }
</script>
```
