agustinl/svelte-tags-input
关于
Fully customizable Svelte component to enter tags
网站与应用开发可以做什么
- Keyboard-driven — Add tags with Enter (configurable), remove with Backspace, navigate autocomplete with Arrow keys
- Autocomplete — Array or async function, with filtering and custom display keys
- Paste & drop — Paste or drop comma-separated text to add several tags at once
- Reorder — Drag tags to change their order
实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文svelte-tags-input
A fully customizable tags input component for Svelte 5
Features
- Keyboard-driven — Add tags with Enter (configurable), remove with Backspace, navigate autocomplete with Arrow keys
- Autocomplete — Array or async function, with filtering and custom display keys
- Paste & drop — Paste or drop comma-separated text to add several tags at once
- Reorder — Drag tags to change their order
- Keyboard & screen reader basics — Keyboard navigation and an accessible (optionally visually hidden) label
- Flexible — Max tags, unique-only, custom validation, blur behavior
- Svelte 5 — Built with runes (
$props,$state,$derived,$bindable) - TypeScript — Type definitions included (
TagsProps)
Requirements
- Svelte 5.x — This package requires Svelte 5 (peer dependency)
Install
npm install svelte-tags-input
Quick Start
<script>
import Tags from 'svelte-tags-input';
let tags = $state([]);
</script>
<Tags bind:tags placeholder="Type and press Enter..." />
Options
| Prop | Type | Default | Description |
|---|---|---|---|
bind:tags |
Array |
[] |
Two-way bound array of tags |
addKeys |
number[] |
[13] (Enter) |
Keys that add a tag |
removeKeys |
number[] |
[8] (Backspace) |
Keys that remove the last tag |
placeholder |
string |
'' |
Input placeholder text |
maxTags |
number | false |
false |
Maximum number of tags allowed |
onlyUnique |
boolean |
false |
Reject duplicate tags |
allowPaste |
boolean |
false |
Enable pasting tags (split by splitWith) |
allowDrop |
boolean |
false |
Enable drag-and-drop of tags |
allowReorder |
boolean |
false |
Reorder tags by dragging them |
splitWith |
string |
',' |
Character to split pasted/dropped text |
autoComplete |
Array | (value: string) => any | false |
false |
Suggestions array or async fetcher |
autoCompleteKey |
string | false |
false |
Key for object items in autoComplete |
autoCompleteShowKey |
string |
autoCompleteKey |
Key to display (when different from search key) |
autoCompleteMarkupKey |
string | false |
false |
Key whose value is rendered as HTML in each suggestion (⚠️ not escaped — sanitize untrusted data) |
autoCompleteFilter |
boolean |
true |
Filter suggestions client-side (disable for API responses) |
autoCompleteStartFocused |
boolean |
false |
Focus first suggestion without typing |
onlyAutocomplete |
boolean |
false |
Only accept tags from the autocomplete list |
minChars |
number |
1 |
Min chars before showing autocomplete (0 = show all on click) |
allowBlur |
boolean |
false |
Add tag on input blur |
cleanOnBlur |
boolean |
false |
Clear input on blur (tags kept) |
disable |
boolean |
false |
Disable the input |
readonly |
boolean |
false |
Read-only display mode |
name |
string |
'svelte-tags-input' |
Input name attribute |
id |
string |
random | Input id attribute |
labelText |
string |
name |
Accessible label text |
labelShow |
boolean |
false |
Show visible label |
onTagAdded |
(tag, tags) => void |
() => {} |
Called when a tag is added |
onTagRemoved |
(tag, tags) => void |
() => {} |
Called when a tag is removed |
onTagClick |
(tag) => void |
() => {} |
Called when a tag is clicked |
onTagReordered |
(tag, tags, fromIndex, toIndex) => void |
() => {} |
Called when a tag is dropped at a new position |
customValidation |
((tag) => boolean) | false |
false |
Custom validation before adding |
Key codes
addKeys and removeKeys take numeric key codes. Common values:
| Key | Code |
|---|---|
| Enter | 13 |
| Tab | 9 |
| Backspace | 8 |
| Escape | 27 |
| Comma | 188 |
| Space | 32 |
See the MDN keyCode reference for others.
Examples
Basic with autocomplete
<script>
import Tags from 'svelte-tags-input';
let tags = $state([]);
const suggestions = ['Apple', 'Banana', 'Cherry', 'Date', 'Elderberry'];
</script>
<Tags
bind:tags
placeholder="Pick a fruit..."
autoComplete={suggestions}
onlyUnique
/>
Full configuration
<script>
import Tags from 'svelte-tags-input';
let tags = $state([]);
const countries = ['Argentina', 'Brazil', 'Chile', 'Colombia', 'Mexico'];
</script>
<Tags
bind:tags
addKeys={[9]}
removeKeys={[27]}
maxTags={5}
allowPaste
allowDrop
splitWith="/"
onlyUnique
placeholder="Add a country..."
autoComplete={countries}
minChars={2}
onlyAutocomplete
allowBlur
cleanOnBlur
labelText="Countries"
labelShow
onTagAdded={(tag, t) => console.log('Added:', tag, t)}
onTagRemoved={(tag, t) => console.log('Removed:', tag, t)}
onTagClick={(tag) => console.log('Clicked:', tag)}
customValidation={(tag) => tag.length >= 3}
/>
Autocomplete with objects
<script>
import Tags from 'svelte-tags-input';
let tags = $state([]);
const users = [
{ name: 'Ada Lovelace', username: 'ada' },
{ name: 'Alan Turing', username: 'alan' },
{ name: 'Grace Hopper', username: 'grace' }
];
</script>
<!-- Search by `name`, show `username` in the selected tag -->
<Tags
bind:tags
autoComplete={users}
autoCompleteKey="name"
autoCompleteShowKey="username"
onlyAutocomplete
/>
When autoComplete returns objects, autoCompleteKey is required and tags will contain the selected objects.
Async autocomplete (API)
<script>
import Tags from 'svelte-tags-input';
let tags = $state([]);
async function fetchCountries(query) {
const res = await fetch(
`https://countries.dev/name/${encodeURIComponent(query)}?fields=name,alpha3Code`
);
if (!res.ok) return []; // 404 when no country matches
return res.json();
}
</script>
<Tags
bind:tags
autoComplete={fetchCountries}
autoCompleteKey="name"
autoCompleteShowKey="alpha3Code"
autoCompleteFilter={false}
/>
Reorder by dragging
<script>
import Tags from 'svelte-tags-input';
let tags = $state(['First', 'Second', 'Third']);
</script>
<Tags
bind:tags
allowReorder
onTagReordered={(tag, t, from, to) => console.log(`Moved ${tag} from ${from} to ${to}`, t)}
/>
Tags are reordered by position, so duplicate and object tags work too. The new order previews while dragging, but tags only updates (and onTagReordered only fires) when the tag is dropped inside the component. Dropping it elsewhere or pressing Escape restores the original order. Reordering is turned off when disable or readonly is set.
Inside a form
The name prop is applied to the visible text input, so a native form submit only sends whatever is currently typed — not the tags. To submit the tags, render a hidden input for each one:
<script>
import Tags from 'svelte-tags-input';
let tags = $state([]);
</script>
<form method="POST" action="/save">
<Tags bind:tags name="tags-text" placeholder="Add tags..." />
{#each tags as tag}
<input type="hidden" name="tags" value={tag} />
{/each}
<button type="submit">Save</button>
</form>
On the server, read them with formData.getAll('tags'). Any text still typed in the input arrives separately as tags-text.
If your tags are objects (from an autoComplete list of objects), serialize them instead:
<input type="hidden" name="tags" value={JSON.stringify(tags)} />
As a custom element (Lit, vanilla JS, other frameworks)
The package doesn't ship a custom element build, but you can wrap it in your own Svelte component and compile that as a custom element:
<!-- TagsInputElement.svelte -->
<svelte:options
customElement={{
tag: 'tags-input',
props: {
tags: { type: 'Array', reflect: false },
placeholder: { type: 'String' },
autoComplete: { type: 'Array', attribute: 'auto-complete' }
}
}}
/>
<script>
import Tags from 'svelte-tags-input';
let { tags = [], placeholder = '', autoComplete = false } = $props();
const host = $host();
const emit = (type, detail) =>
host.dispatchEvent(new CustomEvent(type, { detail, bubbles: true, composed: true }));
</script>
<Tags
bind:tags
{placeholder}
{autoComplete}
onTagAdded={(tag, all) => emit('tag-added', { tag, tags: all })}
onTagRemoved={(tag, all) => emit('tag-removed', { tag, tags: all })}
/>
Compile it with the Svelte compiler options customElement: true and css: 'injected' (so the component styles are included in the shadow DOM), then use it anywhere:
<tags-input placeholder="Add tags..."></tags-input>
<script type="module">
const el = document.querySelector('tags-input');
el.addEventListener('tag-added', (e) => console.log(e.detail.tag, e.detail.tags));
</script>
Expose any other props you need the same way, adding them to props and passing them through to <Tags>.
Styling
The component ships with default styles scoped to it. To override them, target its classes with :global() from a wrapper element:
| Class | Element |
|---|---|
.svelte-tags-input-layout |
Outer container (tags + input) |
.svelte-tags-input-tag |
Each tag |
.svelte-tags-input-tag-remove |
The × remove button inside a tag |
.svelte-tags-input |
The text input |
.svelte-tags-input-matchs |
Autocomplete list (li.focus is the highlighted item) |
.sti-tag-reorderable |
Added to each tag when allowReorder is set |
.sti-tag-dragging |
Added to the tag being dragged |
.sti-layout-disable |
Added to the container when disable is set |
.sti-layout-readonly |
Added to the container when readonly is set |
<div class="my-tags">
<Tags bind:tags />
</div>
<style>
.my-tags :global(.svelte-tags-input-layout .svelte-tags-input-tag) {
background: #4f46e5;
border-radius: 999px;
}
.my-tags :global(.svelte-tags-input-layout:focus-within) {
border-color: #4f46e5;
}
</style>
Development
npm install
npm test # run the test suite
npm run build # build to dist/
Issues and pull requests are welcome.
License
MIT © Agustín
探索相关项目
加载中…