Password input
A native password field with a keyboard-accessible visibility toggle.
pnpm dlx shadcn@latest add @haze/password-input---
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>When to use
- People need to check what they have typed into a password field
- The field has a visible or otherwise accessible label
When not to use
- The value is ordinary text, email or search; use input
- No visibility control is wanted; use input with type password
Installation
Add the Haze registry to components.json once, then install Password input with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/password-inputnpx shadcn@latest add @haze/password-inputyarn dlx shadcn@latest add @haze/password-inputbunx --bun shadcn@latest add @haze/password-inputFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | – | Unique input ID referenced by the field's label. |
| name | string | id | Name submitted with the form. |
| placeholder | string | – | Optional example text; not a replacement for a label. |
| autocomplete | string | "current-password" | Autofill hint; use new-password when creating a password. |
| required | boolean | – | Marks the native input required. |
| disabled | boolean | – | Disables the field and visibility control. |
| toggleLabel | string | "Show password" | Stable accessible name for the visibility toggle. |
| class | string | – | Additional classes on the input. |
| ...attributes | HTML input attributes | – | Other attributes are forwarded to the native input. |
Keyboard interactions
- Tab, Enter or Space
- Focus the visibility button and toggle the password display.
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.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
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": "http://localhost:4329/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: http://localhost:4329/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.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.