command
shadcn-vue/commandFreeComponent
shadcn-vue publishes no description for this item.
Live preview
live · rendered by the registry
Rendered by shadcn-vue on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://shadcn-vue.com/r/styles/new-york/command.jsonSource
1<script setup lang="ts">2import type { ListboxRootEmits, ListboxRootProps } from "reka-ui"3import type { HTMLAttributes } from "vue"4import { reactiveOmit } from "@vueuse/core"5import { ListboxRoot, useFilter, useForwardPropsEmits } from "reka-ui"6import { reactive, ref, watch } from "vue"7import { cn } from "@/lib/utils"8import { provideCommandContext } from "."910const props = withDefaults(defineProps<ListboxRootProps & { class?: HTMLAttributes["class"] }>(), {11 modelValue: "",12})1314const emits = defineEmits<ListboxRootEmits>()1516const delegatedProps = reactiveOmit(props, "class")1718const forwarded = useForwardPropsEmits(delegatedProps, emits)1920const allItems = ref<Map<string, string>>(new Map())21const allGroups = ref<Map<string, Set<string>>>(new Map())2223const { contains } = useFilter({ sensitivity: "base" })24const filterState = reactive({25 search: "",26 filtered: {27 /** The count of all visible items. */28 count: 0,29 /** Map from visible item id to its search score. */30 items: new Map() as Map<string, number>,31 /** Set of groups with at least one visible item. */32 groups: new Set() as Set<string>,33 },34})3536function filterItems() {37 if (!filterState.search) {38 filterState.filtered.count = allItems.value.size39 // Do nothing, each item will know to show itself because search is empty40 return41 }4243 // Reset the groups44 filterState.filtered.groups = new Set()45 let itemCount = 04647 // Check which items should be included48 for (const [id, value] of allItems.value) {49 const score = contains(value, filterState.search)50 filterState.filtered.items.set(id, score ? 1 : 0)51 if (score)52 itemCount++53 }5455 // Check which groups have at least 1 item shown56 for (const [groupId, group] of allGroups.value) {57 for (const itemId of group) {58 if (filterState.filtered.items.get(itemId)! > 0) {59 filterState.filtered.groups.add(groupId)60 break61 }62 }63 }6465 filterState.filtered.count = itemCount66}6768watch(() => filterState.search, () => {69 filterItems()70})7172provideCommandContext({73 allItems,74 allGroups,75 filterState,76})77</script>7879<template>80 <ListboxRoot81 v-bind="forwarded"82 :class="cn('flex h-full w-full flex-col overflow-hidden rounded-md bg-popover text-popover-foreground', props.class)"83 >84 <slot />85 </ListboxRoot>86</template>
What it pulls in
npm packages
Other registry items
Files it writes
More from shadcn-vue
All 65 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| accordionaccordion | shadcn-vue | Components | Free | 2 deps · 5 files | |
| alertalert | shadcn-vue | Components | Free | no deps · 4 files | |
| alert-dialogalert-dialog | shadcn-vue | Components | Free | 2 deps · 10 files | |
| aspect-ratioaspect-ratio | shadcn-vue | Components | Free | 1 dep · 2 files | |
| attachmentattachment | shadcn-vue | Components | Free | 1 dep · 10 files | |
| avataravatar | shadcn-vue | Components | Free | 2 deps · 4 files | |
| badgebadge | shadcn-vue | Components | Free | 2 deps · 2 files | |
| breadcrumbbreadcrumb | shadcn-vue | Components | Free | 1 dep · 8 files |
