|
1 | | -import { useIsFocused } from "@react-navigation/native"; |
2 | 1 | import type { ReactNode } from "react"; |
3 | | -import { useCallback, useEffect, useRef, useState } from "react"; |
| 2 | +import { useEffect, useRef, useState } from "react"; |
4 | 3 | import { View, type ViewInstance } from "react-native"; |
5 | | -import { KeyboardEvents } from "react-native-keyboard-controller"; |
6 | 4 |
|
7 | | -import { OverlayPortal } from "../../components/OverlayPortal"; |
| 5 | +import { useComposerPopoverHost } from "./ComposerPopoverHost"; |
8 | 6 |
|
9 | 7 | const COMPOSER_GAP = 8; |
10 | 8 |
|
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 }; |
21 | 10 |
|
22 | 11 | /** |
23 | 12 | * Places a composer popover just above the composer. Android only delivers |
24 | 13 | * drags to views inside their parent's bounds, so a popover hung above the |
25 | 14 | * 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. |
29 | 17 | */ |
30 | 18 | export function ComposerPopoverAnchor(props: { readonly children: ReactNode }) { |
| 19 | + const host = useComposerPopoverHost(); |
31 | 20 | 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); |
43 | 22 |
|
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 | + ); |
51 | 32 | }); |
52 | | - }, [measureAnchor]); |
| 33 | + }; |
53 | 34 |
|
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. |
56 | 38 | 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 | + } |
67 | 58 |
|
| 59 | + // Covers the composer, so its onLayout fires whenever the composer resizes. |
68 | 60 | 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 | + /> |
103 | 68 | ); |
104 | 69 | } |
0 commit comments