136 lines
3.8 KiB
Vue
136 lines
3.8 KiB
Vue
<template>
|
|
<Teleport to="body">
|
|
<div
|
|
v-if="show"
|
|
ref="contextMenu"
|
|
class="context-menu"
|
|
role="menu"
|
|
tabindex="-1"
|
|
:style="{ top: top + 'px', left: left + 'px' }"
|
|
@click="menuClick"
|
|
@click.capture="schedulePosition"
|
|
@contextmenu.prevent.stop
|
|
@keydown="keyboard"
|
|
>
|
|
<slot />
|
|
</div>
|
|
</Teleport>
|
|
</template>
|
|
<script setup lang="ts">
|
|
import { ref, watch, nextTick, onBeforeUnmount, provide, useId } from "vue";
|
|
import { contextMenuKey } from "@/utils/context-menu";
|
|
const emit = defineEmits(["hide"]);
|
|
const props = defineProps<{ show: boolean; pos: { x: number; y: number } }>();
|
|
const contextMenu = ref<HTMLElement | null>(null),
|
|
left = ref(0),
|
|
top = ref(0);
|
|
let previousFocus: HTMLElement | null = null;
|
|
const hide = () => emit("hide");
|
|
const menuId = useId();
|
|
provide(contextMenuKey, { id: menuId, close: hide });
|
|
function menuClick(event: MouseEvent) {
|
|
if (
|
|
event.target instanceof Element &&
|
|
event.target.closest('button[aria-haspopup="menu"]')
|
|
)
|
|
return;
|
|
hide();
|
|
}
|
|
function position() {
|
|
if (!contextMenu.value) return;
|
|
const rect = contextMenu.value.getBoundingClientRect();
|
|
left.value = Math.max(
|
|
8,
|
|
Math.min(props.pos.x, window.innerWidth - rect.width - 8)
|
|
);
|
|
top.value = Math.max(
|
|
8,
|
|
Math.min(props.pos.y, window.innerHeight - rect.height - 8)
|
|
);
|
|
}
|
|
async function schedulePosition() {
|
|
await nextTick();
|
|
position();
|
|
}
|
|
function outside(event: Event) {
|
|
if (
|
|
event.target instanceof Element &&
|
|
event.target
|
|
.closest("[data-context-owner]")
|
|
?.getAttribute("data-context-owner") === menuId
|
|
)
|
|
return;
|
|
if (
|
|
event.target instanceof Node &&
|
|
!contextMenu.value?.contains(event.target)
|
|
)
|
|
hide();
|
|
}
|
|
function keyboard(event: KeyboardEvent) {
|
|
schedulePosition();
|
|
const buttons = Array.from(
|
|
contextMenu.value?.querySelectorAll<HTMLButtonElement>(
|
|
"button:not(:disabled)"
|
|
) || []
|
|
);
|
|
const index = buttons.indexOf(document.activeElement as HTMLButtonElement);
|
|
if (event.key === "Escape") {
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
hide();
|
|
previousFocus?.focus();
|
|
} else if (event.key === "Tab") hide();
|
|
else if (
|
|
["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key) &&
|
|
buttons.length
|
|
) {
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
const next =
|
|
event.key === "Home"
|
|
? 0
|
|
: event.key === "End"
|
|
? buttons.length - 1
|
|
: (index + (event.key === "ArrowDown" ? 1 : -1) + buttons.length) %
|
|
buttons.length;
|
|
buttons[next].focus();
|
|
}
|
|
}
|
|
function removeListeners() {
|
|
document.removeEventListener("pointerdown", outside, true);
|
|
window.removeEventListener("scroll", outside, true);
|
|
window.removeEventListener("resize", hide);
|
|
}
|
|
watch(
|
|
[() => props.show, () => props.pos],
|
|
async ([visible]) => {
|
|
removeListeners();
|
|
if (!visible) return;
|
|
previousFocus =
|
|
document.activeElement instanceof HTMLElement
|
|
? document.activeElement
|
|
: null;
|
|
await nextTick();
|
|
if (!props.show || !contextMenu.value) return;
|
|
const rect = contextMenu.value.getBoundingClientRect();
|
|
left.value = Math.max(
|
|
8,
|
|
Math.min(props.pos.x, window.innerWidth - rect.width - 8)
|
|
);
|
|
top.value = Math.max(
|
|
8,
|
|
Math.min(props.pos.y, window.innerHeight - rect.height - 8)
|
|
);
|
|
const buttons =
|
|
contextMenu.value.querySelectorAll<HTMLButtonElement>("button");
|
|
buttons.forEach((button) => button.setAttribute("role", "menuitem"));
|
|
buttons[0]?.focus({ preventScroll: true });
|
|
document.addEventListener("pointerdown", outside, true);
|
|
window.addEventListener("scroll", outside, true);
|
|
window.addEventListener("resize", hide);
|
|
},
|
|
{ immediate: true }
|
|
);
|
|
onBeforeUnmount(removeListeners);
|
|
</script>
|