memorium/islands/KMenu.tsx

189 lines
5.2 KiB
TypeScript
Raw Normal View History

2023-07-31 04:19:04 +02:00
import { Signal, useSignal } from "@preact/signals";
import { useRef } from "preact/hooks";
import { useEventListener } from "@lib/hooks/useEventListener.ts";
import { menus } from "@islands/KMenu/commands.ts";
import { MenuEntry } from "@islands/KMenu/types.ts";
2023-08-02 13:11:17 +02:00
import * as icons from "@components/icons.tsx";
2023-07-31 04:19:04 +02:00
const KMenuEntry = (
{ entry, activeIndex, index }: {
entry: MenuEntry;
activeIndex: Signal<number>;
index: number;
},
) => {
return (
<div
onClick={() => activeIndex.value = index}
2023-08-02 13:11:17 +02:00
class={`px-4 py-2 flex items-center gap-1 ${
2023-07-31 04:19:04 +02:00
activeIndex.value === index
? "bg-gray-100 text-gray-900"
: "text-gray-400"
}`}
>
2023-08-02 13:11:17 +02:00
{entry?.icon && icons[entry.icon]({ class: "w-4 h-4 mr-1" })}
2023-07-31 04:19:04 +02:00
{entry.title}
</div>
);
};
export const KMenu = (
{ type = "main", context }: { context: unknown; type: keyof typeof menus },
) => {
const activeMenuType = useSignal(type);
const activeMenu = menus[activeMenuType.value || "main"];
2023-08-02 01:58:03 +02:00
const activeState = useSignal<"normal" | "loading" | "error" | "input">(
"normal",
);
2023-07-31 04:19:04 +02:00
const activeIndex = useSignal(-1);
const input = useRef<HTMLInputElement>(null);
const commandInput = useSignal("");
2023-08-02 13:11:17 +02:00
const visible = useSignal(true);
2023-08-01 21:35:21 +02:00
if (visible.value === false) {
setTimeout(() => {
activeMenuType.value = "main";
activeState.value = "normal";
if (input.current) {
input.current.value = "";
}
commandInput.value = "";
}, 100);
}
const entries = activeMenu?.entries?.filter((entry) => {
const entryVisible = typeof entry["visible"] === "boolean"
? entry.visible
: typeof entry["visible"] === "function"
? entry.visible?.(context)
: true;
const search = commandInput?.value?.toLowerCase();
const searchMatches = entry.title.toLowerCase().includes(search) ||
entry.meta?.toLowerCase()?.includes(search);
return entryVisible && searchMatches;
});
if (entries.length === 1) {
activeIndex.value = 0;
}
2023-07-31 04:19:04 +02:00
function activateEntry(menuEntry?: MenuEntry) {
if (!menuEntry) return;
menuEntry.cb({
activeMenu: activeMenuType,
menus,
activeState,
2023-08-01 21:35:21 +02:00
commandInput,
2023-07-31 04:19:04 +02:00
visible,
}, context);
}
useEventListener("keydown", (ev: KeyboardEvent) => {
if (ev.key === "/" && ev.ctrlKey) {
visible.value = !visible.value;
}
if (ev.key === "ArrowDown") {
const index = activeIndex.value;
if (index + 1 >= entries.length) {
activeIndex.value = 0;
} else {
activeIndex.value += 1;
}
}
if (ev.key === "Enter") {
activateEntry(entries[activeIndex.value]);
}
if (ev.key === "ArrowUp") {
const index = activeIndex.value;
if (index - 1 < 0) {
activeIndex.value = entries.length - 1;
} else {
activeIndex.value -= 1;
}
}
if (ev.key === "Escape") {
visible.value = false;
if (input.current) {
input.current.value = "";
}
}
if (visible.value) {
input.current?.focus();
} else {
input.current?.blur();
}
});
return (
<div
class={`opacity-${visible.value ? 100 : 0} pointer-events-${
visible.value ? "auto" : "none"
} transition grid place-items-center w-full h-full fixed top-0 left-0 z-50`}
2023-08-02 13:11:17 +02:00
style={{ background: "#141217ee" }}
2023-07-31 04:19:04 +02:00
>
<div
2023-08-02 13:11:17 +02:00
class={`relative w-1/2 max-h-64 max-w-[400px] rounded-2xl shadow-2xl nnoisy-gradient overflow-hidden after:opacity-10 bg-gray-700`}
2023-07-31 04:19:04 +02:00
>
<div
class="grid h-12 text-gray-400 border-b border-gray-500 "
style={{ gridTemplateColumns: "4em 1fr" }}
>
2023-08-02 01:58:03 +02:00
{(activeState.value === "normal" || activeState.value === "input") &&
2023-07-31 04:19:04 +02:00
(
<>
<div class="grid place-items-center border-r border-gray-500">
<span class="text-white">
{activeMenu.title}
</span>
</div>
<input
ref={input}
onInput={() => {
commandInput.value = input.current?.value || "";
}}
2023-08-02 13:11:17 +02:00
style={{ outline: "none !important" }}
2023-07-31 04:19:04 +02:00
placeholder="Command"
2023-08-02 13:11:17 +02:00
class="bg-transparent color pl-4 outline outline outline-2 outline-offset-2"
2023-07-31 04:19:04 +02:00
/>
</>
)}
{activeState.value === "loading" && (
<div class="p-4">
Loading...
</div>
)}
</div>
{activeState.value === "normal" &&
(
2023-07-31 04:30:55 +02:00
<div class="" style={{ maxHeight: "12rem", overflowY: "auto" }}>
2023-08-02 01:58:03 +02:00
{entries?.length === 0 && (
<div class="text-gray-400 px-4 py-2">
No Entries
</div>
)}
2023-08-01 21:35:21 +02:00
{entries.map(
2023-07-31 04:19:04 +02:00
(k, index) => {
return (
<KMenuEntry
entry={k}
activeIndex={activeIndex}
index={index}
/>
);
},
)}
</div>
)}
</div>
</div>
);
};