@@ -6,7 +6,10 @@ import { Pressable, ScrollView, View } from "react-native";
66import { useKeyboardState } from "react-native-keyboard-controller" ;
77
88import { useHardwareBack } from "../../lib/backHandler" ;
9- import { deriveMenuHorizontalPlacement } from "../../lib/menu-placement" ;
9+ import {
10+ deriveMenuHorizontalPlacement ,
11+ deriveMenuVerticalPlacement ,
12+ } from "../../lib/menu-placement" ;
1013import { WindowHingeContext } from "../../native/window-hinge-context" ;
1114import { OverlayPortal } from "./OverlayPortal" ;
1215import { useAndroidControlSizing } from "./useAndroidControlSizing" ;
@@ -41,10 +44,9 @@ export type AndroidAnchoredMenuProps = {
4144} ;
4245
4346export function AndroidAnchoredMenu ( props : AndroidAnchoredMenuProps ) {
44- const { scale, menuWidth : desiredMenuWidth } = useAndroidControlSizing ( ) ;
47+ const { scale, menuWidth : desiredMenuWidth , menuItemHeight } = useAndroidControlSizing ( ) ;
4548 const [ anchor , setAnchor ] = useState < AnchorSnapshot | null > ( null ) ;
4649 const [ path , setPath ] = useState < readonly MenuAction [ ] > ( [ ] ) ;
47- const [ rootHeight , setRootHeight ] = useState < number | null > ( null ) ;
4850 const [ overlay , setOverlay ] = useState < OverlayFrame | null > ( null ) ;
4951 const windowHinge = use ( WindowHingeContext ) ;
5052 // State, not a ref: `open` reaches render-prop children, and the compiler
@@ -58,7 +60,6 @@ export function AndroidAnchoredMenu(props: AndroidAnchoredMenuProps) {
5860 setAnchor ( null ) ;
5961 setPath ( [ ] ) ;
6062 setOverlay ( null ) ;
61- setRootHeight ( null ) ;
6263 } ;
6364
6465 const open = ( ) => {
@@ -70,7 +71,6 @@ export function AndroidAnchoredMenu(props: AndroidAnchoredMenuProps) {
7071 const measureOverlay = ( ) => {
7172 overlayRef . current ?. measureInWindow ( ( x , y , width , height ) => {
7273 setOverlay ( { x, y, width, height } ) ;
73- setRootHeight ( height ) ;
7474 } ) ;
7575 } ;
7676
@@ -110,17 +110,19 @@ export function AndroidAnchoredMenu(props: AndroidAnchoredMenuProps) {
110110 margin : SCREEN_MARGIN ,
111111 hinge : windowHinge && { x : windowHinge . x - overlay . x , width : windowHinge . width } ,
112112 } ) ;
113- // This in-window menu preserves keyboard focus; subtract the IME area so its items remain tappable.
114- const usableBottom =
115- overlay === null ? 0 : overlay . height - ( keyboardVisible ? keyboardHeight : 0 ) ;
116- const spaceBelow =
113+ const verticalPlacement =
117114 local === null || overlay === null
118- ? 0
119- : usableBottom - ( local . y + local . height ) - ANCHOR_GAP - SCREEN_MARGIN ;
120- const spaceAbove = local === null ? 0 : local . y - ANCHOR_GAP - SCREEN_MARGIN ;
121- const opensDown = spaceBelow >= 280 || spaceBelow >= spaceAbove ;
122- const maxHeight = Math . min ( opensDown ? spaceBelow : spaceAbove , 480 ) ;
123- const placeable = local !== null && rootHeight !== null ;
115+ ? null
116+ : deriveMenuVerticalPlacement ( {
117+ anchorY : local . y ,
118+ anchorHeight : local . height ,
119+ overlayHeight : overlay . height ,
120+ // This in-window menu preserves keyboard focus; subtract the IME area so its items remain tappable.
121+ keyboardInset : keyboardVisible ? keyboardHeight : 0 ,
122+ gap : ANCHOR_GAP ,
123+ margin : SCREEN_MARGIN ,
124+ minHeight : 2 * menuItemHeight + 14 * scale ,
125+ } ) ;
124126
125127 const onPressItem = ( action : MenuAction ) => {
126128 if ( ( action . subactions ?. length ?? 0 ) > 0 ) {
@@ -167,7 +169,7 @@ export function AndroidAnchoredMenu(props: AndroidAnchoredMenuProps) {
167169 onLayout = { measureOverlay }
168170 >
169171 < Pressable accessible = { false } className = "absolute inset-0" onPress = { close } />
170- { ! placeable || local === null ? null : ! anchor . keyboardWasVisible ? (
172+ { local === null ? null : ! anchor . keyboardWasVisible ? (
171173 < MenuPopup
172174 // Compose aligns to the trigger's start if it fits; hinge-safe triggers keep menus on their side.
173175 anchor = { windowHinge ? { ...local , x : left , width : menuWidth } : local }
@@ -185,12 +187,9 @@ export function AndroidAnchoredMenu(props: AndroidAnchoredMenuProps) {
185187 className = "absolute overflow-hidden bg-card-alt shadow-md"
186188 style = { {
187189 left,
188- maxHeight,
189190 width : menuWidth ,
190191 borderRadius : 4 * scale ,
191- ...( opensDown
192- ? { top : local . y + local . height + ANCHOR_GAP }
193- : { bottom : ( rootHeight ?? 0 ) - local . y + ANCHOR_GAP } ) ,
192+ ...verticalPlacement ,
194193 } }
195194 >
196195 { /* Compose DropdownMenu takes focus; in-window editor menus preserve the keyboard. */ }
0 commit comments