Skip to content

Commit 52ac5b7

Browse files
committed
refactor(plugin-slimsearch): avoid inject/provide
1 parent a567510 commit 52ac5b7

File tree

6 files changed

+20
-29
lines changed

6 files changed

+20
-29
lines changed

Diff for: plugins/search/plugin-slimsearch/src/client/components/SearchBox.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import {
66
useLocaleConfig,
77
} from '@vuepress/helper/client'
88
import type { VNode } from 'vue'
9-
import { computed, defineComponent, h, inject, onMounted, ref } from 'vue'
9+
import { computed, defineComponent, h, onMounted, ref } from 'vue'
1010

11-
import { searchModalSymbol } from '../composables/index.js'
11+
import { useActiveState } from '../composables/index.js'
1212
import { locales, options } from '../define.js'
1313
import { SearchIcon } from './icons.js'
1414

@@ -21,11 +21,11 @@ export default defineComponent({
2121

2222
setup() {
2323
const locale = useLocaleConfig(locales)
24-
const isActive = inject(searchModalSymbol)!
24+
const [isActive, toggleActive] = useActiveState()
2525
const isMacOS = ref(false)
2626

2727
useKeys(options.hotKeys, () => {
28-
if (!isActive.value) isActive.value = true
28+
if (!isActive.value) toggleActive()
2929
})
3030

3131
const controlKeys = computed(() =>

Diff for: plugins/search/plugin-slimsearch/src/client/components/SearchModal.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
import { useSiteLocaleData } from 'vuepress/client'
2323

2424
import {
25-
searchModalSymbol,
25+
useActiveState,
2626
useArrayCycle,
2727
useSuggestions,
2828
} from '../composables/index.js'
@@ -54,7 +54,7 @@ export default defineComponent({
5454
name: 'SearchModal',
5555

5656
setup() {
57-
const isActive = inject(searchModalSymbol)!
57+
const [isActive, toggleActive] = useActiveState()
5858
const siteLocale = useSiteLocaleData()
5959
const locale = useLocaleConfig(locales)
6060
const searchOptions = useSearchOptions()
@@ -96,7 +96,7 @@ export default defineComponent({
9696
}
9797
// hide the modal when pressing the escape key
9898
else if (event.key === 'Escape') {
99-
isActive.value = false
99+
toggleActive(false)
100100
}
101101
})
102102

@@ -140,7 +140,7 @@ export default defineComponent({
140140
h('div', {
141141
class: 'slimsearch-mask',
142142
onClick: () => {
143-
isActive.value = false
143+
toggleActive(false)
144144
input.value = ''
145145
},
146146
}),
@@ -233,7 +233,7 @@ export default defineComponent({
233233
type: 'button',
234234
class: 'slimsearch-close-button',
235235
onClick: () => {
236-
isActive.value = false
236+
toggleActive(false)
237237
input.value = ''
238238
},
239239
},
@@ -245,7 +245,7 @@ export default defineComponent({
245245
queries: queries.value,
246246
isFocusing: !hasSuggestions.value,
247247
onClose: () => {
248-
isActive.value = false
248+
toggleActive(false)
249249
},
250250
onUpdateQuery: (query: string) => {
251251
input.value = query

Diff for: plugins/search/plugin-slimsearch/src/client/composables/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export * from './setup.js'
1+
export * from './useActiveState.js'
22
export * from './useArrayCycle.js'
33
export * from './useMobile.js'
44
export * from './useQueryHistory.js'

Diff for: plugins/search/plugin-slimsearch/src/client/composables/setup.ts

-14
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { useToggle } from '@vueuse/core'
2+
import type { Ref } from 'vue'
3+
4+
export type ActiveState = [
5+
isActive: Ref<boolean>,
6+
toggleActive: (value?: boolean) => void,
7+
]
8+
9+
export const useActiveState = (): ActiveState => useToggle()

Diff for: plugins/search/plugin-slimsearch/src/client/config.ts

-4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@ import { defineClientConfig } from 'vuepress/client'
22

33
import SearchBox from './components/SearchBox.js'
44
import SearchModal from './components/SearchModal.js'
5-
import { setupSearchModal } from './composables/index.js'
65
import { injectSearchConfig } from './helpers/index.js'
76

87
export default defineClientConfig({
98
enhance({ app }) {
109
injectSearchConfig(app)
1110
app.component('SearchBox', SearchBox)
1211
},
13-
setup() {
14-
setupSearchModal()
15-
},
1612
rootComponents: [SearchModal],
1713
})

0 commit comments

Comments
 (0)