Skip to content

Commit

Permalink
feat: expose isDark (#1525)
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Oct 22, 2022
1 parent 63e6957 commit d327811
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/guide/api.md
Expand Up @@ -20,6 +20,7 @@ interface VitePressData {
title: Ref<string>
description: Ref<string>
localePath: Ref<string>
isDark: Ref<boolean>
}
```

Expand Down
14 changes: 12 additions & 2 deletions src/client/app/data.ts
@@ -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 siteData from '@siteData'
import {
Expand All @@ -20,6 +28,7 @@ export interface VitePressData<T = any> {
description: Ref<string>
lang: Ref<string>
localePath: Ref<string>
isDark: Ref<boolean>
}

// site data is a singleton
Expand Down Expand Up @@ -60,7 +69,8 @@ export function initData(route: Route): VitePressData {
}),
description: computed(() => {
return route.data.description || site.value.description
})
}),
isDark: ref(false)
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/client/theme-default/components/VPSwitchAppearance.vue
@@ -1,12 +1,12 @@
<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { ref, onMounted, watch } from 'vue'
import { useData } from 'vitepress'
import { APPEARANCE_KEY } from '../../shared.js'
import VPSwitch from './VPSwitch.vue'
import VPIconSun from './icons/VPIconSun.vue'
import VPIconMoon from './icons/VPIconMoon.vue'
const { site } = useData()
const { site, isDark } = useData()
const checked = ref(false)
const toggle = typeof localStorage !== 'undefined' ? useAppearance() : () => {}
Expand Down Expand Up @@ -68,6 +68,10 @@ function useAppearance() {
return toggle
}
watch(checked, (newIsDark) => {
isDark.value = newIsDark
})
</script>

<template>
Expand Down

0 comments on commit d327811

Please sign in to comment.