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

ComposerPopoverHost.tsx

51 lines1.8 KB

1import type { ReactNode, RefObject } from "react";
2import { createContext, useContext, useMemo, useRef, useState } from "react";
3import { View, type ViewInstance } from "react-native";
4import type { SharedValue } from "react-native-reanimated";
5​
6type ComposerPopoverHostValue = {
7 readonly hostRef: RefObject<ViewInstance | null>;
8 readonly setContent: (content: ReactNode) => void;
9 readonly layoutVersion: number;
10 readonly popoverHeight: SharedValue<number>;
11};
12​
13const ComposerPopoverHostContext = createContext<ComposerPopoverHostValue | null>(null);
14​
15export function useComposerPopoverHost() {
16 return useContext(ComposerPopoverHostContext);
18​
19/**
20 * Mount it inside the composer's KeyboardStickyView: popovers placed here move with the composer and sit inside a parent that
21 * covers them, so Android delivers their scroll gestures. Pass `hidden` whenever the composer itself is hidden: popovers live
22 * outside the composer's subtree, so they don't hide with it.
23 */
24export function ComposerPopoverHost(props: {
25 readonly hidden: boolean;
26 readonly popoverHeight: SharedValue<number>;
27 readonly children: ReactNode;
28}) {
29 const hostRef = useRef<ViewInstance>(null);
30 const [content, setContent] = useState<ReactNode>(null);
31 const [layoutVersion, setLayoutVersion] = useState(0);
32 const value = useMemo(
33 () => ({ hostRef, setContent, layoutVersion, popoverHeight: props.popoverHeight }),
34 [layoutVersion, props.popoverHeight],
35 );
36​
37 return (
38 <ComposerPopoverHostContext.Provider value={value}>
39 <View
40 ref={hostRef}
41 collapsable={false}
42 pointerEvents="box-none"
43 className="absolute inset-0"
44 onLayout={() => setLayoutVersion((version) => version + 1)}
45 >
46 {props.children}
47 {props.hidden ? null : content}
48 </View>
49 </ComposerPopoverHostContext.Provider>
50 );