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
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
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
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
<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
<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>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>
);
}<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('-') } } }) --><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>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
| Property | Type | Default |
|---|---|---|
valueproperty only | unknown[] | [] |
suggestionsproperty only | unknown[] | [] |
max | number | 6 |
disabled | boolean | false |
label | string | "Tags" |
| Event | When |
|---|---|
valuechange | The 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.