Skip to content

Commit

Permalink
apply same Menu logic to ContextMenu to ensure only one is open a…
Browse files Browse the repository at this point in the history
…t any time.
  • Loading branch information
duranb committed Feb 25, 2025
1 parent f9e092e commit 5fef1ec
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/components/context-menu/ContextMenu.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
<svelte:options accessors={true} immutable={true} />

<script lang="ts" context="module">
type HideFns = Set<() => void>;
const hideFns: HideFns = new Set<() => void>();
export function hideAllMenus() {
// TODO: https://github.com/sveltejs/language-tools/issues/1229
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 +45,7 @@
}
export function show(e: MouseEvent): void {
hideAllMenus();
e.preventDefault();
shown = true;
x = e.clientX;
Expand All @@ -44,6 +57,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

0 comments on commit 5fef1ec

Please sign in to comment.