Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions src/client/theme-default/components/VPAlgoliaSearchBox.vue
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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