Files
kandev/apps/web/components/quick-chat/quick-chat-focus.ts
T
2026-08-27 13:08:40 +00:00

33 lines
1.2 KiB
TypeScript

let launcherFocus: HTMLElement | null = null;
let launcherFocusShouldBeSilent = false;
const SILENT_FOCUS_ATTRIBUTE = "data-quick-chat-silent-focus";
function markFocusAsSilent(element: HTMLElement): void {
element.setAttribute(SILENT_FOCUS_ATTRIBUTE, "true");
element.addEventListener("blur", () => element.removeAttribute(SILENT_FOCUS_ATTRIBUTE), {
once: true,
});
}
/** Records the control that opened the shared Quick Chat surface. */
export function captureQuickChatLauncherFocus(options: { silent?: boolean } = {}): void {
launcherFocusShouldBeSilent = false;
if (typeof document === "undefined") return;
launcherFocus = document.activeElement instanceof HTMLElement ? document.activeElement : null;
launcherFocusShouldBeSilent = options.silent ?? true;
}
/** Restores launcher focus after the shared dialog has finished closing. */
export function restoreQuickChatLauncherFocus(): void {
const element = launcherFocus;
const shouldSilenceFocus = launcherFocusShouldBeSilent;
launcherFocus = null;
launcherFocusShouldBeSilent = false;
if (!element) return;
requestAnimationFrame(() => {
if (!element.isConnected) return;
if (shouldSilenceFocus) markFocusAsSilent(element);
element.focus();
});
}