docs/.vitepress/theme/components/VPSwitchAppearance.vue

b2c3e4e6874256bc959c763c2427cd2f3e4e4fcd
wotaku/docs/.vitepress/theme/components/VPSwitchAppearance.vue history · blame · raw

67 lines · 1640 bytes

 1<!--
 2  All Rights Reserved
 3
 4  Copyright (c) 2025 taskylizard
 5
 6  All rights reserved. This code and its associated files may not be copied, modified, distributed, sublicensed, or used in any form, in whole or in part, without prior written permission from the copyright holder.
 7-->
 8<script setup lang="ts">
 9import { useData } from 'vitepress'
10import { inject } from 'vue'
11import VPIconMoon from 'vitepress/dist/client/theme-default/components/icons/VPIconMoon.vue'
12import VPIconSun from 'vitepress/dist/client/theme-default/components/icons/VPIconSun.vue'
13
14const { isDark } = useData()
15
16const toggleAppearance = inject('toggle-appearance', () => {
17  isDark.value = !isDark.value
18})
19</script>
20
21<template>
22  <button type="button" role="switch" title="VPSwitchAppearance" class="VPSwitchAppearance" :aria-checked="isDark"
23    @click="toggleAppearance">
24    <ClientOnly>
25      <Transition name="fade" mode="out-in">
26        <VPIconSun v-if="!isDark" class="sun" />
27        <VPIconMoon v-else class="moon" />
28      </Transition>
29    </ClientOnly>
30  </button>
31</template>
32
33<style lang="scss" scoped>
34.VPSwitchAppearance {
35  display: flex;
36  justify-content: center;
37  align-items: center;
38  width: 36px;
39  height: 36px;
40  color: var(--vp-c-text-2);
41  transition: color 0.5s;
42
43  &:hover {
44    color: var(--vp-c-text-1);
45    transition: color 0.25s;
46  }
47
48  &> :deep(svg) {
49    width: 20px;
50    height: 20px;
51    fill: currentColor;
52  }
53
54  &[data-view-transition='false'] {
55
56    .fade-enter-active,
57    .fade-leave-active {
58      transition: opacity 0.1s ease;
59    }
60
61    .fade-enter-from,
62    .fade-leave-to {
63      opacity: 0;
64    }
65  }
66}
67</style>