docs/.vitepress/theme/components/AnnouncementsPanel.vue
817 lines · 19145 bytes
1<script setup lang="ts">
2import { onClickOutside, useEventListener, useMounted } from '@vueuse/core'
3import { ArrowLeft, CheckCheck, ChevronRight, Megaphone } from 'lucide-vue-next'
4import { inBrowser, useRouter } from 'vitepress'
5import { computed, nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
6import type { Announcement, AnnouncementType } from '../composables/useAnnouncements'
7import { useAnnouncements } from '../composables/useAnnouncements'
8import AnnouncementTypeSelect from './AnnouncementTypeSelect.vue'
9
10const ICON_SIZE = 18
11const ICON_STROKE = 2
12
13const {
14 announcements,
15 enabled,
16 isRead,
17 markRead,
18 markAllRead,
19 hasUnread,
20 unreadCount
21} = useAnnouncements()
22
23const router = useRouter()
24const mounted = useMounted()
25
26const TYPE_META: Record<AnnouncementType, { label: string; icon: string }> = {
27 section: { label: 'Section', icon: 'i-lucide:panel-top' },
28 component: { label: 'Component', icon: 'i-lucide:box' },
29 improvement: { label: 'Improvement', icon: 'i-lucide:sparkles' }
30}
31
32/* Panel open/close & positioning */
33const isOpen = ref(false)
34const wrapperRef = ref<HTMLElement>()
35const buttonRef = ref<HTMLElement>()
36const panelRef = ref<HTMLElement>()
37const scrollRef = ref<HTMLElement>()
38const showFade = ref(true)
39const panelStyle = ref<Record<string, string>>({})
40
41/* Type filter (gear menu) */
42const typeFilter = ref('')
43const filterOpen = ref(false)
44const filterBtnRef = ref<HTMLElement>()
45const filterMenuRef = ref<HTMLElement>()
46const filterStyle = ref<Record<string, string>>({})
47
48/* Detail popup */
49const activePost = ref<Announcement | null>(null)
50const backdropRef = ref<HTMLElement>()
51
52onClickOutside(
53 wrapperRef,
54 () => {
55 // keep panel open while the filter menu or detail popup is up
56 if (!filterOpen.value && !activePost.value) isOpen.value = false
57 },
58 {
59 ignore: [filterMenuRef, backdropRef]
60 }
61)
62
63function onScroll() {
64 if (!scrollRef.value) return
65 const { scrollTop, scrollHeight, clientHeight } = scrollRef.value
66 showFade.value = scrollTop + clientHeight < scrollHeight - 4
67}
68
69function updatePanelPosition() {
70 const wrapper = wrapperRef.value
71 const button = buttonRef.value
72 const panel = panelRef.value
73 if (!wrapper || !button || !panel) return
74
75 if (!inBrowser || window.innerWidth <= 767) {
76 panelStyle.value = {}
77 return
78 }
79
80 const viewportMargin = 12
81 const wrapperRect = wrapper.getBoundingClientRect()
82 const buttonRect = button.getBoundingClientRect()
83 const panelWidth = panel.offsetWidth
84 const centeredLeft = buttonRect.left + buttonRect.width / 2 - panelWidth / 2
85 const maxLeft = Math.max(
86 viewportMargin,
87 window.innerWidth - viewportMargin - panelWidth
88 )
89 const viewportLeft = Math.min(Math.max(centeredLeft, viewportMargin), maxLeft)
90
91 panelStyle.value = {
92 '--ann-panel-left': `${viewportLeft - wrapperRect.left}px`
93 }
94}
95
96watch(isOpen, async (open) => {
97 if (!open) {
98 filterOpen.value = false
99 return
100 }
101 await nextTick()
102 updatePanelPosition()
103 onScroll()
104})
105
106let resizeTick = false
107useEventListener(inBrowser ? window : undefined, 'resize', () => {
108 if (!resizeTick) {
109 window.requestAnimationFrame(() => {
110 if (isOpen.value) updatePanelPosition()
111 if (filterOpen.value) updateFilterPosition()
112 resizeTick = false
113 })
114 resizeTick = true
115 }
116})
117
118function updateFilterPosition() {
119 const btn = filterBtnRef.value
120 if (!btn) return
121 const viewportMargin = 12
122 const menuWidth = filterMenuRef.value?.offsetWidth ?? 200
123 const rect = btn.getBoundingClientRect()
124 const maxLeft = Math.max(viewportMargin, window.innerWidth - viewportMargin - menuWidth)
125 const left = Math.min(Math.max(rect.right - menuWidth, viewportMargin), maxLeft)
126 filterStyle.value = { top: `${rect.bottom + 6}px`, left: `${left}px` }
127}
128
129function toggleFilter() {
130 filterOpen.value = !filterOpen.value
131 if (filterOpen.value) void nextTick(updateFilterPosition)
132}
133
134function onFilterDocClick(e: MouseEvent) {
135 const target = e.target as Node
136 if (
137 filterBtnRef.value && !filterBtnRef.value.contains(target)
138 && filterMenuRef.value && !filterMenuRef.value.contains(target)
139 ) {
140 filterOpen.value = false
141 }
142}
143
144const filtered = computed(() =>
145 typeFilter.value
146 ? announcements.filter((a) => a.type === typeFilter.value)
147 : announcements
148)
149
150/* Detail popup */
151
152function openPost(post: Announcement) {
153 markRead(post.id)
154 if (post.type === 'section') {
155 isOpen.value = false
156 if (post.link) router.go(post.link)
157 return
158 }
159 activePost.value = post
160}
161
162function closePost() {
163 activePost.value = null
164}
165
166function onMarkAllRead() {
167 markAllRead()
168}
169
170watch(activePost, (post) => {
171 if (typeof document !== 'undefined') {
172 document.body.style.overflow = post ? 'hidden' : ''
173 }
174})
175
176function onKeydown(e: KeyboardEvent) {
177 if (e.key !== 'Escape') return
178 if (activePost.value) closePost()
179 else if (filterOpen.value) filterOpen.value = false
180 else if (isOpen.value) isOpen.value = false
181}
182
183useEventListener(inBrowser ? document : undefined, 'click', onFilterDocClick, { capture: true })
184useEventListener(inBrowser ? document : undefined, 'keydown', onKeydown)
185
186onUnmounted(() => {
187 if (typeof document !== 'undefined') document.body.style.overflow = ''
188})
189</script>
190
191<template>
192 <div v-if="enabled" ref="wrapperRef" class="ann-wrapper">
193 <button
194 ref="buttonRef"
195 type="button"
196 class="ann-btn"
197 aria-label="Announcements"
198 :aria-expanded="isOpen"
199 @click="isOpen = !isOpen"
200 >
201 <Megaphone :size="ICON_SIZE" :stroke-width="ICON_STROKE" />
202 <span v-if="mounted && hasUnread" class="ann-dot" aria-hidden="true" />
203 </button>
204
205 <Transition name="ann-panel">
206 <div
207 v-if="isOpen"
208 ref="panelRef"
209 class="ann-panel"
210 :style="panelStyle"
211 role="dialog"
212 aria-label="Announcements"
213 >
214 <div class="ann-header">
215 <button type="button" class="ann-mobile-back" aria-label="Back" @click="isOpen = false">
216 <ArrowLeft :size="18" :stroke-width="2" />
217 </button>
218 <span class="ann-header-title">Announcements</span>
219 <div class="ann-header-actions">
220 <button
221 type="button"
222 class="ann-header-btn"
223 aria-label="Mark all as read"
224 :disabled="unreadCount === 0"
225 @click="onMarkAllRead"
226 >
227 <CheckCheck :size="16" :stroke-width="2" />
228 </button>
229 <button
230 type="button"
231 class="ann-settings-btn"
232 :class="{ 'is-active': filterOpen }"
233 aria-label="Filter by type"
234 :aria-expanded="filterOpen"
235 ref="filterBtnRef"
236 @click="toggleFilter"
237 >
238 <span class="i-lucide:settings" />
239 </button>
240 </div>
241 </div>
242
243 <div ref="scrollRef" class="ann-scroll" @scroll.passive="onScroll">
244 <div v-if="filtered.length === 0" class="ann-empty">
245 <span class="ann-empty-kaomoji">(´・ω・`)</span>
246 <p class="ann-empty-hint">No announcements to show.</p>
247 </div>
248 <ul v-else class="ann-list">
249 <li v-for="post in filtered" :key="post.id" class="ann-item">
250 <button
251 type="button"
252 class="ann-item-btn"
253 :class="{ 'is-unread': !isRead(post.id) }"
254 :aria-label="`${TYPE_META[post.type].label}: ${post.title}`"
255 @click="openPost(post)"
256 >
257 <span class="ann-item-typeicon" :class="TYPE_META[post.type].icon" />
258 <span class="ann-item-title">{{ post.title }}</span>
259 <ChevronRight :size="15" :stroke-width="2" class="ann-item-chevron" />
260 </button>
261 </li>
262 </ul>
263 </div>
264
265 <Transition name="ann-fade">
266 <div v-if="showFade && filtered.length > 0" class="ann-fade-overlay" />
267 </Transition>
268 </div>
269 </Transition>
270
271 <!-- Type filter menu -->
272 <Teleport to="body">
273 <div
274 v-show="filterOpen"
275 ref="filterMenuRef"
276 class="ann-filter-menu VPMenu"
277 :style="filterStyle"
278 role="menu"
279 aria-label="Filter announcements by type"
280 >
281 <AnnouncementTypeSelect v-model="typeFilter" />
282 </div>
283 </Teleport>
284
285 <!-- detail popup -->
286 <Teleport to="body">
287 <Transition name="ann-detail-fade">
288 <div v-if="activePost" ref="backdropRef" class="ann-backdrop" @click.self="closePost">
289 <Transition name="ann-detail-slide" appear>
290 <div v-if="activePost" class="ann-detail" role="dialog" aria-modal="true" :aria-label="activePost.title">
291 <div class="ann-detail-head">
292 <button class="ann-detail-back" aria-label="Back" @click="closePost">
293 <ArrowLeft :size="18" :stroke-width="2" />
294 </button>
295 <span class="ann-detail-title">{{ activePost.title }}</span>
296 <button class="ann-detail-close" aria-label="Close" @click="closePost">
297 <span class="i-lucide:x" />
298 </button>
299 </div>
300 <div class="ann-detail-body">
301 <div class="ann-detail-content vp-doc" v-html="activePost.html" />
302 </div>
303 </div>
304 </Transition>
305 </div>
306 </Transition>
307 </Teleport>
308 </div>
309</template>
310
311<style scoped>
312.ann-wrapper {
313 position: relative;
314 width: 100%;
315 height: 100%;
316 display: flex;
317 align-items: center;
318 justify-content: center;
319}
320
321.ann-btn {
322 position: relative;
323 display: flex;
324 align-items: center;
325 justify-content: center;
326 width: 100%;
327 height: 100%;
328 color: var(--vp-c-text-1);
329 opacity: 0.55;
330 transition:
331 opacity 0.25s,
332 transform 0.2s cubic-bezier(0.16, 1, 0.3, 1);
333}
334
335.ann-btn:hover { opacity: 1; }
336.ann-btn:active { transform: scale(0.94); }
337
338/* unread dot, brand colour */
339.ann-dot {
340 position: absolute;
341 top: calc(50% - 11px);
342 right: calc(50% - 11px);
343 width: 7px;
344 height: 7px;
345 border-radius: 50%;
346 background: var(--vp-c-brand-1);
347 box-shadow: 0 0 0 2px var(--nav-pill-bg, var(--vp-c-bg));
348}
349
350.ann-panel {
351 position: absolute;
352 top: calc(var(--vp-nav-height) / 2 + 20px);
353 left: var(--ann-panel-left, 50%);
354 z-index: 100;
355 width: 320px;
356 max-width: calc(100vw - 24px);
357 --ann-panel-bg: var(--vp-c-bg-elv);
358 background-color: var(--ann-panel-bg);
359 border: 1px solid var(--vp-c-divider);
360 border-radius: 12px;
361 box-shadow: var(--vp-shadow-3);
362 transition: background-color 0.5s;
363 transform: translateX(-50%);
364 transform-origin: top center;
365 overflow: hidden;
366 display: flex;
367 flex-direction: column;
368}
369
370.ann-panel[style*="--ann-panel-left"] {
371 transform: none;
372}
373
374.ann-header {
375 display: flex;
376 align-items: center;
377 gap: 8px;
378 flex-shrink: 0;
379 padding: 12px 12px 8px 16px;
380}
381
382.ann-header-title {
383 flex: 1;
384 font-size: 15px;
385 font-weight: 600;
386 color: var(--vp-c-text-1);
387 min-width: 0;
388 overflow: hidden;
389 white-space: nowrap;
390 text-overflow: ellipsis;
391}
392
393.ann-header-actions {
394 display: flex;
395 align-items: center;
396 gap: 2px;
397 flex-shrink: 0;
398}
399
400/* header buttons, extrepo gear style */
401.ann-header-btn,
402.ann-settings-btn {
403 display: flex;
404 align-items: center;
405 justify-content: center;
406 width: 30px;
407 height: 30px;
408 padding: 0;
409 border: none;
410 border-radius: 0;
411 background: transparent;
412 color: var(--vp-c-text-1);
413 opacity: 0.55;
414 cursor: pointer;
415 transition: opacity 0.25s, transform 0.2s cubic-bezier(0.16, 1, 0.3, 1);
416}
417
418.ann-settings-btn span {
419 font-size: 15px;
420}
421
422.ann-header-btn:hover,
423.ann-settings-btn:hover,
424.ann-settings-btn.is-active {
425 color: var(--vp-c-text-1);
426 opacity: 1;
427 background: transparent;
428}
429
430.ann-header-btn:active,
431.ann-settings-btn:active {
432 transform: scale(0.94);
433}
434
435.ann-header-btn:disabled {
436 opacity: 0.25;
437 cursor: default;
438}
439
440.ann-header-btn:disabled:active {
441 transform: none;
442}
443
444html.effects-disabled .ann-settings-btn,
445html.effects-disabled .ann-header-btn {
446 transition: none;
447}
448
449html.effects-disabled .ann-settings-btn:active,
450html.effects-disabled .ann-header-btn:active {
451 transform: none;
452}
453
454.ann-mobile-back {
455 display: none;
456 align-items: center;
457 justify-content: center;
458 padding: 8px;
459 border: none;
460 background: transparent;
461 color: var(--vp-c-text-1);
462 cursor: pointer;
463 transition: color 0.15s;
464}
465
466.ann-mobile-back:hover {
467 color: var(--vp-c-brand-1);
468}
469
470.ann-scroll {
471 height: 400px;
472 overflow-y: auto;
473 overflow-x: hidden;
474 overscroll-behavior: contain;
475 padding: 0 8px 12px;
476 scrollbar-width: none;
477 -ms-overflow-style: none;
478}
479
480.ann-scroll::-webkit-scrollbar {
481 display: none;
482}
483
484.ann-fade-overlay {
485 position: absolute;
486 bottom: 0;
487 left: 0;
488 right: 0;
489 height: 48px;
490 pointer-events: none;
491 background: linear-gradient(to bottom, transparent, var(--ann-panel-bg));
492 border-radius: 0 0 12px 12px;
493}
494
495.ann-empty {
496 display: flex;
497 flex-direction: column;
498 align-items: center;
499 justify-content: center;
500 min-height: 340px;
501 gap: 10px;
502 text-align: center;
503}
504
505.ann-empty-kaomoji {
506 font-size: 26px;
507 color: var(--vp-c-text-3);
508 line-height: 1;
509 user-select: none;
510}
511
512.ann-empty-hint {
513 font-size: 13px;
514 color: var(--vp-c-text-2);
515 margin: 0;
516}
517
518.ann-list {
519 list-style: none;
520 padding: 0;
521 margin: 0;
522 display: flex;
523 flex-direction: column;
524 gap: 2px;
525}
526
527.ann-item-btn {
528 display: flex;
529 align-items: center;
530 gap: 10px;
531 width: 100%;
532 padding: 9px 8px;
533 border: none;
534 background: transparent;
535 border-radius: 8px;
536 cursor: pointer;
537 text-align: left;
538 transition: background 0.15s, opacity 0.15s;
539}
540
541.ann-item-btn:hover {
542 background: var(--vp-c-default-soft);
543}
544
545.ann-item-typeicon {
546 flex-shrink: 0;
547 font-size: 18px;
548 color: var(--vp-c-text-2);
549}
550
551.ann-item-title {
552 flex: 1;
553 font-size: 13.5px;
554 font-weight: 500;
555 color: var(--vp-c-text-1);
556 overflow: hidden;
557 white-space: nowrap;
558 text-overflow: ellipsis;
559 min-width: 0;
560}
561
562/* dim read items */
563.ann-item-btn:not(.is-unread) {
564 opacity: 0.5;
565}
566
567.ann-item-btn:not(.is-unread):hover {
568 opacity: 1;
569}
570
571.ann-item-btn:not(.is-unread) .ann-item-title {
572 font-weight: 400;
573}
574
575.ann-item-chevron {
576 flex-shrink: 0;
577 color: var(--vp-c-text-3);
578}
579
580/* Filter menu */
581.ann-filter-menu {
582 position: fixed;
583 z-index: 120;
584 width: 260px;
585 max-width: calc(100vw - 24px);
586 max-height: calc(100vh - 24px);
587 overflow-y: auto;
588 padding: 12px;
589 border-radius: 12px;
590 border: 1px solid var(--vp-c-divider);
591 background-color: var(--vp-c-bg-elv);
592 box-shadow: var(--vp-shadow-3, 0 4px 16px rgba(0, 0, 0, 0.3));
593}
594
595/* Detail popup */
596.ann-backdrop {
597 position: fixed;
598 inset: 0;
599 z-index: 200;
600 display: flex;
601 align-items: center;
602 justify-content: center;
603 background: rgba(0, 0, 0, 0.5);
604 backdrop-filter: blur(4px);
605 overscroll-behavior: none;
606}
607
608.ann-detail {
609 width: min(640px, calc(100vw - 32px));
610 max-height: 85vh;
611 display: flex;
612 flex-direction: column;
613 border-radius: 16px;
614 border: 1px solid var(--vp-c-divider);
615 background: var(--vp-c-bg);
616 overflow: hidden;
617}
618
619.ann-detail-head {
620 display: flex;
621 align-items: center;
622 justify-content: space-between;
623 gap: 8px;
624 padding: 10px 12px 10px 16px;
625 border-bottom: 1px solid var(--vp-c-divider);
626 flex-shrink: 0;
627}
628
629.ann-detail-title {
630 flex: 1;
631 font-size: 15px;
632 font-weight: 600;
633 color: var(--vp-c-text-1);
634 overflow: hidden;
635 white-space: nowrap;
636 text-overflow: ellipsis;
637 min-width: 0;
638}
639
640.ann-detail-back {
641 display: none;
642 align-items: center;
643 justify-content: center;
644 padding: 8px;
645 border: none;
646 background: transparent;
647 color: var(--vp-c-text-1);
648 cursor: pointer;
649 transition: color 0.15s;
650}
651
652.ann-detail-back:hover {
653 color: var(--vp-c-brand-1);
654}
655
656.ann-detail-close {
657 display: flex;
658 align-items: center;
659 justify-content: center;
660 width: 28px;
661 height: 28px;
662 border-radius: 6px;
663 border: none;
664 background: transparent;
665 color: var(--vp-c-text-2);
666 font-size: 16px;
667 cursor: pointer;
668 flex-shrink: 0;
669 transition: color 0.15s, transform 0.2s cubic-bezier(0.16, 1, 0.3, 1);
670}
671
672.ann-detail-close:hover {
673 color: var(--vp-c-text-1);
674}
675
676.ann-detail-close:active {
677 transform: scale(0.94);
678}
679
680.ann-detail-body {
681 overflow-y: auto;
682 min-height: 0;
683 padding: 20px 24px 28px;
684 overscroll-behavior: contain;
685}
686
687.ann-detail-content :deep(:first-child) {
688 margin-top: 0;
689}
690
691/* Transitions */
692.ann-panel-enter-active,
693.ann-panel-leave-active {
694 transition: opacity 0.15s, transform 0.15s cubic-bezier(0.16, 1, 0.3, 1);
695}
696
697.ann-panel-enter-from,
698.ann-panel-leave-to {
699 opacity: 0;
700 transform: translateX(-50%) scale(0.95) translateY(-4px);
701}
702
703.ann-panel[style*="--ann-panel-left"].ann-panel-enter-from,
704.ann-panel[style*="--ann-panel-left"].ann-panel-leave-to {
705 transform: scale(0.95) translateY(-4px);
706}
707
708.ann-fade-enter-active,
709.ann-fade-leave-active {
710 transition: opacity 0.2s;
711}
712
713.ann-fade-enter-from,
714.ann-fade-leave-to {
715 opacity: 0;
716}
717
718.ann-detail-fade-enter-active,
719.ann-detail-fade-leave-active { transition: opacity 0.2s ease; }
720.ann-detail-fade-enter-from,
721.ann-detail-fade-leave-to { opacity: 0; }
722
723.ann-detail-slide-enter-active { transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.2s ease; }
724.ann-detail-slide-leave-active { transition: transform 0.2s ease, opacity 0.15s ease; }
725.ann-detail-slide-enter-from { transform: translateY(20px); opacity: 0; }
726.ann-detail-slide-leave-to { transform: translateY(10px); opacity: 0; }
727
728html.effects-disabled .ann-btn,
729html.effects-disabled .ann-header-btn,
730html.effects-disabled .ann-detail-close {
731 transition: none;
732}
733
734html.effects-disabled .ann-btn:active,
735html.effects-disabled .ann-header-btn:active,
736html.effects-disabled .ann-detail-close:active {
737 transform: none;
738}
739
740/* Phone: full-screen panel */
741@media (max-width: 767px) {
742 .ann-panel {
743 position: fixed;
744 top: 0;
745 left: 0;
746 right: 0;
747 bottom: 0;
748 width: 100%;
749 max-width: none;
750 height: 100%;
751 border-radius: 0;
752 border: none;
753 z-index: 9999;
754 transform: none;
755 --ann-panel-bg: var(--vp-c-bg);
756 }
757
758 .ann-header {
759 height: 49px;
760 padding: 6px 8px;
761 border-bottom: 1px solid var(--wk-fs-header-divider);
762 }
763
764 .ann-mobile-back {
765 display: flex;
766 }
767
768 .ann-header-title {
769 font-size: 16px;
770 }
771
772 .ann-scroll {
773 flex: 1;
774 height: auto;
775 min-height: 0;
776 }
777
778 .ann-panel-enter-from,
779 .ann-panel-leave-to {
780 opacity: 0;
781 transform: translateY(8px);
782 }
783
784 .ann-filter-menu {
785 z-index: 10000;
786 }
787
788 .ann-backdrop {
789 align-items: stretch;
790 z-index: 10001;
791 }
792
793 .ann-detail {
794 width: 100vw;
795 height: 100vh;
796 height: 100dvh;
797 max-height: none;
798 border-radius: 0;
799 border: none;
800 }
801
802 .ann-detail-head {
803 height: 49px;
804 padding: 6px 8px;
805 justify-content: flex-start;
806 border-bottom-color: var(--wk-fs-header-divider);
807 }
808
809 .ann-detail-back {
810 display: flex;
811 }
812
813 .ann-detail-close {
814 display: none;
815 }
816}
817</style>