Open History to see the last commit
1import { useState, type ReactNode } from "react";
2import { ActivityIndicator, View } from "react-native";
3
4import type { AppSymbolName } from "./AppSymbol";
5import { AppText as Text } from "./AppText";
6import { cn } from "../lib/cn";
7import { MaterialIconButton } from "./MaterialIconButton";
8import { AndroidAnchoredMenu } from "./AndroidAnchoredMenu";
9import { useScaledTextRole } from "../features/settings/appearance/useScaledTextRole";
10import { useMaterialToolbarLayout } from "./useMaterialToolbarLayout";
11
12export interface AndroidHeaderAction {
13 readonly accessibilityLabel: string;
14 readonly icon: AppSymbolName;
15 readonly onPress: () => void;
16 readonly disabled?: boolean;
17 readonly loading?: boolean;
18 readonly selected?: boolean;
19}
20
21export function AndroidHeaderIconButton(props: {
22 readonly accessibilityLabel: string;
23 readonly icon: AppSymbolName;
24 readonly onPress?: () => void;
25 readonly disabled?: boolean;
26 readonly selected?: boolean;
27}) {
28 return <MaterialIconButton {...props} tintColorClassName="accent-header-foreground" />;
29}
30
31export function AndroidScreenHeader(props: {
32 readonly title: string;
33 readonly titleContent?: ReactNode;
34 readonly subtitle?: string | null;
35 readonly actions?: ReadonlyArray<AndroidHeaderAction>;
36 readonly leading?: ReactNode;
37 readonly trailing?: ReactNode;
38 readonly onBack?: () => void;
39 readonly embedded?: boolean;
40 readonly hideBottomBorder?: boolean;
41}) {
42 const titleTypography = useScaledTextRole("title");
43 const subtitleTypography = useScaledTextRole("label");
44 const { height: materialToolbarHeight, ...headerPadding } = useMaterialToolbarLayout(
45 props.embedded,
46 );
47 const [headerWidth, setHeaderWidth] = useState(0);
48 const actions = props.actions ?? [];
49 const directCount = actions.length > 2 ? (headerWidth >= 600 ? 3 : 1) : actions.length;
50 const visibleActions = actions.slice(0, directCount);
51 const overflowActions = actions.slice(directCount);
52
53 return (
54 <View
55 onLayout={(event) => setHeaderWidth(event.nativeEvent.layout.width)}
56 className="border-b border-header-border bg-header px-2"
57 style={{
58 ...headerPadding,
59 borderBottomWidth: props.hideBottomBorder ? 0 : undefined,
60 }}
61 >
62 <View style={{ minHeight: materialToolbarHeight }} className="flex-row items-center gap-1">
63 {props.onBack ? (
64 <MaterialIconButton
65 accessibilityLabel="Navigate up"
66 icon="arrow.left"
67 tintColorClassName="accent-header-foreground"
68 onPress={props.onBack}
69 />
70 ) : null}
71
72 {props.leading}
73
74 <View className={cn("min-w-0 flex-1", !props.onBack && "pl-1")}>
75 {props.titleContent ?? (
76 <Text numberOfLines={1} style={titleTypography} className="text-header-foreground">
77 {props.title}
78 </Text>
79 )}
80 {props.subtitle ? (
81 <Text
82 numberOfLines={1}
83 style={subtitleTypography}
84 className="mt-px text-[13px] font-t3-medium text-foreground-muted"
85 >
86 {props.subtitle}
87 </Text>
88 ) : null}
89 </View>
90
91 {visibleActions.map((action) =>
92 action.loading ? (
93 <View
94 key={action.accessibilityLabel}
95 accessibilityLabel={action.accessibilityLabel}
96 className="size-12 items-center justify-center"
97 >
98 <ActivityIndicator colorClassName="accent-header-foreground" />
99 </View>
100 ) : (
101 <AndroidHeaderIconButton
102 key={action.accessibilityLabel}
103 accessibilityLabel={action.accessibilityLabel}
104 disabled={action.disabled}
105 selected={action.selected}
106 icon={action.icon}
107 onPress={action.onPress}
108 />
109 ),
110 )}
111 {overflowActions.length > 0 ? (
112 <AndroidAnchoredMenu
113 actions={overflowActions.map((action, index) => ({
114 id: String(index),
115 title: action.accessibilityLabel,
116 attributes: {
117 disabled: Boolean(action.disabled),
118 state: action.selected ? "on" : undefined,
119 },
120 }))}
121 onPressAction={({ nativeEvent }) =>
122 overflowActions[Number(nativeEvent.event)]?.onPress()
123 }
124 >
125 {(open) => (
126 <MaterialIconButton
127 accessibilityLabel="More actions"
128 icon="ellipsis"
129 tintColorClassName="accent-header-foreground"
130 onPress={open}
131 />
132 )}
133 </AndroidAnchoredMenu>
134 ) : null}
135 {props.trailing}
136 </View>
137 </View>
138 );
139}
140
141export function AndroidSheetHeader(
142 props: Omit<Parameters<typeof AndroidScreenHeader>[0], "embedded">,
143) {
144 return <AndroidScreenHeader {...props} embedded />;
145}