Skip to content
15 changes: 11 additions & 4 deletions apps/mobile/src/features/threads/ComposerCommandPopover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,26 @@ interface ComposerCommandPopoverProps {
readonly triggerKind: ComposerTriggerKind | null;
readonly isLoading: boolean;
readonly error?: string | null;
/** Clear glass reads over the feed; over the composer's own text it needs the frosted style. */
readonly overlapsComposer?: boolean;
readonly onSelect: (item: ComposerCommandItem) => void;
}

function PopoverSurface(props: { readonly children: React.ReactNode; readonly style?: ViewStyle }) {
function PopoverSurface(props: {
readonly children: React.ReactNode;
readonly frosted: boolean;
readonly style?: ViewStyle;
}) {
const baseStyle: ViewStyle = {
borderRadius: 16,
overflow: "hidden",
flexShrink: 1,
...props.style,
};

return (
<GlassSurface
glassEffectStyle="clear"
glassEffectStyle={props.frosted ? "regular" : "clear"}
tintColorClassName="accent-glass-surface"
style={baseStyle}
>
Expand Down Expand Up @@ -239,7 +246,7 @@ export const ComposerCommandPopover = memo(function ComposerCommandPopover(
const label = groupLabel(props.triggerKind, props.items);

return (
<PopoverSurface>
<PopoverSurface frosted={props.overlapsComposer === true}>
{label ? (
<View className="px-3.5 pt-2.5 pb-1">
<Text className="text-3xs font-t3-bold tracking-[0.8px] uppercase text-foreground-muted">
Expand All @@ -249,7 +256,7 @@ export const ComposerCommandPopover = memo(function ComposerCommandPopover(
) : null}
{props.items.length > 0 ? (
<ScrollView
className="max-h-[180px]"
className="max-h-[180px] shrink"
keyboardShouldPersistTaps="always"
showsVerticalScrollIndicator={false}
>
Expand Down
65 changes: 48 additions & 17 deletions apps/mobile/src/features/threads/ComposerPopoverAnchor.android.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@ import type { ReactNode } from "react";
import { useEffect, useRef, useState } from "react";
import { View, type ViewInstance } from "react-native";

import { useComposerPopoverHost } from "./ComposerPopoverHost";

const COMPOSER_GAP = 8;
import {
COMPOSER_POPOVER_GAP,
composerPopoverPlacement,
useComposerPopoverHost,
} from "./ComposerPopoverHost";

type Frame = { readonly x: number; readonly y: number; readonly width: number };

/** Android only delivers drags inside parent bounds; bottom-full popover lists cannot scroll. */
export function ComposerPopoverAnchor(props: { readonly children: ReactNode }) {
export function ComposerPopoverAnchor(props: {
readonly children: (overlapsComposer: boolean) => ReactNode;
}) {
const host = useComposerPopoverHost();
const anchorRef = useRef<ViewInstance>(null);
const [frame, setFrame] = useState<Frame | null>(null);
const popoverHeight = host?.popoverHeight;
const placement = composerPopoverPlacement(host);
// A pinned popover's top sits `above` over the composer whatever its height.
const pinnedAbove = placement?.pinnedToTop ? placement.above : null;
const measuredHeightRef = useRef(0);

const measure = () => {
const hostView = host?.hostRef.current;
Expand All @@ -31,25 +39,46 @@ export function ComposerPopoverAnchor(props: { readonly children: ReactNode }) {
// 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
useEffect(() => {
measure();
if (frame === null) {
host?.setContent(null);
return;
}
// The host's top can sit above the screen while the keyboard lifts it, so the room
// estimate, not the anchor's offset, bounds the height.
const bottom = frame.y - COMPOSER_POPOVER_GAP;
const pinnedToTop = placement?.pinnedToTop === true;
const height = placement === null ? bottom : Math.min(bottom, placement.maxHeight);
host?.setContent(
frame === null ? null : (
<View
pointerEvents="box-none"
className={pinnedToTop ? "absolute justify-start" : "absolute justify-end"}
style={{
left: frame.x,
width: frame.width,
top: pinnedToTop ? bottom - placement.above : bottom - height,
height: pinnedToTop ? placement.maxHeight : height,
}}
>
<View
pointerEvents="box-none"
className="absolute top-0 justify-end"
style={{ left: frame.x, width: frame.width, height: frame.y - COMPOSER_GAP }}
className="shrink"
onLayout={(event) => {
measuredHeightRef.current = event.nativeEvent.layout.height;
popoverHeight?.set(pinnedAbove ?? measuredHeightRef.current);
}}
>
<View
onLayout={(event) => {
popoverHeight?.set(event.nativeEvent.layout.height);
}}
>
{props.children}
</View>
{props.children(pinnedToTop)}
</View>
),
</View>,
);
});

// New room can leave the popover's own size unchanged, so no layout event reports it.
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- re-reports the popover's height above the composer when the room estimate changes
useEffect(() => {
if (measuredHeightRef.current === 0) return;
popoverHeight?.set(pinnedAbove ?? measuredHeightRef.current);
}, [pinnedAbove, popoverHeight]);

const setHostContent = host?.setContent;
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- clears the popover host's content and shared height value when the anchor unmounts
useEffect(
Expand All @@ -61,7 +90,9 @@ export function ComposerPopoverAnchor(props: { readonly children: ReactNode }) {
);

if (host === null) {
return <View className="absolute inset-x-0 bottom-full z-10 mb-2">{props.children}</View>;
return (
<View className="absolute inset-x-0 bottom-full z-10 mb-2">{props.children(false)}</View>
);
}

return (
Expand Down
27 changes: 21 additions & 6 deletions apps/mobile/src/features/threads/ComposerPopoverAnchor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ import type { ReactNode } from "react";
import { useEffect } from "react";
import { View } from "react-native";

import { useComposerPopoverHost } from "./ComposerPopoverHost";
import {
COMPOSER_POPOVER_GAP,
composerPopoverPlacement,
useComposerPopoverHost,
} from "./ComposerPopoverHost";

export function ComposerPopoverAnchor(props: { readonly children: ReactNode }) {
const popoverHeight = useComposerPopoverHost()?.popoverHeight;
export function ComposerPopoverAnchor(props: {
readonly children: (overlapsComposer: boolean) => ReactNode;
}) {
const host = useComposerPopoverHost();
const popoverHeight = host?.popoverHeight;
const placement = composerPopoverPlacement(host);
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- resets the popover host's shared height value when the anchor unmounts
useEffect(
() => () => {
Expand All @@ -15,12 +23,19 @@ export function ComposerPopoverAnchor(props: { readonly children: ReactNode }) {
);
return (
<View
className="absolute inset-x-0 bottom-full z-10 mb-2"
className="absolute inset-x-0 z-10"
style={
placement?.pinnedToTop
? { top: -(COMPOSER_POPOVER_GAP + placement.above), maxHeight: placement.maxHeight }
: { bottom: "100%", marginBottom: COMPOSER_POPOVER_GAP, maxHeight: placement?.maxHeight }
}
onLayout={(event) => {
popoverHeight?.set(event.nativeEvent.layout.height);
popoverHeight?.set(
placement?.pinnedToTop ? placement.above : event.nativeEvent.layout.height,
);
}}
>
{props.children}
{props.children(placement?.pinnedToTop === true)}
</View>
);
}
36 changes: 35 additions & 1 deletion apps/mobile/src/features/threads/ComposerPopoverHost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ type ComposerPopoverHostValue = {
readonly setContent: (content: ReactNode) => void;
readonly layoutVersion: number;
readonly popoverHeight: SharedValue<number>;
/** Room between the header and the composer's top, or null when unknown. */
readonly popoverRoom: number | null;
readonly composerHeight: number;
};

const ComposerPopoverHostContext = createContext<ComposerPopoverHostValue | null>(null);
Expand All @@ -16,17 +19,48 @@ export function useComposerPopoverHost() {
return useContext(ComposerPopoverHostContext);
}

export const COMPOSER_POPOVER_GAP = 8;
// The group label plus about three rows.
const COMPOSER_POPOVER_MIN_HEIGHT = 160;

/**
* With too little room above the composer (the Duo's front screen, keyboard up), the popover pins
* below the header and grows down over the composer, stopping at its bottom edge so no row hides
* behind the keyboard. `above` is the height it can use above the composer.
*/
export function composerPopoverPlacement(host: ComposerPopoverHostValue | null) {
if (host?.popoverRoom == null) return null;
const above = Math.max(0, host.popoverRoom - COMPOSER_POPOVER_GAP * 2);
if (above >= COMPOSER_POPOVER_MIN_HEIGHT) {
return { above, maxHeight: above, pinnedToTop: false };
}
const maxHeight = Math.min(
COMPOSER_POPOVER_MIN_HEIGHT,
above + host.composerHeight - COMPOSER_POPOVER_GAP,
);
return { above, maxHeight, pinnedToTop: true };
}

/** Mount inside KeyboardStickyView for Android gestures. Hide explicitly because portal content outlives its subtree. */
export function ComposerPopoverHost(props: {
readonly hidden: boolean;
readonly inFlow?: boolean;
readonly popoverHeight: SharedValue<number>;
readonly popoverRoom: number | null;
readonly composerHeight: number;
readonly children: ReactNode;
}) {
const hostRef = useRef<ViewInstance>(null);
const [content, setContent] = useState<ReactNode>(null);
const [layoutVersion, setLayoutVersion] = useState(0);
const value = { hostRef, setContent, layoutVersion, popoverHeight: props.popoverHeight };
const value = {
hostRef,
setContent,
layoutVersion,
popoverHeight: props.popoverHeight,
popoverRoom: props.popoverRoom,
composerHeight: props.composerHeight,
};

return (
<ComposerPopoverHostContext.Provider value={value}>
Expand Down
17 changes: 10 additions & 7 deletions apps/mobile/src/features/threads/ThreadComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -842,13 +842,16 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
composerMenu.trigger &&
(composerMenu.items.length > 0 || composerMenu.trigger.kind === "pull-request") ? (
<ComposerPopoverAnchor>
<ComposerCommandPopover
items={composerMenu.items}
triggerKind={composerMenu.trigger.kind}
isLoading={composerMenu.isLoading}
error={composerMenu.error}
onSelect={composerMenu.onSelect}
/>
{(overlapsComposer) => (
<ComposerCommandPopover
items={composerMenu.items}
triggerKind={composerMenu.trigger?.kind ?? null}
isLoading={composerMenu.isLoading}
error={composerMenu.error}
overlapsComposer={overlapsComposer}
onSelect={composerMenu.onSelect}
/>
)}
</ComposerPopoverAnchor>
) : null}

Expand Down
11 changes: 11 additions & 0 deletions apps/mobile/src/features/threads/ThreadDetailScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,15 @@ function ThreadDetailBody(
}),
flexGrow: 0,
}));
// The sticky view lifts the composer by the keyboard less its opened offset, and the
// composer's measured height already includes its bottom inset. A measured Android screen
// already sits below its opaque header.
const composerPopoverRoom =
screenAreaHeight -
(Platform.OS === "android" && screenHeight !== null ? 0 : navigationHeaderHeight) -
controlInsets.bottom -
(keyboardStickyEnabled ? Math.max(0, liveKeyboardHeight - composerKeyboardOffset) : 0) -
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
composerOverlapHeight;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// The usage panel stacks with the composer and any approval card, so it gets
// whatever room they leave below the header. Measured only while it is open
// so typing in the composer does not re-render the screen.
Expand Down Expand Up @@ -1575,6 +1584,8 @@ function ThreadDetailBody(
hidden={composerSlotHidden}
popoverHeight={composerPopoverHeight}
inFlow={questionInFlow}
popoverRoom={composerPopoverRoom}
composerHeight={composerOverlapHeight}
>
{/* The fixed sticky host gives this bottom-anchored child a stable
coordinate space. Its top and height can then animate together
Expand Down
Loading