Skip to content

Commit

Permalink
Ensures that only one context menu is open at a time (#1636)
Browse files Browse the repository at this point in the history
  • Loading branch information
duranb authored Feb 26, 2025
1 parent f9e092e commit 0d1b419
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
22 changes: 21 additions & 1 deletion src/components/context-menu/ContextMenu.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
<svelte:options accessors={true} immutable={true} />

<script lang="ts" context="module">
type HideFns = Set<() => void>;
const hideFns: HideFns = new Set<() => void>();
export function hideAllMenus() {
hideFns.forEach(hideFn => {
hideFn();
});
}
</script>

<script lang="ts">
import { createEventDispatcher } from 'svelte';
import { createEventDispatcher, onDestroy, onMount } from 'svelte';
import { fade } from 'svelte/transition';
const dispatch = createEventDispatcher<{
Expand Down Expand Up @@ -33,6 +44,7 @@
}
export function show(e: MouseEvent): void {
hideAllMenus();
e.preventDefault();
shown = true;
x = e.clientX;
Expand All @@ -44,6 +56,14 @@
let x: number;
let y: number;
onMount(() => {
hideFns.add(hide);
});
onDestroy(() => {
hideFns.delete(hide);
});
$: if (div) {
const rect = div.getBoundingClientRect();
if (x + rect.width > window.innerWidth) {
Expand Down
1 change: 0 additions & 1 deletion src/components/menus/Menu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
};
export function hideAllMenus(type?: MenuType) {
// TODO: https://github.com/sveltejs/language-tools/issues/1229
if (type) {
hideFns[type].forEach(hide => {
hide();
Expand Down

0 comments on commit 0d1b419

Please sign in to comment.