Skip to content

Commit ff43cef

Browse files
committed
refactor(plugin-slimsearch): tweaks
1 parent 6588b3f commit ff43cef

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

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

+29-28
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
defineComponent,
1414
h,
1515
inject,
16-
nextTick,
1716
onMounted,
1817
onUnmounted,
1918
ref,
@@ -73,6 +72,8 @@ export default defineComponent({
7372

7473
const inputElement = shallowRef<HTMLInputElement>()
7574
const suggestionsElement = shallowRef<HTMLDivElement>()
75+
const body = shallowRef<HTMLElement>()
76+
const isLocked = useScrollLock(body.value)
7677

7778
const hasSuggestions = computed(
7879
() =>
@@ -99,38 +100,38 @@ export default defineComponent({
99100
}
100101
})
101102

102-
const updateQueries = useDebounceFn(
103-
(): void => {
104-
void (
105-
searchOptions.value.querySplitter?.(input.value) ??
106-
Promise.resolve(input.value.split(' '))
107-
).then((result) => {
108-
queries.value = result
109-
})
110-
},
111-
Math.min(options.searchDelay, options.suggestDelay),
112-
)
103+
onClickOutside(suggestionsElement, () => {
104+
showSuggestion.value = false
105+
})
113106

114-
watchImmediate(input, updateQueries)
107+
watchImmediate(
108+
input,
109+
useDebounceFn(
110+
() =>
111+
(
112+
searchOptions.value.querySplitter?.(input.value) ??
113+
Promise.resolve(input.value.split(' '))
114+
).then((result) => {
115+
queries.value = result
116+
}),
117+
Math.min(options.searchDelay, options.suggestDelay),
118+
),
119+
)
115120

116121
onMounted(() => {
117-
const isLocked = useScrollLock(document.body)
118-
119-
watch(isActive, async (value) => {
120-
isLocked.value = value
121-
if (value) {
122-
await nextTick()
123-
inputElement.value?.focus()
124-
}
125-
})
122+
body.value = document.body
126123

127-
onClickOutside(suggestionsElement, () => {
128-
showSuggestion.value = false
129-
})
124+
watch(
125+
isActive,
126+
(value) => {
127+
if (value) inputElement.value?.focus()
128+
},
129+
{ flush: 'post' },
130+
)
131+
})
130132

131-
onUnmounted(() => {
132-
isLocked.value = false
133-
})
133+
onUnmounted(() => {
134+
isLocked.value = false
134135
})
135136

136137
return (): VNode | null =>

0 commit comments

Comments
 (0)