The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useComposerHandleContext } from "../../composerHandleContext"; |
| 2 | import { useMediaQuery } from "../../hooks/useMediaQuery"; |
| 3 | |
| 4 | const COMPOSER_FLOATING_LAYER_SELECTOR = [ |
| 5 | '[data-composer-drawer-layer="true"]', |
| 6 | '[data-chat-composer-floating-layer="true"]', |
| 7 | ].join(","); |
| 8 | |
| 9 | export const composerFloatingLayerProps = { |
| 10 | "data-chat-composer-floating-layer": "true", |
| 11 | } as const; |
| 12 | |
| 13 | export function useComposerMenuProps() { |
| 14 | const composerRef = useComposerHandleContext(); |
| 15 | const isCoarsePointer = useMediaQuery({ pointer: "coarse" }); |
| 16 | |
| 17 | return { |
| 18 | ...composerFloatingLayerProps, |
| 19 | // Focusing the editor on a touch device opens the on-screen keyboard, so the menu keeps its own focus return. |
| 20 | finalFocus: |
| 21 | composerRef && !isCoarsePointer |
| 22 | ? () => { |
| 23 | const activeElement = document.activeElement; |
| 24 | if (activeElement !== document.body && !isInsideComposerFloatingLayer(activeElement)) { |
| 25 | return false; |
| 26 | } |
| 27 | composerRef.current?.focusAtEnd(); |
| 28 | return false; |
| 29 | } |
| 30 | : undefined, |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | function isInsideComposerFloatingLayer(target: EventTarget | null): boolean { |
| 35 | return target instanceof Element && target.closest(COMPOSER_FLOATING_LAYER_SELECTOR) !== null; |
| 36 | } |
| 37 | |
| 38 | export function isInsideCollapsedComposerControls(target: EventTarget | null): boolean { |
| 39 | return ( |
| 40 | target instanceof Element && |
| 41 | target.closest('[data-chat-composer-collapsed-controls="true"]') !== null |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | export function isInsideRestingComposerControlScope(target: EventTarget | null): boolean { |
| 46 | return ( |
| 47 | target instanceof Element && |
| 48 | (target.closest('[data-chat-composer-resting-controls="true"]') !== null || |
| 49 | target.closest('[data-chat-composer-resting-images="true"]') !== null || |
| 50 | target.closest("[data-composer-context-control]") !== null || |
| 51 | isInsideComposerFloatingLayer(target)) |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * composer.mode marks the trigger it opens; the menu's onOpenChangeComplete then moves focus |
| 57 | * to the checked permission in that menu's own popup, after the menu placed its own focus. |
| 58 | */ |
| 59 | export function markOpenToPermissions(trigger: HTMLElement): void { |
| 60 | trigger.dataset.openToPermissions = "true"; |
| 61 | } |
| 62 | |
| 63 | export function focusRequestedPermission(open: boolean, trigger: HTMLElement | null): void { |
| 64 | if (!trigger?.dataset.openToPermissions) return; |
| 65 | delete trigger.dataset.openToPermissions; |
| 66 | if (open) focusCheckedPermission(trigger); |
| 67 | } |
| 68 | |
| 69 | /** Focuses the checked permission in the menu this trigger opened. */ |
| 70 | export function focusCheckedPermission(trigger: HTMLElement | null): void { |
| 71 | const popupId = trigger?.getAttribute("aria-controls"); |
| 72 | const popup = popupId ? document.getElementById(popupId) : null; |
| 73 | popup |
| 74 | ?.querySelector<HTMLElement>("[data-composer-permissions] [aria-checked='true']") |
| 75 | ?.focus({ preventScroll: true }); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * composer.effort marks the model picker trigger it opens; the picker then moves focus to the |
| 80 | * selected effort level once its popup is open. |
| 81 | */ |
| 82 | export function markOpenToEffort(trigger: HTMLElement): void { |
| 83 | trigger.dataset.openToEffort = "true"; |
| 84 | } |
| 85 | |
| 86 | /** Reads and clears an open-to mark: effort from composer.effort, permissions from composer.mode. */ |
| 87 | export function takeOpenTarget(trigger: HTMLElement | null): "effort" | "permissions" | null { |
| 88 | if (trigger?.dataset.openToEffort) { |
| 89 | delete trigger.dataset.openToEffort; |
| 90 | return "effort"; |
| 91 | } |
| 92 | if (trigger?.dataset.openToPermissions) { |
| 93 | delete trigger.dataset.openToPermissions; |
| 94 | return "permissions"; |
| 95 | } |
| 96 | return null; |
| 97 | } |