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