Zubra UI
Code for
GitHub

tag-input

A list of tags you type: Enter or comma adds one, Backspace in the empty field removes the last, × removes any. Suggestions come from suggestions. A form control.

Try it: events appear here (valuechange).

Source: tag-input.zb, and its registry item.

Install

Compiled (any project)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/tag-input.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/tag-input.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/tag-input.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/tag-input.json
One file, no dependencies (4.4 KB gzipped)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/tag-input.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/tag-input.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/tag-input.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/tag-input.json
.zb source (to edit it with Zubra)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/tag-input.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/tag-input.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/tag-input.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/tag-input.json
No build: a script tag, straight from this site
HTML
<link rel="stylesheet" href="https://zubra-js.github.io/zubra-ui/themes/default.css">
<script type="module" src="https://zubra-js.github.io/zubra-ui/cdn/tag-input.js"></script>

The compiled install writes src/components/zubra/tag-input.js and the theme, and adds @zubra/runtime to your dependencies. The one-file install writes the same path as a plain custom element that needs nothing installed (or download it and load it with a <script type="module">); it's smaller on a page with one to three components, the shared runtime with more.

Usage

index.html
<tag-input label="Topics" name="topics"></tag-input>

<script type="module">
  import './components/zubra/tag-input.js';

  const element = document.querySelector('tag-input');
  element.value = ['web-components', 'design'];
  element.suggestions = [
    'accessibility',
    'css',
    'design',
    'html',
    'javascript',
    'performance',
    'testing',
    'web-components',
  ];
  element.addEventListener('valuechange', (event) => console.log('valuechange', event.detail));
</script>
Example.jsx
import './components/zubra/tag-input.js';

const value = ['web-components', 'design'];
const suggestions = [
  'accessibility',
  'css',
  'design',
  'html',
  'javascript',
  'performance',
  'testing',
  'web-components',
];

export function TagInputExample() {
  return (
    <tag-input
      label="Topics"
      name="topics"
      value={value}
      suggestions={suggestions}
      onvaluechange={(event) => console.log('valuechange', event.detail)}
    ></tag-input>
  );
}
Example.vue
<script setup>
import './components/zubra/tag-input.js';
const value = ['web-components', 'design'];
const suggestions = [
  'accessibility',
  'css',
  'design',
  'html',
  'javascript',
  'performance',
  'testing',
  'web-components',
];
const onValuechange = (event) => console.log('valuechange', event.detail);
</script>

<template>
  <tag-input
    label="Topics"
    name="topics"
    :value.prop="value"
    :suggestions.prop="suggestions"
    @valuechange="onValuechange"
  ></tag-input>
</template>

<!-- vite.config.js: vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.includes('-') } } }) -->
Example.svelte
<script>
  import './components/zubra/tag-input.js';
  const value = ['web-components', 'design'];
  const suggestions = [
    'accessibility',
    'css',
    'design',
    'html',
    'javascript',
    'performance',
    'testing',
    'web-components',
  ];
</script>

<tag-input
  label="Topics"
  name="topics"
  {value}
  {suggestions}
  onvaluechange={(event) => console.log('valuechange', event.detail)}
></tag-input>
example.component.ts
import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import './components/zubra/tag-input.js';

@Component({
  selector: 'app-tag-input-example',
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  template: `
    <tag-input
      label="Topics"
      name="topics"
      [value]="value"
      [suggestions]="suggestions"
      (valuechange)="onValuechange($event)"
    ></tag-input>
  `,
})
export class TagInputExample {
  value = ['web-components', 'design'];

  suggestions = [
    'accessibility',
    'css',
    'design',
    'html',
    'javascript',
    'performance',
    'testing',
    'web-components',
  ];

  onValuechange(event: Event) {
    console.log('valuechange', (event as CustomEvent).detail);
  }
}

API

Properties (each is also an HTML attribute of the same name, unless marked)
PropertyTypeDefault
value
property only
unknown[][]
suggestions
property only
unknown[][]
maxnumber6
disabledbooleanfalse
labelstring"Tags"
Events
EventWhen
valuechangeThe tags changed; detail is the new array.

Size

1.8 KB gzipped for the component, plus the runtime, which every Zubra component on a page shares (up to 22.1 KB, all of it). On its own, with the parts of the runtime it uses, it's 18.6 KB.

Theme

Every component reads shadcn/ui's CSS variables, so any shadcn theme works. Pick a look or a colour, or paste a theme's CSS.

Look: colours, fonts, corners and motion

Accent colour