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

MaterialScrollComposeButton.android.tsx

75 lines3.0 KB

1import { Box, ExtendedFloatingActionButton, Host, Icon, Text } from "@expo/ui/jetpack-compose";
2import { fillMaxWidth, onSizeChanged, size } from "@expo/ui/jetpack-compose/modifiers";
3import { useCallback, useState } from "react";
4import { Pressable, View, type StyleProp, type ViewStyle } from "react-native";
5import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
6import { useScaledTextRole } from "../features/settings/appearance/useScaledTextRole";
7​
8/** Keep the animated width and icon positioning entirely inside Compose, not Yoga. */
9export function MaterialScrollComposeButton(props: {
10 readonly expanded: boolean;
11 readonly onPress: () => void;
12 readonly className?: string;
13 readonly style?: StyleProp<ViewStyle>;
14}) {
15 const { themeAppearance, themeVariables: colors } = useAppearancePreferences();
16 const typography = useScaledTextRole("footnote");
17 const [expandedWidth, setExpandedWidth] = useState(56);
18 const rememberWidth = useCallback(({ width }: { width: number }) => {
19 setExpandedWidth((previous) => Math.max(previous, width));
20 }, []);
21 return (
22 <View pointerEvents="box-none" className={props.className} style={[props.style, { left: 20 }]}>
23 <View pointerEvents="none" importantForAccessibility="no-hide-descendants">
24 <Host
25 matchContents={{ vertical: true }}
26 colorScheme={themeAppearance}
27 ignoreSafeAreaKeyboardInsets
28 style={{ width: "100%" }}
29 >
30 <Box modifiers={[fillMaxWidth()]} contentAlignment="centerEnd">
31 <ExtendedFloatingActionButton
32 expanded={props.expanded}
33 containerColor={colors["--color-primary"]}
34 modifiers={[onSizeChanged(rememberWidth)]}
35 >
36 <ExtendedFloatingActionButton.Icon>
37 <Box modifiers={[size(24, 24)]}>
38 <Icon
39 source={require("../../assets/icons/compose.xml")}
40 size={24}
41 tint={colors["--color-primary-foreground"]}
42 />
43 </Box>
44 </ExtendedFloatingActionButton.Icon>
45 <ExtendedFloatingActionButton.Text>
46 <Text
47 color={colors["--color-primary-foreground"]}
48 style={{ ...typography, fontWeight: "500" }}
49 >
50 New thread
51 </Text>
52 </ExtendedFloatingActionButton.Text>
53 </ExtendedFloatingActionButton>
54 </Box>
55 </Host>
56 </View>
57 {/* The visual host is wider than the button; only this target intercepts list touches. */}
58 <Pressable
59 onPress={props.onPress}
60 accessibilityRole="button"
61 accessibilityLabel="New thread"
62 android_ripple={{ foreground: true }}
63 style={{
64 position: "absolute",
65 right: 0,
66 top: 0,
67 bottom: 0,
68 width: props.expanded ? expandedWidth : 56,
69 borderRadius: 16,
70 overflow: "hidden",
71 }}
72 />
73 </View>
74 );