Skip to content

Commit 3d5104e

Browse files
committed
docs: add Opt/React/Vue/Svelte components
1 parent a3470ba commit 3d5104e

File tree

9 files changed

+179
-2
lines changed

9 files changed

+179
-2
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<script setup lang="ts">
2+
import { computed, ref } from 'vue'
3+
import { useTabsSelectedState } from '../composables/useTabsSelectedState'
4+
5+
const props = defineProps<{
6+
v: string
7+
}>()
8+
9+
// Use the shared tabs state system
10+
const acceptValues = ref(['Vue', 'React', 'Svelte 4', 'Svelte 5'])
11+
const sharedStateKey = ref('frameworks')
12+
13+
const { selected } = useTabsSelectedState(acceptValues, sharedStateKey)
14+
15+
const shouldShow = computed(() => {
16+
if (!selected.value) return false
17+
18+
// Handle multiple values separated by pipe (|)
19+
const values = props.v.split('|').map((v) => v.trim())
20+
return values.includes(selected.value)
21+
})
22+
</script>
23+
24+
<template>
25+
<span v-if="shouldShow" class="opt-text">
26+
<slot></slot>
27+
</span>
28+
</template>
29+
30+
<style scoped>
31+
.opt-text {
32+
display: inline;
33+
}
34+
</style>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script setup lang="ts">
2+
import Opt from './Opt.vue'
3+
</script>
4+
5+
<template>
6+
<Opt v="React">
7+
<slot></slot>
8+
</Opt>
9+
</template>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script setup lang="ts">
2+
import Opt from './Opt.vue'
3+
</script>
4+
5+
<template>
6+
<Opt v="Svelte 4|Svelte 5">
7+
<slot></slot>
8+
</Opt>
9+
</template>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script setup lang="ts">
2+
import Opt from './Opt.vue'
3+
</script>
4+
5+
<template>
6+
<Opt v="Svelte 4">
7+
<slot></slot>
8+
</Opt>
9+
</template>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script setup lang="ts">
2+
import Opt from './Opt.vue'
3+
</script>
4+
5+
<template>
6+
<Opt v="Svelte 5">
7+
<slot></slot>
8+
</Opt>
9+
</template>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script setup lang="ts">
2+
import Opt from './Opt.vue'
3+
</script>
4+
5+
<template>
6+
<Opt v="Vue">
7+
<slot></slot>
8+
</Opt>
9+
</template>
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
import AvailableSince from './AvailableSince.vue'
2+
import Opt from './Opt.vue'
3+
import React from './React.vue'
4+
import Svelte from './Svelte.vue'
5+
import Svelte4 from './Svelte4.vue'
6+
import Svelte5 from './Svelte5.vue'
7+
import Vue from './Vue.vue'
28

3-
export { AvailableSince }
9+
export { AvailableSince, Opt, React, Svelte, Svelte4, Svelte5, Vue }
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import type { InjectionKey, Ref } from 'vue'
2+
import { computed, inject, onMounted, ref } from 'vue'
3+
4+
type TabsSharedState = {
5+
content?: TabsSharedStateContent
6+
}
7+
type TabsSharedStateContent = Record<string, string>
8+
9+
// Use the same injection key as the original vitepress-plugin-tabs
10+
const injectionKey: InjectionKey<TabsSharedState> =
11+
'vitepress:tabSharedState' as unknown as symbol
12+
const ls = typeof localStorage !== 'undefined' ? localStorage : null
13+
const localStorageKey = 'vitepress:tabsSharedState'
14+
15+
const getLocalStorageValue = (): TabsSharedStateContent => {
16+
const rawValue = ls?.getItem(localStorageKey)
17+
if (rawValue) {
18+
try {
19+
return JSON.parse(rawValue)
20+
} catch {}
21+
}
22+
return {}
23+
}
24+
25+
export const useTabsSelectedState = <T extends string>(
26+
acceptValues: Ref<T[]>,
27+
sharedStateKey: Ref<string | undefined>,
28+
) => {
29+
const sharedState = inject(injectionKey)
30+
if (!sharedState) {
31+
throw new Error(
32+
'[vitepress-plugin-tabs] TabsSharedState should be injected',
33+
)
34+
}
35+
36+
onMounted(() => {
37+
if (!sharedState.content) {
38+
sharedState.content = getLocalStorageValue()
39+
}
40+
})
41+
42+
const nonSharedState = ref<T | undefined>()
43+
44+
const selected = computed({
45+
get() {
46+
const key = sharedStateKey.value
47+
const acceptVals = acceptValues.value
48+
if (key) {
49+
const value = sharedState.content?.[key]
50+
if (value && (acceptVals as string[]).includes(value)) {
51+
return value as T
52+
}
53+
} else {
54+
const nonSharedStateVal = nonSharedState.value
55+
if (nonSharedStateVal) {
56+
return nonSharedStateVal
57+
}
58+
}
59+
return acceptVals[0]
60+
},
61+
set(v) {
62+
const key = sharedStateKey.value
63+
if (key) {
64+
if (sharedState.content) {
65+
sharedState.content[key] = v
66+
}
67+
} else {
68+
nonSharedState.value = v
69+
}
70+
},
71+
})
72+
73+
const select = (newValue: T) => {
74+
selected.value = newValue
75+
}
76+
77+
return { selected, select }
78+
}

docs/.vitepress/theme/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ import type { Theme } from 'vitepress'
33
import { enhanceAppWithTabs } from 'vitepress-plugin-tabs/client'
44
import DefaultTheme from 'vitepress/theme'
55
import { h } from 'vue'
6-
import { AvailableSince } from './components'
6+
import {
7+
AvailableSince,
8+
Opt,
9+
React,
10+
Svelte,
11+
Svelte4,
12+
Svelte5,
13+
Vue,
14+
} from './components'
715
import { setupFrameworksTabs } from './frameworksTabs'
816
import './style.css'
917

@@ -17,6 +25,12 @@ export default {
1725
enhanceApp({ app, router, siteData }) {
1826
enhanceAppWithTabs(app)
1927
app.component('AvailableSince', AvailableSince)
28+
app.component('Opt', Opt)
29+
app.component('React', React)
30+
app.component('Vue', Vue)
31+
app.component('Svelte', Svelte)
32+
app.component('Svelte4', Svelte4)
33+
app.component('Svelte5', Svelte5)
2034
},
2135
setup() {
2236
setupFrameworksTabs()

0 commit comments

Comments
 (0)