Open History to see the last commit
1import { useState, type ReactNode } from "react";
2import { View } from "react-native";
3import { useSafeAreaInsets } from "react-native-safe-area-context";
4
5import type { AppSymbolName } from "./AppSymbol";
6import { AppText as Text } from "./AppText";
7import { cn } from "../lib/cn";
8import { MaterialIconButton } from "./MaterialIconButton";
9import { AndroidAnchoredMenu } from "./AndroidAnchoredMenu";
10import { useScaledTextRole } from "../features/settings/appearance/useScaledTextRole";
11import { useMaterialToolbarHeight } from "./useMaterialToolbarHeight";
12
13export interface AndroidHeaderAction {
14 readonly accessibilityLabel: string;
15 readonly icon: AppSymbolName;
16 readonly onPress: () => void;
17 readonly disabled?: 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} variant={props.selected ? "tonal" : "standard"} />;
29}
30
31export function AndroidScreenHeader(props: {
32 readonly title: string;
33 readonly subtitle?: string | null;
34 readonly actions?: ReadonlyArray<AndroidHeaderAction>;
35 readonly leading?: ReactNode;
36 readonly trailing?: ReactNode;
37 readonly onBack?: () => void;
38 readonly embedded?: boolean;
39 readonly hideBottomBorder?: boolean;
40}) {
41 const insets = useSafeAreaInsets();
42 const titleTypography = useScaledTextRole("title");
43 const subtitleTypography = useScaledTextRole("label");
44 const materialToolbarHeight = useMaterialToolbarHeight();
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 pb-2"
55 style={{
56 paddingTop: props.embedded ? 8 : Math.max(insets.top, 12),
57 borderBottomWidth: props.hideBottomBorder ? 0 : undefined,
58 }}
59 >
60 <View
61 style={{ minHeight: materialToolbarHeight }}
62 className="min-h-14 flex-row items-center gap-1"
63 >
64 {props.onBack ? (
65 <MaterialIconButton
66 accessibilityLabel="Navigate up"
67 icon="arrow.left"
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 <Text numberOfLines={1} style={titleTypography} className="text-foreground">
76 {props.title}
77 </Text>
78 {props.subtitle ? (
79 <Text
80 numberOfLines={1}
81 style={subtitleTypography}
82 className="mt-px text-[13px] font-t3-medium text-foreground-muted"
83 >
84 {props.subtitle}
85 </Text>
86 ) : null}
87 </View>
88
89 {visibleActions.map((action) => (
90 <AndroidHeaderIconButton
91 key={action.accessibilityLabel}
92 accessibilityLabel={action.accessibilityLabel}
93 disabled={action.disabled}
94 selected={action.selected}
95 icon={action.icon}
96 onPress={action.onPress}
97 />
98 ))}
99 {overflowActions.length > 0 ? (
100 <AndroidAnchoredMenu
101 actions={overflowActions.map((action, index) => ({
102 id: String(index),
103 title: action.accessibilityLabel,
104 attributes: {
105 disabled: Boolean(action.disabled),
106 state: action.selected ? "on" : undefined,
107 },
108 }))}
109 onPressAction={({ nativeEvent }) =>
110 overflowActions[Number(nativeEvent.event)]?.onPress()
111 }
112 >
113 {(open) => (
114 <MaterialIconButton
115 accessibilityLabel="More actions"
116 icon="ellipsis"
117 onPress={open}
118 />
119 )}
120 </AndroidAnchoredMenu>
121 ) : null}
122 {props.trailing}
123 </View>
124 </View>
125 );
126}
127
128export function AndroidSheetHeader(
129 props: Omit<Parameters<typeof AndroidScreenHeader>[0], "embedded">,
130) {
131 return <AndroidScreenHeader {...props} embedded />;
132}