This commit is contained in:
maropboia 2024-05-03 11:38:35 +06:00
parent cd213aa100
commit 551ebcf5b3

View file

@ -1,22 +1,28 @@
<script setup lang="ts"> <script setup lang="ts">
// Import necessary components and functions from VitePress, Vue, and the local project.
import DefaultTheme from 'vitepress/theme' import DefaultTheme from 'vitepress/theme'
import { useData } from 'vitepress' import { useData } from 'vitepress'
import { nextTick, provide } from 'vue' import { nextTick, provide } from 'vue'
import Sidebar from './components/SidebarCard.vue' import Sidebar from './components/SidebarCard.vue'
import Announcement from './components/Announcement.vue' import Announcement from './components/Announcement.vue'
// Get the isDark value from the VitePress data object.
const { isDark } = useData() const { isDark } = useData()
// Define a function to check if the browser supports transitions and reduced motion preferences.
const enableTransitions = () => const enableTransitions = () =>
'startViewTransition' in document && 'startViewTransition' in document &&
window.matchMedia('(prefers-reduced-motion: no-preference)').matches window.matchMedia('(prefers-reduced-motion: no-preference)').matches
// Provide a method for toggling the appearance (dark/light mode) with an optional MouseEvent parameter.
provide('toggle-appearance', async ({ clientX: x, clientY: y }: MouseEvent) => { provide('toggle-appearance', async ({ clientX: x, clientY: y }: MouseEvent) => {
if (!enableTransitions()) { if (!enableTransitions()) {
// If transitions are not supported, simply toggle the isDark value.
isDark.value = !isDark.value isDark.value = !isDark.value
return return
} }
// Calculate the clip path for the transition animation.
const clipPath = [ const clipPath = [
`circle(0px at ${x}px ${y}px)`, `circle(0px at ${x}px ${y}px)`,
`circle(${Math.hypot( `circle(${Math.hypot(
@ -25,12 +31,14 @@ provide('toggle-appearance', async ({ clientX: x, clientY: y }: MouseEvent) => {
)}px at ${x}px ${y}px)` )}px at ${x}px ${y}px)`
] ]
// @ts-expect-error // Use the startViewTransition function to create a smooth transition effect.
// @ts-expect-error: TypeScript error due to the use of a non-standard function.
await document.startViewTransition(async () => { await document.startViewTransition(async () => {
isDark.value = !isDark.value isDark.value = !isDark.value
await nextTick() await nextTick()
}).ready }).ready
// Apply an animation to the document element for the transition.
document.documentElement.animate( document.documentElement.animate(
{ clipPath: isDark.value ? clipPath.reverse() : clipPath }, { clipPath: isDark.value ? clipPath.reverse() : clipPath },
{ {
@ -41,22 +49,28 @@ provide('toggle-appearance', async ({ clientX: x, clientY: y }: MouseEvent) => {
) )
}) })
// Destructure the Layout component from the DefaultTheme.
const { Layout } = DefaultTheme const { Layout } = DefaultTheme
</script> </script>
<template> <template>
<!-- Use the Layout component as the base for the page. -->
<Layout> <Layout>
<!-- Add the Sidebar component after the default sidebar navigation. -->
<template #sidebar-nav-after> <template #sidebar-nav-after>
<Sidebar /> <Sidebar />
</template> </template>
<!-- Add the Announcement component before the page content on the home page. -->
<template #home-hero-prelink> <template #home-hero-prelink>
<Announcement /> <Announcement />
</template> </template>
<!-- Display the page content. -->
<Content /> <Content />
</Layout> </Layout>
</template> </template>
<style> <style>
// Style the view-transition pseudo-elements for smooth transitions between dark and light modes.
::view-transition-old(root), ::view-transition-old(root),
::view-transition-new(root) { ::view-transition-new(root) {
animation: none; animation: none;
@ -73,10 +87,12 @@ const { Layout } = DefaultTheme
z-index: 9999; z-index: 9999;
} }
// Style the VPSwitchAppearance component.
.VPSwitchAppearance { .VPSwitchAppearance {
width: 22px !important; width: 22px !important;
} }
// Style the check icon within the VPSwitchAppearance component.
.VPSwitchAppearance .check { .VPSwitchAppearance .check {
transform: none !important; transform: none !important;
} }