The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { |
| 2 | CSSProperties, |
| 3 | KeyboardEvent as ReactKeyboardEvent, |
| 4 | PointerEvent as ReactPointerEvent, |
| 5 | } from "react"; |
| 6 | import { useGT } from "gt-react"; |
| 7 | |
| 8 | import { cn } from "~/lib/utils"; |
| 9 | |
| 10 | import { |
| 11 | BROWSER_VIEWPORT_RESIZE_RAIL_SIZE, |
| 12 | type BrowserViewportLayout, |
| 13 | type BrowserViewportResizeDirection, |
| 14 | } from "./browserViewportLayout"; |
| 15 | |
| 16 | interface Props { |
| 17 | readonly layout: BrowserViewportLayout; |
| 18 | readonly activeDirection: BrowserViewportResizeDirection | null; |
| 19 | readonly onPointerDown: ( |
| 20 | direction: BrowserViewportResizeDirection, |
| 21 | event: ReactPointerEvent<HTMLButtonElement>, |
| 22 | ) => void; |
| 23 | readonly onKeyDown: ( |
| 24 | direction: BrowserViewportResizeDirection, |
| 25 | event: ReactKeyboardEvent<HTMLButtonElement>, |
| 26 | ) => void; |
| 27 | } |
| 28 | |
| 29 | type HandleKind = "horizontal" | "vertical" | "corner"; |
| 30 | |
| 31 | const EDGE_BUTTON_CLASS = |
| 32 | "group absolute z-20 touch-none border-0 bg-transparent p-0 outline-none before:absolute before:-inset-1 before:content-[''] focus-visible:bg-foreground/[0.04]"; |
| 33 | const EDGE_GRIP_CLASS = |
| 34 | "pointer-events-none absolute left-1/2 top-1/2 flex -translate-x-1/2 -translate-y-1/2 items-center justify-center text-muted-foreground/55 transition-colors duration-150 group-hover:text-foreground/85 group-focus-visible:text-foreground group-active:text-foreground"; |
| 35 | |
| 36 | function ResizeHandle(props: { |
| 37 | readonly direction: BrowserViewportResizeDirection; |
| 38 | readonly label: string; |
| 39 | readonly kind: HandleKind; |
| 40 | readonly cursorClassName: string; |
| 41 | readonly style: CSSProperties; |
| 42 | readonly active: boolean; |
| 43 | readonly mirrorCorner?: boolean; |
| 44 | readonly onPointerDown: Props["onPointerDown"]; |
| 45 | readonly onKeyDown: Props["onKeyDown"]; |
| 46 | }) { |
| 47 | const { |
| 48 | direction, |
| 49 | label, |
| 50 | kind, |
| 51 | cursorClassName, |
| 52 | style, |
| 53 | active, |
| 54 | mirrorCorner = false, |
| 55 | onPointerDown, |
| 56 | onKeyDown, |
| 57 | } = props; |
| 58 | const gt = useGT(); |
| 59 | return ( |
| 60 | <button |
| 61 | type="button" |
| 62 | aria-label={gt("{label}. Use arrow keys to resize.", { label })} |
| 63 | className={cn(EDGE_BUTTON_CLASS, kind === "corner" && "z-30", cursorClassName)} |
| 64 | style={style} |
| 65 | onPointerDown={(event) => onPointerDown(direction, event)} |
| 66 | onKeyDown={(event) => onKeyDown(direction, event)} |
| 67 | > |
| 68 | <span |
| 69 | className={cn( |
| 70 | EDGE_GRIP_CLASS, |
| 71 | kind === "vertical" && "h-8 w-1.5", |
| 72 | kind === "horizontal" && "h-1.5 w-8", |
| 73 | kind === "corner" && "size-3", |
| 74 | active && "text-foreground", |
| 75 | )} |
| 76 | > |
| 77 | {kind === "vertical" ? ( |
| 78 | <span className="flex gap-px" aria-hidden="true"> |
| 79 | <span className="h-6 w-px rounded-full bg-current" /> |
| 80 | <span className="h-6 w-px rounded-full bg-current" /> |
| 81 | </span> |
| 82 | ) : kind === "horizontal" ? ( |
| 83 | <span className="flex flex-col gap-px" aria-hidden="true"> |
| 84 | <span className="h-px w-6 rounded-full bg-current" /> |
| 85 | <span className="h-px w-6 rounded-full bg-current" /> |
| 86 | </span> |
| 87 | ) : ( |
| 88 | <span |
| 89 | className={cn("relative block size-3", mirrorCorner && "-scale-x-100")} |
| 90 | aria-hidden="true" |
| 91 | > |
| 92 | <span className="absolute bottom-[3px] left-0 h-px w-3 -rotate-45 rounded-full bg-current" /> |
| 93 | <span className="absolute bottom-0 left-[5px] h-px w-2 -rotate-45 rounded-full bg-current" /> |
| 94 | </span> |
| 95 | )} |
| 96 | </span> |
| 97 | </button> |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | export function BrowserViewportResizeHandles({ |
| 102 | layout, |
| 103 | activeDirection, |
| 104 | onPointerDown, |
| 105 | onKeyDown, |
| 106 | }: Props) { |
| 107 | const left = layout.viewportX; |
| 108 | const top = layout.viewportY; |
| 109 | const right = left + layout.viewportWidth; |
| 110 | const bottom = top + layout.viewportHeight; |
| 111 | const railSize = BROWSER_VIEWPORT_RESIZE_RAIL_SIZE; |
| 112 | |
| 113 | const gt = useGT(); |
| 114 | const shared = { activeDirection, onPointerDown, onKeyDown }; |
| 115 | return ( |
| 116 | <> |
| 117 | <ResizeHandle |
| 118 | direction="west" |
| 119 | label={gt("Resize browser viewport from left edge")} |
| 120 | kind="vertical" |
| 121 | cursorClassName="cursor-ew-resize" |
| 122 | style={{ left: left - railSize, top, width: railSize, height: layout.viewportHeight }} |
| 123 | active={shared.activeDirection === "west"} |
| 124 | onPointerDown={shared.onPointerDown} |
| 125 | onKeyDown={shared.onKeyDown} |
| 126 | /> |
| 127 | <ResizeHandle |
| 128 | direction="east" |
| 129 | label={gt("Resize browser viewport from right edge")} |
| 130 | kind="vertical" |
| 131 | cursorClassName="cursor-ew-resize" |
| 132 | style={{ left: right, top, width: railSize, height: layout.viewportHeight }} |
| 133 | active={shared.activeDirection === "east"} |
| 134 | onPointerDown={shared.onPointerDown} |
| 135 | onKeyDown={shared.onKeyDown} |
| 136 | /> |
| 137 | <ResizeHandle |
| 138 | direction="south" |
| 139 | label={gt("Resize browser viewport from bottom edge")} |
| 140 | kind="horizontal" |
| 141 | cursorClassName="cursor-ns-resize" |
| 142 | style={{ left, top: bottom, width: layout.viewportWidth, height: railSize }} |
| 143 | active={shared.activeDirection === "south"} |
| 144 | onPointerDown={shared.onPointerDown} |
| 145 | onKeyDown={shared.onKeyDown} |
| 146 | /> |
| 147 | <ResizeHandle |
| 148 | direction="southwest" |
| 149 | label={gt("Resize browser viewport from bottom-left corner")} |
| 150 | kind="corner" |
| 151 | cursorClassName="cursor-nesw-resize" |
| 152 | style={{ left: left - railSize, top: bottom, width: railSize, height: railSize }} |
| 153 | active={shared.activeDirection === "southwest"} |
| 154 | mirrorCorner |
| 155 | onPointerDown={shared.onPointerDown} |
| 156 | onKeyDown={shared.onKeyDown} |
| 157 | /> |
| 158 | <ResizeHandle |
| 159 | direction="southeast" |
| 160 | label={gt("Resize browser viewport from bottom-right corner")} |
| 161 | kind="corner" |
| 162 | cursorClassName="cursor-nwse-resize" |
| 163 | style={{ left: right, top: bottom, width: railSize, height: railSize }} |
| 164 | active={shared.activeDirection === "southeast"} |
| 165 | onPointerDown={shared.onPointerDown} |
| 166 | onKeyDown={shared.onKeyDown} |
| 167 | /> |
| 168 | </> |
| 169 | ); |
| 170 | } |