KV

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.

Table of contents

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:

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:

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):

<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:

<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:

AttributeAliasesTypeDefaultDescription
data-key<option> textstring""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-sensitivedata-type="password"booleanfalseEnables password masking, a random value generator button (wand), and a visibility toggle button (eye/eye-off) for the row.
data-textareadata-type="textarea"booleanfalseRenders a multiline <textarea> instead of a single-line <input> for the value field.
data-rows—number3Sets the rows attribute when the row is rendered as a <textarea>.
data-removable—booleantrueControls whether the row can be deleted. Set data-removable="false" to lock the row against removal.
data-readonly—booleanfalseMarks both the key and value fields as readonly and prevents DOM tampering.
data-key-readonlydata-readonly-keybooleanfalseMarks only the key input as readonly (strict key mode) while keeping the value editable.
data-value-readonlydata-readonly-valuebooleanfalseMarks only the value field as readonly while keeping the key editable.
disabled—booleanfalseDisables the entire row (key input, value field, action buttons, and removal).
data-key-disableddata-disabled-keybooleanfalseDisables only the key input for the row.
data-value-disableddata-disabled-valuebooleanfalseDisables only the value field and its sensitive action buttons for the row.
data-key-placeholderdata-placeholder-keystring—Overrides the key input placeholder text for this specific row.
data-value-placeholderdata-placeholder-value, data-placeholderstring—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.

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):

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.

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.

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.
<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, internal icon slots use built-in icons, and you can also supply your own SVG icons globally via <template id="z-icons"> or locally per component via <template data-fn="icons">:

Available icon keys

Icon KeyElement TargetDescription
plusadd-buttonDisplayed inside the bottom “Add” button next to the button label.
wandrandom-buttonDisplayed inside the random value generator button on sensitive rows.
eyetoggle-buttonDisplayed when a sensitive row’s value is currently masked (click to reveal).
eye-offtoggle-buttonDisplayed when a sensitive row’s value is currently visible (click to mask).
trash-2remove-buttonDisplayed 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

Using a configuration script

Using global i18n

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

<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

KeyDefaultDescription
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):

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

Using JSON object format

Target specific elements within the component:

<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

<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 ClassDescription
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):

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

Using JSON object format

Target specific elements with custom inline styles:

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

Using configuration script

<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 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:

  • This field is required.

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:

<div class="[min-h]" style="--min-h: 8.75rem">
  <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:

NameTypeDefaultDescription
noninsertablebooleanfalseHides the “Add” button row and prevents users from inserting new rows.
minnumber1Minimum number of rows required. Prevents row deletion once this count is reached and pads initial rows if fewer are provided.
maxnumber0Maximum number of rows allowed (0 means unlimited). Disables the “Add” button once reached.
aria-labelstring""Accessible label for the <table>. Overrides the table-label i18n string when provided.
clsstring""Custom CSS classes (plain string for host-inner or serialized JSON/options object for specific targets).
stlstring""Custom inline styles (plain string for host-inner or serialized JSON/options object for specific targets).
i18nstring""Custom localization strings (serialized JSON/options object).
force-prevent-rerenderbooleanfalsePrevents re-rendering if [data-host-inner] is already present in the DOM (useful for SSR hydration).