Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ComposerPopoverAnchor.android.tsx

79 lines2.2 KB

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 const popoverHeight = host?.popoverHeight;
20​
21 const measure = () => {
22 const hostView = host?.hostRef.current;
23 if (!hostView) {
24 return;
25 }
26 anchorRef.current?.measureLayout(hostView, (x, y, width) => {
27 setFrame((current) =>
28 current?.x === x && current.y === y && current.width === width ? current : { x, y, width },
29 );
30 });
31 };
32​
33 // No dependency array: the composer re-renders when its padding or expanded
34 // state changes, which moves it without resizing the anchor, so measure and
35 // re-project after every render.
36 useEffect(() => {
37 measure();
38 host?.setContent(
39 frame === null ? null : (
40 <View
41 pointerEvents="box-none"
42 className="absolute top-0 justify-end"
43 style={{ left: frame.x, width: frame.width, height: frame.y - COMPOSER_GAP }}
44 >
45 <View
46 onLayout={(event) => {
47 popoverHeight?.set(event.nativeEvent.layout.height);
48 }}
49 >
50 {props.children}
51 </View>
52 </View>
53 ),
54 );
55 });
56​
57 const setHostContent = host?.setContent;
58 useEffect(
59 () => () => {
60 setHostContent?.(null);
61 popoverHeight?.set(0);
62 },
63 [setHostContent, popoverHeight],
64 );
65​
66 if (host === null) {
67 return <View className="absolute inset-x-0 bottom-full z-10 mb-2">{props.children}</View>;
68 }
69​
70 return (
71 <View
72 ref={anchorRef}
73 collapsable={false}
74 pointerEvents="none"
75 className="absolute inset-0"
76 onLayout={measure}
77 />
78 );