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

AndroidHomeFab.android.tsx

93 lines3.8 KB

1import { useCallback, useMemo, 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 = useMemo(
49 () => (floatingTabsEnabled && workspace ? { ...workspace, bottomClearance } : workspace),
50 [floatingTabsEnabled, workspace, bottomClearance],
51 );
52 const scrollState = useRef({ anchor: 0, expanded: true });
53 const onScroll = useCallback((event: NativeSyntheticEvent<NativeScrollEvent>) => {
54 const { contentOffset, contentSize, layoutMeasurement } = event.nativeEvent;
55 const next = updateFabScroll(
56 scrollState.current,
57 contentOffset.y,
58 contentSize.height - layoutMeasurement.height,
59 );
60 if (next.expanded !== scrollState.current.expanded) setExpanded(next.expanded);
61 scrollState.current = next;
62 }, []);
63​
64 // Remount only the FAB when its font or pane changes to clear the cached expanded width.
65 // Floating navigation shares this row; Material split views put New thread in the rail.
66 return (
67 <View className="flex-1" onLayout={(event) => setLayoutWidth(event.nativeEvent.layout.width)}>
68 <WorkspaceTabsContext value={context}>
69 <FabScrollContext value={onScroll}>{props.children}</FabScrollContext>
70 </WorkspaceTabsContext>
71 {!props.disabled &&
72 (!props.sidebar || floatingTabsEnabled) &&
73 state.hasConnections &&
74 layoutWidth !== null ? (
75 <NewThreadButton
76 key={`${appearance.baseFontSize}:${fontScale}:${layoutWidth}`}
77 extended
78 expanded={floatingTabsEnabled ? false : expanded}
79 onPress={props.onStartNewTask}
80 className="absolute"
81 style={
82 floatingTabsEnabled
83 ? {
84 right: FLOATING_TABS.edge,
85 bottom: insets.bottom + FLOATING_TABS.bottom + floating.fabOffset,
86 }
87 : androidHomeFabPosition(insets.bottom, props.sidebar)
88 }
89 />
90 ) : null}
91 </View>
92 );