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

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

80 lines · 1963 bytes

 1<!--
 2  Copyright (c) 2024 taskylizard
 3
 4  Licensed under the Apache License, Version 2.0 (the "License");
 5  you may not use this file except in compliance with the License.
 6  You may obtain a copy of the License at
 7
 8  http://www.apache.org/licenses/LICENSE-2.0
 9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15-->
16<script setup lang="ts">
17import { useData } from 'vitepress'
18import { inject } from 'vue'
19import VPIconMoon from 'vitepress/dist/client/theme-default/components/icons/VPIconMoon.vue'
20import VPIconSun from 'vitepress/dist/client/theme-default/components/icons/VPIconSun.vue'
21
22const { isDark } = useData()
23
24const toggleAppearance = inject('toggle-appearance', () => {
25  isDark.value = !isDark.value
26})
27</script>
28
29<template>
30  <button
31    type="button"
32    role="switch"
33    title="VPSwitchAppearance"
34    class="VPSwitchAppearance"
35    :aria-checked="isDark"
36    @click="toggleAppearance"
37  >
38    <ClientOnly>
39      <Transition name="fade" mode="out-in">
40        <VPIconSun v-if="!isDark" class="sun" />
41        <VPIconMoon v-else class="moon" />
42      </Transition>
43    </ClientOnly>
44  </button>
45</template>
46
47<style lang="scss" scoped>
48.VPSwitchAppearance {
49  display: flex;
50  justify-content: center;
51  align-items: center;
52  width: 36px;
53  height: 36px;
54  color: var(--vp-c-text-2);
55  transition: color 0.5s;
56
57  &:hover {
58    color: var(--vp-c-text-1);
59    transition: color 0.25s;
60  }
61
62  & > :deep(svg) {
63    width: 20px;
64    height: 20px;
65    fill: currentColor;
66  }
67
68  &[data-view-transition='false'] {
69    .fade-enter-active,
70    .fade-leave-active {
71      transition: opacity 0.1s ease;
72    }
73
74    .fade-enter-from,
75    .fade-leave-to {
76      opacity: 0;
77    }
78  }
79}
80</style>