Skip to content

Commit 0e472e6

Browse files
committed
make api preference dynamic
1 parent d7e5624 commit 0e472e6

File tree

5 files changed

+42
-18
lines changed

5 files changed

+42
-18
lines changed

src/.vitepress/inlined-scripts/restorePreference.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
document.documentElement.classList.add(cls)
66
}
77
}
8-
restore('vue-docs-api-preference', 'prefer-composition')
9-
restore('vue-docs-format-preference', 'prefer-sfc')
8+
restore('vue-docs-prefer-composition', 'prefer-composition')
9+
restore('vue-docs-prefer-sfc', 'prefer-sfc')
1010
})()

src/.vitepress/theme/components/PreferenceSwitch.vue

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,42 @@
11
<script setup lang="ts">
22
import { VTSwitch, VTIconChevronDown, VTIconChevronUp } from '@vue/theme'
33
import { useRoute } from 'vitepress'
4-
import { ref, computed } from 'vue'
4+
import { ref, computed, Ref } from 'vue'
5+
import {
6+
preferCompositionKey,
7+
preferComposition,
8+
preferSFCKey,
9+
preferSFC
10+
} from './preferences'
511
612
const route = useRoute()
713
const show = computed(() => /^\/(guide|tutorial|examples)\//.test(route.path))
814
const isOpen = ref(false)
915
1016
const toggleOpen = () => (isOpen.value = !isOpen.value)
1117
const toggleCompositionAPI = useToggleFn(
12-
'vue-docs-api-preference',
18+
preferCompositionKey,
19+
preferComposition,
1320
'prefer-composition'
1421
)
15-
const toggleSFC = useToggleFn('vue-docs-format-preference', 'prefer-sfc')
22+
const toggleSFC = useToggleFn(preferSFCKey, preferSFC, 'prefer-sfc')
1623
17-
function useToggleFn(storageKey: string, className: string) {
24+
function useToggleFn(
25+
storageKey: string,
26+
state: Ref<boolean>,
27+
className: string
28+
) {
1829
if (typeof localStorage === 'undefined') {
1930
return () => {}
2031
}
21-
let enabled = JSON.parse(localStorage.getItem(storageKey) || 'false')
2232
const classList = document.documentElement.classList
23-
return (value = !enabled) => {
24-
if ((enabled = value)) {
33+
return (value = !state.value) => {
34+
if ((state.value = value)) {
2535
classList.add(className)
2636
} else {
2737
classList.remove(className)
2838
}
29-
localStorage.setItem(storageKey, enabled)
39+
localStorage.setItem(storageKey, String(state.value))
3040
}
3141
}
3242
</script>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { ref } from 'vue'
2+
3+
const hasStorage = typeof localStorage !== 'undefined'
4+
const get = (key) =>
5+
hasStorage ? JSON.parse(localStorage.getItem(key) || 'false') : false
6+
7+
export const preferCompositionKey = 'vue-docs-prefer-composition'
8+
export const preferComposition = ref(get(preferCompositionKey))
9+
10+
export const preferSFCKey = 'vue-docs-prefer-sfc'
11+
export const preferSFC = ref(get(preferSFCKey))

src/.vitepress/theme/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { h } from 'vue'
22
import { VPTheme, VTBadge } from '@vue/theme'
33
import PreferenceSwitch from './components/PreferenceSwitch.vue'
4+
import { preferComposition, preferSFC } from './components/preferences'
45
import './styles/inline-demo.css'
56
import './styles/options-boxes.css'
67

@@ -13,5 +14,7 @@ export default {
1314
},
1415
enhanceApp({ app }) {
1516
app.component('Badge', VTBadge)
17+
app.provide('prefer-composition', preferComposition)
18+
app.provide('prefer-sfc', preferSFC)
1619
}
1720
}

src/examples/ExampleRepl.vue

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
<script setup>
1+
<script setup lang="ts">
22
import { Repl, ReplStore } from '@vue/repl'
33
import '@vue/repl/style.css'
44
import data from './data.json'
5+
import { inject, watch } from 'vue'
56
67
const store = new ReplStore()
78
8-
// TODO make dynamic
9-
const preference = document.documentElement.classList.contains(
10-
'prefer-composition'
11-
)
12-
? 'composition'
13-
: 'options'
9+
const preferComposition = inject('prefer-composition')
10+
const preferSFC = inject('prefer-sfc')
1411
1512
function updateExample() {
1613
const hash = ___location.hash.slice(1)
1714
if (data.hasOwnProperty(hash)) {
18-
store.setFiles(data[hash][preference])
15+
store.setFiles(
16+
data[hash][preferComposition.value ? 'composition' : 'options']
17+
)
1918
} else if (!hash) {
2019
___location.hash = '#markdown'
2120
}
2221
}
2322
2423
window.addEventListener('hashchange', updateExample)
24+
watch(preferComposition, updateExample)
2525
updateExample()
2626
</script>
2727

0 commit comments

Comments
 (0)