|
1 | 1 | import type { MenuAction, MenuComponentProps } from "@react-native-menu/menu"; |
2 | 2 | import type { ReactNode } from "react"; |
3 | | -import { use, useCallback, useEffect, useRef, useState } from "react"; |
| 3 | +import { use, useRef, useState } from "react"; |
4 | 4 | import type { StyleProp, ViewInstance, ViewStyle } from "react-native"; |
5 | | -import { BackHandler, Pressable, ScrollView, View } from "react-native"; |
| 5 | +import { Pressable, ScrollView, View } from "react-native"; |
6 | 6 | import { useKeyboardState } from "react-native-keyboard-controller"; |
7 | 7 | import Animated, { FadeIn } from "react-native-reanimated"; |
8 | 8 |
|
| 9 | +import { useHardwareBack } from "../../lib/backHandler"; |
9 | 10 | import { deriveMenuHorizontalPlacement } from "../../lib/menu-placement"; |
10 | 11 | import { WindowHingeContext } from "../../native/window-hinge-context"; |
11 | 12 | import { OverlayPortal } from "./OverlayPortal"; |
@@ -47,46 +48,43 @@ export function AndroidAnchoredMenu(props: AndroidAnchoredMenuProps) { |
47 | 48 | const [rootHeight, setRootHeight] = useState<number | null>(null); |
48 | 49 | const [overlay, setOverlay] = useState<OverlayFrame | null>(null); |
49 | 50 | const windowHinge = use(WindowHingeContext); |
50 | | - const anchorRef = useRef<ViewInstance>(null); |
| 51 | + // State, not a ref: `open` reaches render-prop children, and the compiler |
| 52 | + // bails out of the whole component when a ref might be read during render. |
| 53 | + const [anchorNode, setAnchorNode] = useState<ViewInstance | null>(null); |
51 | 54 | const overlayRef = useRef<ViewInstance>(null); |
52 | 55 |
|
53 | 56 | const keyboardVisible = useKeyboardState((state) => state.isVisible); |
54 | 57 | const keyboardHeight = useKeyboardState((state) => state.height); |
55 | | - const close = useCallback(() => { |
| 58 | + const close = () => { |
56 | 59 | setAnchor(null); |
57 | 60 | setPath([]); |
58 | 61 | setOverlay(null); |
59 | 62 | setRootHeight(null); |
60 | | - }, []); |
| 63 | + }; |
61 | 64 |
|
62 | | - const open = useCallback(() => { |
63 | | - anchorRef.current?.measureInWindow((x, y, width, height) => { |
| 65 | + const open = () => { |
| 66 | + anchorNode?.measureInWindow((x, y, width, height) => { |
64 | 67 | setAnchor({ x, y, width, height, keyboardWasVisible: keyboardVisible }); |
65 | 68 | }); |
66 | | - }, [keyboardVisible]); |
| 69 | + }; |
67 | 70 |
|
68 | | - const measureOverlay = useCallback(() => { |
| 71 | + const measureOverlay = () => { |
69 | 72 | overlayRef.current?.measureInWindow((x, y, width, height) => { |
70 | 73 | setOverlay({ x, y, width, height }); |
71 | 74 | setRootHeight(height); |
72 | 75 | }); |
73 | | - }, []); |
| 76 | + }; |
74 | 77 |
|
75 | | - const submenuDepth = path.length; |
76 | | - useEffect(() => { |
77 | | - if (anchor === null || !anchor.keyboardWasVisible) { |
78 | | - return; |
| 78 | + // Only the in-window (keyboard) presentation needs this; the native popup |
| 79 | + // handles back itself. |
| 80 | + useHardwareBack(anchor?.keyboardWasVisible === true, () => { |
| 81 | + if (path.length > 0) { |
| 82 | + setPath((current) => current.slice(0, -1)); |
| 83 | + } else { |
| 84 | + close(); |
79 | 85 | } |
80 | | - const subscription = BackHandler.addEventListener("hardwareBackPress", () => { |
81 | | - if (submenuDepth > 0) { |
82 | | - setPath((current) => current.slice(0, -1)); |
83 | | - } else { |
84 | | - close(); |
85 | | - } |
86 | | - return true; |
87 | | - }); |
88 | | - return () => subscription.remove(); |
89 | | - }, [anchor, close, submenuDepth]); |
| 86 | + return true; |
| 87 | + }); |
90 | 88 |
|
91 | 89 | const parent = path[path.length - 1] ?? null; |
92 | 90 | const levelActions = (parent?.subactions ?? props.actions).filter( |
@@ -125,31 +123,33 @@ export function AndroidAnchoredMenu(props: AndroidAnchoredMenuProps) { |
125 | 123 | const maxHeight = Math.min(opensDown ? spaceBelow : spaceAbove, 480); |
126 | 124 | const placeable = local !== null && rootHeight !== null; |
127 | 125 |
|
128 | | - const onPressItem = useCallback( |
129 | | - (action: MenuAction) => { |
130 | | - if ((action.subactions?.length ?? 0) > 0) { |
131 | | - setPath((current) => [...current, action]); |
132 | | - return; |
133 | | - } |
134 | | - close(); |
135 | | - if (action.id !== undefined) { |
136 | | - props.onPressAction?.({ |
137 | | - nativeEvent: { event: action.id }, |
138 | | - } as Parameters<NonNullable<MenuComponentProps["onPressAction"]>>[0]); |
139 | | - } |
140 | | - }, |
141 | | - [close, props.onPressAction], |
142 | | - ); |
| 126 | + const onPressItem = (action: MenuAction) => { |
| 127 | + if ((action.subactions?.length ?? 0) > 0) { |
| 128 | + setPath((current) => [...current, action]); |
| 129 | + return; |
| 130 | + } |
| 131 | + close(); |
| 132 | + if (action.id !== undefined) { |
| 133 | + props.onPressAction?.({ |
| 134 | + nativeEvent: { event: action.id }, |
| 135 | + } as Parameters<NonNullable<MenuComponentProps["onPressAction"]>>[0]); |
| 136 | + } |
| 137 | + }; |
143 | 138 |
|
144 | 139 | return ( |
145 | 140 | <> |
146 | 141 | {typeof props.children === "function" ? ( |
147 | | - <View ref={anchorRef} collapsable={false} className={props.className} style={props.style}> |
| 142 | + <View |
| 143 | + ref={setAnchorNode} |
| 144 | + collapsable={false} |
| 145 | + className={props.className} |
| 146 | + style={props.style} |
| 147 | + > |
148 | 148 | {props.children(open)} |
149 | 149 | </View> |
150 | 150 | ) : ( |
151 | 151 | <Pressable |
152 | | - ref={anchorRef} |
| 152 | + ref={setAnchorNode} |
153 | 153 | accessibilityRole="button" |
154 | 154 | className={props.className} |
155 | 155 | collapsable={false} |
|
0 commit comments