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

AndroidScreenHeader.tsx

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