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