# Password Input

import { Callout } from "fumadocs-ui/components/callout";
import { Accordion, Accordions } from "fumadocs-ui/components/accordion";
import { Step, Steps } from "fumadocs-ui/components/steps";
import { Tabs, Tab } from "fumadocs-ui/components/tabs";
import {
  Badge,
  Card,
  CardContent,
  CardHeader,
  CardTitle,
} from "@pycolors/ui";
import {
  ArrowRight,
  CheckCircle,
  CircleAlert,
  ClipboardCheck,
  Eye,
  EyeOff,
  FormInput,
  Info,
  KeyRound,
  LockKeyhole,
  Minus,
  ShieldCheck,
  TextCursorInput,
  Type,
  UserRoundCheck,
  XCircle,
} from "lucide-react";
import { PreferAvoid } from '@/components/docs/prefer-avoid';
import { DocsLinks } from "@/components/docs/docs-links";
import { DocsFeatureGrid } from "@/components/docs/docs-feature-grid";
import { DocsConceptTabs } from "@/components/docs/docs-concept-tabs";

<div className="flex flex-wrap items-center gap-2">
  <Badge variant="outline">
    UI
  </Badge>

  <Badge>
    Password Input
  </Badge>
</div>

## Secure password fields for auth flows

The **PasswordInput** component is a specialized input for password and secret fields.

It builds on top of the base **Input** component and adds a secure show/hide toggle with keyboard and screen-reader support.

Use it for:

* login forms
* registration forms
* password reset flows
* password change forms
* account security settings
* onboarding flows that require credentials

<Callout title="Core idea">
  PasswordInput is for secret text. It should preserve password semantics while giving users a safe way to verify what they typed.
</Callout>

### Import

```tsx title="password-input-import.tsx"
import { PasswordInput }  from "@pycolors/ui";

```

### Basic usage

<Tabs items={["Preview", "Code"]}>
  <Tab value="Preview">
    <Preview>
      <PasswordInput label="Password" placeholder="••••••••" />
    </Preview>
  </Tab>

  <Tab value="Code">
    ```tsx title="basic-password-input.tsx"
    import { PasswordInput } from "@pycolors/ui";

    export function BasicPasswordInput() {
      return (
        <PasswordInput
          label="Password"
          placeholder="••••••••"
        />
      );
    }
    ```
  </Tab>
</Tabs>

## When to use PasswordInput

Use PasswordInput when the value should be hidden by default and revealed only by explicit user intent.

<DocsFeatureGrid
  columns={3}
  tone="platform"
  items={[
  {
    title: 'Authentication',
    description:
      'Use for login, registration, and reset-password forms.',
    icon: LockKeyhole,
  },
  {
    title: 'Password lifecycle',
    description:
      'Use for current password, new password, and confirmation fields.',
    icon: KeyRound,
  },
  {
    title: 'Security settings',
    description:
      'Use in account security flows where users manage credentials.',
    icon: ShieldCheck,
  },
]}
/>

### Behavior

PasswordInput is controlled internally for visibility.

It:

* defaults to `type="password"`
* toggles between `password` and `text`
* uses a semantic button for the toggle
* supports mouse and keyboard interaction
* exposes accessible toggle state with `aria-pressed`
* reserves the right icon area for the visibility toggle

<Callout type="info" title="Security rule">
  Revealing the password should always be an explicit user action. Never show secret values by default.
</Callout>

### With helper text

Use helper text to explain password requirements.

<Tabs items={["Preview", "Code"]}>
  <Tab value="Preview">
    <Preview>
      <PasswordInput label="Password" placeholder="••••••••" helperText="Use at least 8 characters with one number." />
    </Preview>
  </Tab>

  <Tab value="Code">
    ```tsx title="password-input-helper.tsx"
    import { PasswordInput } from "@pycolors/ui";

    export function PasswordInputHelper() {
      return (
        <PasswordInput
          label="Password"
          placeholder="••••••••"
          helperText="Use at least 8 characters with one number."
        />
      );
    }
    ```
  </Tab>
</Tabs>

### Validation error

Use `error` to show field-level password validation.

<Tabs items={["Preview", "Code"]}>
  <Tab value="Preview">
    <Preview>
      <PasswordInput label="Password" placeholder="••••••••" error="Password must be at least 8 characters." />
    </Preview>
  </Tab>

  <Tab value="Code">
    ```tsx title="password-input-error.tsx"
    import { PasswordInput } from "@pycolors/ui";

    export function PasswordInputError() {
      return (
        <PasswordInput
          label="Password"
          placeholder="••••••••"
          error="Password must be at least 8 characters."
        />
      );
    }
    ```
  </Tab>
</Tabs>

<Callout title="Validation rule">
  Keep password errors specific. Avoid vague messages like “Invalid password” when the user can fix the requirement.
</Callout>

### Sizes

PasswordInput supports the same size API as Input.

<Tabs items={["Preview", "Code"]}>
  <Tab value="Preview">
    <Preview>
      <div className="flex w-80 flex-col gap-3">
        <PasswordInput size="sm" label="Small" placeholder="••••••••" />

        <PasswordInput size="md" label="Medium" placeholder="••••••••" />

        <PasswordInput size="lg" label="Large" placeholder="••••••••" />
      </div>
    </Preview>
  </Tab>

  <Tab value="Code">
    ```tsx title="password-input-sizes.tsx"
    import { PasswordInput } from "@pycolors/ui";

    export function PasswordInputSizes() {
      return (
        <div className="flex w-80 flex-col gap-3">
          <PasswordInput size="sm" label="Small" placeholder="••••••••" />
          <PasswordInput size="md" label="Medium" placeholder="••••••••" />
          <PasswordInput size="lg" label="Large" placeholder="••••••••" />
        </div>
      );
    }
    ```
  </Tab>
</Tabs>

### Available sizes

| Size | Purpose                                         |
| ---- | ----------------------------------------------- |
| `sm` | Dense settings screens or compact forms         |
| `md` | Default password field size                     |
| `lg` | Auth pages, onboarding, and high-emphasis forms |

### Usage patterns

<Steps>
  <Step>
    #### Use a clear label

    Labels such as Password, Current password, New password, and Confirm password are direct and predictable.
  </Step>

  <Step>
    #### Add requirements close to the field

    Use helper text for requirements before validation, then error text when the user needs to fix something.
  </Step>

  <Step>
    #### Keep reveal behavior predictable

    The reveal toggle should only change visibility, not validation, focus, or field value.
  </Step>

  <Step>
    #### Validate outside the component

    PasswordInput displays validation state. Your schema, server, or auth layer defines the rules.
  </Step>

  <Step>
    #### Use native autocomplete

    Add `autoComplete` values such as `current-password`, `new-password`, or `one-time-code` when relevant.
  </Step>
</Steps>

## PasswordInput vs Input

| Situation                         | Use                                  |
| --------------------------------- | ------------------------------------ |
| Password field                    | PasswordInput                        |
| Current password                  | PasswordInput                        |
| New password                      | PasswordInput                        |
| Confirm password                  | PasswordInput                        |
| Email, name, URL, search          | Input                                |
| Secret token with reveal behavior | PasswordInput or custom secret input |
| Long secret value                 | Custom textarea-like secret field    |

<Callout title="Decision rule">
  Use PasswordInput when the value is secret and benefits from a visibility toggle.
</Callout>

## API

#### Props

PasswordInput exposes the same API as Input, except:

* `type` is controlled internally
* `rightIcon` is reserved for the visibility toggle

| Prop           | Type                   | Default | Description                        |
| -------------- | ---------------------- | ------- | ---------------------------------- |
| `label`        | `string`               | —       | Accessible label above the field   |
| `helperText`   | `string`               | —       | Helper text below the field        |
| `error`        | `string`               | —       | Validation error message           |
| `leftIcon`     | `React.ReactNode`      | —       | Optional icon rendered on the left |
| `size`         | `"sm" \| "md" \| "lg"` | `"md"`  | Visual density                     |
| `className`    | `string`               | —       | Tailwind override                  |
| `autoComplete` | `string`               | —       | Native autocomplete hint           |

All other supported Input props are forwarded.

### TypeScript types

```ts title="password-input-types.ts"
import type { InputProps } from "@pycolors/ui";

export interface PasswordInputProps
  extends Omit<InputProps, "type" | "rightIcon"> {}
```

### Accessibility

PasswordInput is designed for accessible credential entry.

Guidelines:

* the visibility toggle is a real button
* the toggle should expose `aria-label`
* the toggle should expose `aria-pressed`
* icons should be decorative and use `aria-hidden`
* the input preserves native password semantics
* labels, helper text, and errors follow Input behavior
* use appropriate `autoComplete` values
* do not remove focus rings from the toggle or field

Recommended autocomplete values:

| Context           | `autoComplete`     |
| ----------------- | ------------------ |
| Login password    | `current-password` |
| Register password | `new-password`     |
| Reset password    | `new-password`     |
| Confirm password  | `new-password`     |

<Callout title="Accessibility rule">
  The reveal control must be keyboard accessible and must clearly announce whether the password is currently shown or hidden.
</Callout>

## Prefer / avoid

<PreferAvoid
  prefer={[
  'clear password labels',
  'helper text for requirements',
  'specific validation messages',
  'native autocomplete values',
  'accessible reveal toggle',
]}
  avoid={[
  'showing passwords by default',
  'vague errors like “Invalid password”',
  'custom right icons that conflict with reveal',
  'placeholder-only password fields',
  'validation rules hidden inside the UI component',
]}
/>

### Product copy guidelines

Password copy should reduce friction without weakening security expectations.

<DocsConceptTabs
  items={[
  {
    value: 'Labels',
    title: 'Label examples',
    icon: Type,
    description: (
      <>
        <p>
          Labels should stay explicit, predictable, and easy to scan.
        </p>

        <div className="mt-3 flex flex-wrap gap-2">
          <Badge variant="outline">Password</Badge>
          <Badge variant="outline">Current password</Badge>
          <Badge variant="outline">New password</Badge>
          <Badge variant="outline">Confirm password</Badge>
        </div>
      </>
    ),
  },
  {
    value: 'Helper text',
    title: 'Helper text examples',
    icon: Info,
    description: (
      <>
        <p>
          Use helper text to explain expectations before validation fails.
        </p>

        <div className="mt-3 rounded-[5px] border border-border-subtle bg-muted/30 px-3 py-2 text-sm text-muted-foreground">
          Use at least 8 characters. Use a password you do not use elsewhere.
        </div>
      </>
    ),
  },
  {
    value: 'Errors',
    title: 'Error examples',
    icon: CircleAlert,
    description: (
      <>
        <p>
          Error messages should explain the problem clearly and stay actionable.
        </p>

        <div className="mt-3 space-y-2">
          <div className="rounded-[5px] border border-destructive/20 bg-destructive/5 px-3 py-2 text-sm text-destructive">
            Password must be at least 8 characters.
          </div>

          <div className="rounded-[5px] border border-destructive/20 bg-destructive/5 px-3 py-2 text-sm text-destructive">
            Current password is required.
          </div>
        </div>
      </>
    ),
  },
]}
/>

## Common questions

<Accordions type="single">
  <Accordion title="Can I pass a custom rightIcon?">
    No. `rightIcon` is reserved for the visibility toggle to keep behavior predictable.
  </Accordion>

  <Accordion title="Should PasswordInput validate strength?">
    No. PasswordInput displays validation state. Strength rules should live in your schema, auth layer, or server validation.
  </Accordion>

  <Accordion title="Should the password be visible by default?">
    No. Passwords should be hidden by default and revealed only through explicit user action.
  </Accordion>

  <Accordion title="Can I use PasswordInput for API keys or tokens?">
    Sometimes. If the secret is short and benefits from reveal behavior, PasswordInput can work. For long tokens, build a dedicated secret field.
  </Accordion>
</Accordions>

## Related

<DocsLinks
  variant="list"
  tone="platform"
  items={[
  {
    title: 'Input',
    href: '/docs/ui/input',
    description:
      'Use Input for single-line form fields next to switch-based settings.',
    icon: FormInput,
  },
  {
    title: 'Textarea',
    href: '/docs/ui/textarea',
    description:
      'Use Textarea for longer settings descriptions, notes, and feedback.',
    icon: TextCursorInput,
  },
  {
    title: 'Separator',
    href: '/docs/ui/separator',
    description:
      'Use Separator to structure grouped switch sections and preferences.',
    icon: Minus,
  },
  {
    title: 'Forms guide',
    href: '/docs/guides/forms',
    description:
      'Build accessible forms with labels, descriptions, validation, and clear actions.',
    icon: ClipboardCheck,
  },
]}
/>