mirror of
https://github.com/CharaChorder/DeviceManager.git
synced 2026-01-19 00:13:01 +00:00
40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
import type { Attachment } from "svelte/attachments";
|
|
|
|
export const hotkeys = new Map<string, HTMLElement>();
|
|
|
|
export function tooltip(
|
|
target: HTMLElement | undefined,
|
|
shortcut?: string,
|
|
): Attachment<HTMLElement> {
|
|
return (node: HTMLElement) => {
|
|
function show() {
|
|
if (!target) return;
|
|
target.showPopover({ source: node });
|
|
}
|
|
function hide() {
|
|
if (!target) return;
|
|
target.hidePopover();
|
|
}
|
|
|
|
node.addEventListener("mouseenter", show);
|
|
node.addEventListener("focus", show);
|
|
node.addEventListener("mouseleave", hide);
|
|
node.addEventListener("blur", hide);
|
|
|
|
if (shortcut && node instanceof HTMLElement) {
|
|
hotkeys.set(shortcut, node);
|
|
}
|
|
|
|
return () => {
|
|
node.removeEventListener("mouseenter", show);
|
|
node.removeEventListener("focus", show);
|
|
node.removeEventListener("mouseleave", hide);
|
|
node.removeEventListener("blur", hide);
|
|
|
|
if (shortcut && node instanceof HTMLElement) {
|
|
hotkeys.delete(shortcut);
|
|
}
|
|
};
|
|
};
|
|
}
|