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