Skip to content

Commit

Permalink
fix(theme/search): prevent reactivity loss with i18n (#3121)
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Oct 22, 2023
1 parent 5542f29 commit 50d61fa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
16 changes: 6 additions & 10 deletions src/client/theme-default/components/VPAlgoliaSearchBox.vue
@@ -1,8 +1,8 @@
<script setup lang="ts">
import type { DefaultTheme } from 'vitepress/theme'
import docsearch from '@docsearch/js'
import { onMounted, watch } from 'vue'
import { useRouter, useRoute } from 'vitepress'
import { useRoute, useRouter } from 'vitepress'
import type { DefaultTheme } from 'vitepress/theme'
import { nextTick, onMounted, watch } from 'vue'
import { useData } from '../composables/data'
const props = defineProps<{
Expand All @@ -18,7 +18,8 @@ type DocSearchProps = Parameters<typeof docsearch>[0]
onMounted(update)
watch(localeIndex, update)
function update() {
async function update() {
await nextTick()
const options = {
...props.algolia,
...props.algolia.locales?.[localeIndex.value]
Expand Down Expand Up @@ -86,12 +87,7 @@ function initialize(userOptions: DefaultTheme.AlgoliaSearchOptions) {
function getRelativePath(url: string) {
const { pathname, hash } = new URL(url, location.origin)
return (
pathname.replace(
/\.html$/,
site.value.cleanUrls ? '' : '.html'
) + hash
)
return pathname.replace(/\.html$/, site.value.cleanUrls ? '' : '.html') + hash
}
</script>

Expand Down
9 changes: 7 additions & 2 deletions src/client/theme-default/components/VPLocalSearchBox.vue
Expand Up @@ -4,6 +4,7 @@ import {
computedAsync,
debouncedWatch,
onKeyStroke,
reactify,
useEventListener,
useLocalStorage,
useScrollLock,
Expand All @@ -12,7 +13,7 @@ import {
import { useFocusTrap } from '@vueuse/integrations/useFocusTrap'
import Mark from 'mark.js/src/vanilla.js'
import MiniSearch, { type SearchResult } from 'minisearch'
import { inBrowser, useRouter, dataSymbol } from 'vitepress'
import { dataSymbol, inBrowser, useRouter } from 'vitepress'
import {
computed,
createApp,
Expand All @@ -22,6 +23,7 @@ import {
onMounted,
ref,
shallowRef,
toRef,
watch,
watchEffect,
type Ref
Expand Down Expand Up @@ -352,7 +354,10 @@ const defaultTranslations: { modal: ModalTranslations } = {
}
}
const $t = createTranslate(theme.value.search?.options, defaultTranslations)
const $t = reactify(createTranslate)(
toRef(() => theme.value.search?.options),
defaultTranslations
)
// Back
Expand Down
9 changes: 7 additions & 2 deletions src/client/theme-default/components/VPNavBarSearchButton.vue
@@ -1,4 +1,6 @@
<script lang="ts" setup>
import { reactify } from '@vueuse/core'
import { toRef } from 'vue'
import type { ButtonTranslations } from '../../../../types/local-search'
import { useData } from '../composables/data'
import { createTranslate } from '../support/translation'
Expand All @@ -9,11 +11,14 @@ const { theme } = useData()
const defaultTranslations: { button: ButtonTranslations } = {
button: {
buttonText: 'Search',
buttonAriaLabel: 'Search',
buttonAriaLabel: 'Search'
}
}
const $t = createTranslate(theme.value.search?.options, defaultTranslations)
const $t = reactify(createTranslate)(
toRef(() => theme.value.search?.options),
defaultTranslations
)
</script>

<template>
Expand Down

0 comments on commit 50d61fa

Please sign in to comment.