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

AndroidHomeFab.android.tsx

90 lines3.6 KB

1import { useRef, useState, type ComponentProps } from "react";
2import {
3 useWindowDimensions,
4 View,
5 type NativeScrollEvent,
6 type NativeSyntheticEvent,
7} from "react-native";
8import { useSafeAreaInsets } from "react-native-safe-area-context";
9import { NewThreadButton } from "../../components/NewThreadButton";
10import {
11 androidHomeFabPosition,
12 type AndroidHomeFabLayout as SharedAndroidHomeFabLayout,
13} from "./AndroidHomeFab.shared";
14import { useWorkspaceState } from "../../state/workspace";
15import { FabScrollContext } from "./FabScrollContext";
16import { updateFabScroll } from "./fab-scroll";
17import { useAndroidControlSizing } from "../../components/ui/useAndroidControlSizing";
18import { useEnabledNavigationTabs } from "../workspace/navigation-tabs";
19import { WorkspaceTabsContext, useWorkspaceTabs } from "../workspace/WorkspaceTabsContext";
20import { FLOATING_TABS, floatingTabsMetrics } from "../workspace/floating-workspace-tabs";
21import {
22 useAppearancePreferences,
23 useAndroidFloatingTabs,
24} from "../settings/appearance/AppearancePreferencesProvider";
25​
26export function AndroidHomeFabLayout(props: ComponentProps<typeof SharedAndroidHomeFabLayout>) {
27 const floatingTabsEnabled = useAndroidFloatingTabs();
28 const insets = useSafeAreaInsets();
29 const { appearance } = useAppearancePreferences();
30 const { fontScale } = useWindowDimensions();
31 const [layoutWidth, setLayoutWidth] = useState<number | null>(null);
32 const { state } = useWorkspaceState();
33 const [expanded, setExpanded] = useState(true);
34 const { scale, fabSize } = useAndroidControlSizing();
35 const tabs = useEnabledNavigationTabs();
36 const floating = floatingTabsMetrics(
37 tabs.length + 1,
38 scale,
39 fabSize,
40 props.paneWidth ?? layoutWidth ?? undefined,
41 );
42 const workspace = useWorkspaceTabs();
43 const bottomClearance =
44 insets.bottom +
45 FLOATING_TABS.bottom +
46 FLOATING_TABS.edge +
47 Math.max(floating.height, floating.fabOffset + fabSize);
48 const context = floatingTabsEnabled && workspace ? { ...workspace, bottomClearance } : workspace;
49 const scrollState = useRef({ anchor: 0, expanded: true });
50 const onScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
51 const { contentOffset, contentSize, layoutMeasurement } = event.nativeEvent;
52 const next = updateFabScroll(
53 scrollState.current,
54 contentOffset.y,
55 contentSize.height - layoutMeasurement.height,
56 );
57 if (next.expanded !== scrollState.current.expanded) setExpanded(next.expanded);
58 scrollState.current = next;
59 };
60​
61 // Remount only the FAB when its font or pane changes to clear the cached expanded width.
62 // Floating navigation shares this row; Material split views put New thread in the rail.
63 return (
64 <View className="flex-1" onLayout={(event) => setLayoutWidth(event.nativeEvent.layout.width)}>
65 <WorkspaceTabsContext value={context}>
66 <FabScrollContext value={onScroll}>{props.children}</FabScrollContext>
67 </WorkspaceTabsContext>
68 {!props.disabled &&
69 (!props.sidebar || floatingTabsEnabled) &&
70 state.hasConnections &&
71 layoutWidth !== null ? (
72 <NewThreadButton
73 key={`${appearance.baseFontSize}:${fontScale}:${layoutWidth}`}
74 extended
75 expanded={floatingTabsEnabled ? false : expanded}
76 onPress={props.onStartNewTask}
77 className="absolute"
78 style={
79 floatingTabsEnabled
80 ? {
81 right: FLOATING_TABS.edge,
82 bottom: insets.bottom + FLOATING_TABS.bottom + floating.fabOffset,
83 }
84 : androidHomeFabPosition(insets.bottom, props.sidebar)
85 }
86 />
87 ) : null}
88 </View>
89 );