The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { useGT } from "gt-react"; |
| 2 | import { |
| 3 | type CSSProperties, |
| 4 | type FocusEvent, |
| 5 | type MouseEventHandler, |
| 6 | type ReactNode, |
| 7 | useLayoutEffect, |
| 8 | useRef, |
| 9 | useState, |
| 10 | } from "react"; |
| 11 | import { |
| 12 | type PreviewPanelInlineSize, |
| 13 | usePreviewPanelInlineSize, |
| 14 | } from "~/hooks/usePreviewPanelInlineSize"; |
| 15 | import { RESIZABLE_WIDTH_PROPERTY } from "~/hooks/useResizableWidth"; |
| 16 | import { canReturnFocusTo, workspaceAreaProps } from "../../lib/workspaceFocus"; |
| 17 | import { cn } from "~/lib/utils"; |
| 18 | |
| 19 | import { RightPanelResizeHandle } from "./RightPanelResizeHandle"; |
| 20 | |
| 21 | export type PreviewPanelMode = "inline" | "sheet" | "sidebar" | "embedded"; |
| 22 | |
| 23 | interface PreviewPanelShellProps { |
| 24 | workspaceArea?: boolean; |
| 25 | workspaceFocusKey?: string; |
| 26 | workspaceLabel?: string; |
| 27 | mode: PreviewPanelMode; |
| 28 | maximized?: boolean; |
| 29 | inlineSize?: PreviewPanelInlineSize; |
| 30 | open?: boolean; |
| 31 | /** Use a distinct key per surface to keep remembered widths independent. */ |
| 32 | widthStorageKey?: string; |
| 33 | defaultWidth?: number; |
| 34 | onMouseDown?: MouseEventHandler<HTMLDivElement>; |
| 35 | onMouseUp?: MouseEventHandler<HTMLDivElement>; |
| 36 | children: ReactNode; |
| 37 | } |
| 38 | |
| 39 | export function PreviewPanelShell(props: PreviewPanelShellProps) { |
| 40 | if (props.inlineSize) { |
| 41 | return <PreviewPanelShellFrame {...props} inlineSize={props.inlineSize} />; |
| 42 | } |
| 43 | |
| 44 | return <ResizablePreviewPanelShell {...props} />; |
| 45 | } |
| 46 | |
| 47 | function ResizablePreviewPanelShell(props: PreviewPanelShellProps) { |
| 48 | const inlineSize = usePreviewPanelInlineSize({ |
| 49 | enabled: props.mode === "inline" && !props.maximized, |
| 50 | widthStorageKey: props.widthStorageKey, |
| 51 | defaultWidth: props.defaultWidth, |
| 52 | }); |
| 53 | return <PreviewPanelShellFrame {...props} inlineSize={inlineSize} />; |
| 54 | } |
| 55 | |
| 56 | // Prefers the element focus entered the panel from, falling back to the panel toggle. |
| 57 | function returnFocus(host: HTMLElement, origin: HTMLElement | null) { |
| 58 | const { ownerDocument } = host; |
| 59 | // focus() is a no-op on a disabled origin, so activeElement says whether it took. |
| 60 | if (origin?.isConnected && !host.contains(origin) && canReturnFocusTo(origin)) { |
| 61 | origin.focus({ preventScroll: true }); |
| 62 | if (ownerDocument.activeElement === origin) return true; |
| 63 | } |
| 64 | const toggle = ownerDocument.querySelector<HTMLButtonElement>("[data-right-panel-toggle]"); |
| 65 | toggle?.focus(); |
| 66 | return toggle !== null && ownerDocument.activeElement === toggle; |
| 67 | } |
| 68 | |
| 69 | function PreviewPanelShellFrame( |
| 70 | props: PreviewPanelShellProps & { inlineSize: PreviewPanelInlineSize }, |
| 71 | ) { |
| 72 | const gt = useGT(); |
| 73 | const panelArea = workspaceAreaProps("panel", 4, props.workspaceLabel ?? gt("Workspace panel")); |
| 74 | const isInline = props.mode === "inline"; |
| 75 | const collapsible = isInline && props.open !== undefined; |
| 76 | const open = props.open ?? true; |
| 77 | const maximized = props.maximized ?? false; |
| 78 | const hostRef = useRef<HTMLDivElement | null>(null); |
| 79 | const returnedFocus = useRef(false); |
| 80 | const focusOrigin = useRef<HTMLElement | null>(null); |
| 81 | const lastBlurred = useRef<EventTarget | null>(null); |
| 82 | const { width, handlers, observeRow } = props.inlineSize; |
| 83 | // Width animates only for an open or close. Any other width change disables |
| 84 | // the transition in the same commit that changes the width, so the browser |
| 85 | // never starts one; the next open or close re-enables it in its own commit, |
| 86 | // so nothing waits for a paint. |
| 87 | const [widthTransition, setWidthTransition] = useState(() => ({ |
| 88 | open, |
| 89 | width, |
| 90 | maximized, |
| 91 | enabled: true, |
| 92 | })); |
| 93 | if ( |
| 94 | widthTransition.open !== open || |
| 95 | widthTransition.width !== width || |
| 96 | widthTransition.maximized !== maximized |
| 97 | ) { |
| 98 | setWidthTransition({ |
| 99 | open, |
| 100 | width, |
| 101 | maximized, |
| 102 | enabled: widthTransition.open !== open, |
| 103 | }); |
| 104 | } |
| 105 | const suppressWidthTransition = collapsible && !widthTransition.enabled; |
| 106 | const attachHost = (host: HTMLDivElement) => { |
| 107 | hostRef.current = host; |
| 108 | const stopObservingRow = observeRow(host); |
| 109 | return () => { |
| 110 | hostRef.current = null; |
| 111 | stopObservingRow(); |
| 112 | }; |
| 113 | }; |
| 114 | // React focus events, unlike a listener added in attachHost, also see content that focuses |
| 115 | // itself while mounting (a terminal does, from a ref that attaches before the host's). |
| 116 | // Focus moving within the panel, portalled menus included, first blurs an element of it. |
| 117 | const rememberFocusOrigin = (event: FocusEvent<HTMLDivElement>) => { |
| 118 | const from = event.relatedTarget; |
| 119 | if (from === null || from === lastBlurred.current) return; |
| 120 | focusOrigin.current = from instanceof HTMLElement ? from : null; |
| 121 | }; |
| 122 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- on unmount, returns DOM focus to where it came from if removal dropped it on the body |
| 123 | useLayoutEffect(() => { |
| 124 | const host = hostRef.current; |
| 125 | if (!host || !collapsible) return; |
| 126 | return () => { |
| 127 | if (!host.contains(host.ownerDocument.activeElement) && !returnedFocus.current) return; |
| 128 | const origin = focusOrigin.current; |
| 129 | // Unmount moves the toggle; restore focus at its committed location if removal sent focus to body. |
| 130 | queueMicrotask(() => { |
| 131 | if (host.ownerDocument.activeElement === host.ownerDocument.body) { |
| 132 | returnFocus(host, origin); |
| 133 | } |
| 134 | }); |
| 135 | }; |
| 136 | }, [collapsible, hostRef]); |
| 137 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- moves focus out of the host and sets inert on it before a collapse paints |
| 138 | useLayoutEffect(() => { |
| 139 | const host = hostRef.current; |
| 140 | if (!host) return; |
| 141 | const collapsed = collapsible && !open; |
| 142 | if (!collapsed) returnedFocus.current = false; |
| 143 | // Move focus before making its current owner inert, while the toggle is reachable. |
| 144 | if (collapsed && host.contains(host.ownerDocument.activeElement)) { |
| 145 | returnedFocus.current = returnFocus(host, focusOrigin.current); |
| 146 | } |
| 147 | // An origin belongs to one open; the next one records its own. |
| 148 | if (collapsed) focusOrigin.current = null; |
| 149 | host.toggleAttribute("inert", collapsed); |
| 150 | }, [collapsible, open, hostRef]); |
| 151 | return ( |
| 152 | <div |
| 153 | ref={attachHost} |
| 154 | onFocus={rememberFocusOrigin} |
| 155 | onBlur={(event) => { |
| 156 | lastBlurred.current = event.target; |
| 157 | }} |
| 158 | {...(props.workspaceArea ? panelArea : {})} |
| 159 | data-workspace-focus-key={props.workspaceFocusKey} |
| 160 | data-workspace-unavailable={collapsible && !open ? "true" : undefined} |
| 161 | onMouseDown={props.onMouseDown} |
| 162 | onMouseUp={props.onMouseUp} |
| 163 | className={cn( |
| 164 | "relative flex h-full min-h-0 min-w-0 max-w-full flex-col self-stretch bg-background", |
| 165 | isInline |
| 166 | ? maximized |
| 167 | ? // Maximizing meets the sidebar border or window edge; omit the duplicate border. |
| 168 | "flex-1" |
| 169 | : "shrink-0 border-l border-border" |
| 170 | : "w-full", |
| 171 | collapsible && |
| 172 | "[[data-panel-animations=true]_&]:transition-[width] [[data-panel-animations=true]_&]:duration-(--panel-animation-duration) [[data-panel-animations=true]_&]:ease-out", |
| 173 | collapsible && open && "[[data-panel-animations=true]_&]:starting:w-0!", |
| 174 | collapsible && !open && "pointer-events-none", |
| 175 | )} |
| 176 | style={ |
| 177 | isInline |
| 178 | ? ({ |
| 179 | [RESIZABLE_WIDTH_PROPERTY]: `${width}px`, |
| 180 | width: maximized |
| 181 | ? "100%" |
| 182 | : collapsible && !open |
| 183 | ? "0px" |
| 184 | : `var(${RESIZABLE_WIDTH_PROPERTY})`, |
| 185 | transitionDuration: suppressWidthTransition ? "0ms" : undefined, |
| 186 | } as CSSProperties) |
| 187 | : undefined |
| 188 | } |
| 189 | data-preview-panel-mode={props.mode} |
| 190 | data-preview-panel-maximized={maximized ? "true" : "false"} |
| 191 | > |
| 192 | {isInline && !maximized ? <RightPanelResizeHandle handlers={handlers} /> : null} |
| 193 | {/* Content stays pinned to the right edge, so closing uncovers the chat |
| 194 | instead of sliding the tab bar under the title bar controls. */} |
| 195 | <div |
| 196 | className={cn( |
| 197 | "h-full min-h-0 w-full", |
| 198 | collapsible && "overflow-clip", |
| 199 | collapsible && !maximized && "flex justify-end", |
| 200 | )} |
| 201 | > |
| 202 | <div |
| 203 | className={cn( |
| 204 | "flex h-full min-h-0 min-w-0 flex-col", |
| 205 | collapsible && !maximized && "shrink-0", |
| 206 | )} |
| 207 | style={ |
| 208 | collapsible && !maximized |
| 209 | ? { width: `calc(var(${RESIZABLE_WIDTH_PROPERTY}) - 1px)` } |
| 210 | : undefined |
| 211 | } |
| 212 | > |
| 213 | {props.children} |
| 214 | </div> |
| 215 | </div> |
| 216 | </div> |
| 217 | ); |
| 218 | } |