# Tags

> A Tags widget to capture a list of free-form text values as removable chips

Tags widgets are [Input Fields](https://golemui.com/dx/widgets-reference/input-fields/overview.md) that capture an
ordered list of free-form text values. Each committed value becomes a removable pill
shown to the left of the input, and the value bound at the widget's `path` is a `string[]`.
There is no prop that caps the number of tags; the widget never silently drops an entry.


```ts title="tags.ts"

export default [
  gui.inputs.tags('keywords', {
    label: 'Keywords',
  }),
];
```

_Live demo on the docs page. Form definition: [tags.ts](https://golemui.com/template/widgets-dx/tags/tags.ts)._

## Props

Use these `props` to customize your Tags widget.

| prop              | type                                              | description                                                                                       |
| ----------------- | ------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| `allowDuplicates` | boolean                                           | When `true`, the same tag can be added again. Defaults to `false`, so a repeated tag is skipped   |
| `hint`            | string                                            | A description displayed below the row                                                             |
| `icon`            | string                                            | CSS class string for a leading icon (rendered to the left of the pills)                           |
| `placeholder`     | string                                            | Placeholder text shown in the input when empty                                                    |
| `removeAriaLabel` | string                                            | ARIA label prefix used on each chip's remove button. Defaults to `Remove tag`                     |
| `removeIcon`      | string                                            | CSS class string for a custom remove-icon. When omitted, a built-in `×` glyph is rendered         |
| `separators`      | `('Enter' \| ',' \| 'Tab' \| 'blur' \| string)[]` | Triggers that commit the current draft as a new chip. Defaults to `['Enter', ',', 'Tab', 'blur']` |
| `trim`            | boolean                                           | When `true` (default), whitespace is trimmed from each value before committing                    |

### Placeholder

Use the property `placeholder` to set the text shown inside the empty input.


```ts title="tags.ts"

export default [
  gui.inputs.tags('keywords', {
    placeholder: 'Add a keyword and press Enter',
    label: 'Keywords',
  }),
];
```

_Live demo on the docs page. Form definition: [tags-placeholder.ts](https://golemui.com/template/widgets-dx/tags/tags-placeholder.ts)._

### Hint

<Aside>A `label` is required for the `hint` to be displayed.</Aside>

Use the property `hint` to add a description below the row.


```ts title="tags.ts"

export default [
  gui.inputs.tags('keywords', {
    hint: 'Use Enter, Tab or comma to add a tag',
    placeholder: 'Add a keyword…',
    label: 'Keywords',
  }),
];
```

_Live demo on the docs page. Form definition: [tags-hint.ts](https://golemui.com/template/widgets-dx/tags/tags-hint.ts)._

### Icon

Use the property `icon` to render a leading icon to the left of the pills. The value is a
CSS class string — the same convention used by Textinput.


```ts title="tags.ts"

export default [
  gui.inputs.tags('keywords', {
    icon: 'label',
    placeholder: 'Add a keyword…',
    label: 'Keywords',
  }),
];
```

_Live demo on the docs page. Form definition: [tags-icon.ts](https://golemui.com/template/widgets-dx/tags/tags-icon.ts)._

### Separators

By default a tag is committed on `Enter`, `,`, `Tab`, or `blur`. Override the property
`separators` to restrict the commit triggers — useful when you want the user to commit
explicitly (e.g. `Enter` only) and treat commas as part of the tag value.

Pasting text that contains any of the configured character separators (e.g. commas) also
splits the paste into multiple chips in a single commit. Pasted text goes in at the caret and
replaces the selected text, as in a native input.


```ts title="tags.ts"

export default [
  gui.inputs.tags('keywords', {
    hint: 'Only Enter or comma commit a tag — Tab and blur do not',
    placeholder: 'Type something and press Enter or ,',
    separators: ['Enter', ','],
    label: 'Keywords',
  }),
];
```

_Live demo on the docs page. Form definition: [tags-separators.ts](https://golemui.com/template/widgets-dx/tags/tags-separators.ts)._

### Allow duplicates

By default the input skips a tag that the value already holds. It shows no error, because the
value does not change. Set `allowDuplicates: true` to let the user add the same tag more than
once.


```ts title="tags.ts"

export default [
  gui.inputs.tags('keywords', {
    hint: 'Try adding the same tag twice. Both tags stay',
    placeholder: 'Add a tag…',
    allowDuplicates: true,
    label: 'Keywords',
  }),
];
```

_Live demo on the docs page. Form definition: [tags-allow-duplicates.ts](https://golemui.com/template/widgets-dx/tags/tags-allow-duplicates.ts)._

The input checks only the tags the user adds, not a value from your data. To reject a value that
holds a repeated tag, such as `['vip', 'vip']` from a server, use the array validator's
`uniqueItems`, as in [Validation](#validation).

### Trim

`trim` defaults to `true` so leading/trailing whitespace is removed before a tag is
committed. Set it to `false` when surrounding whitespace is semantically meaningful (for
example, deliberately quoting a phrase with padding).


```ts title="tags.ts"

export default [
  gui.inputs.tags('keywords', {
    hint: 'trim is off — leading/trailing whitespace is preserved verbatim',
    placeholder: 'Try adding \'  spaced  \'',
    trim: false,
    label: 'Keywords',
  }),
];
```

_Live demo on the docs page. Form definition: [tags-trim.ts](https://golemui.com/template/widgets-dx/tags/tags-trim.ts)._

### Max items

There is no prop that caps the number of chips. Use the array validator's `maxItems`
instead: entry is never silently blocked, the extra tag is committed and the validator
flags it, so the user is told why. Combine it with `messages` to change the wording, as in
[Validation](#validation).


```ts title="tags.ts"

export default [
  gui.inputs.tags('keywords', {
    hint: 'A maxItems validator flags a fourth tag instead of blocking it',
    placeholder: 'Up to 3 tags',
    label: 'Keywords (max 3)',
    validator: {
      maxItems: 3,
    },
  }),
];
```

_Live demo on the docs page. Form definition: [tags-limit.ts](https://golemui.com/template/widgets-dx/tags/tags-limit.ts)._

### Customize the remove button

`removeIcon` swaps the built-in `×` glyph for any icon (CSS class string, same convention
as `icon`). `removeAriaLabel` sets the prefix announced by assistive tech alongside the
tag value — useful for translations or wording adjustments (e.g. `"Discard tag: foo"`).


```ts title="tags.ts"

export default [
  gui.inputs.tags('keywords', {
    hint: 'Custom remove-button icon and ARIA label',
    placeholder: 'Add a tag…',
    removeIcon: 'delete',
    removeAriaLabel: 'Discard tag',
    label: 'Keywords',
  }),
];
```

_Live demo on the docs page. Form definition: [tags-remove.ts](https://golemui.com/template/widgets-dx/tags/tags-remove.ts)._

### Validation

The widget supports the standard array validator: `required`, `minItems`, `maxItems` and `uniqueItems`.
A tag past `maxItems` is committed and flagged, never dropped. Validation errors surface like any other input: below the row, with `aria-invalid` and
`aria-errormessage` wired automatically. Override the default error copy per-rule with a
`messages` object — each key matches the rule it customises, and values can be a string or
a [Localizable](https://golemui.com/dx/features/i18n.md) for i18n.


```ts title="tags.ts"

export default [
  gui.inputs.tags('keywords', {
    hint: 'Between 1 and 5 tags',
    placeholder: 'Add a keyword…',
    label: 'Keywords',
    validator: {
      required: true,
      minItems: 1,
      maxItems: 5,
      messages: {
        required: 'Please add at least one keyword',
        minItems: 'You need at least 1 keyword to continue',
        maxItems: 'No more than 5 keywords, please',
      },
    },
  }),
];
```

_Live demo on the docs page. Form definition: [tags-unique.ts](https://golemui.com/template/widgets-dx/tags/tags-unique.ts)._

## Styling

Tags can be styled as explained in the [Styling Guide](https://golemui.com/dx/styling/overview.md).

### CSS Variables

The chip and count-badge visuals are shared with the Range Calendar / Range Date Input
pills, so they pick up the same intent / spacing / radius tokens.

| CSS Variable             | Description                                                                             |
| ------------------------ | --------------------------------------------------------------------------------------- |
| `--gui-color-border`     | Group border colour                                                                     |
| `--gui-color-bg`         | Group background colour and scroll-fade                                                 |
| `--gui-color-fg`         | Input text colour                                                                       |
| `--gui-color-primary`    | Group focus-ring colour                                                                 |
| `--gui-color-error`      | Group border colour when invalid                                                        |
| `--gui-intent-primary`   | Chip and count-bubble background colour                                                 |
| `--gui-color-white`      | Chip text / remove-icon colour                                                          |
| `--gui-radius-md`        | Chip corner radius                                                                      |
| `--gui-radius-full`      | Count-bubble corner radius                                                              |
| `--gui-space-1`          | Chip vertical padding                                                                   |
| `--gui-space-2`          | Row gap between chips                                                                   |
| `--gui-space-3`          | Gap between chip text and remove button                                                 |
| `--gui-font-xs`          | Default value of `--gui-pill-font-size`                                                 |
| `--gui-pill-height`      | Chip height. `24px`, `32px` under `pointer: coarse`                                     |
| `--gui-pill-action-size` | Remove icon size. `16px`, `24px` under `pointer: coarse`                                |
| `--gui-pill-action-hit`  | Remove icon hit area. `24px`, `32px` under `pointer: coarse`                            |
| `--gui-pill-font-size`   | Chip text font size. `var(--gui-font-xs)`, `var(--gui-font-sm)` under `pointer: coarse` |

### Anatomy

This is the anatomy of the Tags Widget in case you want to use your CSS styles.


```html title="tags.html"
<gui-tags-input class="gui-tags gui-field">
  <gui-tags class="gui-field">
    <span class="gui-label" id="fieldUid_label">
      <span class="gui-label__text">Label</span>
      <div class="gui-widget-hint" id="fieldUid_hint" aria-hidden="true">Hint</div>
    </span>

    <div class="gui-widget">
      <div
        role="group"
        class="gui-widget-input gui-tags-input gui-tags-input--icon"
        aria-labelledby="fieldUid_label"
        aria-describedby="fieldUid_hint"
      >
        <span aria-hidden="true" class="gui-widget-icon label" data-icon="label"></span>

        <gui-pills class="gui-tags__pills">
          <div class="gui-pills__strip-wrapper gui-pills--end-shadow gui-pills--start-shadow">
            <div class="gui-pills__strip" role="toolbar" tabindex="-1" aria-label="Selected tags">
              <span class="gui-sentinel gui-sentinel__start"></span>

              <button
                type="button"
                class="gui-pills__pill"
                tabindex="-1"
                aria-label="design"
                aria-description="Remove tag"
              >
                <span class="gui-pills__pill-text">design</span>

                <span class="gui-pills__pill-remove" aria-hidden="true">
                  <svg
                    xmlns="http://www.w3.org/2000/svg"
                    width="16"
                    height="16"
                    viewBox="0 0 256 256"
                    fill="currentColor"
                    aria-hidden="true"
                  >
                    <path d="M165.66,101.66,139.3…"></path>
                  </svg>
                </span>
              </button>

              <button
                type="button"
                class="gui-pills__pill"
                tabindex="-1"
                aria-label="product"
                aria-description="Remove tag"
              >
                <span class="gui-pills__pill-text">product</span>

                <span class="gui-pills__pill-remove" aria-hidden="true">
                  <svg
                    xmlns="http://www.w3.org/2000/svg"
                    width="16"
                    height="16"
                    viewBox="0 0 256 256"
                    fill="currentColor"
                    aria-hidden="true"
                  >
                    <path d="M165.66,101.66,139.3…"></path>
                  </svg>
                </span>
              </button>

              <span class="gui-sentinel gui-sentinel__end"></span>
            </div>
          </div>

          <div class="gui-pills__compact">
            <button
              type="button"
              aria-haspopup="true"
              class="gui-pills__count"
              aria-label="2 tags"
              aria-expanded="false"
              aria-controls="fieldUid_pills_dropdown"
            >
              2
            </button>
          </div>
        </gui-pills>

        <input
          type="text"
          class="gui-tags__input"
          id="fieldUid"
          aria-labelledby="fieldUid_label"
          placeholder="Custom placeholder"
        />
      </div>
    </div>

    <div class="gui-validator gui-validator--empty" id="fieldUid_errors" role="alert"></div>
  </gui-tags>
</gui-tags-input>
```
