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

ScreenHeader.android.tsx

203 lines7.0 KB

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