feat: make custom context menu imitate browser context menu behavior
This commit is contained in:
parent
99ed210d26
commit
839920f124
2 changed files with 23 additions and 10 deletions
13
app.vue
13
app.vue
|
@ -17,12 +17,21 @@ const contextMenu = useState<ContextMenuInterface>("contextMenu", () => ({ show:
|
|||
onMounted(() => {
|
||||
loadPreferredThemes()
|
||||
|
||||
document.addEventListener("pointerdown", (e) => {
|
||||
document.addEventListener("contextmenu", (e) => {
|
||||
if (contextMenu.value.show) {
|
||||
e.preventDefault();
|
||||
if (e.target instanceof Element && !e.target.classList.contains("context-menu-item")) {
|
||||
removeContextMenu(contextMenu);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener("mousedown", (e) => {
|
||||
if (e.target instanceof HTMLElement && e.target.classList.contains("context-menu-item")) return;
|
||||
console.log("click");
|
||||
console.log("target:", e.target);
|
||||
console.log(e.target instanceof HTMLDivElement);
|
||||
if (contextMenu.value.show) {
|
||||
if (e.button != 2 && contextMenu.value.show) {
|
||||
console.log("context menu is shown, hiding");
|
||||
removeContextMenu(contextMenu);
|
||||
}
|
||||
|
|
|
@ -6,6 +6,9 @@ export default (e: MouseEvent | PointerEvent, sections: ContextMenuSection[]) =>
|
|||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
const contextMenu = useState<ContextMenuInterface>("contextMenu");
|
||||
if (contextMenu.value.show) {
|
||||
removeContextMenu(contextMenu);
|
||||
} else {
|
||||
console.log("Menu sections:", sections);
|
||||
if (sections.length) {
|
||||
console.log("Showing context menu");
|
||||
|
@ -15,4 +18,5 @@ export default (e: MouseEvent | PointerEvent, sections: ContextMenuSection[]) =>
|
|||
contextMenu.value.sections = sections;
|
||||
console.log("Showed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue