Open History to see the last commit
1import { NativeThemeHost } from "./ui/NativeThemeHost.android";
2import { Box, ExtendedFloatingActionButton, Icon, Text } from "@expo/ui/jetpack-compose";
3import {
4 defaultMinSize,
5 fillMaxWidth,
6 height,
7 onSizeChanged,
8 size,
9} from "@expo/ui/jetpack-compose/modifiers";
10import { useState } from "react";
11import { I18nManager, Pressable, View, type StyleProp, type ViewStyle } from "react-native";
12import { useAndroidControlSizing } from "./ui/useAndroidControlSizing";
13import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
14import { resolveScaledTextRole } from "../lib/appearancePreferences";
15
16/** Keep the animated width and icon positioning entirely inside Compose, not Yoga. */
17export function ScrollComposeButton(props: {
18 readonly expanded: boolean;
19 readonly onPress: () => void;
20 readonly size?: number;
21 readonly className?: string;
22 readonly style?: StyleProp<ViewStyle>;
23}) {
24 const { appearance, themeVariables: colors } = useAppearancePreferences();
25 const typography = resolveScaledTextRole("footnote", appearance.baseFontSize);
26 const { iconSize, fabSize: scaledFabSize } = useAndroidControlSizing();
27 const fabSize = props.size ?? scaledFabSize;
28 const nativeSize = Math.max(56, fabSize);
29 const scale = fabSize / nativeSize;
30 const nativeIconSize = Math.round(iconSize / scale);
31 const [buttonWidth, setButtonWidth] = useState(nativeSize);
32 const rememberWidth = ({ width }: { width: number }) => {
33 setButtonWidth((previous) => Math.max(previous, width));
34 };
35 return (
36 <View
37 pointerEvents="box-none"
38 className={props.className}
39 style={[props.style, { left: 20, height: fabSize }]}
40 >
41 <View
42 pointerEvents="none"
43 importantForAccessibility="no-hide-descendants"
44 style={{
45 transform: [{ scale }],
46 transformOrigin: I18nManager.isRTL ? "top left" : "top right",
47 }}
48 >
49 <NativeThemeHost
50 matchContents={{ vertical: true }}
51 ignoreSafeAreaKeyboardInsets
52 style={{ width: "100%" }}
53 >
54 <Box modifiers={[fillMaxWidth()]} contentAlignment="centerEnd">
55 <ExtendedFloatingActionButton
56 expanded={props.expanded}
57 containerColor={colors["--color-primary"]}
58 modifiers={[
59 defaultMinSize({ minWidth: nativeSize }),
60 height(nativeSize),
61 onSizeChanged(rememberWidth),
62 ]}
63 >
64 <ExtendedFloatingActionButton.Icon>
65 <Box modifiers={[size(nativeIconSize, nativeIconSize)]}>
66 <Icon
67 source={require("../../assets/icons/compose.xml")}
68 size={nativeIconSize}
69 tint={colors["--color-primary-foreground"]}
70 />
71 </Box>
72 </ExtendedFloatingActionButton.Icon>
73 <ExtendedFloatingActionButton.Text>
74 <Text
75 color={colors["--color-primary-foreground"]}
76 style={{
77 fontSize: typography.fontSize / scale,
78 lineHeight: typography.lineHeight / scale,
79 fontWeight: "500",
80 }}
81 >
82 New thread
83 </Text>
84 </ExtendedFloatingActionButton.Text>
85 </ExtendedFloatingActionButton>
86 </Box>
87 </NativeThemeHost>
88 </View>
89 {/* The visual host is wider than the button; only this target intercepts list touches. */}
90 <Pressable
91 onPress={props.onPress}
92 accessibilityRole="button"
93 accessibilityLabel="New thread"
94 android_ripple={{ foreground: true }}
95 style={{
96 position: "absolute",
97 right: 0,
98 top: 0,
99 bottom: 0,
100 width: props.expanded ? buttonWidth * scale : fabSize,
101 borderRadius: 16 * scale,
102 overflow: "hidden",
103 }}
104 />
105 </View>
106 );
107}