|
| 1 | +import type { ReactNode } from "react"; |
| 2 | +import { useCallback, useEffect, useRef, useState } from "react"; |
| 3 | +import { View, type ViewInstance } from "react-native"; |
| 4 | +import { useKeyboardState } from "react-native-keyboard-controller"; |
| 5 | + |
| 6 | +import { OverlayPortal } from "../../components/OverlayPortal"; |
| 7 | + |
| 8 | +const COMPOSER_GAP = 8; |
| 9 | + |
| 10 | +type Frame = { |
| 11 | + readonly x: number; |
| 12 | + readonly y: number; |
| 13 | + readonly width: number; |
| 14 | + readonly height: number; |
| 15 | +}; |
| 16 | + |
| 17 | +function sameFrame(a: Frame | null, b: Frame) { |
| 18 | + return a !== null && a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Places a composer popover just above the composer. Android only delivers |
| 23 | + * drags to views inside their parent's bounds, so a popover hung above the |
| 24 | + * composer with `bottom-full` takes taps but its list never scrolls. Render it |
| 25 | + * through OverlayPortal instead, positioned from the composer's window frame. |
| 26 | + */ |
| 27 | +export function ComposerPopoverAnchor(props: { readonly children: ReactNode }) { |
| 28 | + const anchorRef = useRef<ViewInstance>(null); |
| 29 | + const overlayRef = useRef<ViewInstance>(null); |
| 30 | + const [anchor, setAnchor] = useState<Frame | null>(null); |
| 31 | + const [overlay, setOverlay] = useState<Frame | null>(null); |
| 32 | + const keyboardHeight = useKeyboardState((state) => state.height); |
| 33 | + |
| 34 | + const measureAnchor = useCallback(() => { |
| 35 | + anchorRef.current?.measureInWindow((x, y, width, height) => { |
| 36 | + const next = { x, y, width, height }; |
| 37 | + setAnchor((current) => (sameFrame(current, next) ? current : next)); |
| 38 | + }); |
| 39 | + }, []); |
| 40 | + |
| 41 | + const measureOverlay = useCallback(() => { |
| 42 | + overlayRef.current?.measureInWindow((x, y, width, height) => { |
| 43 | + const next = { x, y, width, height }; |
| 44 | + setOverlay((current) => (sameFrame(current, next) ? current : next)); |
| 45 | + }); |
| 46 | + }, []); |
| 47 | + |
| 48 | + // The composer moves without resizing when the keyboard opens or closes. |
| 49 | + useEffect(measureAnchor, [measureAnchor, keyboardHeight]); |
| 50 | + |
| 51 | + return ( |
| 52 | + <> |
| 53 | + {/* Covers the composer so its onLayout fires when the composer grows. */} |
| 54 | + <View |
| 55 | + ref={anchorRef} |
| 56 | + collapsable={false} |
| 57 | + pointerEvents="none" |
| 58 | + className="absolute inset-0" |
| 59 | + onLayout={measureAnchor} |
| 60 | + /> |
| 61 | + <OverlayPortal> |
| 62 | + <View |
| 63 | + ref={overlayRef} |
| 64 | + collapsable={false} |
| 65 | + pointerEvents="box-none" |
| 66 | + className="absolute inset-0" |
| 67 | + onLayout={measureOverlay} |
| 68 | + > |
| 69 | + {anchor === null || overlay === null ? null : ( |
| 70 | + <View |
| 71 | + className="absolute" |
| 72 | + style={{ |
| 73 | + left: anchor.x - overlay.x, |
| 74 | + width: anchor.width, |
| 75 | + bottom: overlay.height - (anchor.y - overlay.y) + COMPOSER_GAP, |
| 76 | + }} |
| 77 | + > |
| 78 | + {props.children} |
| 79 | + </View> |
| 80 | + )} |
| 81 | + </View> |
| 82 | + </OverlayPortal> |
| 83 | + </> |
| 84 | + ); |
| 85 | +} |
0 commit comments