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/** Android only delivers drags inside parent bounds; bottom-full popover lists cannot scroll. */
12export function ComposerPopoverAnchor(props: { readonly children: ReactNode }) {
13 const host = useComposerPopoverHost();
14 const anchorRef = useRef<ViewInstance>(null);
15 const [frame, setFrame] = useState<Frame | null>(null);
16 const popoverHeight = host?.popoverHeight;
17
18 const measure = () => {
19 const hostView = host?.hostRef.current;
20 if (!hostView) {
21 return;
22 }
23 anchorRef.current?.measureLayout(hostView, (x, y, width) => {
24 setFrame((current) =>
25 current?.x === x && current.y === y && current.width === width ? current : { x, y, width },
26 );
27 });
28 };
29
30 // Measure every render: composer padding or expansion can move the anchor without resizing it.
31 useEffect(() => {
32 measure();
33 host?.setContent(
34 frame === null ? null : (
35 <View
36 pointerEvents="box-none"
37 className="absolute top-0 justify-end"
38 style={{ left: frame.x, width: frame.width, height: frame.y - COMPOSER_GAP }}
39 >
40 <View
41 onLayout={(event) => {
42 popoverHeight?.set(event.nativeEvent.layout.height);
43 }}
44 >
45 {props.children}
46 </View>
47 </View>
48 ),
49 );
50 });
51
52 const setHostContent = host?.setContent;
53 useEffect(
54 () => () => {
55 setHostContent?.(null);
56 popoverHeight?.set(0);
57 },
58 [setHostContent, popoverHeight],
59 );
60
61 if (host === null) {
62 return <View className="absolute inset-x-0 bottom-full z-10 mb-2">{props.children}</View>;
63 }
64
65 return (
66 <View
67 ref={anchorRef}
68 collapsable={false}
69 pointerEvents="none"
70 className="absolute inset-0"
71 onLayout={measure}
72 />
73 );
74}