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

AndroidScreenHeader.tsx

214 lines7.5 KB

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