The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { MenuAction, MenuComponentProps } from "@react-native-menu/menu"; |
| 2 | import type { ReactNode } from "react"; |
| 3 | import { use, useRef, useState } from "react"; |
| 4 | import type { StyleProp, ViewInstance, ViewStyle } from "react-native"; |
| 5 | import { Pressable, ScrollView, View } from "react-native"; |
| 6 | import { useKeyboardState } from "react-native-keyboard-controller"; |
| 7 | |
| 8 | import { useHardwareBack } from "../../lib/backHandler"; |
| 9 | import { |
| 10 | deriveMenuHorizontalPlacement, |
| 11 | deriveMenuVerticalPlacement, |
| 12 | } from "../../lib/menu-placement"; |
| 13 | import { WindowHingeContext } from "../../native/window-hinge-context"; |
| 14 | import { OverlayPortal } from "./OverlayPortal"; |
| 15 | import { useAndroidControlSizing } from "./useAndroidControlSizing"; |
| 16 | import { MenuPopup } from "./MenuPopup"; |
| 17 | |
| 18 | const SCREEN_MARGIN = 12; |
| 19 | const ANCHOR_GAP = 6; |
| 20 | |
| 21 | // Freeze both snapshots at opening so keyboard and screen reflows cannot flip menu direction. |
| 22 | type AnchorSnapshot = { |
| 23 | readonly x: number; |
| 24 | readonly y: number; |
| 25 | readonly width: number; |
| 26 | readonly height: number; |
| 27 | readonly keyboardWasVisible: boolean; |
| 28 | }; |
| 29 | |
| 30 | type OverlayFrame = { |
| 31 | readonly x: number; |
| 32 | readonly y: number; |
| 33 | readonly width: number; |
| 34 | readonly height: number; |
| 35 | }; |
| 36 | |
| 37 | export type AndroidAnchoredMenuProps = { |
| 38 | readonly actions: readonly MenuAction[]; |
| 39 | readonly title?: string; |
| 40 | readonly onPressAction?: MenuComponentProps["onPressAction"]; |
| 41 | readonly className?: string; |
| 42 | readonly style?: StyleProp<ViewStyle>; |
| 43 | readonly children: ReactNode | ((open: () => void) => ReactNode); |
| 44 | }; |
| 45 | |
| 46 | export function AndroidAnchoredMenu(props: AndroidAnchoredMenuProps) { |
| 47 | const { scale, menuWidth: desiredMenuWidth, menuItemHeight } = useAndroidControlSizing(); |
| 48 | const [anchor, setAnchor] = useState<AnchorSnapshot | null>(null); |
| 49 | const [path, setPath] = useState<readonly MenuAction[]>([]); |
| 50 | const [overlay, setOverlay] = useState<OverlayFrame | null>(null); |
| 51 | const windowHinge = use(WindowHingeContext); |
| 52 | // State, not a ref: `open` reaches render-prop children, and the compiler |
| 53 | // bails out of the whole component when a ref might be read during render. |
| 54 | const [anchorNode, setAnchorNode] = useState<ViewInstance | null>(null); |
| 55 | const overlayRef = useRef<ViewInstance>(null); |
| 56 | |
| 57 | const keyboardVisible = useKeyboardState((state) => state.isVisible); |
| 58 | const keyboardHeight = useKeyboardState((state) => state.height); |
| 59 | const close = () => { |
| 60 | setAnchor(null); |
| 61 | setPath([]); |
| 62 | setOverlay(null); |
| 63 | }; |
| 64 | // The in-window menu is pinned to the trigger's spot above the keyboard, which is stale once it hides. |
| 65 | if (anchor?.keyboardWasVisible === true && !keyboardVisible) { |
| 66 | close(); |
| 67 | } |
| 68 | |
| 69 | const open = () => { |
| 70 | anchorNode?.measureInWindow((x, y, width, height) => { |
| 71 | setAnchor({ x, y, width, height, keyboardWasVisible: keyboardVisible }); |
| 72 | }); |
| 73 | }; |
| 74 | |
| 75 | const measureOverlay = () => { |
| 76 | overlayRef.current?.measureInWindow((x, y, width, height) => { |
| 77 | setOverlay({ x, y, width, height }); |
| 78 | }); |
| 79 | }; |
| 80 | |
| 81 | // Only the in-window (keyboard) presentation needs this; the native popup |
| 82 | // handles back itself. |
| 83 | useHardwareBack(anchor?.keyboardWasVisible === true, () => { |
| 84 | if (path.length > 0) { |
| 85 | setPath((current) => current.slice(0, -1)); |
| 86 | } else { |
| 87 | close(); |
| 88 | } |
| 89 | return true; |
| 90 | }); |
| 91 | |
| 92 | const parent = path[path.length - 1] ?? null; |
| 93 | const levelActions = (parent?.subactions ?? props.actions).filter( |
| 94 | (action) => !(action.attributes?.hidden ?? false), |
| 95 | ); |
| 96 | |
| 97 | const local = |
| 98 | anchor === null || overlay === null |
| 99 | ? null |
| 100 | : { |
| 101 | x: anchor.x - overlay.x, |
| 102 | y: anchor.y - overlay.y, |
| 103 | width: anchor.width, |
| 104 | height: anchor.height, |
| 105 | }; |
| 106 | const { left, width: menuWidth } = |
| 107 | local === null || overlay === null |
| 108 | ? { left: 0, width: desiredMenuWidth } |
| 109 | : deriveMenuHorizontalPlacement({ |
| 110 | anchorX: local.x, |
| 111 | anchorWidth: local.width, |
| 112 | overlayWidth: overlay.width, |
| 113 | desiredWidth: desiredMenuWidth, |
| 114 | margin: SCREEN_MARGIN, |
| 115 | hinge: windowHinge && { x: windowHinge.x - overlay.x, width: windowHinge.width }, |
| 116 | }); |
| 117 | const verticalPlacement = |
| 118 | local === null || overlay === null |
| 119 | ? null |
| 120 | : deriveMenuVerticalPlacement({ |
| 121 | anchorY: local.y, |
| 122 | anchorHeight: local.height, |
| 123 | overlayHeight: overlay.height, |
| 124 | // This in-window menu preserves keyboard focus; subtract the IME area so its items remain tappable. |
| 125 | keyboardInset: keyboardVisible ? keyboardHeight : 0, |
| 126 | gap: ANCHOR_GAP, |
| 127 | margin: SCREEN_MARGIN, |
| 128 | minHeight: 2 * menuItemHeight + 14 * scale, |
| 129 | }); |
| 130 | |
| 131 | const onPressItem = (action: MenuAction) => { |
| 132 | if ((action.subactions?.length ?? 0) > 0) { |
| 133 | setPath((current) => [...current, action]); |
| 134 | return; |
| 135 | } |
| 136 | close(); |
| 137 | if (action.id !== undefined) { |
| 138 | props.onPressAction?.({ |
| 139 | nativeEvent: { event: action.id }, |
| 140 | } as Parameters<NonNullable<MenuComponentProps["onPressAction"]>>[0]); |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | return ( |
| 145 | <> |
| 146 | {typeof props.children === "function" ? ( |
| 147 | <View |
| 148 | ref={setAnchorNode} |
| 149 | collapsable={false} |
| 150 | className={props.className} |
| 151 | style={props.style} |
| 152 | > |
| 153 | {props.children(open)} |
| 154 | </View> |
| 155 | ) : ( |
| 156 | <Pressable |
| 157 | ref={setAnchorNode} |
| 158 | accessibilityRole="button" |
| 159 | className={props.className} |
| 160 | collapsable={false} |
| 161 | style={props.style} |
| 162 | onPress={open} |
| 163 | > |
| 164 | <View pointerEvents="none">{props.children}</View> |
| 165 | </Pressable> |
| 166 | )} |
| 167 | {anchor === null ? null : ( |
| 168 | <OverlayPortal> |
| 169 | <View |
| 170 | ref={overlayRef} |
| 171 | collapsable={false} |
| 172 | className="absolute inset-0" |
| 173 | onLayout={measureOverlay} |
| 174 | > |
| 175 | <Pressable accessible={false} className="absolute inset-0" onPress={close} /> |
| 176 | {local === null ? null : !anchor.keyboardWasVisible ? ( |
| 177 | <MenuPopup |
| 178 | // Compose aligns to the trigger's start if it fits; hinge-safe triggers keep menus on their side. |
| 179 | anchor={windowHinge ? { ...local, x: left, width: menuWidth } : local} |
| 180 | menuWidth={menuWidth} |
| 181 | actions={levelActions} |
| 182 | title={props.title} |
| 183 | parent={parent} |
| 184 | onPress={onPressItem} |
| 185 | onBack={() => setPath((current) => current.slice(0, -1))} |
| 186 | onClose={close} |
| 187 | /> |
| 188 | ) : ( |
| 189 | <View |
| 190 | // No entering animation: it freezes this frame before the Compose host reports its size. |
| 191 | className="absolute overflow-hidden bg-card-alt shadow-md" |
| 192 | style={{ |
| 193 | left, |
| 194 | width: menuWidth, |
| 195 | borderRadius: 4 * scale, |
| 196 | ...verticalPlacement, |
| 197 | }} |
| 198 | > |
| 199 | {/* Compose DropdownMenu takes focus; in-window editor menus preserve the keyboard. */} |
| 200 | |
| 201 | {/* The first item tap must act while the keyboard is open. */} |
| 202 | <ScrollView |
| 203 | contentContainerStyle={{ paddingVertical: 7 * scale }} |
| 204 | bounces={false} |
| 205 | keyboardShouldPersistTaps="always" |
| 206 | showsVerticalScrollIndicator={false} |
| 207 | > |
| 208 | <MenuPopup |
| 209 | inline |
| 210 | anchor={local} |
| 211 | menuWidth={menuWidth} |
| 212 | actions={levelActions} |
| 213 | title={props.title} |
| 214 | parent={parent} |
| 215 | onPress={onPressItem} |
| 216 | onBack={() => setPath((current) => current.slice(0, -1))} |
| 217 | onClose={close} |
| 218 | /> |
| 219 | </ScrollView> |
| 220 | </View> |
| 221 | )} |
| 222 | </View> |
| 223 | </OverlayPortal> |
| 224 | )} |
| 225 | </> |
| 226 | ); |
| 227 | } |