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

AndroidScreenHeader.tsx

134 lines4.5 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 <MaterialIconButton {...props} tintColorClassName="accent-header-foreground" />;
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 tintColorClassName="accent-header-foreground"
69 onPress={props.onBack}
70 />
71 ) : null}
72​
73 {props.leading}
74​
75 <View className={cn("min-w-0 flex-1", !props.onBack && "pl-1")}>
76 <Text numberOfLines={1} style={titleTypography} className="text-header-foreground">
77 {props.title}
78 </Text>
79 {props.subtitle ? (
80 <Text
81 numberOfLines={1}
82 style={subtitleTypography}
83 className="mt-px text-[13px] font-t3-medium text-foreground-muted"
84 >
85 {props.subtitle}
86 </Text>
87 ) : null}
88 </View>
89​
90 {visibleActions.map((action) => (
91 <AndroidHeaderIconButton
92 key={action.accessibilityLabel}
93 accessibilityLabel={action.accessibilityLabel}
94 disabled={action.disabled}
95 selected={action.selected}
96 icon={action.icon}
97 onPress={action.onPress}
98 />
99 ))}
100 {overflowActions.length > 0 ? (
101 <AndroidAnchoredMenu
102 actions={overflowActions.map((action, index) => ({
103 id: String(index),
104 title: action.accessibilityLabel,
105 attributes: {
106 disabled: Boolean(action.disabled),
107 state: action.selected ? "on" : undefined,
108 },
109 }))}
110 onPressAction={({ nativeEvent }) =>
111 overflowActions[Number(nativeEvent.event)]?.onPress()
112 }
113 >
114 {(open) => (
115 <MaterialIconButton
116 accessibilityLabel="More actions"
117 icon="ellipsis"
118 tintColorClassName="accent-header-foreground"
119 onPress={open}
120 />
121 )}
122 </AndroidAnchoredMenu>
123 ) : null}
124 {props.trailing}
125 </View>
126 </View>
127 );
129​
130export function AndroidSheetHeader(
131 props: Omit<Parameters<typeof AndroidScreenHeader>[0], "embedded">,
132) {
133 return <AndroidScreenHeader {...props} embedded />;