Skip to content
Merged
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
15 changes: 8 additions & 7 deletions src/lib/context-target.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import type { Action } from "svelte/action";
import ContextMenu, { type ContextMenuItem } from "./ContextMenu.svelte";
import { mount, unmount } from "svelte";
import { get, writable, type Writable } from "svelte/store";

export interface ContextMenuOptions {
items: ContextMenuItem[];
}

interface ContextMenuWritable extends Writable<ContextMenu | undefined> {
interface ContextMenuStore extends Writable<Record<string, any> | undefined> {
close: () => void;
}

export let contextMenu: ContextMenuWritable = createContextMenuStore();

function createContextMenuStore(): ContextMenuWritable {
const store: Writable<ContextMenu | undefined> = writable(undefined);
function createContextMenuStore(): ContextMenuStore {
const store: Writable<Record<string, any> | undefined> = writable(undefined);

const close = () => {
const cm = get(store);
if (cm) {
cm.$destroy(); // Ensure that the context menu has a $destroy method
unmount(cm);
store.set(undefined);
}
};
Expand All @@ -29,6 +28,8 @@ function createContextMenuStore(): ContextMenuWritable {
};
}

export const contextMenu: ContextMenuStore = createContextMenuStore();

export const contextTarget: Action<HTMLElement, ContextMenuOptions> = (
node: HTMLElement,
options: ContextMenuOptions,
Expand All @@ -45,7 +46,7 @@ export const contextTarget: Action<HTMLElement, ContextMenuOptions> = (
contextMenu.close();

contextMenu.set(
new ContextMenu({
mount(ContextMenu, {
target: document.body,
props: {
target: node,
Expand Down