fix(theme/search): prevent reactivity loss with i18n (#3121)

pull/3122/head
Divyansh Singh 1 year ago committed by GitHub
parent 5542f29569
commit 50d61faefa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,8 +1,8 @@
<script setup lang="ts"> <script setup lang="ts">
import type { DefaultTheme } from 'vitepress/theme'
import docsearch from '@docsearch/js' import docsearch from '@docsearch/js'
import { onMounted, watch } from 'vue' import { useRoute, useRouter } from 'vitepress'
import { useRouter, useRoute } from 'vitepress' import type { DefaultTheme } from 'vitepress/theme'
import { nextTick, onMounted, watch } from 'vue'
import { useData } from '../composables/data' import { useData } from '../composables/data'
const props = defineProps<{ const props = defineProps<{
@ -18,7 +18,8 @@ type DocSearchProps = Parameters<typeof docsearch>[0]
onMounted(update) onMounted(update)
watch(localeIndex, update) watch(localeIndex, update)
function update() { async function update() {
await nextTick()
const options = { const options = {
...props.algolia, ...props.algolia,
...props.algolia.locales?.[localeIndex.value] ...props.algolia.locales?.[localeIndex.value]
@ -86,12 +87,7 @@ function initialize(userOptions: DefaultTheme.AlgoliaSearchOptions) {
function getRelativePath(url: string) { function getRelativePath(url: string) {
const { pathname, hash } = new URL(url, location.origin) const { pathname, hash } = new URL(url, location.origin)
return ( return pathname.replace(/\.html$/, site.value.cleanUrls ? '' : '.html') + hash
pathname.replace(
/\.html$/,
site.value.cleanUrls ? '' : '.html'
) + hash
)
} }
</script> </script>

@ -4,6 +4,7 @@ import {
computedAsync, computedAsync,
debouncedWatch, debouncedWatch,
onKeyStroke, onKeyStroke,
reactify,
useEventListener, useEventListener,
useLocalStorage, useLocalStorage,
useScrollLock, useScrollLock,
@ -12,7 +13,7 @@ import {
import { useFocusTrap } from '@vueuse/integrations/useFocusTrap' import { useFocusTrap } from '@vueuse/integrations/useFocusTrap'
import Mark from 'mark.js/src/vanilla.js' import Mark from 'mark.js/src/vanilla.js'
import MiniSearch, { type SearchResult } from 'minisearch' import MiniSearch, { type SearchResult } from 'minisearch'
import { inBrowser, useRouter, dataSymbol } from 'vitepress' import { dataSymbol, inBrowser, useRouter } from 'vitepress'
import { import {
computed, computed,
createApp, createApp,
@ -22,6 +23,7 @@ import {
onMounted, onMounted,
ref, ref,
shallowRef, shallowRef,
toRef,
watch, watch,
watchEffect, watchEffect,
type Ref type Ref
@ -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 // Back

@ -1,4 +1,6 @@
<script lang="ts" setup> <script lang="ts" setup>
import { reactify } from '@vueuse/core'
import { toRef } from 'vue'
import type { ButtonTranslations } from '../../../../types/local-search' import type { ButtonTranslations } from '../../../../types/local-search'
import { useData } from '../composables/data' import { useData } from '../composables/data'
import { createTranslate } from '../support/translation' import { createTranslate } from '../support/translation'
@ -9,11 +11,14 @@ const { theme } = useData()
const defaultTranslations: { button: ButtonTranslations } = { const defaultTranslations: { button: ButtonTranslations } = {
button: { button: {
buttonText: 'Search', 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> </script>
<template> <template>

Loading…
Cancel
Save