|
| 1 | +<script setup lang="ts"> |
| 2 | +import { nextTick, onBeforeUnmount, ref, watch } from 'vue'; |
| 3 | +
|
| 4 | +type Alignment = 'start' | 'end'; |
| 5 | +
|
| 6 | +const props = withDefaults(defineProps<{ |
| 7 | + anchor: HTMLElement | null; |
| 8 | + open: boolean; |
| 9 | + align?: Alignment; |
| 10 | +}>(), { |
| 11 | + align: 'start', |
| 12 | +}); |
| 13 | +
|
| 14 | +const emit = defineEmits<{ |
| 15 | + close: []; |
| 16 | +}>(); |
| 17 | +
|
| 18 | +const panelRef = ref<HTMLElement | null>(null); |
| 19 | +const panelStyle = ref<Record<string, string>>({}); |
| 20 | +let opener: HTMLElement | null = null; |
| 21 | +let listenersAttached = false; |
| 22 | +
|
| 23 | +function positionPanel(): void { |
| 24 | + const anchor = props.anchor; |
| 25 | + const panel = panelRef.value; |
| 26 | + if (!anchor || !panel) return; |
| 27 | +
|
| 28 | + const anchorRect = anchor.getBoundingClientRect(); |
| 29 | + const gap = 4; |
| 30 | + const margin = 16; |
| 31 | + const panelWidth = panel.offsetWidth; |
| 32 | + const panelHeight = panel.offsetHeight; |
| 33 | + const preferredLeft = props.align === 'end' |
| 34 | + ? anchorRect.right - panelWidth |
| 35 | + : anchorRect.left; |
| 36 | + const maxLeft = Math.max(margin, window.innerWidth - margin - panelWidth); |
| 37 | + let left = Math.min(Math.max(preferredLeft, margin), maxLeft); |
| 38 | + let top = anchorRect.bottom + gap; |
| 39 | +
|
| 40 | + if (top + panelHeight > window.innerHeight - margin) { |
| 41 | + top = Math.max(margin, anchorRect.top - panelHeight - gap); |
| 42 | + } |
| 43 | +
|
| 44 | + const maxTop = Math.max(margin, window.innerHeight - margin - panelHeight); |
| 45 | + top = Math.min(Math.max(top, margin), maxTop); |
| 46 | + left = Math.min(Math.max(left, margin), maxLeft); |
| 47 | + panelStyle.value = { |
| 48 | + top: `${Math.round(top)}px`, |
| 49 | + left: `${Math.round(left)}px`, |
| 50 | + }; |
| 51 | +} |
| 52 | +
|
| 53 | +function onViewportChange(): void { |
| 54 | + if (props.open) positionPanel(); |
| 55 | +} |
| 56 | +
|
| 57 | +function onPointerDown(event: PointerEvent): void { |
| 58 | + const target = event.target; |
| 59 | + if (target instanceof Node && (panelRef.value?.contains(target) || props.anchor?.contains(target))) return; |
| 60 | + emit('close'); |
| 61 | +} |
| 62 | +
|
| 63 | +function onKeydown(event: KeyboardEvent): void { |
| 64 | + if (event.key === 'Escape') emit('close'); |
| 65 | +} |
| 66 | +
|
| 67 | +function attachListeners(): void { |
| 68 | + if (listenersAttached) return; |
| 69 | + document.addEventListener('pointerdown', onPointerDown); |
| 70 | + document.addEventListener('keydown', onKeydown); |
| 71 | + document.addEventListener('scroll', onViewportChange, true); |
| 72 | + window.addEventListener('resize', onViewportChange); |
| 73 | + listenersAttached = true; |
| 74 | +} |
| 75 | +
|
| 76 | +function detachListeners(): void { |
| 77 | + if (!listenersAttached) return; |
| 78 | + document.removeEventListener('pointerdown', onPointerDown); |
| 79 | + document.removeEventListener('keydown', onKeydown); |
| 80 | + document.removeEventListener('scroll', onViewportChange, true); |
| 81 | + window.removeEventListener('resize', onViewportChange); |
| 82 | + listenersAttached = false; |
| 83 | +} |
| 84 | +
|
| 85 | +function restoreFocusIfNeeded(): void { |
| 86 | + const panel = panelRef.value; |
| 87 | + const activeElement = document.activeElement; |
| 88 | + if (!panel || !(activeElement instanceof Node) || !panel.contains(activeElement)) return; |
| 89 | +
|
| 90 | + const target = props.anchor ?? opener; |
| 91 | + if (target?.isConnected) target.focus(); |
| 92 | +} |
| 93 | +
|
| 94 | +watch(() => props.open, (open, wasOpen) => { |
| 95 | + if (open) { |
| 96 | + if (!wasOpen) { |
| 97 | + opener = document.activeElement instanceof HTMLElement ? document.activeElement : null; |
| 98 | + } |
| 99 | + void nextTick(() => { |
| 100 | + if (!props.open) return; |
| 101 | + positionPanel(); |
| 102 | + attachListeners(); |
| 103 | + }); |
| 104 | + return; |
| 105 | + } |
| 106 | +
|
| 107 | + restoreFocusIfNeeded(); |
| 108 | + detachListeners(); |
| 109 | + panelStyle.value = {}; |
| 110 | +}, { immediate: true }); |
| 111 | +
|
| 112 | +watch([() => props.anchor, () => props.align], () => { |
| 113 | + if (props.open) void nextTick(positionPanel); |
| 114 | +}); |
| 115 | +
|
| 116 | +onBeforeUnmount(() => { |
| 117 | + restoreFocusIfNeeded(); |
| 118 | + detachListeners(); |
| 119 | +}); |
| 120 | +</script> |
| 121 | + |
| 122 | +<template> |
| 123 | + <Teleport to="body"> |
| 124 | + <div |
| 125 | + v-if="open" |
| 126 | + ref="panelRef" |
| 127 | + class="popover" |
| 128 | + :style="panelStyle" |
| 129 | + role="menu" |
| 130 | + tabindex="-1" |
| 131 | + > |
| 132 | + <slot /> |
| 133 | + </div> |
| 134 | + </Teleport> |
| 135 | +</template> |
| 136 | + |
| 137 | +<style scoped> |
| 138 | +.popover { |
| 139 | + position: fixed; |
| 140 | + z-index: 200; |
| 141 | + box-sizing: border-box; |
| 142 | + max-width: calc(100vw - 32px); |
| 143 | + max-height: calc(100vh - 32px); |
| 144 | + overflow-y: auto; |
| 145 | + padding: 2px; |
| 146 | + border: 1px solid var(--line); |
| 147 | + border-radius: var(--r-md); |
| 148 | + background: var(--panel); |
| 149 | + box-shadow: 0 8px 24px color-mix(in srgb, var(--ink) 18%, transparent); |
| 150 | +} |
| 151 | +</style> |
0 commit comments