mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-28 17:50:28 +08:00
56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
interface Position {
|
|
x: number;
|
|
y: number;
|
|
id: string;
|
|
target: HTMLElement;
|
|
}
|
|
|
|
export function createListContextMenu<
|
|
T extends { id: string; labels: string[] },
|
|
>(items: () => T[]) {
|
|
let position: Position | undefined = $state();
|
|
|
|
const current = $derived.by(() => {
|
|
if (!position) return undefined;
|
|
const { id } = position;
|
|
const item = items().find(item => item.id === id);
|
|
return item && { ...position, item };
|
|
});
|
|
|
|
const labelSuggestions = $derived(
|
|
items()
|
|
.flatMap(item => item.labels)
|
|
.filter((label, index, all) => all.indexOf(label) === index),
|
|
);
|
|
|
|
// The item can drop out of the list after a refresh; forget it so the menu
|
|
// doesn't pop back up if it ever reappears.
|
|
$effect(() => {
|
|
if (position && !current) position = undefined;
|
|
});
|
|
|
|
return {
|
|
get current() {
|
|
return current;
|
|
},
|
|
get labelSuggestions() {
|
|
return labelSuggestions;
|
|
},
|
|
isActive(id: string): boolean {
|
|
return current?.item.id === id;
|
|
},
|
|
open(event: MouseEvent, id: string) {
|
|
event.preventDefault();
|
|
position = {
|
|
x: event.clientX,
|
|
y: event.clientY,
|
|
id,
|
|
target: event.currentTarget as HTMLElement,
|
|
};
|
|
},
|
|
close() {
|
|
position = undefined;
|
|
},
|
|
};
|
|
}
|