Skip to content

Commit 444d89e

Browse files
committed
refactor(plugin-docsearch): simplify the Docsearch component
1 parent 23a4c68 commit 444d89e

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

ecosystem/plugin-docsearch/src/client/components/Docsearch.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { DocsearchOptions } from '../../shared/index.js'
77
import { useDocsearchShim } from '../composables/index.js'
88

99
declare const __DOCSEARCH_INJECT_STYLES__: boolean
10+
declare const __DOCSEARCH_OPTIONS__: DocsearchOptions
1011

1112
if (__DOCSEARCH_INJECT_STYLES__) {
1213
import('@docsearch/css')
@@ -24,7 +25,8 @@ export const Docsearch = defineComponent({
2425
},
2526
options: {
2627
type: Object as PropType<DocsearchOptions>,
27-
required: true,
28+
required: false,
29+
default: () => __DOCSEARCH_OPTIONS__,
2830
},
2931
},
3032

@@ -33,8 +35,8 @@ export const Docsearch = defineComponent({
3335
const lang = usePageLang()
3436
const docsearchShim = useDocsearchShim()
3537

36-
// resolve docsearch props for current locale
37-
const propsLocale = computed(() => ({
38+
// resolve docsearch options for current locale
39+
const optionsLocale = computed(() => ({
3840
...props.options,
3941
...props.options.locales?.[routeLocale.value],
4042
}))
@@ -43,7 +45,7 @@ export const Docsearch = defineComponent({
4345

4446
const initialize = (): void => {
4547
const rawFacetFilters =
46-
propsLocale.value.searchParameters?.facetFilters ?? []
48+
optionsLocale.value.searchParameters?.facetFilters ?? []
4749
facetFilters.splice(
4850
0,
4951
facetFilters.length,
@@ -53,10 +55,10 @@ export const Docsearch = defineComponent({
5355
// @ts-expect-error: https://github.com/microsoft/TypeScript/issues/50690
5456
docsearch({
5557
...docsearchShim,
56-
...propsLocale.value,
58+
...optionsLocale.value,
5759
container: `#${props.containerId}`,
5860
searchParameters: {
59-
...propsLocale.value.searchParameters,
61+
...optionsLocale.value.searchParameters,
6062
facetFilters,
6163
},
6264
})
@@ -67,7 +69,7 @@ export const Docsearch = defineComponent({
6769

6870
// re-initialize if the options is changed
6971
watch(
70-
[routeLocale, propsLocale],
72+
[routeLocale, optionsLocale],
7173
(
7274
[curRouteLocale, curPropsLocale],
7375
[prevRouteLocale, prevPropsLocale]
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
import { defineClientConfig } from '@vuepress/client'
2-
import { h } from 'vue'
3-
import type { DocsearchOptions } from '../shared/index.js'
42
import { Docsearch } from './components/index.js'
53

6-
declare const __DOCSEARCH_OPTIONS__: DocsearchOptions
7-
8-
const options = __DOCSEARCH_OPTIONS__
9-
104
export default defineClientConfig({
115
enhance({ app }) {
12-
// wrap the `<Docsearch />` component with plugin options
13-
app.component('Docsearch', () => h(Docsearch, { options }))
6+
app.component('Docsearch', Docsearch)
147
},
158
})

0 commit comments

Comments
 (0)