Skip to content

Commit d7c8378

Browse files
shivamhwpclaude
andcommitted
fix(mobile): render the Android picker in a layer that moves with the composer
The overlay portal sat outside the keyboard transform, so the picker had to be re-measured on keyboard events and landed in the wrong place. Render it into a full-size host inside the composer's KeyboardStickyView instead, which moves with the keyboard and still covers the picker so Android delivers its scrolls. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 80e96b6 commit d7c8378

3 files changed

Lines changed: 312 additions & 307 deletions

File tree

Lines changed: 47 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,69 @@
1-
import { useIsFocused } from "@react-navigation/native";
21
import type { ReactNode } from "react";
3-
import { useCallback, useEffect, useRef, useState } from "react";
2+
import { useEffect, useRef, useState } from "react";
43
import { View, type ViewInstance } from "react-native";
5-
import { KeyboardEvents } from "react-native-keyboard-controller";
64

7-
import { OverlayPortal } from "../../components/OverlayPortal";
5+
import { useComposerPopoverHost } from "./ComposerPopoverHost";
86

97
const COMPOSER_GAP = 8;
108

11-
type Frame = {
12-
readonly x: number;
13-
readonly y: number;
14-
readonly width: number;
15-
readonly height: number;
16-
};
17-
18-
function sameFrame(a: Frame | null, b: Frame) {
19-
return a !== null && a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height;
20-
}
9+
type Frame = { readonly x: number; readonly y: number; readonly width: number };
2110

2211
/**
2312
* Places a composer popover just above the composer. Android only delivers
2413
* drags to views inside their parent's bounds, so a popover hung above the
2514
* composer with `bottom-full` takes taps but its list never scrolls. Render it
26-
* 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.
15+
* into ComposerPopoverHost instead, which shares the composer's keyboard
16+
* transform, so only the composer's resting layout needs measuring.
2917
*/
3018
export function ComposerPopoverAnchor(props: { readonly children: ReactNode }) {
19+
const host = useComposerPopoverHost();
3120
const anchorRef = useRef<ViewInstance>(null);
32-
const overlayRef = useRef<ViewInstance>(null);
33-
const [anchor, setAnchor] = useState<Frame | null>(null);
34-
const [overlay, setOverlay] = useState<Frame | null>(null);
35-
const isFocused = useIsFocused();
36-
37-
const measureAnchor = useCallback(() => {
38-
anchorRef.current?.measureInWindow((x, y, width, height) => {
39-
const next = { x, y, width, height };
40-
setAnchor((current) => (sameFrame(current, next) ? current : next));
41-
});
42-
}, []);
21+
const [frame, setFrame] = useState<Frame | null>(null);
4322

44-
// The overlay remounts when the screen regains focus, and the composer may
45-
// have moved while it was away, so measure both.
46-
const measureOverlay = useCallback(() => {
47-
measureAnchor();
48-
overlayRef.current?.measureInWindow((x, y, width, height) => {
49-
const next = { x, y, width, height };
50-
setOverlay((current) => (sameFrame(current, next) ? current : next));
23+
const measure = () => {
24+
const hostView = host?.hostRef.current;
25+
if (!hostView) {
26+
return;
27+
}
28+
anchorRef.current?.measureLayout(hostView, (x, y, width) => {
29+
setFrame((current) =>
30+
current?.x === x && current.y === y && current.width === width ? current : { x, y, width },
31+
);
5132
});
52-
}, [measureAnchor]);
33+
};
5334

54-
// The composer moves without resizing when the keyboard opens or closes.
55-
// Measure once the keyboard has settled, not while it animates.
35+
// No dependency array: the composer re-renders when its padding or expanded
36+
// state changes, which moves it without resizing the anchor, so measure and
37+
// re-project after every render.
5638
useEffect(() => {
57-
const subscriptions = [
58-
KeyboardEvents.addListener("keyboardDidShow", measureAnchor),
59-
KeyboardEvents.addListener("keyboardDidHide", measureAnchor),
60-
];
61-
return () => {
62-
for (const subscription of subscriptions) {
63-
subscription.remove();
64-
}
65-
};
66-
}, [measureAnchor]);
39+
measure();
40+
host?.setContent(
41+
frame === null ? null : (
42+
<View
43+
pointerEvents="box-none"
44+
className="absolute top-0 justify-end"
45+
style={{ left: frame.x, width: frame.width, height: frame.y - COMPOSER_GAP }}
46+
>
47+
{props.children}
48+
</View>
49+
),
50+
);
51+
});
52+
53+
useEffect(() => () => host?.setContent(null), [host]);
54+
55+
if (host === null) {
56+
return <View className="absolute inset-x-0 bottom-full z-10 mb-2">{props.children}</View>;
57+
}
6758

59+
// Covers the composer, so its onLayout fires whenever the composer resizes.
6860
return (
69-
<>
70-
{/* Covers the composer so its onLayout fires when the composer grows. */}
71-
<View
72-
ref={anchorRef}
73-
collapsable={false}
74-
pointerEvents="none"
75-
className="absolute inset-0"
76-
onLayout={measureAnchor}
77-
/>
78-
{!isFocused ? null : (
79-
<OverlayPortal>
80-
<View
81-
ref={overlayRef}
82-
collapsable={false}
83-
pointerEvents="box-none"
84-
className="absolute inset-0"
85-
onLayout={measureOverlay}
86-
>
87-
{anchor === null || overlay === null ? null : (
88-
<View
89-
className="absolute"
90-
style={{
91-
left: anchor.x - overlay.x,
92-
width: anchor.width,
93-
bottom: overlay.height - (anchor.y - overlay.y) + COMPOSER_GAP,
94-
}}
95-
>
96-
{props.children}
97-
</View>
98-
)}
99-
</View>
100-
</OverlayPortal>
101-
)}
102-
</>
61+
<View
62+
ref={anchorRef}
63+
collapsable={false}
64+
pointerEvents="none"
65+
className="absolute inset-0"
66+
onLayout={measure}
67+
/>
10368
);
10469
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { ReactNode, RefObject } from "react";
2+
import { createContext, useContext, useMemo, useRef, useState } from "react";
3+
import { View, type ViewInstance } from "react-native";
4+
5+
type ComposerPopoverHostValue = {
6+
readonly hostRef: RefObject<ViewInstance | null>;
7+
readonly setContent: (content: ReactNode) => void;
8+
};
9+
10+
const ComposerPopoverHostContext = createContext<ComposerPopoverHostValue | null>(null);
11+
12+
export function useComposerPopoverHost() {
13+
return useContext(ComposerPopoverHostContext);
14+
}
15+
16+
/**
17+
* Full-size layer around the composer for its popovers. Mount it inside the
18+
* composer's KeyboardStickyView: popovers placed here move with the composer
19+
* and sit inside a parent that covers them, so Android delivers their scroll
20+
* gestures. It wraps the composer because measureLayout needs an ancestor.
21+
*/
22+
export function ComposerPopoverHost(props: { readonly children: ReactNode }) {
23+
const hostRef = useRef<ViewInstance>(null);
24+
const [content, setContent] = useState<ReactNode>(null);
25+
const value = useMemo(() => ({ hostRef, setContent }), []);
26+
27+
return (
28+
<ComposerPopoverHostContext.Provider value={value}>
29+
<View ref={hostRef} collapsable={false} pointerEvents="box-none" className="absolute inset-0">
30+
{props.children}
31+
{content}
32+
</View>
33+
</ComposerPopoverHostContext.Provider>
34+
);
35+
}

0 commit comments

Comments
 (0)