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