# Password group

A password input with a show/hide toggle.

Source: https://www.zazz.sh/api/primitives/password-group/

## Installation

```bash
pnpm dlx zazz-ui add password-group
```

Also adds `fields`, `kbd`, `button`, `input`, which it depends on.

Run `zazz-ui init` first if the project has no `zazz/` folder yet.

## Examples

### Default

```html
<form
  style="
    --display: flex-col;
    --gap: 4;
    --w: 100%;
    --max-w: 96;
  "
>
  <div data-ui="field">
    <label data-field-slot="label" for="pw-preview">Password</label>
    <ui-password>
      <label data-ui="password-group">
        <input
          data-ui="input"
          id="pw-preview"
          name="password"
          type="password"
          autocomplete="new-password"
          minlength="8"
          required
          placeholder="••••••••"
          aria-describedby="pw-preview-hint pw-preview-warning"
        />
        <span data-password-group-slot="addon" data-password-group-align="inline-end">
          <button
            data-ui="button"
            data-password-group-slot="toggle"
            type="button"
            data-button-variant="ghost"
            data-button-size="icon-sm"
            aria-pressed="false"
            aria-label="Show password"
            aria-describedby="pw-preview-warning"
          >
            <svg
              data-password-group-slot="icon icon-show"
              xmlns="http://www.w3.org/2000/svg"
              viewBox="0 0 256 256"
              aria-hidden="true"
            >
              <rect width="256" height="256" fill="none" />
              <path
                d="M128,56C48,56,16,128,16,128s32,72,112,72,112-72,112-72S208,56,128,56Z"
                fill="none"
                stroke="currentColor"
                stroke-linecap="round"
                stroke-linejoin="round"
                stroke-width="16"
              />
              <circle
                cx="128"
                cy="128"
                r="40"
                fill="none"
                stroke="currentColor"
                stroke-linecap="round"
                stroke-linejoin="round"
                stroke-width="16"
              />
            </svg>
            <svg
              data-password-group-slot="icon icon-hide"
              xmlns="http://www.w3.org/2000/svg"
              viewBox="0 0 256 256"
              aria-hidden="true"
            >
              <rect width="256" height="256" fill="none" />
              <line
                x1="48"
                y1="40"
                x2="208"
                y2="216"
                fill="none"
                stroke="currentColor"
                stroke-linecap="round"
                stroke-linejoin="round"
                stroke-width="16"
              />
              <path
                d="M154.9,157.6A40,40,0,0,1,101,98.4"
                fill="none"
                stroke="currentColor"
                stroke-linecap="round"
                stroke-linejoin="round"
                stroke-width="16"
              />
              <path
                d="M73.8,69.7C33.6,90.6,16,128,16,128s32,72,112,72a118.1,118.1,0,0,0,54.1-12.8"
                fill="none"
                stroke="currentColor"
                stroke-linecap="round"
                stroke-linejoin="round"
                stroke-width="16"
              />
              <path
                d="M208.6,169.1C229.8,149.1,240,128,240,128S208,56,128,56a126,126,0,0,0-20.5,1.6"
                fill="none"
                stroke="currentColor"
                stroke-linecap="round"
                stroke-linejoin="round"
                stroke-width="16"
              />
            </svg>
          </button>
        </span>
      </label>
    </ui-password>
    <div data-field-slot="description">
      <span data-field-slot="hint" id="pw-preview-hint">Use eight or more characters.</span>
      <span data-field-slot="error" role="alert">Password must be at least 8 characters.</span>
    </div>
    <span style="--sr: only" id="pw-preview-warning"
      >Warning: showing the password makes it visible to anyone near your screen.</span
    >
  </div>
</form>
```

## Behavior

`primitives/password-group/password-group.js`

`<ui-password>`: HTML web component for password visibility.
Light-DOM custom element that adds show/hide behavior to a
standard password field. Wrap the existing `.password-group` markup: the
element finds the input and the `[data-password-group-slot~="toggle"]` button, flips the
input between `type="password"` and `type="text"` on click, and keeps
`aria-pressed` and `aria-label` in sync. The icon swap is pure CSS, driven
by `aria-pressed` (see password-group.css).

The revealed/hidden state is a signal (`base/signals.ts`): the click handler
is the input adapter, `resolveToggleState` is the pure derivation, and one
effect is the output adapter that writes `type`, `aria-pressed`, and
`aria-label` together. The effect also runs once on connect, so the toggle
self-corrects to match the input's actual starting type even if the
markup's static attributes drift from it.

Without JavaScript the field degrades to a regular password input; the
toggle button simply does nothing.

Configuration (attributes on `<ui-password>`):
- `data-password-group-label-show`: Toggle label while the password is hidden (default "Show password").
- `data-password-group-label-hide`: Toggle label while the password is visible (default "Hide password").

## Attributes

| Attribute | Kind | Values |
| --- | --- | --- |
| `data-ui="password-group"` | identity | free value |
| `<ui-password>` | tag | free value |
| `data-password-group-align` | preset | `block-end` `block-start` `inline-end` `inline-start` |
| `data-password-group-slot` | slot | `addon` `icon` `icon-hide` `icon-show` `text` `toggle` |
| `data-password-group-label-hide` | config | free value |
| `data-password-group-label-show` | config | free value |

Requires: fields, input, button.

## Hooks

| Hook | Default | State |
| --- | --- | --- |
| `--ui-password-group-cursor` | `text` | — |
| `--ui-password-group-display` | `flex` | — |
| `--ui-password-group-flex-wrap` | `wrap` | — |
| `--ui-password-group-font-size` | `var(--font-size-sm)` | — |
| `--ui-password-group-font-weight` | `var(--font-weight-strong)` | — |
| `--ui-password-group-gap` | `var(--spacing)` | — |
| `--ui-password-group-items` | `center` | — |
| `--ui-password-group-min-h` | `var(--ui-field-h)` | — |
| `--ui-password-group-p` | `0` | — |
| `--ui-password-group-text` | `var(--color-muted-foreground)` | — |
| `--ui-password-group-textarea-min-h` | `3lh` | — |
| `--ui-password-group-textarea-px` | `calc(var(--spacing) * 2)` | — |
| `--ui-password-group-textarea-py` | `calc(var(--spacing) * 1.5)` | — |
| `--ui-password-group-w` | `100%` | — |
