ContentSearch 组件继承了 CommandPalette 组件,因此您可以传递任何属性,例如 icon、placeholder 等。
使用 files 和 navigation props,以及您使用 @nuxt/content 中的 queryCollectionSearchSections 和 queryCollectionNavigation 组合式函数获取的 files 和 navigation 值。
useContentSearch 组合式函数打开 CommandPalette:const { open } = useContentSearch()。使用 shortcut prop 更改 defineShortcuts 中用于打开 ContentSearch 组件的快捷键。默认为 meta_k ( K)。
<template>
<UApp>
<ClientOnly>
<LazyUContentSearch
v-model:search-term="searchTerm"
shortcut="meta_k"
:files="files"
:navigation="navigation"
:fuse="{ resultLimit: 42 }"
/>
</ClientOnly>
</UApp>
</template>
默认情况下,一组命令将被添加到命令面板中,以便您可以在浅色模式和深色模式之间切换。这仅在特定页面中未强制使用 colorMode 时才生效,这可以通过 definePageMeta 实现
<script setup lang="ts">
definePageMeta({
colorMode: 'dark'
})
</script>
您可以通过将 color-mode prop 设置为 false 来禁用此行为
<template>
<UApp>
<ClientOnly>
<LazyUContentSearch
v-model:search-term="searchTerm"
:color-mode="false"
:files="files"
:navigation="navigation"
:fuse="{ resultLimit: 42 }"
/>
</ClientOnly>
</UApp>
</template>
app.vue 中在您的 app.vue 或布局中使用 ContentSearch 组件
<script setup lang="ts">
const { data: navigation } = await useAsyncData('navigation', () => queryCollectionNavigation('content'))
const { data: files } = useLazyAsyncData('search', () => queryCollectionSearchSections('content'), {
server: false
})
const links = [{
label: 'Docs',
icon: 'i-lucide-book',
to: '/docs/getting-started'
}, {
label: 'Components',
icon: 'i-lucide-box',
to: '/docs/components'
}, {
label: 'Showcase',
icon: 'i-lucide-presentation',
to: '/showcase'
}]
const searchTerm = ref('')
</script>
<template>
<UApp>
<ClientOnly>
<LazyUContentSearch
v-model:search-term="searchTerm"
:files="files"
shortcut="meta_k"
:navigation="navigation"
:links="links"
:fuse="{ resultLimit: 42 }"
/>
</ClientOnly>
</UApp>
</template>
ContentSearch 组件包裹在ClientOnly组件中,以便它不会在服务器上渲染。| 属性 | 默认值 | 类型 |
|---|---|---|
图标 |
|
输入框中显示的图标。 |
placeholder |
|
输入框的占位文本。 |
autofocus |
|
组件挂载时自动聚焦输入框。 |
loading |
当为 | |
loadingIcon |
|
当 |
close |
|
在输入框中显示关闭按钮(例如,在模态框中时很有用)。 |
closeIcon |
|
关闭按钮中显示的图标。 |
快捷键 |
|
打开搜索的键盘快捷键(由 |
links |
作为命令面板中第一个组显示的链接组。
| |
导航 |
| |
分组 |
在导航组和颜色模式组之间显示的自定义组。 | |
文件 |
| |
fuse |
|
用于 `useFuse` 的选项useFuse传递给CommandPalette. |
colorMode |
|
当为 |
title |
| |
description |
| |
叠加层 |
|
在模态框后面渲染一个叠加层。 |
过渡动画 |
|
在打开或关闭时为模态框添加动画效果。 |
内容 |
模态框的内容。
| |
可关闭的 |
|
当为 |
全屏 |
|
当为 |
modal |
对话框的模态性。当设置为 | |
portal |
|
在传送门中渲染模态框。 |
搜索词 |
|
|
ui |
|
| 插槽 | 类型 |
|---|---|
空 |
|
页脚 | |
返回 | |
close |
|
item | |
item-leading | |
item-label | |
项目描述 | |
item-trailing |
|
内容 |
|
| 事件 | 类型 |
|---|---|
update:searchTerm |
|
export default defineAppConfig({
ui: {
contentSearch: {
slots: {
modal: '',
input: '[&>input]:text-base/5'
},
variants: {
fullscreen: {
false: {
modal: 'sm:max-w-3xl sm:h-[28rem]'
}
}
}
}
}
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'
export default defineConfig({
plugins: [
vue(),
ui({
ui: {
contentSearch: {
slots: {
modal: '',
input: '[&>input]:text-base/5'
},
variants: {
fullscreen: {
false: {
modal: 'sm:max-w-3xl sm:h-[28rem]'
}
}
}
}
}
})
]
})