# Key-Value

An interactive key-value table component for managing dynamic pairs of keys and values, supporting per-row sensitivity, multiline textareas, granular field locking, and native form integration.

## Usage

The Key-Value component (`<z-kv>`) is designed for configuration panels, environment variable managers, metadata editors, and any interface requiring structured key-value pair inputs. Common use cases include:

- Environment variable and secret management (
`.env` style editors)
- HTTP request header and query parameter builders
- SEO metadata and custom field editors
- Application and webhook configuration dashboards
- Dynamic product attributes and specification tables

Once everything is configured, you may use the `<z-kv>` markup in your HTML code:

```html
<!-- Basic key-value table (starts with 1 empty row by default) -->
<z-kv></z-kv>

<!-- With form integration and pre-populated rows -->
<form method="POST" action="/save-settings">
  <z-kv aria-label="Environment Variables">
    <select data-fn="data-source" hidden>
      <option data-key="APP_ENV" value="production"></option>
      <option data-key="API_SECRET" value="" data-sensitive></option>
    </select>
  </z-kv>
  <button type="submit">Save</button>
</form>

```

## Data source

Initial rows and fine-grained per-row behaviors are configured using a hidden `<select data-fn="data-source">` element inside `<z-kv>`. Each `<option>` represents a single row in the table:

```html
<z-kv>
  <select data-fn="data-source" hidden>
    <option data-key="SITE_NAME" value="Acme Corp"></option>
    <option data-key="SUPPORT_EMAIL" value="help@acme.com"></option>
  </select>
</z-kv>

```

You can also pass the key as the text content of the `<option>` if `data-key` is omitted (as long as the text differs from the `value` attribute):

```html
<z-kv>
  <select data-fn="data-source" hidden>
    <option value="Acme Corp">SITE_NAME</option>
  </select>
</z-kv>

```

### Reactive data source

Adding the `data-reactive` attribute to `<select data-fn="data-source">` enables a `MutationObserver` that automatically syncs and re-renders the component whenever `<option>` elements or their attributes are modified externally in the DOM:

```html
<z-kv>
  <select data-fn="data-source" data-reactive hidden>
    <option data-key="FEATURE_FLAG" value="true"></option>
  </select>
</z-kv>

```

### Per-row configuration

Each `<option>` inside `<select data-fn="data-source">` supports the following attributes for granular control over its row:

| Attribute | Aliases | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `data-key` | `<option>` text | `string` | `""` | Sets the key name for the row and binds it to the value input's `name` attribute. |
| `value` | — | `string` | `""` | Sets the value for the row. |
| `data-sensitive` | `data-type="password"` | `boolean` | `false` | Enables password masking, a random value generator button (`wand`), and a visibility toggle button (`eye`/`eye-off`) for the row. |
| `data-textarea` | `data-type="textarea"` | `boolean` | `false` | Renders a multiline `<textarea>` instead of a single-line `<input>` for the value field. |
| `data-rows` | — | `number` | `3` | Sets the `rows` attribute when the row is rendered as a `<textarea>`. |
| `data-removable` | — | `boolean` | `true` | Controls whether the row can be deleted. Set `data-removable="false"` to lock the row against removal. |
| `data-readonly` | — | `boolean` | `false` | Marks both the key and value fields as `readonly` and prevents DOM tampering. |
| `data-key-readonly` | `data-readonly-key` | `boolean` | `false` | Marks only the key input as `readonly` (strict key mode) while keeping the value editable. |
| `data-value-readonly` | `data-readonly-value` | `boolean` | `false` | Marks only the value field as `readonly` while keeping the key editable. |
| `disabled` | — | `boolean` | `false` | Disables the entire row (key input, value field, action buttons, and removal). |
| `data-key-disabled` | `data-disabled-key` | `boolean` | `false` | Disables only the key input for the row. |
| `data-value-disabled` | `data-disabled-value` | `boolean` | `false` | Disables only the value field and its sensitive action buttons for the row. |
| `data-key-placeholder` | `data-placeholder-key` | `string` | — | Overrides the key input placeholder text for this specific row. |
| `data-value-placeholder` | `data-placeholder-value`, `data-placeholder` | `string` | — | Overrides the value input/textarea placeholder text for this specific row. |

## Row features

### Sensitive rows and random value generation

When an `<option>` has `data-sensitive` (or `data-type="password"`), that specific row masks its value and renders two additional controls:

1. **Random generator button (`wand`):** Generates a 16-character random string when clicked, automatically reveals the value, and moves focus to the value field. To prevent accidental overwrites, the generator button is automatically disabled once the value field is non-empty, or if the value field is disabled/readonly.
2. **Visibility toggle button (`eye` / `eye-off`):** Toggles the value field between masked and plain text modes.

```html
<z-kv>
  <select data-fn="data-source" hidden>
    <!-- Standard plain-text row -->
    <option data-key="PUBLIC_URL" value="[https://example.com](https://example.com)"></option>

    <!-- Sensitive row with empty value (wand button enabled) -->
    <option data-key="WEBHOOK_SECRET" value="" data-sensitive></option>

    <!-- Sensitive row with existing value (masked by default, wand disabled) -->
    <option data-key="STRIPE_KEY" value="sk_live_987654321" data-sensitive></option>
  </select>
</z-kv>

```

### Multiline textarea rows

Use `data-textarea` (or `data-type="textarea"`) on an `<option>` to render a `<textarea>` for long values such as JSON payloads, descriptions, or SSH keys. You can customize the visible height using `data-rows` (defaults to `3`), and even combine `data-textarea` with `data-sensitive` (which masks the textarea using `-webkit-text-security: disc`):

```html
<z-kv>
  <select data-fn="data-source" hidden>
    <option
      data-key="WEBHOOK_PAYLOAD"
      value='{"event": "user.created"}'
      data-textarea
      data-rows="4"
    ></option>
    <option
      data-key="SSH_PRIVATE_KEY"
      value=""
      data-textarea
      data-rows="5"
      data-sensitive
    ></option>
  </select>
</z-kv>

```

### Strict keys, readonly fields, and non-removable rows

For predefined schema keys (such as `meta-description` or system keys) where users should edit the value without renaming or deleting the key, combine `data-key-readonly` and `data-removable="false"`. Built-in tamper guards automatically revert unauthorized DOM edits if a user attempts to modify a `readonly` or `disabled` field via browser DevTools.

```html
<z-kv>
  <select data-fn="data-source" hidden>
    <!-- Strict key: user can edit the value, but cannot rename or delete the row -->
    <option
      data-key="meta-description"
      value="Default page description"
      data-key-readonly
      data-textarea
      data-removable="false"
    ></option>

    <!-- Fully locked system row: neither key nor value can be edited or removed -->
    <option
      data-key="SYSTEM_ID"
      value="sys_01HXYZ"
      data-readonly
      data-removable="false"
    ></option>

    <!-- Disabled legacy row -->
    <option
      data-key="LEGACY_TOKEN"
      value="deprecated_val"
      disabled
    ></option>
  </select>
</z-kv>

```

### Row limits and non-insertable mode

Control how many rows can exist in the table using `min`, `max`, and `noninsertable`:

- 
**`min`:** Enforces a minimum number of rows (defaults to `1`). If the data source has fewer rows than `min`, empty rows are automatically appended on initialization, and the remove button is disabled once the row count reaches `min`.
- 
**`max`:** Caps the total number of rows (defaults to `0` for unlimited). Once reached, the "Add" button is disabled.
- 
**`noninsertable`:** Hides the bottom "Add" button row completely and disables the `Ctrl/Cmd + Enter` shortcut.

```html
<!-- Fixed schema table: cannot add new rows, cannot delete existing rows -->
<z-kv noninsertable>
  <select data-fn="data-source" hidden>
    <option data-key="meta-title" value="" data-key-readonly data-removable="false"></option>
    <option data-key="meta-description" value="" data-key-readonly data-textarea data-removable="false"></option>
  </select>
</z-kv>

<!-- Dynamic table allowing between 2 and 5 rows -->
<z-kv min="2" max="5"></z-kv>

```

## Form integration

Because `<z-kv>` renders in the Light DOM, every row's value `<input>` or `<textarea>` automatically binds its `name` attribute to that row's key (`option.data.key`).

- When a row's key is empty, the corresponding value field is automatically 
`disabled` so unnamed values are never submitted.
- Once a key is entered, the value field is enabled and participates directly in native 
`<form>` submission (`FormData`) under the key's name.

```html
<form method="POST" action="/api/config">
  <z-kv>
    <select data-fn="data-source" hidden>
      <option data-key="HOST" value="localhost"></option>
      <option data-key="PORT" value="8080"></option>
    </select>
  </z-kv>
  <button type="submit">Save Configuration</button>
</form>
<!-- Submits: HOST=localhost&PORT=8080 -->

```

## Icons

By default, all internal icon slots render empty so you can supply your own SVG icons without bundle bloat. You can provide icons globally via `<template id="z-icons">` or locally per component via `<template data-fn="icons">`:

```html
<z-kv>
  <template data-fn="icons">
    <svg data-key="plus" viewBox="0 0 24 24" width="16" height="16"><!-- ... --></svg>
    <svg data-key="wand" viewBox="0 0 24 24" width="16" height="16"><!-- ... --></svg>
    <svg data-key="eye" viewBox="0 0 24 24" width="16" height="16"><!-- ... --></svg>
    <svg data-key="eye-off" viewBox="0 0 24 24" width="16" height="16"><!-- ... --></svg>
    <svg data-key="trash-2" viewBox="0 0 24 24" width="16" height="16"><!-- ... --></svg>
  </template>
</z-kv>

```

### Available icon keys

| Icon Key | Element Target | Description |
| --- | --- | --- |
| `plus` | `add-button` | Displayed inside the bottom "Add" button next to the button label. |
| `wand` | `random-button` | Displayed inside the random value generator button on sensitive rows. |
| `eye` | `toggle-button` | Displayed when a sensitive row's value is currently masked (click to reveal). |
| `eye-off` | `toggle-button` | Displayed when a sensitive row's value is currently visible (click to mask). |
| `trash-2` | `remove-button` | Displayed inside the remove row button in the actions column. |

## Internationalization

The Key-Value component supports internationalization through multiple methods with the following priority order (highest to lowest):

- 
**Component-level i18n** (via `i18n` attribute or `<script data-fn="config">`)
- 
**Global component-specific namespace** (via `<script id="z-i18n">`)
- 
**Default values**

### Using the i18n attribute

```html
<z-kv
  i18n='{
    "header-key": "Schlüssel",
    "header-value": "Wert",
    "add-button-text": "Hinzufügen"
  }'
></z-kv>

```

### Using a configuration script

```html
<z-kv>
  <script type="application/json" data-fn="config">
    {
      "i18n": {
        "header-key": "Variable",
        "header-value": "Secret",
        "placeholder-key": "e.g. API_KEY",
        "placeholder-value": "Paste secret..."
      }
    }
  </script>
</z-kv>

```

### Using global i18n

Place this in your document `<head>` or before any `<z-kv>` components:

```html
<script id="z-i18n" type="application/json">
{
  "z-kv": {
    "header-key": "Schlüssel",
    "header-value": "Wert",
    "placeholder-key": "Schlüssel eingeben",
    "placeholder-value": "Wert eingeben",
    "add-button-text": "Hinzufügen",
    "add-row-label": "Zeile hinzufügen",
    "remove-row-label": "Zeile {index} entfernen",
    "toggle-visibility-label": "Sichtbarkeit für Zeile {index} umschalten",
    "generate-random-label": "Zufallswert für Zeile {index} generieren",
    "table-label": "Schlüssel-Wert-Paare",
    "actions-label": "Aktionen",
    "key-label": "Schlüssel für Zeile {index}",
    "value-label": "Wert für Zeile {index}"
  }
}
</script>

```

### Available i18n options

| Key | Default | Description |
| --- | --- | --- |
| `header-key` | `"Key"` | Column header text for the key column. |
| `header-value` | `"Value"` | Column header text for the value column. |
| `actions-label` | `"Actions"` | Column header text and ARIA group label for the actions column. |
| `placeholder-key` | `"Enter key"` | Default placeholder for key input fields (unless overridden per row). |
| `placeholder-value` | `"Enter value"` | Default placeholder for value input/textarea fields (unless overridden per row). |
| `add-button-text` | `"Add"` | Visible text label inside the bottom add button. |
| `add-row-label` | `"Add row"` | `aria-label` for the bottom add button. |
| `remove-row-label` | `"Remove row {index}"` | `aria-label` for a row's remove button. Supports `{index}` replacement (1-based). |
| `toggle-visibility-label` | `"Toggle visibility for row {index}"` | `aria-label` for a sensitive row's visibility toggle button. Supports `{index}`. |
| `generate-random-label` | `"Generate random value for row {index}"` | `aria-label` for a sensitive row's random generator button. Supports `{index}`. |
| `table-label` | `"Key-value pairs"` | Default `aria-label` for the `<table>` element if the `aria-label` attribute is not set. |
| `key-label` | `"Key for row {index}"` | `aria-label` for a row's key input field. Supports `{index}`. |
| `value-label` | `"Value for row {index}"` | `aria-label` for a row's value input or textarea field. Supports `{index}`. |

## Custom classes

The Key-Value component supports custom CSS classes through the `cls` attribute or configuration script. Every styled element also exposes a matching `data-part="<target>"` attribute in the DOM for easy inspection in browser DevTools.

### Using simple string format

Apply a class to the default element (`host-inner`):

```html
<z-kv cls="z-kv my-custom-kv-wrapper"></z-kv>

```

### Using JSON object format

Target specific elements within the component:

```html
<z-kv
  cls='{
    "host-inner": "z-kv rounded-lg",
    "table": "z-table z-table-divider z-table-striped",
    "input": "z-input z-input-sm",
    "add-button": "z-button z-button-primary z-button-sm"
  }'
></z-kv>

```

### Using configuration script

```html
<z-kv>
  <script type="application/json" data-fn="config">
    {
      "cls": {
        "table": "z-table z-table-divider",
        "add-button": "z-button z-button-default z-button-sm",
        "remove-button": "z-button z-button-danger z-button-icon"
      }
    }
  </script>
</z-kv>

```

### Available cls targets

| Target (`data-part`) | Default Class | Description |
| --- | --- | --- |
| `host-inner` | `"z-kv"` | Outer wrapper `<div>` around the table (default target for string `cls`/`stl`). |
| `table` | `"z-table z-table-divider"` | The `<table>` element. |
| `header-row` | `""` | The `<tr>` element inside `<thead>`. |
| `header-key` | `""` | The Key column `<th>` element. |
| `header-value` | `""` | The Value column `<th>` element. |
| `header-actions` | `"z-table-shrink"` | The Actions column `<th>` element. |
| `row` | `""` | Each data `<tr>` element inside `<tbody>`. |
| `data-key` | `""` | The `<td>` cell containing the key input. |
| `key-input` | `""` | Specific class for the key `<input>`. Falls back to `input` if empty. |
| `input` | `"z-input"` | Shared fallback class for both key and value `<input>` elements. |
| `textarea` | `"z-textarea"` | Fallback class for `<textarea>` elements (falls back to `input` if empty). |
| `data-value` | `""` | The `<td>` cell containing the value wrapper. |
| `value-wrapper` | `"z-kv-value-wrapper"` | Flex container `<div>` holding the random generator button and value field. |
| `random-button` | `""` | Specific class for the `wand` button. Falls back to `button` if empty. |
| `value-input` | `"z-input"` | Specific class for the value `<input>`. Falls back to `input` if empty. |
| `value-textarea` | `"z-textarea"` | Specific class for the value `<textarea>`. Falls back to `textarea` then `input`. |
| `data-actions` | `"z-table-shrink"` | The `<td>` cell containing the row action buttons. |
| `actions` | `"z-kv-actions"` | Flex wrapper `<div>` around the visibility toggle and remove buttons. |
| `button` | `"z-button z-button-default z-button-icon"` | Shared fallback class for icon action buttons (`random-button`, `toggle-button`, `remove-button`). |
| `toggle-button` | `""` | Specific class for the password visibility toggle button. Falls back to `button`. |
| `remove-button` | `""` | Specific class for the remove row button. Falls back to `button`. |
| `add-row` | `""` | The bottom `<tr>` housing the add button. |
| `add-cell` | `""` | The `<td colspan="3">` cell spanning the bottom row. |
| `add-wrapper` | `"z-kv-add-wrapper"` | Flex container `<div>` centering the add button inside `add-cell`. |
| `add-button` | `"z-button z-button-default"` | The bottom "Add" `<button>` with icon and text label. |

## Custom inline styles

The Key-Value component supports custom inline styles through the `stl` attribute or configuration script.

### Using simple string format

Apply inline styles to the default element (`host-inner`):

```html
<z-kv stl="max-width\: 800px;"></z-kv>

```

### Using JSON object format

Target specific elements with custom inline styles:

```html
<z-kv
  stl='{
    "host-inner": "max-width: 900px;",
    "data-key": "width: 30%;",
    "add-cell": "padding: 0.75rem;"
  }'
></z-kv>

```

### Using configuration script

```html
<z-kv>
  <script type="application/json" data-fn="config">
    {
      "stl": {
        "data-key": "width: 240px;",
        "value-textarea": "min-height: 80px;"
      }
    }
  </script>
</z-kv>

```

### Available stl targets

The `stl` attribute supports the exact same targets as the `cls` attribute. See the [Available cls targets](https://www.google.com/search?q=%23available-cls-targets) table above for the complete list of targetable elements.

## Error state

To indicate a validation error state on the component or specific inputs, apply `.z-form-danger` via the `cls` attribute:

```html
<z-kv
  cls='{
    "host-inner": "z-kv z-form-danger",
    "input": "z-input z-form-danger"
  }'
></z-kv>

```

## Preventing layout shift

When loading Web Components, a brief delay before hydration may cause a flash of unstyled content. Reserve vertical space on a wrapper element or use `force-prevent-rerender` when pre-rendering `[data-host-inner]` on the server:

```html
<div style="min-height: 140px;">
  <z-kv></z-kv>
</div>

```

## Accessibility

The Key-Value component is built with comprehensive keyboard navigation and screen reader support:

- 
**Semantic Table & ARIA Roles:** Uses explicit `role="table"`, `role="row"`, `role="columnheader"` with `scope="col"`, `role="cell"`, and `role="group"` on each row's action bar.
- 
**Dynamic Row-Indexed Labels:** Every input and action button is labeled with its 1-based row number (e.g., `"Key for row 1"`, `"Value for row 1"`, `"Remove row 1"`, `"Toggle visibility for row 1"`), ensuring screen reader users always know which row they are interacting with.
- 
**Toggle Button State:** The password visibility toggle button maintains `aria-pressed="true|false"` to announce whether the secret value is currently visible.
- 
**Keyboard Shortcuts:** Pressing `Ctrl + Enter` (or `Cmd + Enter` on macOS) anywhere inside the component appends a new row (unless `noninsertable` or `max` is reached).
- 
**Focus Management:**
- Adding a row automatically moves focus to the new row's key input.
- Clicking the random value generator (
`wand`) button automatically shifts focus to that row's value input so keyboard focus is never lost when the generator button becomes disabled.
- Removing a row shifts focus to the adjacent row's key input, or to the "Add" button if the table becomes empty (
`min="0"`).

## Attributes

The following attributes are available on the `<z-kv>` element:

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `noninsertable` | `boolean` | `false` | Hides the "Add" button row and prevents users from inserting new rows. |
| `min` | `number` | `1` | Minimum number of rows required. Prevents row deletion once this count is reached and pads initial rows if fewer are provided. |
| `max` | `number` | `0` | Maximum number of rows allowed (`0` means unlimited). Disables the "Add" button once reached. |
| `aria-label` | `string` | `""` | Accessible label for the `<table>`. Overrides the `table-label` i18n string when provided. |
| `cls` | `string` | `""` | Custom CSS classes (plain string for `host-inner` or serialized JSON/options object for specific targets). |
| `stl` | `string` | `""` | Custom inline styles (plain string for `host-inner` or serialized JSON/options object for specific targets). |
| `i18n` | `string` | `""` | Custom localization strings (serialized JSON/options object). |
| `force-prevent-rerender` | `boolean` | `false` | Prevents re-rendering if `[data-host-inner]` is already present in the DOM (useful for SSR hydration). |
```

Make me a token-efficient version of this doc. The goal is to reduce token consumption of the users without losing contexts/important part of the component. Basically, we reduce noise. AI will consume this not human.