内容搜索

一个随时可用的命令面板,可添加到您的文档中。
此组件仅在安装了 @nuxt/content 模块时可用。

用法

ContentSearch 组件继承自 CommandPalette 组件,因此你可以传递任何属性,例如 iconplaceholder 等。

使用 filesnavigation 属性,并配合从 @nuxt/content 获取的 filesnavigation 值,以及 queryCollectionSearchSectionsqueryCollectionNavigation 组合式函数。

你可以通过按下 K 键,或者使用 ContentSearchButton 组件,或者使用 useContentSearch 组合式函数来打开 CommandPalette: const { open } = useContentSearch()

快捷键

使用 shortcut 属性来更改 defineShortcuts 中用于打开 ContentSearch 组件的快捷键。默认值为 meta_k K)。

app.vue
<template>
  <UApp>
    <ClientOnly>
      <LazyUContentSearch
        v-model:search-term="searchTerm"
        shortcut="meta_k"
        :files="files"
        :navigation="navigation"
        :fuse="{ resultLimit: 42 }"
      />
    </ClientOnly>
  </UApp>
</template>

颜色模式

默认情况下,一组命令将被添加到命令面板,以便你可以切换浅色和深色模式。这仅在 colorMode 没有在特定页面中强制设置时生效,可以通过 definePageMeta 实现。

pages/index.vue
<script setup lang="ts">
definePageMeta({
  colorMode: 'dark'
})
</script>

你可以通过将 color-mode 属性设置为 false 来禁用此行为。

app.vue
<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 或布局中Dónde使用 ContentSearch 组件。

app.vue
<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组件中,以避免在服务器端渲染。

API

属性

属性默认值类型
图标

appConfig.ui.icons.search

字符串 | 对象

输入框中显示的图标。

placeholder

t('commandPalette.placeholder')

string

输入框的占位文本。

autofocus

true

boolean

组件挂载时自动聚焦输入框。

loading

boolean

当为 true 时,将显示加载图标。

loadingIcon

appConfig.ui.icons.loading

字符串 | 对象

loading prop 为 true 时显示的图标。

close

true

布尔值 | 部分<ButtonProps>

在输入框中显示一个关闭按钮(例如,当在 Modal 中时很有用)。 { size: 'md', color: 'neutral', variant: 'ghost' }

closeIcon

appConfig.ui.icons.close

字符串 | 对象

关闭按钮中显示的图标。

shortcut

'meta_k'

string

用于打开搜索的键盘快捷键(由defineShortcuts)

links

ContentSearchLink[]

链接组显示为命令面板中的第一个组。

navigation

ContentNavigationItem[]

分组

CommandPaletteGroup<ContentSearchItem>[]

在导航和颜色模式组之间显示的自定义组。

files

ContentSearchFile[]

fuse

{ fuseOptions:{ includeMatches: true } }

UseFuseOptions<ContentSearchLink>

用于 `useFuse` 的选项useFuse传递给CommandPalette.

colorMode

true

boolean

当设置为 true 时,主题命令将被添加到组中。

title

string

description

string

叠加层

true

boolean

在模态框后面渲染一个叠加层。

过渡动画

true

boolean

在打开或关闭时为模态框添加动画效果。

内容

DialogContentProps & Partial<EmitsToProps<DialogContentImplEmits>>

模态框的内容。

可关闭的

true

boolean

当为 false 时,点击外部或按下 Escape 键时模态框将不会关闭。

全屏

false

boolean

当为 true 时,模态框将占据整个屏幕。

modal

boolean

对话框的模态性。当设置为 true 时,
与外部元素的交互将被禁用,并且只有对话框内容对屏幕阅读器可见。

portal

true

string | false | true | HTMLElement

在传送门中渲染模态框。

搜索词

''

string

ui

{ modal?: ClassNameValue; input?: ClassNameValue; } &{ root?: ClassNameValue; input?: ClassNameValue; close?: ClassNameValue; back?: ClassNameValue; content?: ClassNameValue; footer?: ClassNameValue; viewport?: ClassNameValue; group?: ClassNameValue; empty?: ClassNameValue; label?: ClassNameValue; item?: ClassNameValue; itemLeadingIcon?: ClassNameValue; itemLeadingAvatar?: ClassNameValue; itemLeadingAvatarSize?: ClassNameValue; itemLeadingChip?: ClassNameValue; itemLeadingChipSize?: ClassNameValue; itemTrailing?: ClassNameValue; itemTrailingIcon?: ClassNameValue; itemTrailingHighlightedIcon?: ClassNameValue; itemTrailingKbds?: ClassNameValue; itemTrailingKbdsSize?: ClassNameValue; itemLabel?: ClassNameValue; itemLabelBase?: ClassNameValue; itemLabelPrefix?: ClassNameValue; itemLabelSuffix?: ClassNameValue; }

插槽

插槽类型

{ searchTerm?: string | undefined; }

页脚

{ ui: {}; }

返回

{ ui: {}; }

close

{ ui: {}; }

item

{ item: ContentSearchItem; index: number; }

item-leading

{ item: ContentSearchItem; index: number; }

item-label

{ item: ContentSearchItem; index: number; }

item-trailing

{ item: ContentSearchItem; index: number; }

内容

{}

事件

事件类型
update:searchTerm

[value: string]

主题

app.config.ts
export default defineAppConfig({
  ui: {
    contentSearch: {
      slots: {
        modal: '',
        input: '[&>input]:text-base/5'
      },
      variants: {
        fullscreen: {
          false: {
            modal: 'sm:max-w-3xl sm:h-[28rem]'
          }
        }
      }
    }
  }
})
vite.config.ts
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]'
              }
            }
          }
        }
      }
    })
  ]
})

更新日志

095a0— fix: proxy modal props to support fullscreen

3173b— fix: proxySlots 响应性 (#4969)

61b60— 功能:允许传递组件而不是名称 (#4766)

946c2— fix: make ui.modal work

5cb65— 特性:导入 @nuxt/ui-pro 组件