Repository navigation
Expand file tree
/
Copy pathCustomSwitchAppearance.vue
More file actions
67 lines (56 loc) · 1.67 KB
/
Copy pathCustomSwitchAppearance.vue
File metadata and controls
67 lines (56 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<script setup lang="ts">
import { inject, onMounted, ref } from 'vue'
import { useData } from 'vitepress'
import VPIconMoon from 'vitepress/dist/client/theme-default/components/icons/VPIconMoon.vue'
import VPIconSun from 'vitepress/dist/client/theme-default/components/icons/VPIconSun.vue'
const { isDark } = useData()
const toggleAppearance = inject('toggle-appearance', () => {
isDark.value = !isDark.value
})
const supportsViewTransition = ref(false)
onMounted(() => {
supportsViewTransition.value = 'startViewTransition' in document
&& window.matchMedia('(prefers-reduced-motion: no-preference)').matches
})
</script>
<template>
<button type="button" role="switch" title="Toggle dark mode" class="c-switch-appearance" :aria-checked="isDark"
:data-view-transition="supportsViewTransition" @click="toggleAppearance">
<ClientOnly>
<Transition name="fade" mode="out-in">
<VPIconSun v-if="!isDark" class="sun" />
<VPIconMoon v-else class="moon" />
</Transition>
</ClientOnly>
</button>
</template>
<style scoped>
.c-switch-appearance {
display: flex;
justify-content: center;
align-items: center;
width: 36px;
height: 36px;
color: var(--vp-c-text-2);
transition: color 0.5s;
}
.c-switch-appearance:hover {
color: var(--vp-c-text-1);
transition: color 0.25s;
}
.c-switch-appearance :deep(svg) {
width: 20px;
height: 20px;
fill: currentColor;
}
/* `<Transition name="fade">` needs these -- VitePress ships no global `fade`
classes, so without them the icon swap was an instant cut. */
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.15s ease;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
}
</style>