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

AndroidScreenHeader.tsx

142 lines4.8 KB

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