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 { 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";
11import { splitAndroidHeaderActions } from "./headerMenu.android";
12import { useScaledTextRole } from "../../features/settings/appearance/useScaledTextRole";
13import { useToolbarLayout } from "./useToolbarLayout";
14import { useControlTheme } from "./useControlTheme";
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 destructive?: boolean;
24 readonly overflowOnly?: boolean;
25}
26
27export function AndroidHeaderIconButton(props: {
28 readonly accessibilityLabel: string;
29 readonly icon: AppSymbolName;
30 readonly onPress?: () => void;
31 readonly disabled?: boolean;
32 readonly selected?: boolean;
33}) {
34 return <IconButton {...props} tintColorClassName="accent-header-foreground" />;
35}
36
37export function AndroidScreenHeader(props: {
38 readonly title: string;
39 readonly titleContent?: ReactNode;
40 readonly subtitle?: string | null;
41 readonly actions?: ReadonlyArray<AndroidHeaderAction>;
42 readonly leading?: ReactNode;
43 readonly trailing?: ReactNode;
44 readonly onBack?: () => void;
45 readonly embedded?: boolean;
46 readonly hideBottomBorder?: boolean;
47 /** One row: the subtitle follows the title, for screens short on height. */
48 readonly compact?: boolean;
49 /**
50 * Matches the thread list toolbar's height, so switching between list tabs keeps the
51 * header and the content below it in place.
52 */
53 readonly listToolbar?: boolean;
54}) {
55 const { colors } = useControlTheme();
56 const titleTypography = useScaledTextRole("title");
57 const subtitleTypography = useScaledTextRole("label");
58 const { paddingTop, paddingBottom } = useToolbarLayout(props.embedded);
59 const [headerWidth, setHeaderWidth] = useState(0);
60 const { visibleActions, overflowActions } = splitAndroidHeaderActions(
61 props.actions ?? [],
62 headerWidth >= 600,
63 );
64
65 return (
66 <View
67 onLayout={(event) => setHeaderWidth(event.nativeEvent.layout.width)}
68 className="border-b border-header-border bg-header"
69 style={{
70 paddingTop,
71 paddingBottom,
72 borderBottomWidth: props.hideBottomBorder ? 0 : undefined,
73 }}
74 >
75 <NativeAppBar
76 singleLine={props.compact}
77 listToolbar={props.listToolbar}
78 navigationIcon={
79 props.onBack || props.leading ? (
80 <View collapsable={false} className="flex-row items-center">
81 {props.onBack ? (
82 <IconButton
83 accessibilityLabel="Navigate up"
84 icon="arrow.left"
85 tintColorClassName="accent-header-foreground"
86 onPress={props.onBack}
87 />
88 ) : null}
89
90 {props.leading}
91 </View>
92 ) : undefined
93 }
94 title={
95 props.titleContent ? (
96 <RNHostView matchContents={{ vertical: true }}>
97 <View collapsable={false}>{props.titleContent}</View>
98 </RNHostView>
99 ) : !props.subtitle ? (
100 <NativeText
101 color={colors["--color-header-foreground"]}
102 maxLines={1}
103 overflow="ellipsis"
104 style={titleTypography}
105 >
106 {props.title}
107 </NativeText>
108 ) : props.compact ? (
109 <Row verticalAlignment="center" horizontalArrangement={{ spacedBy: 8 }}>
110 <NativeText
111 color={colors["--color-header-foreground"]}
112 maxLines={1}
113 overflow="ellipsis"
114 style={titleTypography}
115 >
116 {props.title}
117 </NativeText>
118 <NativeText
119 color={colors["--color-foreground-muted"]}
120 maxLines={1}
121 overflow="ellipsis"
122 style={subtitleTypography}
123 modifiers={[weight(1)]}
124 >
125 {props.subtitle}
126 </NativeText>
127 </Row>
128 ) : (
129 <Column>
130 <NativeText
131 color={colors["--color-header-foreground"]}
132 maxLines={1}
133 overflow="ellipsis"
134 style={titleTypography}
135 >
136 {props.title}
137 </NativeText>
138 {props.subtitle ? (
139 <NativeText
140 color={colors["--color-foreground-muted"]}
141 maxLines={1}
142 overflow="ellipsis"
143 style={subtitleTypography}
144 >
145 {props.subtitle}
146 </NativeText>
147 ) : null}
148 </Column>
149 )
150 }
151 actions={
152 visibleActions.length > 0 || overflowActions.length > 0 || props.trailing ? (
153 <View collapsable={false} className="flex-row items-center">
154 {visibleActions.map((action) =>
155 action.loading ? (
156 <View
157 key={action.accessibilityLabel}
158 accessibilityLabel={action.accessibilityLabel}
159 className="size-12 items-center justify-center"
160 >
161 <AppActivityIndicator colorClassName="accent-header-foreground" />
162 </View>
163 ) : (
164 <AndroidHeaderIconButton
165 key={action.accessibilityLabel}
166 accessibilityLabel={action.accessibilityLabel}
167 disabled={action.disabled}
168 selected={action.selected}
169 icon={action.icon}
170 onPress={action.onPress}
171 />
172 ),
173 )}
174 {overflowActions.length > 0 ? (
175 <AndroidAnchoredMenu
176 actions={overflowActions.map((action, index) => ({
177 id: String(index),
178 title: action.accessibilityLabel,
179 attributes: {
180 disabled: Boolean(action.disabled),
181 destructive: action.destructive,
182 state: action.selected ? "on" : undefined,
183 },
184 }))}
185 onPressAction={({ nativeEvent }) =>
186 overflowActions[Number(nativeEvent.event)]?.onPress()
187 }
188 >
189 {(open) => (
190 <IconButton
191 accessibilityLabel="More actions"
192 icon="ellipsis"
193 tintColorClassName="accent-header-foreground"
194 onPress={open}
195 />
196 )}
197 </AndroidAnchoredMenu>
198 ) : null}
199 {props.trailing}
200 </View>
201 ) : undefined
202 }
203 />
204 </View>
205 );
206}
207
208export function AndroidSheetHeader(
209 props: Omit<Parameters<typeof AndroidScreenHeader>[0], "embedded">,
210) {
211 return <AndroidScreenHeader {...props} embedded />;
212}