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

SplitButton.android.tsx

145 lines5.0 KB

1import {
2 Box,
3 DropdownMenu,
4 Icon,
5 LoadingIndicator,
6 RNHostView,
7 Row,
8 SplitButtonLayout,
9 Text,
10} from "@expo/ui/jetpack-compose";
11import {
12 defaultMinSize,
13 fillMaxWidth,
14 rotate,
15 semantics,
16 size,
17} from "@expo/ui/jetpack-compose/modifiers";
18import { useState } from "react";
19import { Pressable } from "react-native";
20​
21import { useScaledTextRole } from "../../features/settings/appearance/useScaledTextRole";
22import { SymbolView } from "./AppSymbol";
23import { Button } from "./Button";
24import { MenuPopupItems } from "./MenuPopup.android";
25import { NativeThemeHost } from "./NativeThemeHost.android";
26import type { SplitButtonProps } from "./SplitButton.types";
27import { useAndroidControlSizing } from "./useAndroidControlSizing";
28import { useControlTheme } from "./useControlTheme";
29​
30export function SplitButton(props: SplitButtonProps) {
31 const { colors } = useControlTheme();
32 const typography = useScaledTextRole("footnote");
33 const { scale, iconSize, mediumIconSize, menuWidth } = useAndroidControlSizing();
34 const [expanded, setExpanded] = useState(false);
35 if (props.actions.length === 0) return <Button {...props} tone="primary" fullWidth />;
36 const enabled = !props.disabled && !props.loading;
37 const menuEnabled = props.actions.some((action) => !action.attributes?.disabled);
38 const buttonColors = {
39 containerColor: colors["--color-primary"],
40 contentColor: colors["--color-primary-foreground"],
41 disabledContainerColor: colors["--color-subtle-strong"],
42 disabledContentColor: colors["--color-foreground-muted"],
43 };
44 // A usable menu beside a disabled action still takes the disabled palette, so the pair reads as one control.
45 const trailingColors = enabled
46 ? buttonColors
47 : {
48 ...buttonColors,
49 containerColor: buttonColors.disabledContainerColor,
50 contentColor: buttonColors.disabledContentColor,
51 };
52 const label = (
53 <Row verticalAlignment="center">
54 {props.loading ? (
55 <>
56 <LoadingIndicator
57 modifiers={[size(mediumIconSize, mediumIconSize)]}
58 color={colors["--color-foreground-muted"]}
59 />
60 <Box modifiers={[size(8 * scale, 1)]} />
61 </>
62 ) : props.icon ? (
63 <>
64 {/* The glyph host consumes touches, so it runs the action itself. */}
65 <RNHostView matchContents modifiers={[size(mediumIconSize, mediumIconSize)]}>
66 <Pressable
67 accessible={false}
68 disabled={!enabled}
69 onPress={props.onPress}
70 importantForAccessibility="no-hide-descendants"
71 style={{ width: mediumIconSize, height: mediumIconSize }}
72 >
73 <SymbolView
74 name={props.icon}
75 size={mediumIconSize}
76 tintColor={enabled ? buttonColors.contentColor : buttonColors.disabledContentColor}
77 />
78 </Pressable>
79 </RNHostView>
80 <Box modifiers={[size(8 * scale, 1)]} />
81 </>
82 ) : null}
83 <Text style={{ ...typography, fontWeight: "500" }}>{props.label}</Text>
84 </Row>
85 );
86 return (
87 <NativeThemeHost
88 matchContents={{ vertical: true }}
89 ignoreSafeAreaKeyboardInsets
90 style={{ width: "100%" }}
91 >
92 <SplitButtonLayout modifiers={[fillMaxWidth()]}>
93 <SplitButtonLayout.LeadingButton
94 enabled={enabled}
95 onClick={props.onPress}
96 colors={buttonColors}
97 modifiers={[
98 fillMaxWidth(),
99 defaultMinSize({ minHeight: 48 }),
100 semantics({ contentDescription: props.accessibilityLabel ?? props.label }),
101 ]}
102 >
103 {label}
104 </SplitButtonLayout.LeadingButton>
105 <SplitButtonLayout.TrailingButton
106 checked={expanded}
107 onCheckedChange={setExpanded}
108 enabled={menuEnabled}
109 colors={trailingColors}
110 modifiers={[
111 defaultMinSize({ minWidth: 48, minHeight: 48 }),
112 semantics({ contentDescription: props.menuTitle }),
113 ]}
114 >
115 <DropdownMenu
116 expanded={expanded && menuEnabled}
117 onDismissRequest={() => setExpanded(false)}
118 color={colors["--color-card-alt"]}
119 >
120 <DropdownMenu.Trigger>
121 <Icon
122 source={require("../../../assets/icons/chevron-down.xml")}
123 size={iconSize}
124 modifiers={[rotate(expanded ? 180 : 0)]}
125 />
126 </DropdownMenu.Trigger>
127 <DropdownMenu.Items>
128 <MenuPopupItems
129 menuWidth={menuWidth}
130 actions={props.actions}
131 title={props.menuTitle}
132 parent={null}
133 onPress={(action) => {
134 setExpanded(false);
135 if (action.id !== undefined) props.onPressAction(action.id);
136 }}
137 onBack={() => {}}
138 />
139 </DropdownMenu.Items>
140 </DropdownMenu>
141 </SplitButtonLayout.TrailingButton>
142 </SplitButtonLayout>
143 </NativeThemeHost>
144 );