Disable native context menu

Prevents the browser/webview default context menu app-wide, while
preserving it for text selections and editable fields where the
native menu is still useful.
This commit is contained in:
Rūdolfs Ošiņš
2026-05-14 14:58:06 +02:00
parent 22170c7256
commit 67108bd443
2 changed files with 20 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
export function disableNativeContextMenu(): void {
window.addEventListener("contextmenu", event => {
const selection = window.getSelection();
if (
selection &&
!selection.isCollapsed &&
selection.toString().length > 0
) {
return;
}
const target = event.target as HTMLElement | null;
if (target?.closest("input, textarea, [contenteditable]")) {
return;
}
event.preventDefault();
});
}
+3
View File
@@ -3,9 +3,12 @@ import { hotkeyKeyUX, hotkeyMacCompat, startKeyUX } from "keyux";
import { mount } from "svelte";
import App from "./App.svelte";
import { disableNativeContextMenu } from "./lib/disableNativeContextMenu";
const app = mount(App, { target: document.body });
disableNativeContextMenu();
const mac = hotkeyMacCompat();
startKeyUX(window, [hotkeyKeyUX([mac])]);