# Password Input

A password field with a button that shows or hides what was typed.

A password field with a button that shows or hides what was typed. A password field with a show / hide Button at its end: an InputGroup (the Input's look and sizes) whose end addon is a ghost, xs, square, muted Button with a tooltip, as Search's clear button. Ark UI's password-input switches the Input's type, names the Button (Show password / Hide password) and keeps the caret in the field when the Button is pressed with the pointer. Inside a Field it is wired to the label, helper and error.

Kind: component · Category: Inputs · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `password-input` machine, through Ark UI

## Add

```bash
npx fndn add password-input
```

It writes the folder `password-input/` into the project's ui folder (`index.ts`, `password-input.tsx`, `PasswordInput.vue`, `PasswordInput.svelte`, `password-input.recipe.ts`), and what it is built from: [Input Group](/components/input-group.md), [Button](/components/button.md), [Tooltip](/components/tooltip.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { PasswordInput } from "@/components/ui/password-input";
```

```vue
import { PasswordInput } from "@/components/ui/password-input";
```

```svelte
import { PasswordInput } from "$lib/components/ui/password-input";
```

## Parts

In the DOM the root carries `data-slot="password-input"` and each part `data-slot="password-input-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `password-input` |  | Ark's password-input root around the InputGroup; class and style land here. |
| `input` | `input` | `password-input-input` |  | The Input inside the InputGroup (data-password-input="input"): type password, or text while shown; spellcheck and autocapitalize off; autocomplete current-password or new-password. |
| `visibility-trigger` | `button` | `password-input-visibility-trigger` |  | The Button (data-password-input="visibility-trigger"): ghost, xs, square, muted; an eye while hidden, an eye with a slash while shown. aria-label and tooltip say what a press does; aria-controls the input. |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string` |  | Bindable (React: `defaultValue` and `onValueChange`; Vue: `v-model:value`; Svelte: `bind:value`). The Input's value. Controlled: value with onChange. Uncontrolled: defaultValue. |
| `visible` | `boolean` | `false` | Bindable (React: `defaultVisible` and `onVisibleChange`; Vue: `v-model:visible`; Svelte: `bind:visible`). The password shown as text. Controlled with onVisibleChange; uncontrolled: defaultVisible. |
| `size` | `"xs" \| "sm" \| "md" \| "lg"` | `"md"` | The Input's sizes (`lib/control.ts`): xs 24px, sm 28px, md 32px, lg 36px. |
| `variant` | `"outline" \| "subtle" \| "ghost"` | `"outline"` | The shared control's looks (`lib/control.ts`), at every size. outline: the form field (field colour, hairline). subtle: the inspector (inset grey, no edge). ghost: nothing until touched. Focused, open and invalid look the same in every variant. |
| `invalid` | `boolean` | `false` |  |
| `disabled` | `boolean` | `false` |  |
| `autoComplete` | `"current-password" \| "new-password"` | `"current-password"` |  |
| `ignorePasswordManagers` | `boolean` | `false` | Ask password managers not to fill or offer this field. |
| `showLabel` | `string` | `"Show password"` | The Button's name while the password is hidden. |
| `hideLabel` | `string` | `"Hide password"` | The Button's name while the password is shown. |
| `start` | `node (children / slot)` |  | Addon before the field (a lock icon). |
| `placeholder` | `string` |  |  |
| `name` | `string` |  | Form field name. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `visibleChange` | (visible: boolean) after a press on the Button |

## States

`visible`, `hover`, `focus`, `invalid`, `disabled`

## Keyboard and accessibility

Keyboard:

- Tab: the field, then the Button
- Space / Enter on the Button: show or hide; focus stays on the Button

Notes:

- The Button's aria-label says what a press does (Show password / Hide password) and changes with it; aria-controls points at the input.
- A pointer press on the Button keeps the caret in the field.
- Needs a name: a Field label or an aria-label.

## Motion

- the InputGroup's ring and fill; the Button's press scale and hover fill (pointer only)
- the icon swaps at once, from the pointer and the keyboard alike

## Tokens

`color.ink-3`

## Dependencies

- [Input Group](/components/input-group.md)
- [Button](/components/button.md)
- [Tooltip](/components/tooltip.md)

## Examples

### Password

React · `password.tsx`

```tsx
import { Field } from "@/components/ui/field";
import { PasswordInput } from "@/components/ui/password-input";

/* the eye button shows the password while you check it; current-password lets managers fill it */
export default function Password() {
  return (
    <Field label="Password" className="w-full max-w-80">
      <PasswordInput autoComplete="current-password" defaultValue="tidal-ledger-9-orbit" />
    </Field>
  );
}
```

Vue · `password.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Field } from "@/components/ui/field";
import { PasswordInput } from "@/components/ui/password-input";

const password = ref("tidal-ledger-9-orbit");
</script>

<!-- the eye button shows the password while you check it; current-password lets managers fill it -->
<template>
  <Field label="Password" class="w-full max-w-80">
    <PasswordInput v-model:value="password" auto-complete="current-password" />
  </Field>
</template>
```

Svelte · `password.svelte`

```svelte
<script lang="ts">
  import { Field } from "$lib/components/ui/field";
  import { PasswordInput } from "$lib/components/ui/password-input";

  let password = $state("tidal-ledger-9-orbit");
</script>

<!-- the eye button shows the password while you check it; current-password lets managers fill it -->
<Field label="Password" class="w-full max-w-80">
  <PasswordInput bind:value={password} autoComplete="current-password" />
</Field>
```

### Sign in

React · `sign-in.tsx`

```tsx
import { Button } from "@/components/ui/button";
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import { PasswordInput } from "@/components/ui/password-input";

export default function SignIn() {
  return (
    <form className="flex w-full max-w-80 flex-col gap-4" onSubmit={(event) => event.preventDefault()}>
      <Field label="Work email">
        <Input type="email" autoComplete="email" placeholder="maya@northwind.example" />
      </Field>
      <Field label="Password">
        <PasswordInput autoComplete="current-password" />
      </Field>
      <Button type="submit" variant="primary">Sign in</Button>
    </form>
  );
}
```

Vue · `sign-in.vue`

```vue
<script setup lang="ts">
import { Button } from "@/components/ui/button";
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import { PasswordInput } from "@/components/ui/password-input";
</script>

<template>
  <form class="flex w-full max-w-80 flex-col gap-4" @submit.prevent>
    <Field label="Work email">
      <Input type="email" autocomplete="email" placeholder="maya@northwind.example" />
    </Field>
    <Field label="Password">
      <PasswordInput auto-complete="current-password" />
    </Field>
    <Button type="submit" variant="primary">Sign in</Button>
  </form>
</template>
```

Svelte · `sign-in.svelte`

```svelte
<script lang="ts">
  import { Button } from "$lib/components/ui/button";
  import { Field } from "$lib/components/ui/field";
  import { Input } from "$lib/components/ui/input";
  import { PasswordInput } from "$lib/components/ui/password-input";
</script>

<form class="flex w-full max-w-80 flex-col gap-4" onsubmit={(event) => event.preventDefault()}>
  <Field label="Work email">
    <Input type="email" autocomplete="email" placeholder="maya@northwind.example" />
  </Field>
  <Field label="Password">
    <PasswordInput autoComplete="current-password" />
  </Field>
  <Button type="submit" variant="primary">Sign in</Button>
</form>
```

### Choose a new password

React · `new-password.tsx`

```tsx
import { useState } from "react";
import { Field } from "@/components/ui/field";
import { PasswordInput } from "@/components/ui/password-input";

/* new-password: password managers offer to generate one; the error replaces the helper while too short */
export default function NewPassword() {
  const [password, setPassword] = useState("swordfish");
  const short = password.length > 0 && password.length < 12;
  return (
    <Field label="New password" helper="At least 12 characters." error={`Too short: ${12 - password.length} more characters.`} invalid={short} className="w-full max-w-80">
      <PasswordInput autoComplete="new-password" value={password} onChange={(event) => setPassword(event.target.value)} />
    </Field>
  );
}
```

Vue · `new-password.vue`

```vue
<script setup lang="ts">
import { computed, ref } from "vue";
import { Field } from "@/components/ui/field";
import { PasswordInput } from "@/components/ui/password-input";

const password = ref("swordfish");
const short = computed(() => password.value.length > 0 && password.value.length < 12);
</script>

<!-- new-password: password managers offer to generate one; the error replaces the helper while too short -->
<template>
  <Field label="New password" helper="At least 12 characters." :error="`Too short: ${12 - password.length} more characters.`" :invalid="short" class="w-full max-w-80">
    <PasswordInput v-model:value="password" auto-complete="new-password" />
  </Field>
</template>
```

Svelte · `new-password.svelte`

```svelte
<script lang="ts">
  import { Field } from "$lib/components/ui/field";
  import { PasswordInput } from "$lib/components/ui/password-input";

  let password = $state("swordfish");
  const short = $derived(password.length > 0 && password.length < 12);
</script>

<!-- new-password: password managers offer to generate one; the error replaces the helper while too short -->
<Field label="New password" helper="At least 12 characters." error={`Too short: ${12 - password.length} more characters.`} invalid={short} class="w-full max-w-80">
  <PasswordInput bind:value={password} autoComplete="new-password" />
</Field>
```

### Confirm a sensitive change

React · `confirm-action.tsx`

```tsx
import { Field } from "@/components/ui/field";
import { PasswordInput } from "@/components/ui/password-input";
import { Lock } from "@phosphor-icons/react";

/* asked again before a sensitive change; the lock marks the field as a confirmation */
export default function ConfirmAction() {
  return (
    <Field label="Confirm with your password" helper="Required to rotate the production API key." className="w-full max-w-80">
      <PasswordInput start={<Lock weight="bold" aria-hidden />} autoComplete="current-password" />
    </Field>
  );
}
```

Vue · `confirm-action.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { PhLock } from "@phosphor-icons/vue";
import { Field } from "@/components/ui/field";
import { PasswordInput } from "@/components/ui/password-input";

const password = ref("");
</script>

<!-- asked again before a sensitive change; the lock marks the field as a confirmation -->
<template>
  <Field label="Confirm with your password" helper="Required to rotate the production API key." class="w-full max-w-80">
    <PasswordInput v-model:value="password" auto-complete="current-password">
      <template #start><PhLock weight="bold" aria-hidden="true" /></template>
    </PasswordInput>
  </Field>
</template>
```

Svelte · `confirm-action.svelte`

```svelte
<script lang="ts">
  import Lock from "phosphor-svelte/lib/LockIcon";
  import { Field } from "$lib/components/ui/field";
  import { PasswordInput } from "$lib/components/ui/password-input";

  let password = $state("");
</script>

<!-- asked again before a sensitive change; the lock marks the field as a confirmation -->
{#snippet start()}<Lock weight="bold" aria-hidden="true" />{/snippet}
<Field label="Confirm with your password" helper="Required to rotate the production API key." class="w-full max-w-80">
  <PasswordInput bind:value={password} {start} autoComplete="current-password" />
</Field>
```

## Recipe

`password-input.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* password-input — an InputGroup whose end addon is the show / hide Button (Ark UI's
 * password-input: the input's type, the trigger's label and aria-controls, focus kept in the
 * field when the trigger is pressed with the pointer). Every look is the InputGroup's, the Input's
 * and the Button's (ghost, xs, square, muted, as Search's clear button); the root only places
 * the group. The trigger's icon swaps at once: it states what a press will do next. */
export const passwordInput = tv({
  slots: {
    base: "flex w-full min-w-0",
    input: "",
    visibilityTrigger: "",
  },
});

export type PasswordInputRecipeProps = VariantProps<typeof passwordInput>;
```
