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

AndroidScreenHeader.tsx

140 lines4.6 KB

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