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

AndroidHomeFab.android.tsx

44 lines1.8 KB

1import { useCallback, useRef, useState, type ComponentProps } from "react";
2import { View, type NativeScrollEvent, type NativeSyntheticEvent } from "react-native";
3import { useSafeAreaInsets } from "react-native-safe-area-context";
4import { MaterialNewThreadButton } from "../../components/MaterialNewThreadButton";
5import type { AndroidHomeFabLayout as SharedAndroidHomeFabLayout } from "./AndroidHomeFab.shared";
6import { useWorkspaceState } from "../../state/workspace";
7import { MaterialFabScrollContext } from "./MaterialFabScrollContext";
8import { updateMaterialFabScroll } from "./material-fab-scroll";
9​
10export function AndroidHomeFabLayout(props: ComponentProps<typeof SharedAndroidHomeFabLayout>) {
11 const insets = useSafeAreaInsets();
12 const { state } = useWorkspaceState();
13 const [expanded, setExpanded] = useState(true);
14 const scrollState = useRef({ anchor: 0, expanded: true });
15 const onScroll = useCallback((event: NativeSyntheticEvent<NativeScrollEvent>) => {
16 const { contentOffset, contentSize, layoutMeasurement } = event.nativeEvent;
17 const next = updateMaterialFabScroll(
18 scrollState.current,
19 contentOffset.y,
20 contentSize.height - layoutMeasurement.height,
21 );
22 if (next.expanded !== scrollState.current.expanded) setExpanded(next.expanded);
23 scrollState.current = next;
24 }, []);
25​
26 return (
27 <View className="flex-1">
28 <MaterialFabScrollContext value={onScroll}>{props.children}</MaterialFabScrollContext>
29 {state.hasConnections ? (
30 <MaterialNewThreadButton
31 extended
32 expanded={expanded}
33 onPress={props.onStartNewTask}
34 className="absolute right-5"
35 style={{
36 bottom: props.sidebar
37 ? Math.max(insets.bottom, 12) + 6
38 : Math.max(insets.bottom, 16) + 16,
39 }}
40 />
41 ) : null}
42 </View>
43 );