1+ import { useIsFocused } from "@react-navigation/native" ;
12import type { ReactNode } from "react" ;
23import { useCallback , useEffect , useRef , useState } from "react" ;
34import { View , type ViewInstance } from "react-native" ;
4- import { useKeyboardState } from "react-native-keyboard-controller" ;
5+ import { KeyboardEvents } from "react-native-keyboard-controller" ;
56
67import { OverlayPortal } from "../../components/OverlayPortal" ;
78
@@ -23,13 +24,15 @@ function sameFrame(a: Frame | null, b: Frame) {
2324 * drags to views inside their parent's bounds, so a popover hung above the
2425 * composer with `bottom-full` takes taps but its list never scrolls. Render it
2526 * through OverlayPortal instead, positioned from the composer's window frame.
27+ * The portal lives above navigation, so it only shows while this screen has
28+ * focus; the thread screen stays mounted under pushed screens.
2629 */
2730export function ComposerPopoverAnchor ( props : { readonly children : ReactNode } ) {
2831 const anchorRef = useRef < ViewInstance > ( null ) ;
2932 const overlayRef = useRef < ViewInstance > ( null ) ;
3033 const [ anchor , setAnchor ] = useState < Frame | null > ( null ) ;
3134 const [ overlay , setOverlay ] = useState < Frame | null > ( null ) ;
32- const keyboardHeight = useKeyboardState ( ( state ) => state . height ) ;
35+ const isFocused = useIsFocused ( ) ;
3336
3437 const measureAnchor = useCallback ( ( ) => {
3538 anchorRef . current ?. measureInWindow ( ( x , y , width , height ) => {
@@ -46,7 +49,18 @@ export function ComposerPopoverAnchor(props: { readonly children: ReactNode }) {
4649 } , [ ] ) ;
4750
4851 // The composer moves without resizing when the keyboard opens or closes.
49- useEffect ( measureAnchor , [ measureAnchor , keyboardHeight ] ) ;
52+ // Measure once the keyboard has settled, not while it animates.
53+ useEffect ( ( ) => {
54+ const subscriptions = [
55+ KeyboardEvents . addListener ( "keyboardDidShow" , measureAnchor ) ,
56+ KeyboardEvents . addListener ( "keyboardDidHide" , measureAnchor ) ,
57+ ] ;
58+ return ( ) => {
59+ for ( const subscription of subscriptions ) {
60+ subscription . remove ( ) ;
61+ }
62+ } ;
63+ } , [ measureAnchor ] ) ;
5064
5165 return (
5266 < >
@@ -58,28 +72,30 @@ export function ComposerPopoverAnchor(props: { readonly children: ReactNode }) {
5872 className = "absolute inset-0"
5973 onLayout = { measureAnchor }
6074 />
61- < OverlayPortal >
62- < View
63- ref = { overlayRef }
64- collapsable = { false }
65- pointerEvents = "box-none"
66- className = "absolute inset-0"
67- onLayout = { measureOverlay }
68- >
69- { anchor === null || overlay === null ? null : (
70- < View
71- className = "absolute"
72- style = { {
73- left : anchor . x - overlay . x ,
74- width : anchor . width ,
75- bottom : overlay . height - ( anchor . y - overlay . y ) + COMPOSER_GAP ,
76- } }
77- >
78- { props . children }
79- </ View >
80- ) }
81- </ View >
82- </ OverlayPortal >
75+ { ! isFocused ? null : (
76+ < OverlayPortal >
77+ < View
78+ ref = { overlayRef }
79+ collapsable = { false }
80+ pointerEvents = "box-none"
81+ className = "absolute inset-0"
82+ onLayout = { measureOverlay }
83+ >
84+ { anchor === null || overlay === null ? null : (
85+ < View
86+ className = "absolute"
87+ style = { {
88+ left : anchor . x - overlay . x ,
89+ width : anchor . width ,
90+ bottom : overlay . height - ( anchor . y - overlay . y ) + COMPOSER_GAP ,
91+ } }
92+ >
93+ { props . children }
94+ </ View >
95+ ) }
96+ </ View >
97+ </ OverlayPortal >
98+ ) }
8399 </ >
84100 ) ;
85101}
0 commit comments