Skip to content

Commit

Permalink
feat: giscus dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenKS12138 committed Mar 9, 2024
1 parent f528de5 commit 31ed979
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
20 changes: 15 additions & 5 deletions src/components/ArticleWrapper.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts" setup>
import dayjs from 'dayjs'
import Giscus from '@giscus/vue';
import { useMyDark } from '~/composables/dark';
const { frontmatter } = defineProps<{
frontmatter: {
Expand All @@ -21,9 +22,17 @@ useHead({
]
})
const isDark = useDark()
const [isDark, _] = useMyDark();
const discusTheme = computed(() => isDark.value ? 'dark' : 'light');
const showGiscus = ref(true);
watch(isDark, () => {
showGiscus.value = false;
nextTick(() => {
showGiscus.value = true;
})
});
const discusTheme = isDark.value ? 'dark' : 'light';
</script>

Expand All @@ -34,9 +43,10 @@ const discusTheme = isDark.value ? 'dark' : 'light';
<div class="mt-2 italic">{{ (frontmatter.tags || []).map(x => `#${x}`).join(' ') }}</div>
</div>
<slot></slot>
<Giscus id="comments" repo="ChenKS12138/ChenKS12138.github.io" repoId="MDEwOlJlcG9zaXRvcnkyNzA4Njc3ODM="
category="Announcements" categoryId="DIC_kwDOECUdR84Cd10_" mapping="og:title" reactionsEnabled="1" emitMetadata="0"
inputPosition="bottom" :theme="discusTheme" lang="zh-CN" loading="lazy" />
<Giscus v-if="showGiscus" ref="giscusRef" id="comments" repo="ChenKS12138/ChenKS12138.github.io"
repoId="MDEwOlJlcG9zaXRvcnkyNzA4Njc3ODM=" category="Announcements" categoryId="DIC_kwDOECUdR84Cd10_"
mapping="og:title" reactionsEnabled="1" emitMetadata="0" inputPosition="bottom" :theme="discusTheme" lang="zh-CN"
loading="lazy" />
<!-- <div class="rounded bg-gray mt-10 p-1 text-white italic indent-md text-lg">
CC BY-SA 3.0协议 。转载请注明出处!
</div> -->
Expand Down
5 changes: 2 additions & 3 deletions src/components/DarkMode.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<script setup lang="ts">
const isDark = useDark()
const toggleDark = useToggle(isDark)
import { useMyDark } from '~/composables/dark';
const [_, toggleDark] = useMyDark();
</script>

<template>
<button class="cursor-pointer text-white bg-transparent py-0.75 px-2.5 text-sm" @click="toggleDark()">
<span class="inline-block align-middle dark:i-carbon-moon i-carbon-sun" />
<!-- <span class="pl-2">{{ isDark ? 'Dark' : 'Light' }}</span> -->
</button>
</template>
14 changes: 14 additions & 0 deletions src/composables/dark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const isDarkRef = ref(false);

const isDark = useDark()
const toggleDark = useToggle(isDark)
isDarkRef.value = isDark.value;

export function useMyDark() {
const toggleDarkWrapped = () => {
toggleDark();
isDarkRef.value = isDark.value;
}

return [isDarkRef, toggleDarkWrapped] as const;
}

0 comments on commit 31ed979

Please sign in to comment.