feat: expose isDark (#1525)

pull/1530/head
Divyansh Singh 2 years ago committed by GitHub
parent 63e695745e
commit d327811fd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -20,6 +20,7 @@ interface VitePressData {
title: Ref<string> title: Ref<string>
description: Ref<string> description: Ref<string>
localePath: Ref<string> localePath: Ref<string>
isDark: Ref<boolean>
} }
``` ```

@ -1,4 +1,12 @@
import { InjectionKey, Ref, shallowRef, readonly, computed, inject } from 'vue' import {
InjectionKey,
Ref,
computed,
inject,
readonly,
ref,
shallowRef
} from 'vue'
import { Route } from './router.js' import { Route } from './router.js'
import siteData from '@siteData' import siteData from '@siteData'
import { import {
@ -20,6 +28,7 @@ export interface VitePressData<T = any> {
description: Ref<string> description: Ref<string>
lang: Ref<string> lang: Ref<string>
localePath: Ref<string> localePath: Ref<string>
isDark: Ref<boolean>
} }
// site data is a singleton // site data is a singleton
@ -60,7 +69,8 @@ export function initData(route: Route): VitePressData {
}), }),
description: computed(() => { description: computed(() => {
return route.data.description || site.value.description return route.data.description || site.value.description
}) }),
isDark: ref(false)
} }
} }

@ -1,12 +1,12 @@
<script lang="ts" setup> <script lang="ts" setup>
import { ref, onMounted } from 'vue' import { ref, onMounted, watch } from 'vue'
import { useData } from 'vitepress' import { useData } from 'vitepress'
import { APPEARANCE_KEY } from '../../shared.js' import { APPEARANCE_KEY } from '../../shared.js'
import VPSwitch from './VPSwitch.vue' import VPSwitch from './VPSwitch.vue'
import VPIconSun from './icons/VPIconSun.vue' import VPIconSun from './icons/VPIconSun.vue'
import VPIconMoon from './icons/VPIconMoon.vue' import VPIconMoon from './icons/VPIconMoon.vue'
const { site } = useData() const { site, isDark } = useData()
const checked = ref(false) const checked = ref(false)
const toggle = typeof localStorage !== 'undefined' ? useAppearance() : () => {} const toggle = typeof localStorage !== 'undefined' ? useAppearance() : () => {}
@ -68,6 +68,10 @@ function useAppearance() {
return toggle return toggle
} }
watch(checked, (newIsDark) => {
isDark.value = newIsDark
})
</script> </script>
<template> <template>

Loading…
Cancel
Save