Open History to see the last commit
1import { AppActivityIndicator } from "./AppActivityIndicator";
2import { Column, Row, Text as NativeText, RNHostView } from "@expo/ui/jetpack-compose";
3import { weight } from "@expo/ui/jetpack-compose/modifiers";
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 overflowOnly?: boolean;
27}
28
29export function AndroidHeaderIconButton(props: {
30 readonly accessibilityLabel: string;
31 readonly icon: AppSymbolName;
32 readonly onPress?: () => void;
33 readonly disabled?: boolean;
34 readonly selected?: boolean;
35}) {
36 return <IconButton {...props} tintColorClassName="accent-header-foreground" />;
37}
38
39export function AndroidScreenHeader(props: {
40 readonly title: string;
41 readonly titleContent?: ReactNode;
42 readonly subtitle?: string | null;
43 readonly actions?: ReadonlyArray<AndroidHeaderAction>;
44 readonly leading?: ReactNode;
45 readonly trailing?: ReactNode;
46 readonly onBack?: () => void;
47 readonly embedded?: boolean;
48 readonly hideBottomBorder?: boolean;
49 /** One row with the subtitle after the title, for screens short on height. */
50 readonly compact?: boolean;
51}) {
52 const { colors } = useControlTheme();
53 const titleTypography = useScaledTextRole("title");
54 const subtitleTypography = useScaledTextRole("label");
55 const inFormSheet = use(RoutedFormSheetContext);
56 const { paddingTop, paddingBottom } = useToolbarLayout(props.embedded || inFormSheet);
57 const [headerWidth, setHeaderWidth] = useState(0);
58 const { visibleActions, overflowActions } = splitAndroidHeaderActions(
59 props.actions ?? [],
60 headerWidth >= 600,
61 );
62
63 return (
64 <View
65 onLayout={(event) => setHeaderWidth(event.nativeEvent.layout.width)}
66 className="border-b border-header-border bg-header"
67 style={{
68 paddingTop,
69 paddingBottom,
70 borderBottomWidth: props.hideBottomBorder ? 0 : undefined,
71 }}
72 >
73 {inFormSheet ? (
74 <View
75 pointerEvents="none"
76 className="absolute top-1 h-1 w-8 self-center rounded-full bg-header-foreground/40"
77 />
78 ) : null}
79 <NativeAppBar
80 singleLine={props.compact}
81 navigationIcon={
82 props.onBack || props.leading ? (
83 <View collapsable={false} className="flex-row items-center">
84 {props.onBack ? (
85 <IconButton
86 accessibilityLabel="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 ) : props.compact ? (
112 <Row verticalAlignment="center" horizontalArrangement={{ spacedBy: 8 }}>
113 <NativeText
114 color={colors["--color-header-foreground"]}
115 maxLines={1}
116 overflow="ellipsis"
117 style={titleTypography}
118 >
119 {props.title}
120 </NativeText>
121 <NativeText
122 color={colors["--color-foreground-muted"]}
123 maxLines={1}
124 overflow="ellipsis"
125 style={subtitleTypography}
126 modifiers={[weight(1)]}
127 >
128 {props.subtitle}
129 </NativeText>
130 </Row>
131 ) : (
132 <Column>
133 <NativeText
134 color={colors["--color-header-foreground"]}
135 maxLines={1}
136 overflow="ellipsis"
137 style={titleTypography}
138 >
139 {props.title}
140 </NativeText>
141 {props.subtitle ? (
142 <NativeText
143 color={colors["--color-foreground-muted"]}
144 maxLines={1}
145 overflow="ellipsis"
146 style={subtitleTypography}
147 >
148 {props.subtitle}
149 </NativeText>
150 ) : null}
151 </Column>
152 )
153 }
154 actions={
155 visibleActions.length > 0 || overflowActions.length > 0 || props.trailing ? (
156 <View collapsable={false} className="flex-row items-center">
157 {visibleActions.map((action) =>
158 action.loading ? (
159 <View
160 key={action.accessibilityLabel}
161 accessibilityLabel={action.accessibilityLabel}
162 className="size-12 items-center justify-center"
163 >
164 <AppActivityIndicator colorClassName="accent-header-foreground" />
165 </View>
166 ) : (
167 <AndroidHeaderIconButton
168 key={action.accessibilityLabel}
169 accessibilityLabel={action.accessibilityLabel}
170 disabled={action.disabled}
171 selected={action.selected}
172 icon={action.icon}
173 onPress={action.onPress}
174 />
175 ),
176 )}
177 {overflowActions.length > 0 ? (
178 <AndroidAnchoredMenu
179 actions={overflowActions.map((action, index) => ({
180 id: String(index),
181 title: action.accessibilityLabel,
182 subtitle: action.subtitle,
183 state: action.selected ? "on" : undefined,
184 attributes: {
185 disabled: Boolean(action.disabled),
186 destructive: action.destructive,
187 },
188 }))}
189 onPressAction={({ nativeEvent }) =>
190 overflowActions[Number(nativeEvent.event)]?.onPress()
191 }
192 >
193 {(open) => (
194 <IconButton
195 accessibilityLabel="More actions"
196 icon="ellipsis"
197 tintColorClassName="accent-header-foreground"
198 onPress={open}
199 />
200 )}
201 </AndroidAnchoredMenu>
202 ) : null}
203 {props.trailing}
204 </View>
205 ) : undefined
206 }
207 />
208 </View>
209 );
210}