diff --git a/apps/web/src/components/chat/PanelLayoutControls.tsx b/apps/web/src/components/chat/PanelLayoutControls.tsx index 4db623bcbb07..7252ead1d0fb 100644 --- a/apps/web/src/components/chat/PanelLayoutControls.tsx +++ b/apps/web/src/components/chat/PanelLayoutControls.tsx @@ -5,7 +5,7 @@ import { Maximize2Icon, Minimize2Icon, } from "lucide-react"; -import { memo, type ReactNode } from "react"; +import { memo, type ReactNode, useLayoutEffect, useRef } from "react"; import { cn } from "~/lib/utils"; @@ -15,6 +15,9 @@ import { useMobileSidebarSheetMounted } from "../ui/sidebar"; import { Toggle } from "../ui/toggle"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; +/** Matches the right inset of `ThreadPanelHeaderControl`. */ +const THREAD_PANEL_HEADER_INSET_PX = 12; + export interface PanelLayoutControlsProps { showThreadPanelControl?: boolean; showTerminalControl?: boolean; @@ -68,7 +71,7 @@ export const PanelLayoutControls = memo(function PanelLayoutControls({ ); const threadPanelControl = ( - }> + }> {threadPanelPresentation === "popover" ? ( ) : ( @@ -195,9 +198,32 @@ export function WorkspaceTitlebarControls(props: { readonly onToggleMaximized: () => void; readonly children: ReactNode; }) { + const { rightPanelOpen, maximized } = props; + const controlsRef = useRef(null); + const previous = useRef({ rightPanelOpen, maximized }); + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- starts the toggle's slide before the commit that swapped it in paints; the animation is cancelled on change or unmount. + useLayoutEffect(() => { + const { rightPanelOpen: wasOpen, maximized: wasMaximized } = previous.current; + previous.current = { rightPanelOpen, maximized }; + if (!wasOpen || rightPanelOpen || wasMaximized) return; + const toggle = controlsRef.current?.querySelector("[data-thread-panel-toggle]"); + const panel = toggle?.ownerDocument.querySelector("[data-preview-panel-mode='inline']"); + const timing = panel?.getAnimations()[0]?.effect?.getTiming(); + if (!toggle || !panel || !timing) return; + // The header's toggle was just removed, so slide this one in from where it sat. + const headerRight = panel.getBoundingClientRect().left - THREAD_PANEL_HEADER_INSET_PX; + const offset = headerRight - toggle.getBoundingClientRect().right; + const animation = toggle.animate( + [{ transform: `translateX(${offset}px)` }, { transform: "none" }], + { ...timing, easing: getComputedStyle(panel).transitionTimingFunction }, + ); + return () => animation.cancel(); + }, [rightPanelOpen, maximized]); + const mobileSidebarSheetMounted = useMobileSidebarSheetMounted(); return (