Controls and surfaces
password-field
A password input with a button that shows or hides what was typed.
When to use it
Every place a password is typed. The value is hidden again when the form is sent, and the button keeps the caret where it was. Set autoComplete to "current-password" (signing in) or "new-password" (choosing one) so a password manager knows which to offer. For a code sent by message use otp-field.
See it working
The demonstration the browser checks drive, with the keyboard as well as the pointer. More of it is on the Primitives page.
Alert dialog
The project is kept.
Collapsible
The options are closed.
Hover card
The first published program was written by Ada Lovelace.
Menubar
Command: none yet. Ruler: hidden. Zoom: 100.
Navigation menu
Toolbar
Styles: none. Saved 0 times.
One-time code
Typed: nothing yet. Sent: nothing yet.
Password
Words for screen readers
Rosalind Franklin
Add it
npx shadcn@latest add @rivier-ui/password-fieldThe registry is named once in components.json: see Installation.
Example
Import it from @/components/ui/password-field.
<Field>
<FieldLabel>Password</FieldLabel>
<PasswordField>
<FieldControl><PasswordFieldInput name="password" autoComplete="current-password" /></FieldControl>
<PasswordFieldToggle />
</PasswordField>
</Field>What it needs
A client component: it uses state, effects or browser behaviour. Packages: class-variance-authority, lucide-react, radix-ui.
It imports
Source
The whole file. Once copied it is yours: the registry item holds the same text.
"use client";
/**
* @name password-field
* @category ui
* @description A password input with a button that shows or hides what was typed.
* @when Every place a password is typed. The value is hidden again when the form is sent, and the button keeps the caret where it was. Set `autoComplete` to "current-password" (signing in) or "new-password" (choosing one) so a password manager knows which to offer. For a code sent by message use otp-field.
* @example
* <Field>
* <FieldLabel>Password</FieldLabel>
* <PasswordField>
* <FieldControl><PasswordFieldInput name="password" autoComplete="current-password" /></FieldControl>
* <PasswordFieldToggle />
* </PasswordField>
* </Field>
*/
import { unstable_PasswordToggleField as PasswordPrimitive } from "radix-ui";
import type { VariantProps } from "class-variance-authority";
import { Eye, EyeOff } from "lucide-react";
import type { ComponentProps } from "react";
import { inputVariants } from "@/components/ui/input";
import { cn } from "@/lib/cn";
export type PasswordFieldProps = ComponentProps<"div"> & Pick<ComponentProps<typeof PasswordPrimitive.Root>, "visible" | "defaultVisible" | "onVisibilityChange">;
/** Holds whether the password is shown: `visible`, `defaultVisible`, `onVisibilityChange`. */
export function PasswordField({ className, visible, defaultVisible, onVisibilityChange, children, ...props }: PasswordFieldProps) {
return (
<PasswordPrimitive.Root visible={visible} defaultVisible={defaultVisible} onVisibilityChange={onVisibilityChange}>
<div data-slot="password-field" className={cn("relative w-full", className)} {...props}>
{children}
</div>
</PasswordPrimitive.Root>
);
}
/** `size` is the control's height (sm, md, lg), as on input. */
export type PasswordFieldInputProps = Omit<ComponentProps<typeof PasswordPrimitive.Input>, "size"> & VariantProps<typeof inputVariants>;
/** The input, with room at the end for the button. */
export function PasswordFieldInput({ className, size, ...props }: PasswordFieldInputProps) {
return <PasswordPrimitive.Input data-slot="password-field-input" className={cn(inputVariants({ size }), "pe-11", className)} {...props} />;
}
export type PasswordFieldToggleProps = ComponentProps<typeof PasswordPrimitive.Toggle> & {
/** The button's accessible name in each state. Translate it with the rest of the interface. */
labels?: { show: string; hide: string };
};
/** The button at the end of the input. Its children replace the eye. */
export function PasswordFieldToggle({ className, labels = { show: "Show password", hide: "Hide password" }, children, ...props }: PasswordFieldToggleProps) {
return (
<PasswordPrimitive.Slot
render={({ visible }) => (
<PasswordPrimitive.Toggle
data-slot="password-field-toggle"
aria-label={visible ? labels.hide : labels.show}
className={cn(
"absolute inset-y-0 end-0 inline-flex aspect-square items-center justify-center rounded-control text-muted-foreground -outline-offset-2 transition-[color] duration-instant ease-out hover:text-foreground disabled:pointer-events-none disabled:opacity-50 motion-reduce:transition-none",
className,
)}
{...props}
>
{children ?? (
<PasswordPrimitive.Icon
aria-hidden="true"
className="size-4"
visible={<EyeOff strokeWidth={1.5} />}
hidden={<Eye strokeWidth={1.5} />}
/>
)}
</PasswordPrimitive.Toggle>
)}
/>
);
}