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