Skip to content

Commit

Permalink
fix(tabs): fix hydration mismatch error when non default tab is selec…
Browse files Browse the repository at this point in the history
…ted (#40)
  • Loading branch information
sapphi-red authored Sep 18, 2023
1 parent fa83ee6 commit 1669888
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-beds-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vitepress-plugin-tabs': patch
---

fix hydration mismatch error when non default tab is selected.
38 changes: 28 additions & 10 deletions packages/vitepress-plugin-tabs/src/client/useTabsSelectedState.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { ref, computed, reactive, inject, watch } from 'vue'
import { ref, computed, reactive, inject, watch, onMounted } from 'vue'
import type { App, Ref, InjectionKey } from 'vue'

type TabsSharedState = Record<string, string>
type TabsSharedState = {
content?: TabsSharedStateContent
}
type TabsSharedStateContent = Record<string, string>

const injectionKey: InjectionKey<TabsSharedState> =
'vitepress:tabSharedState' as unknown as symbol
const ls = typeof localStorage !== 'undefined' ? localStorage : null
const localStorageKey = 'vitepress:tabsSharedState'

const getLocalStorageValue = (): TabsSharedState => {
const getLocalStorageValue = (): TabsSharedStateContent => {
const rawValue = ls?.getItem(localStorageKey)
if (rawValue) {
try {
Expand All @@ -17,16 +20,23 @@ const getLocalStorageValue = (): TabsSharedState => {
}
return {}
}
const setLocalStorageValue = (v: TabsSharedState) => {
const setLocalStorageValue = (v: TabsSharedStateContent) => {
if (!ls) return
ls.setItem(localStorageKey, JSON.stringify(v))
}

export const provideTabsSharedState = (app: App) => {
const state = reactive(getLocalStorageValue())
watch(state, newState => {
setLocalStorageValue(newState)
})
const state = reactive<TabsSharedState>({})
watch(
() => state.content,
(newStateContent, oldStateContent) => {
// skip initialize
if (newStateContent && oldStateContent) {
setLocalStorageValue(newStateContent)
}
},
{ deep: true }
)

app.provide(injectionKey, state)
}
Expand All @@ -42,14 +52,20 @@ export const useTabsSelectedState = <T extends string>(
)
}

onMounted(() => {
if (!sharedState.content) {
sharedState.content = getLocalStorageValue()
}
})

const nonSharedState = ref<T | undefined>()

const selected = computed({
get() {
const key = sharedStateKey.value
const acceptVals = acceptValues.value
if (key) {
const value = sharedState[key]
const value = sharedState.content?.[key]
if (value && (acceptVals as string[]).includes(value)) {
return value as T
}
Expand All @@ -64,7 +80,9 @@ export const useTabsSelectedState = <T extends string>(
set(v) {
const key = sharedStateKey.value
if (key) {
sharedState[key] = v
if (sharedState.content) {
sharedState.content[key] = v
}
} else {
nonSharedState.value = v
}
Expand Down

0 comments on commit 1669888

Please sign in to comment.