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

ScreenHeader.android.tsx

221 lines7.8 KB

1import { useCallback, useEffect, useRef, useState } from "react";
2import { BackHandler, Keyboard, Pressable, TextInput, View } from "react-native";
3import { useMaterialToolbarLayout } from "./useMaterialToolbarLayout";
4import { NativeStackScreenOptions } from "../native/StackHeader";
5import { AndroidWorkspaceSidebarButton } from "../features/layout/workspace-sidebar-toolbar";
6import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
7import { AndroidScreenHeader } from "./AndroidScreenHeader";
8import { ScreenHeaderButton } from "./ScreenHeaderButton.android";
9import { SymbolView } from "./AppSymbol";
10import { ControlPillMenu } from "./ControlPill";
11import { MaterialSearchField } from "./MaterialSearchField";
12import { androidHeaderMenuActions, findHeaderMenuAction } from "./headerMenu.android";
13import type { ScreenHeaderProps } from "./ScreenHeader.types";
14import { useAndroidControlSizing } from "./useAndroidControlSizing";
15​
16export function ScreenHeader(props: ScreenHeaderProps) {
17 const { search } = props;
18 const { paddingTop, paddingBottom } = useMaterialToolbarLayout();
19 const { scale, buttonSize, iconSize, smallIconSize } = useAndroidControlSizing();
20 const { themeVariables } = useAppearancePreferences();
21 const inputRef = useRef<TextInput>(null);
22 const [searchOpen, setSearchOpen] = useState(false);
23 const searching = search !== undefined && (searchOpen || search.value.length > 0);
24 const onSearchChange = search?.onChangeText;
25 const closeSearch = useCallback(() => {
26 onSearchChange?.("");
27 setSearchOpen(false);
28 Keyboard.dismiss();
29 }, [onSearchChange]);
30 useEffect(() => {
31 if (!searching || search?.mode === "inline") return;
32 const subscription = BackHandler.addEventListener("hardwareBackPress", () => {
33 closeSearch();
34 return true;
35 });
36 return () => subscription.remove();
37 }, [closeSearch, searching, search?.mode]);
38 const menuView = props.menus?.map((menu) => (
39 <ControlPillMenu
40 key={menu.title}
41 actions={androidHeaderMenuActions(menu.items)}
42 isAnchoredToRight
43 title={menu.status ?? menu.title}
44 onPressAction={({ nativeEvent }) => {
45 const action = findHeaderMenuAction(menu.items, nativeEvent.event);
46 if (action && !action.disabled) action.onPress();
47 }}
48 >
49 {search?.mode === "inline" ? (
50 <Pressable
51 accessibilityLabel={menu.title}
52 accessibilityRole="button"
53 className="items-center justify-center rounded-full bg-subtle"
54 style={{ width: buttonSize, height: buttonSize }}
55 >
56 <SymbolView
57 name={menu.icon}
58 size={smallIconSize}
59 tintColorClassName="accent-header-foreground"
60 type="monochrome"
61 />
62 </Pressable>
63 ) : (
64 <ScreenHeaderButton accessibilityLabel={menu.title} icon={menu.icon} />
65 )}
66 </ControlPillMenu>
67 ));
68 const options = (
69 <NativeStackScreenOptions
70 options={{
71 ...props.options,
72 ...(props.matchSearchSurface
73 ? { contentStyle: { backgroundColor: themeVariables["--color-header"] } }
74 : undefined),
75 headerShown: false,
76 title: props.title,
77 }}
78 optionsVersion={props.optionsVersion}
79 />
80 );
81 if (search?.mode === "inline") {
82 return (
83 <>
84 {options}
85 <View
86 className="border-b border-header-border bg-header px-3"
87 style={{
88 paddingTop,
89 paddingBottom: 8.75 * scale,
90 borderBottomWidth: 0,
91 }}
92 >
93 <View className="flex-row items-center gap-2" style={{ minHeight: buttonSize }}>
94 {props.onBack ? (
95 <Pressable
96 accessibilityLabel="Navigate up"
97 accessibilityRole="button"
98 hitSlop={8}
99 onPress={props.onBack}
100 className="items-center justify-center"
101 style={{ width: buttonSize, height: buttonSize }}
102 >
103 <SymbolView
104 name="chevron.left"
105 size={iconSize}
106 tintColorClassName="accent-header-foreground"
107 type="monochrome"
108 />
109 </Pressable>
110 ) : null}
111 <View
112 className="flex-1 flex-row items-center bg-input"
113 style={{
114 minHeight: Math.max(48, 38.5 * scale),
115 gap: 8.75 * scale,
116 borderRadius: 14 * scale,
117 paddingHorizontal: 12.25 * scale,
118 }}
119 >
120 <SymbolView
121 name="magnifyingglass"
122 size={Math.round(17 * scale)}
123 tintColorClassName="accent-header-foreground"
124 type="monochrome"
125 />
126 <TextInput
127 accessibilityLabel={search.placeholder}
128 autoCapitalize="none"
129 onChangeText={search.onChangeText}
130 value={search.value}
131 placeholder={
132 search.compactToolbar
133 ? (search.compactPlaceholder ?? search.placeholder)
134 : search.placeholder
135 }
136 placeholderTextColorClassName="accent-placeholder"
137 className="flex-1 text-base font-sans text-header-foreground"
138 style={{ paddingVertical: 7 * scale }}
139 />
140 </View>
141 {menuView}
142 </View>
143 </View>
144 </>
145 );
146 }
147 const header = (
148 <AndroidScreenHeader
149 title={props.title}
150 subtitle={props.subtitle}
151 onBack={props.onBack}
152 leading={props.sidebar !== false ? <AndroidWorkspaceSidebarButton /> : undefined}
153 hideBottomBorder={props.hideBottomBorder}
154 actions={
155 search
156 ? [
157 {
158 accessibilityLabel: search.placeholder,
159 icon: "magnifyingglass",
160 onPress: () => setSearchOpen(true),
161 },
162 ...(search.refreshInToolbar && search.onRefresh
163 ? [
164 {
165 accessibilityLabel: search.refreshAccessibilityLabel ?? "Refresh",
166 icon: "arrow.clockwise" as const,
167 onPress: search.onRefresh,
168 },
169 ]
170 : []),
171 ...(props.actions ?? []),
172 ]
173 : props.actions
174 }
175 trailing={
176 <>
177 {menuView}
178 {props.trailing}
179 </>
180 }
181 />
182 );
183 return (
184 <>
185 {options}
186 {search ? (
187 <View>
188 <View
189 pointerEvents={searching ? "none" : "auto"}
190 accessibilityElementsHidden={searching}
191 importantForAccessibility={searching ? "no-hide-descendants" : "auto"}
192 style={{ opacity: searching ? 0 : 1 }}
193 >
194 {header}
195 </View>
196 {searching ? (
197 <View className="absolute inset-0 bg-header px-2" style={{ paddingTop, paddingBottom }}>
198 <View className="flex-1 flex-row items-center gap-1">
199 <ScreenHeaderButton
200 accessibilityLabel={search.closeAccessibilityLabel ?? "Close search"}
201 icon="arrow.left"
202 onPress={closeSearch}
203 />
204 <MaterialSearchField
205 inputRef={inputRef}
206 accessibilityLabel={search.placeholder}
207 clearAccessibilityLabel={search.clearAccessibilityLabel ?? "Clear search"}
208 placeholder={search.placeholder}
209 value={search.value}
210 onChangeText={search.onChangeText}
211 />
212 </View>
213 </View>
214 ) : null}
215 </View>
216 ) : (
217 header
218 )}
219 </>
220 );