Commit d8de6843ec
Unsigned
Layout: unified · split
docs/.vitepress/theme/components/BookmarksPanel.vue +54 −6
| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <script setup lang="ts"> |
| 2 | import { onClickOutside } from '@vueuse/core' | |
| 2 | import { onClickOutside, useEventListener } from '@vueuse/core' | |
| 3 | 3 | import { Bookmark, Hash, X } from 'lucide-vue-next' |
| 4 | import { computed, ref } from 'vue' | |
| 4 | import { inBrowser } from 'vitepress' | |
| 5 | 5 | import type { DefaultTheme } from 'vitepress/theme' |
| 6 | import { computed, nextTick, ref, watch } from 'vue' | |
| 6 | 7 | import type { HomeCard } from '../../configs/constants' |
| 7 | 8 | import { homeCards, sidebar } from '../../configs/constants' |
| 8 | 9 | import type { Bookmark as BookmarkItem } from '../composables/useBookmarks' |
| @@ -14,8 +15,11 @@ const ICON_STROKE = 2 | ||
| 14 | 15 | const { bookmarks, remove } = useBookmarks() |
| 15 | 16 | const isOpen = ref(false) |
| 16 | 17 | const wrapperRef = ref<HTMLElement>() |
| 18 | const buttonRef = ref<HTMLElement>() | |
| 19 | const panelRef = ref<HTMLElement>() | |
| 17 | 20 | const scrollRef = ref<HTMLElement>() |
| 18 | 21 | const showFade = ref(true) |
| 22 | const panelStyle = ref<Record<string, string>>({}) | |
| 19 | 23 | |
| 20 | 24 | onClickOutside(wrapperRef, () => { |
| 21 | 25 | isOpen.value = false |
| @@ -27,6 +31,45 @@ function onScroll() { | ||
| 27 | 31 | showFade.value = scrollTop + clientHeight < scrollHeight - 4 |
| 28 | 32 | } |
| 29 | 33 | |
| 34 | function updatePanelPosition() { | |
| 35 | const wrapper = wrapperRef.value | |
| 36 | const button = buttonRef.value | |
| 37 | const panel = panelRef.value | |
| 38 | if (!wrapper || !button || !panel) return | |
| 39 | ||
| 40 | if (!inBrowser || window.innerWidth <= 767) { | |
| 41 | panelStyle.value = {} | |
| 42 | return | |
| 43 | } | |
| 44 | ||
| 45 | const viewportMargin = 12 | |
| 46 | const wrapperRect = wrapper.getBoundingClientRect() | |
| 47 | const buttonRect = button.getBoundingClientRect() | |
| 48 | const panelWidth = panel.offsetWidth | |
| 49 | const centeredLeft = buttonRect.left + buttonRect.width / 2 - panelWidth / 2 | |
| 50 | const maxLeft = Math.max( | |
| 51 | viewportMargin, | |
| 52 | window.innerWidth - viewportMargin - panelWidth | |
| 53 | ) | |
| 54 | const viewportLeft = Math.min(Math.max(centeredLeft, viewportMargin), maxLeft) | |
| 55 | ||
| 56 | panelStyle.value = { | |
| 57 | '--bookmarks-panel-left': `${viewportLeft - wrapperRect.left}px`, | |
| 58 | '--bookmarks-panel-shift': '0px' | |
| 59 | } | |
| 60 | } | |
| 61 | ||
| 62 | watch(isOpen, async (open) => { | |
| 63 | if (!open) return | |
| 64 | await nextTick() | |
| 65 | updatePanelPosition() | |
| 66 | onScroll() | |
| 67 | }) | |
| 68 | ||
| 69 | useEventListener(inBrowser ? window : undefined, 'resize', () => { | |
| 70 | if (isOpen.value) updatePanelPosition() | |
| 71 | }) | |
| 72 | ||
| 30 | 73 | function flattenSidebarPaths(items: DefaultTheme.SidebarItem[]): string[] { |
| 31 | 74 | const paths: string[] = [] |
| 32 | 75 | for (const item of items) { |
| @@ -124,6 +167,7 @@ const grouped = computed((): GroupEntry[] => { | ||
| 124 | 167 | <template> |
| 125 | 168 | <div ref="wrapperRef" class="bookmarks-wrapper"> |
| 126 | 169 | <button |
| 170 | ref="buttonRef" | |
| 127 | 171 | type="button" |
| 128 | 172 | class="bookmarks-btn" |
| 129 | 173 | aria-label="Bookmarks" |
| @@ -134,7 +178,7 @@ const grouped = computed((): GroupEntry[] => { | ||
| 134 | 178 | </button> |
| 135 | 179 | |
| 136 | 180 | <Transition name="bookmarks-panel"> |
| 137 | <div v-if="isOpen" class="bookmarks-panel" role="dialog" aria-label="Bookmarks"> | |
| 181 | <div v-if="isOpen" ref="panelRef" class="bookmarks-panel" :style="panelStyle" role="dialog" aria-label="Bookmarks"> | |
| 138 | 182 | <div class="bookmarks-mobile-header"> |
| 139 | 183 | <span class="bookmarks-mobile-title">Bookmarks</span> |
| 140 | 184 | <button type="button" class="bookmarks-mobile-close" aria-label="Close" @click="isOpen = false"> |
| @@ -237,15 +281,17 @@ const grouped = computed((): GroupEntry[] => { | ||
| 237 | 281 | .bookmarks-panel { |
| 238 | 282 | position: absolute; |
| 239 | 283 | top: calc(var(--vp-nav-height) / 2 + 20px); |
| 240 | right: 0; | |
| 284 | left: var(--bookmarks-panel-left, 50%); | |
| 241 | 285 | z-index: 100; |
| 242 | 286 | width: 282px; |
| 287 | max-width: calc(100vw - 24px); | |
| 243 | 288 | background-color: var(--vp-c-bg-elv); |
| 244 | 289 | border: 1px solid var(--vp-c-divider); |
| 245 | 290 | border-radius: 12px; |
| 246 | 291 | box-shadow: var(--vp-shadow-3); |
| 247 | 292 | transition: background-color 0.5s; |
| 248 | transform-origin: top right; | |
| 293 | transform: translateX(var(--bookmarks-panel-shift, -50%)); | |
| 294 | transform-origin: top center; | |
| 249 | 295 | overflow: hidden; |
| 250 | 296 | } |
| 251 | 297 | |
| @@ -434,7 +480,7 @@ const grouped = computed((): GroupEntry[] => { | ||
| 434 | 480 | .bookmarks-panel-enter-from, |
| 435 | 481 | .bookmarks-panel-leave-to { |
| 436 | 482 | opacity: 0; |
| 437 | transform: scale(0.95) translateY(-4px); | |
| 483 | transform: translateX(var(--bookmarks-panel-shift, -50%)) scale(0.95) translateY(-4px); | |
| 438 | 484 | } |
| 439 | 485 | |
| 440 | 486 | .bookmarks-fade-enter-active, |
| @@ -469,10 +515,12 @@ const grouped = computed((): GroupEntry[] => { | ||
| 469 | 515 | right: 0; |
| 470 | 516 | bottom: 0; |
| 471 | 517 | width: 100%; |
| 518 | max-width: none; | |
| 472 | 519 | height: 100%; |
| 473 | 520 | border-radius: 0; |
| 474 | 521 | border: none; |
| 475 | 522 | z-index: 9999; |
| 523 | transform: none; | |
| 476 | 524 | transform-origin: center top; |
| 477 | 525 | display: flex; |
| 478 | 526 | flex-direction: column; |
docs/.vitepress/theme/components/settings/SettingsFlyout.vue +45 −5
| @@ -1,11 +1,35 @@ | ||
| 1 | 1 | <script setup lang="ts"> |
| 2 | import { onMounted, onUnmounted, ref } from 'vue' | |
| 2 | import { nextTick, onMounted, onUnmounted, ref } from 'vue' | |
| 3 | 3 | |
| 4 | 4 | const open = ref(false) |
| 5 | 5 | const el = ref<HTMLElement>() |
| 6 | const button = ref<HTMLElement>() | |
| 7 | const menu = ref<HTMLElement>() | |
| 8 | const menuStyle = ref<Record<string, string>>({}) | |
| 6 | 9 | |
| 7 | 10 | function toggle() { |
| 8 | 11 | open.value = !open.value |
| 12 | if (open.value) void nextTick(updateMenuPosition) | |
| 13 | } | |
| 14 | ||
| 15 | function updateMenuPosition() { | |
| 16 | if (!el.value || !button.value || !menu.value) return | |
| 17 | ||
| 18 | const viewportMargin = 12 | |
| 19 | const elRect = el.value.getBoundingClientRect() | |
| 20 | const buttonRect = button.value.getBoundingClientRect() | |
| 21 | const menuWidth = menu.value.offsetWidth | |
| 22 | const centeredLeft = buttonRect.left + buttonRect.width / 2 - menuWidth / 2 | |
| 23 | const maxLeft = Math.max( | |
| 24 | viewportMargin, | |
| 25 | window.innerWidth - viewportMargin - menuWidth | |
| 26 | ) | |
| 27 | const viewportLeft = Math.min(Math.max(centeredLeft, viewportMargin), maxLeft) | |
| 28 | ||
| 29 | menuStyle.value = { | |
| 30 | '--settings-menu-left': `${viewportLeft - elRect.left}px`, | |
| 31 | '--settings-menu-shift': '0px' | |
| 32 | } | |
| 9 | 33 | } |
| 10 | 34 | |
| 11 | 35 | function onDocClick(e: MouseEvent) { |
| @@ -14,13 +38,25 @@ function onDocClick(e: MouseEvent) { | ||
| 14 | 38 | } |
| 15 | 39 | } |
| 16 | 40 | |
| 17 | onMounted(() => document.addEventListener('click', onDocClick, true)) | |
| 18 | onUnmounted(() => document.removeEventListener('click', onDocClick, true)) | |
| 41 | function onResize() { | |
| 42 | if (open.value) updateMenuPosition() | |
| 43 | } | |
| 44 | ||
| 45 | onMounted(() => { | |
| 46 | document.addEventListener('click', onDocClick, true) | |
| 47 | window.addEventListener('resize', onResize) | |
| 48 | }) | |
| 49 | ||
| 50 | onUnmounted(() => { | |
| 51 | document.removeEventListener('click', onDocClick, true) | |
| 52 | window.removeEventListener('resize', onResize) | |
| 53 | }) | |
| 19 | 54 | </script> |
| 20 | 55 | |
| 21 | 56 | <template> |
| 22 | 57 | <div ref="el" class="VPFlyout"> |
| 23 | 58 | <button |
| 59 | ref="button" | |
| 24 | 60 | type="button" |
| 25 | 61 | class="button" |
| 26 | 62 | aria-haspopup="true" |
| @@ -33,7 +69,7 @@ onUnmounted(() => document.removeEventListener('click', onDocClick, true)) | ||
| 33 | 69 | </span> |
| 34 | 70 | </button> |
| 35 | 71 | |
| 36 | <div v-show="open" class="menu"> | |
| 72 | <div v-show="open" ref="menu" class="menu" :style="menuStyle"> | |
| 37 | 73 | <div class="VPMenu"> |
| 38 | 74 | <slot /> |
| 39 | 75 | </div> |
| @@ -68,14 +104,18 @@ onUnmounted(() => document.removeEventListener('click', onDocClick, true)) | ||
| 68 | 104 | .menu { |
| 69 | 105 | position: absolute; |
| 70 | 106 | top: calc(var(--vp-nav-height) / 2 + 20px); |
| 71 | right: 0; | |
| 107 | left: var(--settings-menu-left, 50%); | |
| 72 | 108 | z-index: 100; |
| 109 | max-width: calc(100vw - 24px); | |
| 110 | transform: translateX(var(--settings-menu-shift, -50%)); | |
| 111 | transform-origin: top center; | |
| 73 | 112 | } |
| 74 | 113 | |
| 75 | 114 | .VPMenu { |
| 76 | 115 | border-radius: 12px; |
| 77 | 116 | padding: 12px; |
| 78 | 117 | min-width: 128px; |
| 118 | max-width: 100%; | |
| 79 | 119 | border: 1px solid var(--vp-c-divider); |
| 80 | 120 | background-color: var(--vp-c-bg-elv); |
| 81 | 121 | box-shadow: var(--vp-shadow-3); |