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

AndroidScreenHeader.android.tsx

186 lines6.5 KB

1import { AppActivityIndicator } from "./AppActivityIndicator";
2import { Column, Text as NativeText, RNHostView } from "@expo/ui/jetpack-compose";
3import { use, useState, type ReactNode } from "react";
4import { View } from "react-native";
5​
6import type { AppSymbolName } from "./AppSymbol";
7import { NativeAppBar } from "./NativeAppBar.android";
8import { IconButton } from "./IconButton";
9import { AndroidAnchoredMenu } from "./AndroidAnchoredMenu.android";
10import { splitAndroidHeaderActions } from "./headerMenu.android";
11import { useScaledTextRole } from "../../features/settings/appearance/useScaledTextRole";
12import { useToolbarLayout } from "./useToolbarLayout";
13import { useControlTheme } from "./useControlTheme";
14import { RoutedFormSheetContext } from "./RoutedFormSheetContext";
15​
16export interface AndroidHeaderAction {
17 readonly accessibilityLabel: string;
18 readonly icon: AppSymbolName;
19 readonly onPress: () => void;
20 readonly disabled?: boolean;
21 readonly loading?: boolean;
22 readonly selected?: boolean;
23 readonly subtitle?: string;
24 readonly destructive?: boolean;
25 readonly overflowOnly?: boolean;
27​
28export function AndroidHeaderIconButton(props: {
29 readonly accessibilityLabel: string;
30 readonly icon: AppSymbolName;
31 readonly onPress?: () => void;
32 readonly disabled?: boolean;
33 readonly selected?: boolean;
34}) {
35 return <IconButton {...props} tintColorClassName="accent-header-foreground" />;
37​
38export function AndroidScreenHeader(props: {
39 readonly title: string;
40 readonly titleContent?: ReactNode;
41 readonly subtitle?: string | null;
42 readonly actions?: ReadonlyArray<AndroidHeaderAction>;
43 readonly leading?: ReactNode;
44 readonly trailing?: ReactNode;
45 readonly onBack?: () => void;
46 readonly embedded?: boolean;
47 readonly hideBottomBorder?: boolean;
48}) {
49 const { colors } = useControlTheme();
50 const titleTypography = useScaledTextRole("title");
51 const subtitleTypography = useScaledTextRole("label");
52 const inFormSheet = use(RoutedFormSheetContext);
53 const { paddingTop, paddingBottom } = useToolbarLayout(props.embedded || inFormSheet);
54 const [headerWidth, setHeaderWidth] = useState(0);
55 const { visibleActions, overflowActions } = splitAndroidHeaderActions(
56 props.actions ?? [],
57 headerWidth >= 600,
58 );
59​
60 return (
61 <View
62 onLayout={(event) => setHeaderWidth(event.nativeEvent.layout.width)}
63 className="border-b border-header-border bg-header"
64 style={{
65 paddingTop,
66 paddingBottom,
67 borderBottomWidth: props.hideBottomBorder ? 0 : undefined,
68 }}
69 >
70 {inFormSheet ? (
71 <View
72 pointerEvents="none"
73 className="absolute top-1 h-1 w-8 self-center rounded-full bg-header-foreground/40"
74 />
75 ) : null}
76 <NativeAppBar
77 navigationIcon={
78 props.onBack || props.leading ? (
79 <View collapsable={false} className="flex-row items-center">
80 {props.onBack ? (
81 <IconButton
82 accessibilityLabel="Navigate up"
83 icon="arrow.left"
84 tintColorClassName="accent-header-foreground"
85 onPress={props.onBack}
86 />
87 ) : null}
88​
89 {props.leading}
90 </View>
91 ) : undefined
92 }
93 title={
94 props.titleContent ? (
95 <RNHostView matchContents={{ vertical: true }}>
96 <View collapsable={false}>{props.titleContent}</View>
97 </RNHostView>
98 ) : !props.subtitle ? (
99 <NativeText
100 color={colors["--color-header-foreground"]}
101 maxLines={1}
102 overflow="ellipsis"
103 style={titleTypography}
104 >
105 {props.title}
106 </NativeText>
107 ) : (
108 <Column>
109 <NativeText
110 color={colors["--color-header-foreground"]}
111 maxLines={1}
112 overflow="ellipsis"
113 style={titleTypography}
114 >
115 {props.title}
116 </NativeText>
117 {props.subtitle ? (
118 <NativeText
119 color={colors["--color-foreground-muted"]}
120 maxLines={1}
121 overflow="ellipsis"
122 style={subtitleTypography}
123 >
124 {props.subtitle}
125 </NativeText>
126 ) : null}
127 </Column>
128 )
129 }
130 actions={
131 visibleActions.length > 0 || overflowActions.length > 0 || props.trailing ? (
132 <View collapsable={false} className="flex-row items-center">
133 {visibleActions.map((action) =>
134 action.loading ? (
135 <View
136 key={action.accessibilityLabel}
137 accessibilityLabel={action.accessibilityLabel}
138 className="size-12 items-center justify-center"
139 >
140 <AppActivityIndicator colorClassName="accent-header-foreground" />
141 </View>
142 ) : (
143 <AndroidHeaderIconButton
144 key={action.accessibilityLabel}
145 accessibilityLabel={action.accessibilityLabel}
146 disabled={action.disabled}
147 selected={action.selected}
148 icon={action.icon}
149 onPress={action.onPress}
150 />
151 ),
152 )}
153 {overflowActions.length > 0 ? (
154 <AndroidAnchoredMenu
155 actions={overflowActions.map((action, index) => ({
156 id: String(index),
157 title: action.accessibilityLabel,
158 subtitle: action.subtitle,
159 state: action.selected ? "on" : undefined,
160 attributes: {
161 disabled: Boolean(action.disabled),
162 destructive: action.destructive,
163 },
164 }))}
165 onPressAction={({ nativeEvent }) =>
166 overflowActions[Number(nativeEvent.event)]?.onPress()
167 }
168 >
169 {(open) => (
170 <IconButton
171 accessibilityLabel="More actions"
172 icon="ellipsis"
173 tintColorClassName="accent-header-foreground"
174 onPress={open}
175 />
176 )}
177 </AndroidAnchoredMenu>
178 ) : null}
179 {props.trailing}
180 </View>
181 ) : undefined
182 }
183 />
184 </View>
185 );