π The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.
Vue dockergolangkanbanknowledge-baseknowledge-managementmarkdownnote-takingnotesnotes-appnotionnuxtnuxtjsoffline-firstoidcpwaselfhostedssovuevuejswiki
<template>
<div :class="[display === 'row' ? 'row' : 'column']">
<div class="tags-input-wrapper">
<div class="tags-input">
<input
v-model="tagInput"
type="text"
:placeholder="t('components.tagInput.placeholder')"
:class="{ minimal: minimal }"
@keydown.enter="handleEnter"
@keydown.arrow-up="tagHandleArrowUp"
@keydown.arrow-down="tagHandleArrowDown"
@keydown.escape="hideSuggestions"
@input="handleTagInput"
@focus="showSuggestions = true"
@blur="hideSuggestions"
/>
<button :class="minimal ? 'add-tag-btn-min' : 'add-tag-btn'" @click="addTag()">
<Icon name="plus" fill="inherit" />
</button>
</div>
<div v-if="showSuggestions && filteredTagSuggestions.length > 0" class="tag-suggestions">
<div
v-for="(tag, index) in filteredTagSuggestions"
:key="tag"
class="tag-suggestion"
:class="{ selected: selectedSuggestionIndex === index }"
@mousedown="addTag(tag)"
@mouseenter="selectedSuggestionIndex = index"
>
{{ tag }}
</div>
</div>
</div>
<div v-if="selectedTags?.length" :class="`selected-tags `">
<span v-for="tag in selectedTags" :key="tag" class="tag-chip" :class="colored ? resolveTagColor(tag) : 'primary'">
{{ tag }}
<button class="remove-tag" @click.stop="removeTag(tag)">Γ</button>
</span>
</div>
</div>
</template>
<script setup lang="ts">
import { resolveTagColor } from '~/helpers/node';
const props = defineProps<{
display?: 'column' | 'row';
minimal?: boolean;
modelValue?: string; // separate tags with commas
}>();
const emit = defineEmits<{
(e: 'update:modelValue', value: string): void;
}>();
const nodesStore = useNodesStore();
const preferencesStore = usePreferencesStore();
const { t } = useI18nT();
const selectedTags = ref<string[]>(stringToTags(props.modelValue || ''));
const tagInput = ref('');
const showSuggestions = ref(false);
const selectedSuggestionIndex = ref(-1);
const colored = preferencesStore.get('documentColoredTags');
watch(
() => props.modelValue,
newVal => {
selectedTags.value = stringToTags(newVal || '');
},
);
function stringToTags(str: string): string[] {
return str
.split(',')
.map(tag => tag.trim())
.filter(tag => tag.length > 0);
}
const filteredTagSuggestions = computed(() => {
if (!tagInput.value) return nodesStore.allTags;
const input = tagInput.value.toLowerCase();
return nodesStore.allTags.filter(tag => tag.toLowerCase().includes(input) && !tagInput.value.includes(tag)).slice(0, 5);
});
function addTag(tag?: string) {
const newTag = tag?.trim() || tagInput.value.trim();
if (newTag && !selectedTags.value.includes(newTag)) {
selectedTags.value.push(newTag);
tagInput.value = '';
showSuggestions.value = false;
emit('update:modelValue', selectedTags.value.join(','));
}
}
function handleTagInput() {
selectedSuggestionIndex.value = 0;
showSuggestions.value = filteredTagSuggestions.value.length > 0;
}
function hideSuggestions() {
setTimeout(() => {
showSuggestions.value = false;
}, 200);
}
function handleEnter(event: KeyboardEvent) {
event.preventDefault();
let tagToAdd: string | undefined = '';
if (showSuggestions.value && filteredTagSuggestions.value.length > 0) {
tagToAdd = filteredTagSuggestions.value[selectedSuggestionIndex.value];
}
addTag(tagToAdd);
}
function tagHandleArrowUp(event: KeyboardEvent) {
event.preventDefault();
if (showSuggestions.value && filteredTagSuggestions.value.length > 0) {
selectedSuggestionIndex.value = selectedSuggestionIndex.value > 0 ? selectedSuggestionIndex.value - 1 : filteredTagSuggestions.value.length - 1;
}
}
function tagHandleArrowDown(event: KeyboardEvent) {
event.preventDefault();
if (showSuggestions.value && filteredTagSuggestions.value.length > 0) {
selectedSuggestionIndex.value = selectedSuggestionIndex.value < filteredTagSuggestions.value.length - 1 ? selectedSuggestionIndex.value + 1 : 0;
}
}
function removeTag(tag: string) {
selectedTags.value = selectedTags.value.filter(t => t !== tag);
emit('update:modelValue', selectedTags.value.join(','));
}
defineExpose({ addTag });
</script>
<style scoped lang="scss">
.column {
display: flex;
flex-direction: column;
gap: 6px;
}
.row {
display: flex;
flex-direction: row;
align-items: baseline;
gap: 12px;
}
.tags-input-wrapper {
position: relative;
}
.tags-input {
display: flex;
gap: 6px;
}
input {
flex: 1;
min-width: 200px;
&:focus {
border-color: var(--primary);
}
&.minimal {
box-sizing: border-box;
height: 34px;
padding: 0 12px;
border: 1px solid transparent;
border-radius: var(--radius-md);
font-family: inherit;
font-size: 0.9rem;
font-weight: 500;
color: var(--text-primary);
background: var(--surface-raised);
outline: none;
transition: all $transition-base ease;
&:hover {
border-color: var(--border);
background: var(--surface-overlay);
}
&:focus {
border-color: var(--primary);
background: var(--surface-base);
box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 15%, transparent);
}
&::placeholder {
font-weight: 400;
color: var(--text-secondary);
opacity: 0.8;
}
}
}
.add-tag-btn {
display: flex;
justify-content: center;
align-items: center;
padding: 6px 10px;
border: none;
border-radius: var(--radius-xs);
color: white;
background: var(--primary);
cursor: pointer;
&:hover {
background: var(--primary-dark);
}
}
.add-tag-btn-min {
display: flex;
justify-content: center;
align-items: center;
padding: 4px;
border: none;
border-radius: var(--radius-md);
color: var(--text-body);
cursor: pointer;
&:hover {
background: var(--surface-transparent);
}
}
.tag-suggestions {
position: absolute;
top: 100%;
right: 40px;
left: 0;
z-index: 10;
max-height: 150px;
margin-top: 2px;
border: 1px solid var(--border);
border-radius: var(--radius-xs);
background: var(--surface-base);
box-shadow: var(--shadow-md);
overflow-y: auto;
}
.tag-suggestion {
padding: 6px 10px;
font-size: 12px;
cursor: pointer;
&:hover,
&.selected {
background: var(--border);
}
}
.selected-tags {
display: flex;
flex-wrap: wrap;
gap: 4px;
margin-top: 6px;
}
.tag-chip {
display: inline-flex;
align-items: center;
gap: 4px;
padding: 3px 6px;
border-radius: var(--radius-lg);
font-size: 12px;
.remove-tag {
display: flex;
justify-content: center;
align-items: center;
width: 14px;
height: 14px;
padding: 0;
border: none;
border-radius: 50%;
background: none;
cursor: pointer;
&:hover {
color: var(--text-inverse);
}
}
}
</style>