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

previewActionBus.ts

26 lines816 Bytes

1"use client";
2​
3export type PreviewAction =
4 | "toggle-panel"
5 | "refresh"
6 | "focus-url"
7 | "zoom-in"
8 | "zoom-out"
9 | "reset-zoom";
10​
11const EVENT_NAME = "t3code:preview-action";
12​
13export function dispatchPreviewAction(action: PreviewAction): void {
14 if (typeof window === "undefined") return;
15 window.dispatchEvent(new CustomEvent<PreviewAction>(EVENT_NAME, { detail: action }));
17​
18export function subscribePreviewAction(listener: (action: PreviewAction) => void): () => void {
19 if (typeof window === "undefined") return () => {};
20 const handler = (event: Event) => {
21 const detail = (event as CustomEvent<PreviewAction>).detail;
22 if (typeof detail === "string") listener(detail);
23 };
24 window.addEventListener(EVENT_NAME, handler);
25 return () => window.removeEventListener(EVENT_NAME, handler);