docs/.vitepress/theme/components/settings/SpotlightHoverBlock.vue
221 lines · 5530 bytes
1<script setup lang="ts">
2import {
3 useElementBounding,
4 useElementByPoint,
5 useElementVisibility,
6 useEventListener,
7 useMouse,
8 useMouseInElement,
9 useStorage
10} from '@vueuse/core'
11import { useRoute } from 'vitepress'
12import { inject, nextTick, onMounted, reactive, ref, watch } from 'vue'
13
14import {
15 InjectionKey,
16 SpotlightStyle,
17 SpotlightStylesStorageKey
18} from '../../constants'
19
20const props = defineProps<{ enabled: boolean }>()
21
22const options = inject(InjectionKey, {})
23
24const shouldRecalculate = ref(false)
25const boxStyles = ref<Record<string, string | number>>({ display: 'none' })
26const vpDocElement = ref<HTMLDivElement>()
27const highlightedElement = ref<HTMLElement>()
28
29const route = useRoute()
30const spotlightStyle = useStorage<SpotlightStyle>(
31 SpotlightStylesStorageKey,
32 options.spotlight?.defaultStyle || SpotlightStyle.Aside
33)
34
35const { x, y } = useMouse({ type: 'client' })
36const { isOutside } = useMouseInElement(vpDocElement)
37const { element } = useElementByPoint({ x, y })
38const bounding = reactive(useElementBounding(element))
39const elementVisibility = useElementVisibility(highlightedElement)
40
41useEventListener('scroll', bounding.update, true)
42
43function computeBoxStyles(bounding: {
44 height: number
45 left: number
46 top: number
47 width: number
48}) {
49 return {
50 display: 'block',
51 width: `${bounding.width + 8}px`,
52 height: `${bounding.height + 8}px`,
53 left: `${bounding.left - 4}px`,
54 top: `${bounding.top - 4}px`,
55 transition: 'all 0.2s ease',
56 borderRadius: '8px'
57 }
58}
59
60function findChildElementUnderVPDocElement(element: HTMLElement | null) {
61 if (element === null) return null
62
63 if (
64 element.parentElement ===
65 document.querySelector('.VPDoc main .vp-doc > div')
66 ) {
67 return element
68 } else return findChildElementUnderVPDocElement(element.parentElement)
69}
70
71function watchHandler() {
72 if (!(element.value && !isOutside.value)) return
73
74 const el = findChildElementUnderVPDocElement(element.value)
75 highlightedElement.value = el || undefined
76
77 if (highlightedElement.value && highlightedElement.value.tagName === 'P') {
78 const val = highlightedElement.value
79 const style = window.getComputedStyle(val)
80 const lineHeight = Number.parseFloat(style.lineHeight)
81 const lines = Math.floor(val.offsetHeight / lineHeight)
82
83 const rect = val.getBoundingClientRect()
84 const relativeY = y.value - rect.top
85
86 for (let i = 0; i < lines; i++) {
87 const top = i * lineHeight
88 const height = lineHeight
89 const left = val.offsetLeft
90 const width = val.offsetWidth
91
92 if (relativeY >= top && relativeY < top + height) {
93 boxStyles.value = computeBoxStyles({
94 top: top + rect.top,
95 left: left + rect.left,
96 width,
97 height
98 })
99
100 break
101 }
102 }
103 } else {
104 if (highlightedElement.value) {
105 const rect = highlightedElement.value.getBoundingClientRect()
106
107 boxStyles.value = computeBoxStyles({
108 top: rect.top,
109 left: rect.left,
110 width: rect.width,
111 height: rect.height
112 })
113 }
114 }
115}
116
117onMounted(() => {
118 if (!document) return
119 if (!document.body) return
120
121 document.body.style.setProperty(
122 '--vp-nolebase-enhanced-readabilities-spotlight-under-bg-color',
123 options?.spotlight?.hoverBlockColor || `rgb(240 197 52 / 10%)`
124 )
125
126 vpDocElement.value = document.querySelector(
127 '.VPDoc main .vp-doc'
128 ) as HTMLDivElement
129})
130
131watch(route, async () => {
132 // Wait for the next tick to ensure the DOM is updated since the route change
133 // may trigger a page reload.
134 await nextTick()
135
136 vpDocElement.value = document.querySelector(
137 '.VPDoc main .vp-doc'
138 ) as HTMLDivElement
139
140 shouldRecalculate.value = true
141 boxStyles.value = { display: 'none' }
142
143 bounding.update()
144
145 watchHandler()
146 shouldRecalculate.value = false
147})
148
149watch([x, y], () => {
150 if (props.enabled) watchHandler()
151})
152
153watch(bounding, (val) => {
154 if (!props.enabled) return
155
156 if (val.width === 0 && val.height === 0) boxStyles.value = { display: 'none' }
157 else watchHandler()
158})
159
160watch(elementVisibility, (val) => {
161 if (props.enabled && !val) boxStyles.value = { display: 'none' }
162})
163
164watch(
165 () => props.enabled,
166 (val) => {
167 if (!val) boxStyles.value = { display: 'none' }
168 }
169)
170</script>
171
172<template>
173 <Teleport to="body">
174 <div
175 v-if="props.enabled && !shouldRecalculate"
176 :style="boxStyles"
177 aria-hidden="true"
178 focusable="false"
179 pointer-events-none
180 fixed
181 class="VPNolebaseEnhancedReadabilitiesSpotlightHoverBlock"
182 :class="[
183 spotlightStyle === SpotlightStyle.Under
184 ? 'VPNolebaseEnhancedReadabilitiesSpotlightHoverBlockUnder'
185 : '',
186 spotlightStyle === SpotlightStyle.Aside
187 ? 'VPNolebaseEnhancedReadabilitiesSpotlightHoverBlockAside'
188 : ''
189 ]"
190 />
191 </Teleport>
192</template>
193
194<style less scoped>
195:root {
196 --vp-nolebase-enhanced-readabilities-spotlight-under-bg-color: rgb(
197 240
198 197
199 52
200 /
201 10%
202 );
203}
204
205.VPNolebaseEnhancedReadabilitiesSpotlightHoverBlock.VPNolebaseEnhancedReadabilitiesSpotlightHoverBlockUnder {
206 background-color: var(
207 --vp-nolebase-enhanced-readabilities-spotlight-under-bg-color
208 );
209}
210
211.VPNolebaseEnhancedReadabilitiesSpotlightHoverBlock.VPNolebaseEnhancedReadabilitiesSpotlightHoverBlockAside::before {
212 content: "";
213 position: absolute;
214 display: block;
215 width: 4px;
216 height: 100%;
217 border-radius: 4px;
218 background-color: var(--vp-c-brand);
219 left: -24px;
220}
221</style>