Redesign file context menus and download archives using global preference
This commit is contained in:
1 parent
6aca927851
commit
042392f611
22 files changed
+530
-363
No files matched your search
@@ -1,47 +1,99 @@
|
||||
<template>
|
||||
<div
|
||||
class="context-menu"
|
||||
ref="contextMenu"
|
||||
v-show="show"
|
||||
:style="{
|
||||
top: `${props.pos.y}px`,
|
||||
left: `${left}px`,
|
||||
}"
|
||||
>
|
||||
<slot />
|
||||
</div>
|
||||
<Teleport to="body">
|
||||
<div
|
||||
v-if="show"
|
||||
ref="contextMenu"
|
||||
class="context-menu"
|
||||
role="menu"
|
||||
tabindex="-1"
|
||||
:style="{ top: top + 'px', left: left + 'px' }"
|
||||
@click="hide"
|
||||
@contextmenu.prevent.stop
|
||||
@keydown="keyboard"
|
||||
>
|
||||
<slot />
|
||||
</div>
|
||||
</Teleport>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, watch, computed, onUnmounted } from "vue";
|
||||
|
||||
import { ref, watch, nextTick, onBeforeUnmount } from "vue";
|
||||
const emit = defineEmits(["hide"]);
|
||||
const props = defineProps<{ show: boolean; pos: { x: number; y: number } }>();
|
||||
const contextMenu = ref<HTMLElement | null>(null);
|
||||
|
||||
const left = computed(() => {
|
||||
return Math.min(
|
||||
props.pos.x,
|
||||
window.innerWidth - (contextMenu.value?.clientWidth ?? 0)
|
||||
const contextMenu = ref<HTMLElement | null>(null),
|
||||
left = ref(0),
|
||||
top = ref(0);
|
||||
let previousFocus: HTMLElement | null = null;
|
||||
const hide = () => emit("hide");
|
||||
function outside(event: Event) {
|
||||
if (
|
||||
event.target instanceof Node &&
|
||||
!contextMenu.value?.contains(event.target)
|
||||
)
|
||||
hide();
|
||||
}
|
||||
function keyboard(event: KeyboardEvent) {
|
||||
const buttons = Array.from(
|
||||
contextMenu.value?.querySelectorAll<HTMLButtonElement>(
|
||||
"button:not(:disabled)"
|
||||
) || []
|
||||
);
|
||||
});
|
||||
|
||||
const hideContextMenu = () => {
|
||||
emit("hide");
|
||||
};
|
||||
|
||||
watch(
|
||||
() => props.show,
|
||||
(val) => {
|
||||
if (val) {
|
||||
document.addEventListener("click", hideContextMenu);
|
||||
} else {
|
||||
document.removeEventListener("click", hideContextMenu);
|
||||
}
|
||||
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 }
|
||||
);
|
||||
|
||||
onUnmounted(() => {
|
||||
document.removeEventListener("click", hideContextMenu);
|
||||
});
|
||||
onBeforeUnmount(removeListeners);
|
||||
</script>
|
||||
Reference in new issue
Block a user