diff --git a/src/ContextMenu/ContextMenu.svelte b/src/ContextMenu/ContextMenu.svelte index 81d8b90b..c89bfb13 100644 --- a/src/ContextMenu/ContextMenu.svelte +++ b/src/ContextMenu/ContextMenu.svelte @@ -24,40 +24,61 @@ const dispatch = createEventDispatcher(); const position = writable([x, y]); + const currentIndex = writable(-1); + const hasPopup = writable(false); const ctx = getContext("ContextMenu"); + let options = []; let direction = 1; let prevX = 0; let prevY = 0; + let focusIndex = -1; function close() { open = false; x = 0; y = 0; + prevX = 0; + prevY = 0; + focusIndex = -1; } - setContext("ContextMenu", { position, close }); + setContext("ContextMenu", { + currentIndex, + position, + close, + setPopup: (popup) => { + hasPopup.set(popup); + }, + }); afterUpdate(() => { - if (open && level === 1) { - if (prevX !== x || prevY !== y) ref.focus(); - prevX = x; - prevY = y; + if (open) { + options = [...ref.querySelectorAll("li[data-nested='false']")]; + + if (level === 1) { + if (prevX !== x || prevY !== y) ref.focus(); + prevX = x; + prevY = y; + } + + dispatch("open"); } else { dispatch("close"); } + + if (!$hasPopup && options[focusIndex]) options[focusIndex].focus(); }); $: level = !ctx ? 1 : 2; + $: currentIndex.set(focusIndex);