UNPKG

@copilotkit/react-ui

Version:

<div align="center"> <a href="https://copilotkit.ai" target="_blank"> <img src="https://github.com/copilotkit/copilotkit/raw/main/assets/banner.png" alt="CopilotKit Logo"> </a>

146 lines (144 loc) 5.08 kB
import { useChatContext } from "./chunk-UN2E3HCK.mjs"; import { useDarkMode } from "./chunk-JGMFJZMG.mjs"; // src/components/chat/Window.tsx import React, { useCallback, useEffect } from "react"; import { useCopilotContext } from "@copilotkit/react-core"; import { jsx, jsxs } from "react/jsx-runtime"; var Window = ({ children, clickOutsideToClose, shortcut, hitEscapeToClose }) => { var _a; const windowRef = React.useRef(null); const context = useCopilotContext(); const { open, setOpen } = useChatContext(); const handleClickOutside = useCallback( (event) => { var _a2; if (!clickOutsideToClose) { return; } const parentElement = (_a2 = windowRef.current) == null ? void 0 : _a2.parentElement; let className = ""; if (event.target instanceof HTMLElement) { className = event.target.className; } if (open && parentElement && !parentElement.contains(event.target) && // prevent closing the window when clicking on the debug menu !className.includes("copilotKitDebugMenu")) { setOpen(false); } }, [clickOutsideToClose, open, setOpen] ); const handleKeyDown = useCallback( (event) => { var _a2; const target = event.target; const isInput = target.tagName === "INPUT" || target.tagName === "SELECT" || target.tagName === "TEXTAREA" || target.isContentEditable; const isDescendantOfWrapper = (_a2 = windowRef.current) == null ? void 0 : _a2.contains(target); if (open && event.key === "Escape" && (!isInput || isDescendantOfWrapper) && hitEscapeToClose) { setOpen(false); } else if (event.key === shortcut && (isMacOS() && event.metaKey || !isMacOS() && event.ctrlKey) && (!isInput || isDescendantOfWrapper)) { setOpen(!open); } }, [hitEscapeToClose, shortcut, open, setOpen] ); const adjustForMobile = useCallback(() => { const copilotKitWindow = windowRef.current; const vv = window.visualViewport; if (!copilotKitWindow || !vv) { return; } if (window.innerWidth < 640 && open) { copilotKitWindow.style.height = `${vv.height}px`; copilotKitWindow.style.left = `${vv.offsetLeft}px`; copilotKitWindow.style.top = `${vv.offsetTop}px`; document.body.style.position = "fixed"; document.body.style.width = "100%"; document.body.style.height = `${window.innerHeight}px`; document.body.style.overflow = "hidden"; document.body.style.touchAction = "none"; document.body.addEventListener("touchmove", preventScroll, { passive: false }); } else { copilotKitWindow.style.height = ""; copilotKitWindow.style.left = ""; copilotKitWindow.style.top = ""; document.body.style.position = ""; document.body.style.height = ""; document.body.style.width = ""; document.body.style.overflow = ""; document.body.style.top = ""; document.body.style.touchAction = ""; document.body.removeEventListener("touchmove", preventScroll); } }, [open]); useEffect(() => { document.addEventListener("mousedown", handleClickOutside); document.addEventListener("keydown", handleKeyDown); if (window.visualViewport) { window.visualViewport.addEventListener("resize", adjustForMobile); adjustForMobile(); } return () => { document.removeEventListener("mousedown", handleClickOutside); document.removeEventListener("keydown", handleKeyDown); if (window.visualViewport) { window.visualViewport.removeEventListener("resize", adjustForMobile); } }; }, [adjustForMobile, handleClickOutside, handleKeyDown]); const isDark = useDarkMode(); const showPoweredBy = !((_a = context.copilotApiConfig) == null ? void 0 : _a.publicApiKey); const poweredByStyle = { visibility: "visible", display: "block", position: "static", textAlign: "center", fontSize: "12px", padding: "3px 0", color: isDark ? "rgb(69, 69, 69)" : "rgb(214, 214, 214)" }; return /* @__PURE__ */ jsxs( "div", { className: `copilotKitWindow ${showPoweredBy ? " poweredByContainer" : ""} ${open ? " open" : ""}`, ref: windowRef, children: [ children, showPoweredBy && // @ts-expect-error -- expecting position not to be a string, but it can be. /* @__PURE__ */ jsx("p", { className: "poweredBy", style: poweredByStyle, children: "Powered by CopilotKit" }) ] } ); }; var preventScroll = (event) => { let targetElement = event.target; const hasParentWithClass = (element, className) => { while (element && element !== document.body) { if (element.classList.contains(className)) { return true; } element = element.parentElement; } return false; }; if (!hasParentWithClass(targetElement, "copilotKitMessages")) { event.preventDefault(); } }; function isMacOS() { return /Mac|iMac|Macintosh/i.test(navigator.userAgent); } export { Window }; //# sourceMappingURL=chunk-HIORSNVD.mjs.map