@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 76 lines 2.37 KB
Raw
1 import type { ReactNode } from "react";
2 import { useEffect, useRef, useState } from "react";
3 import { View, type ViewInstance } from "react-native";
4
5 import { useComposerPopoverHost } from "./ComposerPopoverHost";
6
7 const COMPOSER_GAP = 8;
8
9 type 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. */
12 export 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 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- measures the anchor view after every commit and hands its popover content to the composer popover host
32 useEffect(() => {
33 measure();
34 host?.setContent(
35 frame === null ? null : (
36 <View
37 pointerEvents="box-none"
38 className="absolute top-0 justify-end"
39 style={{ left: frame.x, width: frame.width, height: frame.y - COMPOSER_GAP }}
40 >
41 <View
42 onLayout={(event) => {
43 popoverHeight?.set(event.nativeEvent.layout.height);
44 }}
45 >
46 {props.children}
47 </View>
48 </View>
49 ),
50 );
51 });
52
53 const setHostContent = host?.setContent;
54 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- clears the popover host's content and shared height value when the anchor unmounts
55 useEffect(
56 () => () => {
57 setHostContent?.(null);
58 popoverHeight?.set(0);
59 },
60 [setHostContent, popoverHeight],
61 );
62
63 if (host === null) {
64 return <View className="absolute inset-x-0 bottom-full z-10 mb-2">{props.children}</View>;
65 }
66
67 return (
68 <View
69 ref={anchorRef}
70 collapsable={false}
71 pointerEvents="none"
72 className="absolute inset-0"
73 onLayout={measure}
74 />
75 );
76 }