UNPKG

@assistant-ui/react

Version:

Open-source TypeScript/React library for building production-grade AI chat experiences

41 lines (40 loc) • 1.97 kB
"use client"; import { COMPOSER_INPUT_SELECTOR } from "./composerInputSelector.js"; import { useThreadRootElementRef } from "../thread/ThreadRootElementContext.js"; import { c } from "@assistant-ui/tap/react-shim/compiler-runtime"; import "@assistant-ui/tap/react-shim"; import { useAui } from "@assistant-ui/store"; // src/primitives/composer/useComposerCancelWithFocus.ts const COMPOSER_SELECTOR = "[data-aui-composer-type]"; const THREAD_SELECTOR = "[data-aui-thread-root]"; const useComposerCancelWithFocus = (cancel) => { const $ = c(4); const aui = useAui(); const threadRootRef = useThreadRootElementRef(); let t0; if ($[0] !== aui.composer || $[1] !== cancel || $[2] !== threadRootRef) { t0 = (source) => { const root = threadRootRef?.current; const doc = source?.ownerDocument; const active = doc?.activeElement; const composer = source?.closest(COMPOSER_SELECTOR); const shouldRestore = aui.composer.getState().type === "edit" && root && active && composer?.getAttribute("data-aui-composer-type") === "edit" && composer.contains(active) && composer.closest(THREAD_SELECTOR) === root; const candidates = shouldRestore ? Array.from(root.querySelectorAll(COMPOSER_INPUT_SELECTOR)).filter((input) => input.closest(THREAD_SELECTOR) === root && input.closest(COMPOSER_SELECTOR)?.getAttribute("data-aui-composer-type") === "thread") : []; cancel(); if (!doc || !active || !shouldRestore) return; if (doc.activeElement !== active && doc.activeElement !== doc.body) return; for (const input_0 of candidates) { if (!input_0.isConnected || input_0.closest("[hidden], [inert]")) continue; input_0.focus({ preventScroll: true }); if (doc.activeElement !== active && doc.activeElement !== doc.body) break; } }; $[0] = aui.composer; $[1] = cancel; $[2] = threadRootRef; $[3] = t0; } else t0 = $[3]; return t0; }; export { useComposerCancelWithFocus }; //# sourceMappingURL=useComposerCancelWithFocus.js.map