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