Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

commandPaletteBus.ts

30 lines1.1 KB

1// Tiny event bus allowing components to programmatically open the command palette
2// without owning its React state.
3const COMMAND_PALETTE_OPEN_EVENT = "t3code:open-command-palette";
4​
5export interface CommandPaletteOpenDetail {
6 readonly open?: "add-project" | "new-thread-in";
7}
8​
9export function openCommandPalette(detail?: CommandPaletteOpenDetail): void {
10 window.dispatchEvent(
11 new CustomEvent(COMMAND_PALETTE_OPEN_EVENT, detail ? { detail } : undefined),
12 );
14​
15export function onOpenCommandPalette(
16 listener: (detail: CommandPaletteOpenDetail) => void,
17): () => void {
18 const handler = (event: Event) => {
19 listener((event as CustomEvent<CommandPaletteOpenDetail>).detail ?? {});
20 };
21 window.addEventListener(COMMAND_PALETTE_OPEN_EVENT, handler);
22 return () => window.removeEventListener(COMMAND_PALETTE_OPEN_EVENT, handler);
24​
25/** Read at event time so consumers do not subscribe to transient dialog state. */
26export function isCommandPaletteOpen(): boolean {
27 return (
28 typeof document !== "undefined" && document.querySelector("[data-command-palette]") !== null
29 );