The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { type ReactNode, useLayoutEffect, useState } from "react"; |
| 2 | import { createPortal } from "react-dom"; |
| 3 | import { observeResize } from "~/lib/observeResize"; |
| 4 | |
| 5 | type ComposerCommandMenuPosition = { |
| 6 | bottom: number; |
| 7 | left: number; |
| 8 | maxHeight: number; |
| 9 | width: number; |
| 10 | }; |
| 11 | |
| 12 | function composerCommandMenuPositionsEqual( |
| 13 | a: ComposerCommandMenuPosition, |
| 14 | b: ComposerCommandMenuPosition, |
| 15 | ): boolean { |
| 16 | return ( |
| 17 | a.bottom === b.bottom && a.left === b.left && a.maxHeight === b.maxHeight && a.width === b.width |
| 18 | ); |
| 19 | } |
| 20 | |
| 21 | export function ComposerCommandMenuLayer(props: { |
| 22 | anchor: HTMLElement | null; |
| 23 | children: ReactNode; |
| 24 | /** Keeps the 96px floor past the window top: the stash then loses its header, not its entries. */ |
| 25 | canOverflowTop?: boolean; |
| 26 | }) { |
| 27 | const { anchor, canOverflowTop = false } = props; |
| 28 | const [position, setPosition] = useState<ComposerCommandMenuPosition | null>(null); |
| 29 | |
| 30 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- positions the menu from the composer's DOM rect, tracking window resize, scroll and ancestor resizes |
| 31 | useLayoutEffect(() => { |
| 32 | if (!anchor) return; |
| 33 | |
| 34 | const updatePosition = () => { |
| 35 | const form = anchor.closest<HTMLElement>('[data-chat-composer-form="true"]'); |
| 36 | const mainSurface = form?.querySelector<HTMLElement>( |
| 37 | '[data-chat-composer-main-surface="true"]', |
| 38 | ); |
| 39 | const rect = (mainSurface ?? form ?? anchor).getBoundingClientRect(); |
| 40 | const rootFontSizePx = |
| 41 | Number.parseFloat(window.getComputedStyle(document.documentElement).fontSize) || 16; |
| 42 | const drawerInsetRem = Number.parseFloat( |
| 43 | window.getComputedStyle(form ?? anchor).getPropertyValue("--chat-composer-drawer-inset"), |
| 44 | ); |
| 45 | const drawerInset = drawerInsetRem * rootFontSizePx; |
| 46 | const width = rect.width - drawerInset * 2; |
| 47 | if (width <= 0) { |
| 48 | setPosition(null); |
| 49 | return; |
| 50 | } |
| 51 | const composerOverlap = rootFontSizePx + 1; |
| 52 | const spaceAbove = rect.top + composerOverlap; |
| 53 | const bottom = window.innerHeight - spaceAbove; |
| 54 | const floorHeight = Math.max(96, spaceAbove - 24); |
| 55 | const maxHeight = canOverflowTop ? floorHeight : Math.min(floorHeight, window.innerHeight); |
| 56 | const next = { |
| 57 | // With too little room above, the menu slides down over the composer instead of past the window top. |
| 58 | bottom: canOverflowTop ? bottom : Math.min(bottom, window.innerHeight - maxHeight), |
| 59 | left: rect.left + drawerInset, |
| 60 | maxHeight, |
| 61 | width, |
| 62 | }; |
| 63 | setPosition((current) => |
| 64 | current && composerCommandMenuPositionsEqual(current, next) ? current : next, |
| 65 | ); |
| 66 | }; |
| 67 | |
| 68 | updatePosition(); |
| 69 | window.addEventListener("resize", updatePosition); |
| 70 | window.addEventListener("scroll", updatePosition, true); |
| 71 | |
| 72 | const observed: Element[] = [anchor]; |
| 73 | for (let element = anchor.parentElement; element; element = element.parentElement) { |
| 74 | observed.push(element); |
| 75 | } |
| 76 | const stopObserving = observeResize(observed, updatePosition); |
| 77 | |
| 78 | return () => { |
| 79 | stopObserving(); |
| 80 | window.removeEventListener("resize", updatePosition); |
| 81 | window.removeEventListener("scroll", updatePosition, true); |
| 82 | }; |
| 83 | }, [anchor, canOverflowTop]); |
| 84 | |
| 85 | if (!anchor || !position) return null; |
| 86 | |
| 87 | return createPortal( |
| 88 | <div |
| 89 | className="pointer-events-auto fixed z-40 flex flex-col" |
| 90 | data-composer-drawer-layer="true" |
| 91 | style={{ |
| 92 | bottom: position.bottom, |
| 93 | left: position.left, |
| 94 | maxHeight: position.maxHeight, |
| 95 | width: position.width, |
| 96 | }} |
| 97 | > |
| 98 | {props.children} |
| 99 | </div>, |
| 100 | document.body, |
| 101 | ); |
| 102 | } |