# Password

A masked text field with a show/hide button and an optional strength meter. Use it for sign-in, sign-up and password change forms.

- Package: [@vigdx/ui](https://www.npmjs.com/package/@vigdx/ui)
- Selectors: `vig-password`
- Source: [libs/ui/src/lib/molecules/password/password.ts](https://github.com/bakyurekk/vigdx/blob/main/libs/ui/src/lib/molecules/password/password.ts)

```typescript title="Import"
import { Password } from '@vigdx/ui/password';
```

## Basic

Bind `[(value)]` to a string signal. The eye button reveals the text and hides it again. Set `autocomplete` (`current-password` or `new-password`) so password managers fill the right field.

```typescript title="password-basic.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { Password } from '@vigdx/ui/password';

@Component({
  selector: 'app-password-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Password],
  template: `
    <div class="max-w-sm">
      <vig-password label="Password" autocomplete="current-password" [(value)]="password" />
    </div>
    <p class="text-sm text-muted-foreground">{{ password().length }} characters typed.</p>
  `,
})
export class PasswordBasicDemo {
  protected readonly password = signal('');
}
```

## Strength meter

`feedback` shows a three-step meter under the field. By default a password is medium with two character classes and 6 characters, and strong with lower case, upper case and a digit and 8 characters. Pass a `strength` function, `(value) => 'weak' | 'medium' | 'strong'`, to use your own rules (the regex inputs `mediumRegex`/`strongRegex` are deprecated).

```typescript title="password-strength.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Password } from '@vigdx/ui/password';

@Component({
  selector: 'app-password-strength',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Password],
  template: `
    <div class="grid gap-4 sm:grid-cols-2">
      <vig-password
        label="New password"
        autocomplete="new-password"
        hint="At least 8 characters with upper case, lower case and a digit."
        [feedback]="true"
      />
      <vig-password
        label="Passphrase"
        autocomplete="new-password"
        hint="Medium from 12 characters, strong from 20."
        [strength]="passphraseStrength"
        [feedback]="true"
      />
    </div>
  `,
})
export class PasswordStrengthDemo {
  protected readonly passphraseStrength = (value: string) =>
    value.length >= 20 ? 'strong' : value.length >= 12 ? 'medium' : 'weak';
}
```

## Label on top, hint and error

`labelPosition="top"` puts the label above the field. `hint` adds help text; errors show once the field is `touched` and `invalid`, and `errorMessages` replaces the built-in text for each error kind.

```typescript title="password-hint-error.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Password } from '@vigdx/ui/password';

@Component({
  selector: 'app-password-hint-error',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Password],
  template: `
    <div class="grid gap-4 sm:grid-cols-2">
      <vig-password
        label="Password"
        labelPosition="top"
        autocomplete="current-password"
        hint="Case-sensitive."
      />
      <vig-password
        label="Password"
        labelPosition="top"
        autocomplete="new-password"
        value="abc"
        [touched]="true"
        [invalid]="true"
        [errors]="[{ kind: 'minLength' }]"
        [errorMessages]="{ minLength: 'Use at least 8 characters.' }"
      />
    </div>
  `,
})
export class PasswordHintErrorDemo {}
```

## Toggle, disabled, read-only and sizes

`[toggleMask]="false"` removes the eye button. `disabled` also disables the button; `readonly` keeps the field focusable. `size` is `sm`, `md` (default) or `lg`.

```typescript title="password-states.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Password } from '@vigdx/ui/password';

@Component({
  selector: 'app-password-states',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Password],
  template: `
    <div class="grid gap-4 sm:grid-cols-3">
      <vig-password label="No toggle" value="s3cret" [toggleMask]="false" />
      <vig-password label="Disabled" value="s3cret" [disabled]="true" />
      <vig-password label="Read-only" value="s3cret" [readonly]="true" />
    </div>
    <div class="grid gap-4 sm:grid-cols-3">
      <vig-password label="Small" size="sm" />
      <vig-password label="Medium" size="md" />
      <vig-password label="Large" size="lg" />
    </div>
  `,
})
export class PasswordStatesDemo {}
```

## Translated labels

Every text the field adds can be set per instance: `promptLabel`, `weakLabel`, `mediumLabel` and `strongLabel` for the meter, `showPasswordAriaLabel` for the button (its state is `aria-pressed`, so `hidePasswordAriaLabel` is deprecated). Without them the texts follow the locale set with `provideVigdxUi`.

```typescript title="password-labels.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Password } from '@vigdx/ui/password';

@Component({
  selector: 'app-password-labels',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Password],
  template: `
    <div class="max-w-sm">
      <vig-password
        label="Parola"
        autocomplete="new-password"
        value="Abcdef"
        promptLabel="Bir parola girin"
        weakLabel="Zayıf"
        mediumLabel="Orta"
        strongLabel="Güçlü"
        showPasswordAriaLabel="Parolayı göster"
        hidePasswordAriaLabel="Parolayı gizle"
        [feedback]="true"
      />
    </div>
  `,
})
export class PasswordLabelsDemo {}
```

## Reactive Forms

Import `VigValueAccessor` next to the component to use `formControlName`, `[formControl]` or `ngModel`. Built-in validators such as `Validators.minLength` show the kit's messages; submit the empty form to see them.

```typescript title="password-reactive-forms.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { Button } from '@vigdx/ui/button';
import { VigValueAccessor } from '@vigdx/ui/core';
import { InputField } from '@vigdx/ui/input-field';
import { Password } from '@vigdx/ui/password';

@Component({
  selector: 'app-password-reactive-forms',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [ReactiveFormsModule, Button, InputField, Password, VigValueAccessor],
  template: `
    <form class="flex flex-col gap-4" [formGroup]="form" (ngSubmit)="submit()">
      <div class="grid gap-4 sm:grid-cols-2">
        <vig-input label="Email" type="email" autocomplete="username" formControlName="email" />
        <vig-password label="Password" autocomplete="current-password" formControlName="password" />
      </div>
      <div class="flex items-center gap-3">
        <vig-button type="submit" label="Sign in" />
        <span class="text-sm text-muted-foreground" role="status">{{ status() }}</span>
      </div>
    </form>
  `,
})
export class PasswordReactiveFormsDemo {
  protected readonly form = new FormGroup({
    email: new FormControl('', {
      nonNullable: true,
      validators: [Validators.required, Validators.email],
    }),
    password: new FormControl('', {
      nonNullable: true,
      validators: [Validators.required, Validators.minLength(8)],
    }),
  });
  protected readonly status = signal('');

  protected submit(): void {
    this.form.markAllAsTouched();
    this.status.set(this.form.valid ? 'Signed in.' : '');
  }
}
```

## Signal Forms

`vig-password` implements `FormValueControl<string>`, so `[formField]` binds value, validation and `minLength` / `maxLength`. A `validate()` rule compares the confirmation with the new password.

```typescript title="password-signal-forms.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { FormField, form, minLength, required, validate } from '@angular/forms/signals';
import { Password } from '@vigdx/ui/password';

@Component({
  selector: 'app-password-signal-forms',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [FormField, Password],
  template: `
    <div class="grid gap-4 sm:grid-cols-2">
      <vig-password
        label="New password"
        autocomplete="new-password"
        [feedback]="true"
        [formField]="account.password"
      />
      <vig-password
        label="Repeat password"
        autocomplete="new-password"
        [formField]="account.confirm"
      />
    </div>
    <p class="text-sm text-muted-foreground">Valid: {{ account().valid() ? 'yes' : 'no' }}</p>
  `,
})
export class PasswordSignalFormsDemo {
  protected readonly model = signal({ password: '', confirm: '' });
  protected readonly account = form(this.model, (path) => {
    required(path.password);
    minLength(path.password, 8);
    required(path.confirm);
    validate(path.confirm, ({ value, valueOf }) =>
      value() === valueOf(path.password)
        ? undefined
        : { kind: 'mismatch', message: 'The passwords do not match.' },
    );
  });
}
```

## Styling hooks

Internal elements carry a stable `data-part` and the component's `data-scope`; both are public API, so removing one is a breaking change. Select a part by both, e.g. `[data-scope="password"][data-part="field"] { … }`: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through `aria-*` and, where listed, a `data-state`; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other `data-*` attributes listed below (such as `data-opening` while an overlay animates) are API too. See [Customizing](https://vigdx.com/docs/theming.md#customizing).

Scope: `[data-scope="password"]`

| Part | Element | data-state |
| --- | --- | --- |
| `field` | Positioned wrapper of the label and the control. |   |
| `label` | The visible (or floating) label. |   |
| `input` | The native `<input>`. |   |
| `hint` | The `vig-hint` (a `display: contents` host; style its `text` part). |   |
| `disabled-reason` | The `vig-hint` with the disabled reason (`display: contents`). |   |
| `error` | The `vig-field-error` (a `display: contents` host; style its `message` part). |   |
| `toggle` | The show-password button (`aria-pressed`). |   |
| `meter` | The strength meter (`feedback`). | `empty, weak, medium, strong` |
| `meter-segment` | One of the three meter bars. |   |
| `strength` | The strength text under the meter. |   |

## Accessibility

- The control is a native `<input type="password">` named by a real `<label for>`; it switches to `type="text"` while revealed.
- The show/hide control is a `<button>` with a constant name ("Show password" by default) whose `aria-pressed` tells whether the password is shown. While revealed, the field turns off auto-capitalization, autocorrect and spell-check.
- The strength text sits in an `aria-live="polite"` region, so screen readers announce changes while typing; the coloured bars are hidden from assistive technology.
- Hint, disabled reason and error are linked through `aria-describedby`; a touched, invalid field sets `aria-invalid="true"`.
- `autocomplete` defaults to `current-password`, or `new-password` with `feedback`, so browsers and password managers can help users (WCAG 1.3.5); set it yourself for other cases. The label floats when the browser autofills the field.

## API

### Password

Selector: `vig-password` · Kind: component

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `autocomplete` | `string \| undefined` | `undefined` | Native `autocomplete`; defaults to `new-password` with `feedback` (a password being chosen) and `current-password` otherwise, so password managers offer to generate or fill it. |
| `dirty` | `boolean` | `false` | — |
| `disabled` | `boolean` | `false` | — |
| `disabledReasons` | `readonly WithOptionalFieldTree<DisabledReason>[]` | `[]` | — |
| `errorMessages` | `Record<string, string>` | — | — |
| `errors` | `readonly ValidationError.WithOptionalFieldTree[]` | `[]` | — |
| `feedback` | `boolean` | `false` | Show the strength meter under the field. |
| `hidden` | `boolean` | `false` | — |
| `hideLabel` | `boolean` | `false` | Visually hides the label — it still names the control for assistive tech — and drops the row reserved for it, giving a shorter control for dense toolbars such as table filters. Pair it with a placeholder. |
| `hidePasswordAriaLabel` | `string \| undefined` | `undefined` | **Deprecated**: The reveal toggle keeps one name (`showPasswordAriaLabel`) and exposes its state through `aria-pressed`, so this label is no longer rendered. Removed in the next major. |
| `hint` | `string \| undefined` | `undefined` | — |
| `invalid` | `boolean` | `false` | — |
| `label` (required) | `string` | — | — |
| `labelPosition` | `LabelPosition` | `'float'` | `float` (default) rests the label inside the field; `top` sets it above a shorter field, for forms laid out with labels over their controls. |
| `maxLength` | `number \| undefined` | `undefined` | — |
| `mediumLabel` | `string \| undefined` | `undefined` | — |
| `mediumRegex` | — | `MEDIUM_REGEX` | **Deprecated**: Use the `strength` function input. A regex source rating `medium`; ignored when `strength` is set. |
| `minLength` | `number \| undefined` | `undefined` | — |
| `name` | `string` | `''` | — |
| `pending` | `boolean` | `false` | — |
| `placeholder` | `string \| undefined` | `undefined` | — |
| `promptLabel` | `string \| undefined` | `undefined` | — |
| `readonly` | `boolean` | `false` | — |
| `required` | `boolean` | `false` | — |
| `showPasswordAriaLabel` | `string \| undefined` | `undefined` | Name of the reveal toggle. It stays the same in both states — the toggle's `aria-pressed` tells whether the password is shown. |
| `size` | `FieldSize` | `'md'` | — |
| `strength` | `PasswordStrengthFn \| undefined` | `undefined` | Rates the password for the meter (template name `strength`). Unset, two character classes and 6+ characters are `medium`, lower + upper + digit and 8+ characters `strong` (PrimeNG's defaults). |
| `strongLabel` | `string \| undefined` | `undefined` | — |
| `strongRegex` | — | `STRONG_REGEX` | **Deprecated**: Use the `strength` function input. A regex source rating `strong`; ignored when `strength` is set. |
| `toggleMask` | `boolean` | `true` | Show the eye button that reveals the password. |
| `touched` | `boolean` | `false` | — |
| `value` (model) | `string` | `''` | — |
| `weakLabel` | `string \| undefined` | `undefined` | — |

#### Outputs

| Name | Payload | Description |
| --- | --- | --- |
| `touch` | `void` | — |
| `valueChange` | `string` | — |

#### Methods

| Signature | Description |
| --- | --- |
| `focus(options?: FocusOptions): void` | Moves focus to the control's interactive element (the input, or the trigger of a popup control). Signal Forms calls it through `field().focusBoundControl()`; the host element itself cannot take focus. |
| `reset(): void` | Returns the control to its pristine look: drops in-progress text, closes an open popup, clears errors the component raised itself and the submitted state of `errorDisplay: 'interacted'`. Signal Forms calls it from `field().reset()`; `VigValueAccessor` from `control.reset()`. It never changes the value: the bound form owns that. |

### VigValueAccessor

Kind: directive

Bridges the vigdx form fields to Reactive Forms (`formControl`, `formControlName`) and template-driven forms (`ngModel`). Import it next to the component; Signal Forms (`[formField]`) need no accessor.

#### Methods

| Signature | Description |
| --- | --- |
| `registerOnChange(fn: (value: unknown) => void): void` | — |
| `registerOnTouched(fn: () => void): void` | — |
| `setDisabledState(disabled: boolean): void` | — |
| `writeValue(value: unknown): void` | — |
