Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensures that only one context menu is open at a time #1636

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading