|
| 1 | +<script setup lang="ts"> |
| 2 | +import SlotDocAfter from '@theme/components/SlotDocAfter.vue' |
| 3 | +import SlotDocFooterBefore from '@theme/components/SlotDocFooterBefore.vue' |
| 4 | +import { ImagePreviewService } from '@theme/directives/image-preview' |
| 5 | +import { getColor } from '@utils/index' |
| 6 | +import { StyleProvider, Themes } from '@varlet/ui' |
| 7 | +import { useEventListener } from '@vueuse/core' |
| 8 | +import { inBrowser, useData } from 'vitepress' |
| 9 | +import DefaultTheme from 'vitepress/theme' |
| 10 | +import { onMounted, provide, watchEffect } from 'vue' |
| 11 | +
|
| 12 | +
|
| 13 | +const { isDark } = useData() |
| 14 | +
|
| 15 | +
|
| 16 | +watchEffect(() => { |
| 17 | + StyleProvider({ |
| 18 | + ...(isDark.value ? Themes.dark : null), |
| 19 | + '--hsl-primary': '226, 55%, 45%', |
| 20 | + }) |
| 21 | +}) |
| 22 | +
|
| 23 | +const spawnParticles = (x: number, y: number, count: number = 50) => { |
| 24 | + for (let i = 0; i < count; i++) { |
| 25 | + const particle = document.createElement('div') |
| 26 | + const length = 4 + Math.random() * 4 |
| 27 | + Object.assign(particle.style, { |
| 28 | + position: 'fixed', |
| 29 | + left: `${ x - length * .5 }px`, |
| 30 | + top: `${ y - length * .5 }px`, |
| 31 | + width: `${ length }px`, |
| 32 | + height: `${ length }px`, |
| 33 | + borderRadius: '50%', |
| 34 | + pointerEvents: 'none', |
| 35 | + background: getColor(), |
| 36 | + zIndex: '99999', |
| 37 | + }) |
| 38 | + document.body.appendChild(particle) |
| 39 | + |
| 40 | + const distance = 80 + Math.random() * 40 |
| 41 | + const angle = Math.random() * Math.PI * 2 |
| 42 | + const destX = Math.cos(angle) * distance |
| 43 | + const destY = Math.sin(angle) * distance |
| 44 | + particle |
| 45 | + .animate( |
| 46 | + [ |
| 47 | + { transform: 'translate(0, 0)', opacity: 1 }, |
| 48 | + { transform: `translate(${ destX }px, ${ destY }px)`, opacity: 0 }, |
| 49 | + ], |
| 50 | + { |
| 51 | + easing: 'ease-out', |
| 52 | + duration: 700 + Math.random() * 300, |
| 53 | + }, |
| 54 | + ) |
| 55 | + .onfinish = () => particle.remove() |
| 56 | + } |
| 57 | +} |
| 58 | +
|
| 59 | +const imagePreviewFn = () => { |
| 60 | + if (!inBrowser) return |
| 61 | + |
| 62 | + const scope = document.querySelector('.VPDoc .main') |
| 63 | + if (!scope) return |
| 64 | + |
| 65 | + const getUrl = (img: HTMLImageElement) => img.getAttribute('src') || '' |
| 66 | + const list = [ ...scope?.querySelectorAll('img') ].map(el => getUrl(el)) |
| 67 | + |
| 68 | + document |
| 69 | + .querySelectorAll<HTMLImageElement>('p > img') |
| 70 | + .forEach((img) => { |
| 71 | + img.setAttribute('style', 'cursor: pointer;') |
| 72 | + useEventListener(img, 'click', () => { |
| 73 | + ImagePreviewService.open({ |
| 74 | + url: getUrl(img), |
| 75 | + previewUrlList: list, |
| 76 | + }) |
| 77 | + }) |
| 78 | + }) |
| 79 | +} |
| 80 | +
|
| 81 | +const doThemeTransition = async (location: { x: number; y: number }, toggleFn: () => void, isDark: boolean) => { |
| 82 | + const style = document.createElement('style') |
| 83 | + style.innerHTML = ` |
| 84 | + ::view-transition-old(root), |
| 85 | + ::view-transition-new(root) { |
| 86 | + animation: none; |
| 87 | + mix-blend-mode: normal; |
| 88 | + } |
| 89 | + |
| 90 | + ::view-transition-old(root), |
| 91 | + .dark::view-transition-new(root) { |
| 92 | + z-index: 1; |
| 93 | + } |
| 94 | + |
| 95 | + ::view-transition-new(root), |
| 96 | + .dark::view-transition-old(root) { |
| 97 | + z-index: 9999; |
| 98 | + } |
| 99 | + ` |
| 100 | + document.head.appendChild(style) |
| 101 | + |
| 102 | + const enableTransitions = () => 'startViewTransition' in document && |
| 103 | + window.matchMedia('(prefers-reduced-motion: no-preference)').matches |
| 104 | + |
| 105 | + if (!enableTransitions()) { |
| 106 | + toggleFn() |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + const transition = document.startViewTransition(async () => { |
| 111 | + toggleFn() |
| 112 | + await Promise.resolve() |
| 113 | + }) |
| 114 | + await transition.ready |
| 115 | + |
| 116 | + const { x, y } = location |
| 117 | + const maxRadius = Math.hypot( |
| 118 | + Math.max(x, innerWidth - x), |
| 119 | + Math.max(y, innerHeight - y), |
| 120 | + ) |
| 121 | + const clipPath = [ |
| 122 | + `circle(0px at ${ x }px ${ y }px)`, |
| 123 | + `circle(${ maxRadius }px at ${ x }px ${ y }px)`, |
| 124 | + ] |
| 125 | + document.documentElement.animate( |
| 126 | + { |
| 127 | + clipPath: isDark ? clipPath.reverse() : clipPath, |
| 128 | + }, |
| 129 | + { |
| 130 | + duration: 300, |
| 131 | + easing: 'ease-in', |
| 132 | + fill: 'forwards', |
| 133 | + pseudoElement: `::view-transition-${ isDark ? 'old' : 'new' }(root)`, |
| 134 | + }, |
| 135 | + ) |
| 136 | +} |
| 137 | +
|
| 138 | +
|
| 139 | +provide('toggle-appearance', async ({ clientX: x, clientY: y }: MouseEvent) => { |
| 140 | + await doThemeTransition( |
| 141 | + { x, y }, |
| 142 | + () => { |
| 143 | + isDark.value = !isDark.value |
| 144 | + }, |
| 145 | + !isDark.value, |
| 146 | + ) |
| 147 | +}) |
| 148 | +
|
| 149 | +useEventListener('click', (e) => { |
| 150 | + spawnParticles(e.clientX, e.clientY) |
| 151 | +}) |
| 152 | +
|
| 153 | +onMounted(() => { |
| 154 | + imagePreviewFn() |
| 155 | +}) |
| 156 | +</script> |
| 157 | + |
| 158 | +<template> |
| 159 | + <DefaultTheme.Layout> |
| 160 | + <template #doc-after> |
| 161 | + <SlotDocAfter /> |
| 162 | + </template> |
| 163 | + |
| 164 | + <template #doc-footer-before> |
| 165 | + <SlotDocFooterBefore /> |
| 166 | + </template> |
| 167 | + |
| 168 | + <template #layout-bottom></template> |
| 169 | + </DefaultTheme.Layout> |
| 170 | +</template> |
| 171 | + |
| 172 | +<style> |
| 173 | +.VPSwitchAppearance { |
| 174 | + width: 22px !important; |
| 175 | + |
| 176 | + & .check { |
| 177 | + top: calc(3px / 2); |
| 178 | + left: calc(3px / 2); |
| 179 | + transform: none !important; |
| 180 | + } |
| 181 | +} |
| 182 | +</style> |
0 commit comments