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

AppBar.android.tsx

225 lines7.8 KB

1import { useRef, useState } from "react";
2import { Keyboard, Pressable, TextInput, type TextInputInstance, View } from "react-native";
3import { useHardwareBack } from "../../lib/backHandler";
4import { useToolbarLayout } from "./useToolbarLayout";
5import { NativeStackScreenOptions } from "../../native/StackHeader";
6import { AndroidWorkspaceSidebarButton } from "../../features/layout/workspace-sidebar-toolbar";
7import { useAppearancePreferences } from "../../features/settings/appearance/AppearancePreferencesProvider";
8import { AndroidScreenHeader } from "./AndroidScreenHeader";
9import { ScreenHeaderButton } from "./ScreenHeaderButton.android";
10import { SymbolView } from "./AppSymbol";
11import { Menu } from "./Menu";
12import { SearchField } from "./SearchField";
13import { androidHeaderMenuActions, findHeaderMenuAction } from "./headerMenu.android";
14import type { AppBarProps } from "./AppBar.types";
15import { useAndroidControlSizing } from "./useAndroidControlSizing";
16​
17export function AppBar(props: AppBarProps) {
18 const { search } = props;
19 const { paddingTop, paddingBottom } = useToolbarLayout();
20 const { scale, buttonSize, iconSize, smallIconSize } = useAndroidControlSizing();
21 const { themeVariables } = useAppearancePreferences();
22 const inputRef = useRef<TextInputInstance>(null);
23 const [searchOpen, setSearchOpen] = useState(false);
24 const searching = search !== undefined && (searchOpen || search.value.length > 0);
25 const onSearchChange = search?.onChangeText;
26 const closeSearch = () => {
27 onSearchChange?.("");
28 setSearchOpen(false);
29 Keyboard.dismiss();
30 };
31 useHardwareBack(searching && search?.mode !== "inline", () => {
32 closeSearch();
33 return true;
34 });
35 const menuView = props.menus?.map((menu) => (
36 <Menu
37 key={menu.title}
38 actions={androidHeaderMenuActions(menu.items)}
39 isAnchoredToRight
40 title={menu.status ?? menu.title}
41 onPressAction={({ nativeEvent }) => {
42 const action = findHeaderMenuAction(menu.items, nativeEvent.event);
43 if (action && !action.disabled) action.onPress();
44 }}
45 >
46 {search?.mode === "inline" ? (
47 <Pressable
48 accessibilityLabel={menu.title}
49 accessibilityRole="button"
50 className="items-center justify-center rounded-full bg-subtle"
51 style={{ width: buttonSize, height: buttonSize }}
52 >
53 <SymbolView
54 name={menu.icon}
55 size={smallIconSize}
56 tintColorClassName="accent-header-foreground"
57 type="monochrome"
58 />
59 </Pressable>
60 ) : (
61 <ScreenHeaderButton accessibilityLabel={menu.title} icon={menu.icon} />
62 )}
63 </Menu>
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"
84 style={{
85 paddingTop,
86 paddingBottom: 8.75 * scale,
87 borderBottomWidth: 0,
88 }}
89 >
90 <View className="flex-row items-center gap-2" style={{ minHeight: buttonSize }}>
91 {props.onBack ? (
92 <Pressable
93 accessibilityLabel="Navigate up"
94 accessibilityRole="button"
95 hitSlop={8}
96 onPress={props.onBack}
97 className="items-center justify-center"
98 style={{ width: buttonSize, height: buttonSize }}
99 >
100 <SymbolView
101 name="chevron.left"
102 size={iconSize}
103 tintColorClassName="accent-header-foreground"
104 type="monochrome"
105 />
106 </Pressable>
107 ) : null}
108 <View
109 className="flex-1 flex-row items-center bg-input"
110 style={{
111 minHeight: Math.max(48, 38.5 * scale),
112 gap: 8.75 * scale,
113 borderRadius: 14 * scale,
114 paddingHorizontal: 12.25 * scale,
115 }}
116 >
117 <SymbolView
118 name="magnifyingglass"
119 size={Math.round(17 * scale)}
120 tintColorClassName="accent-header-foreground"
121 type="monochrome"
122 />
123 <TextInput
124 accessibilityLabel={search.placeholder}
125 autoCapitalize="none"
126 onChangeText={search.onChangeText}
127 value={search.value}
128 placeholder={
129 search.compactToolbar
130 ? (search.compactPlaceholder ?? search.placeholder)
131 : search.placeholder
132 }
133 placeholderTextColorClassName="accent-placeholder"
134 className="flex-1 text-base font-sans text-header-foreground"
135 style={{ paddingVertical: 7 * scale }}
136 />
137 </View>
138 {menuView}
139 </View>
140 </View>
141 </>
142 );
143 }
144 const header = (
145 <AndroidScreenHeader
146 title={props.title}
147 subtitle={props.subtitle}
148 onBack={props.onBack}
149 leading={props.sidebar !== false ? <AndroidWorkspaceSidebarButton /> : undefined}
150 hideBottomBorder={props.hideBottomBorder}
151 compact={props.compact}
152 actions={
153 search
154 ? [
155 {
156 accessibilityLabel: search.placeholder,
157 icon: "magnifyingglass",
158 onPress: () => setSearchOpen(true),
159 },
160 ...(search.refreshInToolbar && search.onRefresh
161 ? [
162 {
163 accessibilityLabel: search.refreshAccessibilityLabel ?? "Refresh",
164 icon: "arrow.clockwise" as const,
165 onPress: search.onRefresh,
166 },
167 ]
168 : []),
169 ...(props.actions ?? []),
170 ]
171 : props.actions
172 }
173 trailing={
174 menuView?.length || props.trailing ? (
175 <>
176 {menuView}
177 {props.trailing}
178 </>
179 ) : undefined
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
198 className="absolute inset-0 bg-header"
199 // Match Material TopAppBar's navigation inset so the back button stays put.
200 style={{ paddingTop, paddingBottom, paddingHorizontal: 4 }}
201 >
202 <View className="flex-1 flex-row items-center gap-1">
203 <ScreenHeaderButton
204 accessibilityLabel={search.closeAccessibilityLabel ?? "Close search"}
205 icon="arrow.left"
206 onPress={closeSearch}
207 />
208 <SearchField
209 inputRef={inputRef}
210 accessibilityLabel={search.placeholder}
211 clearAccessibilityLabel={search.clearAccessibilityLabel ?? "Clear search"}
212 placeholder={search.placeholder}
213 value={search.value}
214 onChangeText={search.onChangeText}
215 />
216 </View>
217 </View>
218 ) : null}
219 </View>
220 ) : (
221 header
222 )}
223 </>
224 );