Open History to see the last commit
1import type { ReactNode } from "react";
2import { useEffect, useRef, useState } from "react";
3import { View, type ViewInstance } from "react-native";
4
5import { useComposerPopoverHost } from "./ComposerPopoverHost";
6
7const COMPOSER_GAP = 8;
8
9type Frame = { readonly x: number; readonly y: number; readonly width: number };
10
11/**
12 * Android only delivers drags to views inside their parent's bounds, so a popover hung above the
13 * composer with `bottom-full` takes taps but its list never scrolls.
14 */
15export function ComposerPopoverAnchor(props: { readonly children: ReactNode }) {
16 const host = useComposerPopoverHost();
17 const anchorRef = useRef<ViewInstance>(null);
18 const [frame, setFrame] = useState<Frame | null>(null);
19
20 const measure = () => {
21 const hostView = host?.hostRef.current;
22 if (!hostView) {
23 return;
24 }
25 anchorRef.current?.measureLayout(hostView, (x, y, width) => {
26 setFrame((current) =>
27 current?.x === x && current.y === y && current.width === width ? current : { x, y, width },
28 );
29 });
30 };
31
32 // No dependency array: the composer re-renders when its padding or expanded
33 // state changes, which moves it without resizing the anchor, so measure and
34 // re-project after every render.
35 useEffect(() => {
36 measure();
37 host?.setContent(
38 frame === null ? null : (
39 <View
40 pointerEvents="box-none"
41 className="absolute top-0 justify-end"
42 style={{ left: frame.x, width: frame.width, height: frame.y - COMPOSER_GAP }}
43 >
44 {props.children}
45 </View>
46 ),
47 );
48 });
49
50 const setHostContent = host?.setContent;
51 useEffect(() => () => setHostContent?.(null), [setHostContent]);
52
53 if (host === null) {
54 return <View className="absolute inset-x-0 bottom-full z-10 mb-2">{props.children}</View>;
55 }
56
57 return (
58 <View
59 ref={anchorRef}
60 collapsable={false}
61 pointerEvents="none"
62 className="absolute inset-0"
63 onLayout={measure}
64 />
65 );
66}