The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useState } from "react"; |
| 2 | import { observeResize } from "../../lib/observeResize"; |
| 3 | |
| 4 | function measureComposerMultilinePrompt(body: HTMLElement): boolean | null { |
| 5 | const editor = body.querySelector<HTMLElement>('[data-testid="composer-editor"]'); |
| 6 | if (!editor || editor.clientWidth === 0) return null; |
| 7 | |
| 8 | const bodyStyle = getComputedStyle(body); |
| 9 | const expandedWidth = |
| 10 | body.clientWidth - |
| 11 | Number.parseFloat(bodyStyle.paddingLeft) - |
| 12 | Number.parseFloat(bodyStyle.paddingRight); |
| 13 | const lineHeight = Number.parseFloat(getComputedStyle(editor).lineHeight); |
| 14 | const range = document.createRange(); |
| 15 | range.selectNodeContents(editor); |
| 16 | const bounds = range.getBoundingClientRect(); |
| 17 | |
| 18 | // Measure against expanded width to prevent inline actions oscillating collapse. |
| 19 | return bounds.height > lineHeight + 1 || bounds.width > expandedWidth + 1; |
| 20 | } |
| 21 | |
| 22 | /** Whether the prompt wraps, and the ref callback that measures it on the composer body. */ |
| 23 | export function useComposerMultilinePrompt(): readonly [ |
| 24 | boolean, |
| 25 | (body: HTMLElement | null) => (() => void) | undefined, |
| 26 | ] { |
| 27 | const [isMultiline, setIsMultiline] = useState(false); |
| 28 | const observeBody = (body: HTMLElement | null) => { |
| 29 | if (!body) return; |
| 30 | // Measuring reads layout, so one measurement per frame runs after all of the frame's DOM |
| 31 | // writes instead of forcing a layout after each mutation. |
| 32 | let frame: number | null = null; |
| 33 | const measure = () => { |
| 34 | if (frame !== null) return; |
| 35 | frame = requestAnimationFrame(() => { |
| 36 | frame = null; |
| 37 | const next = measureComposerMultilinePrompt(body); |
| 38 | if (next !== null) setIsMultiline(next); |
| 39 | }); |
| 40 | }; |
| 41 | measure(); |
| 42 | const editor = body.querySelector<HTMLElement>('[data-testid="composer-editor"]'); |
| 43 | const stopObserving = observeResize(editor ? [body, editor] : body, measure); |
| 44 | const mutationObserver = new MutationObserver(measure); |
| 45 | mutationObserver.observe(body, { childList: true, characterData: true, subtree: true }); |
| 46 | return () => { |
| 47 | if (frame !== null) cancelAnimationFrame(frame); |
| 48 | stopObserving(); |
| 49 | mutationObserver.disconnect(); |
| 50 | }; |
| 51 | }; |
| 52 | return [isMultiline, observeBody] as const; |
| 53 | } |