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

AppBar.android.tsx

259 lines8.4 KB

1import { type RefObject, use, useRef, useState } from "react";
2import { Keyboard, type TextInputInstance, View } from "react-native";
3import { useHardwareBack } from "../../lib/backHandler";
4import { useToolbarLayout } from "./useToolbarLayout";
5import { NativeStackScreenOptions } from "../../native/StackHeader";
6import { useSidebarToggle } from "../../features/layout/useSidebarToggle";
7import { InspectorCloseContext } from "../../features/layout/inspector-bar-store";
8import { useAppearancePreferences } from "../../features/settings/appearance/AppearancePreferencesProvider";
9import { AndroidHeaderIconButton, AndroidScreenHeader } from "./AndroidScreenHeader";
10import { ScreenHeaderButton } from "./ScreenHeaderButton.android";
11import { Menu } from "./Menu";
12import { SearchField } from "./SearchField";
13import { androidHeaderMenuActions, findHeaderMenuAction } from "./headerMenu.android";
14import type { AppBarMenuItem, AppBarProps, AppBarSearch } from "./AppBar.types";
15import { useHasWorkspaceAppBar, WorkspaceAppBar } from "./WorkspaceAppBar.android";
16​
17function selectMenuItem(items: ReadonlyArray<AppBarMenuItem>, id: string) {
18 const action = findHeaderMenuAction(items, id);
19 if (action && !action.disabled) action.onPress();
21​
22interface SearchState {
23 readonly searching: boolean;
24 readonly open: () => void;
25 readonly close: () => void;
26 readonly inputRef: RefObject<TextInputInstance | null>;
28​
29export function AppBar(props: AppBarProps) {
30 return props.search ? (
31 <SearchableAppBar {...props} search={props.search} />
32 ) : (
33 <AppBarView {...props} />
34 );
36​
37// Only a bar with search registers for hardware back: its focus check needs a navigation
38// context, and sheet headers such as Arrange threads render outside one.
39function SearchableAppBar(props: AppBarProps & { readonly search: AppBarSearch }) {
40 const { search } = props;
41 const [searchOpen, setSearchOpen] = useState(false);
42 const inputRef = useRef<TextInputInstance>(null);
43 const inline = search.mode === "inline";
44 const controlled = search.active !== undefined;
45 const searching =
46 inline || (controlled ? search.active === true : searchOpen || search.value.length > 0);
47 const open = () => {
48 if (!controlled) setSearchOpen(true);
49 search.onOpen?.();
50 inputRef.current?.focus();
51 };
52 const close = () => {
53 if (!controlled) {
54 search.onChangeText("");
55 setSearchOpen(false);
56 Keyboard.dismiss();
57 }
58 search.onClose?.();
59 };
60 useHardwareBack(searching && !inline && !controlled, () => {
61 close();
62 return true;
63 });
64 return <AppBarView {...props} searchState={{ searching, open, close, inputRef }} />;
66​
67function AppBarView(props: AppBarProps & { readonly searchState?: SearchState }) {
68 const { search, searchState } = props;
69 const { paddingTop, paddingBottom } = useToolbarLayout(props.sheet);
70 const { themeVariables } = useAppearancePreferences();
71 const inWorkspace = useHasWorkspaceAppBar();
72 const sidebarToggle = useSidebarToggle();
73 const closeInspector = use(InspectorCloseContext);
74 const inline = search?.mode === "inline";
75 const searching = searchState?.searching === true;
76 const actions = props.refresh
77 ? [
78 ...(props.actions ?? []),
79 { ...props.refresh, icon: "arrow.clockwise" as const, overflowOnly: true },
80 ]
81 : props.actions;
82 const openSearch = () => searchState?.open();
83 const closeSearch = () => searchState?.close();
84​
85 const searchLabel = search?.accessibilityLabel ?? search?.placeholder ?? "";
86 const searchField =
87 search && searchState ? (
88 <SearchField
89 inputRef={searchState.inputRef}
90 accessibilityLabel={searchLabel}
91 clearAccessibilityLabel={search.clearAccessibilityLabel ?? "Clear search"}
92 placeholder={
93 search.compactToolbar
94 ? (search.compactPlaceholder ?? search.placeholder)
95 : search.placeholder
96 }
97 value={search.value}
98 onChangeText={search.onChangeText}
99 autoFocus={!inline}
100 />
101 ) : null;
102​
103 if (inWorkspace) {
104 const menu = props.menus?.[0];
105 const icon = typeof menu?.icon === "string" ? menu.icon : menu?.icon.android;
106 return (
107 <WorkspaceAppBar
108 title={props.title}
109 titleContent={props.titleContent}
110 search={
111 search
112 ? {
113 accessibilityLabel: searchLabel,
114 active: searching,
115 field: searchField,
116 onOpen: openSearch,
117 onClose: closeSearch,
118 }
119 : undefined
120 }
121 filter={
122 menu
123 ? {
124 accessibilityLabel: menu.title,
125 customized: icon?.endsWith(".fill") === true,
126 actions: androidHeaderMenuActions(menu.items),
127 onSelect: (id) => selectMenuItem(menu.items, id),
128 }
129 : undefined
130 }
131 />
132 );
133 }
134​
135 const menuView = props.menus?.map((menu) => (
136 <Menu
137 key={menu.title}
138 actions={androidHeaderMenuActions(menu.items)}
139 isAnchoredToRight
140 title={menu.status ?? menu.title}
141 onPressAction={({ nativeEvent }) => selectMenuItem(menu.items, nativeEvent.event)}
142 >
143 <ScreenHeaderButton accessibilityLabel={menu.title} icon={menu.icon} />
144 </Menu>
145 ));
146 const options =
147 props.embedded || props.sheet ? null : (
148 <NativeStackScreenOptions
149 options={{
150 ...props.options,
151 ...(props.matchSearchSurface
152 ? { contentStyle: { backgroundColor: themeVariables["--color-header"] } }
153 : undefined),
154 headerShown: false,
155 title: props.title,
156 }}
157 optionsVersion={props.optionsVersion}
158 />
159 );
160 const header = (
161 <AndroidScreenHeader
162 title={props.title}
163 titleContent={props.titleContent}
164 subtitle={props.subtitle}
165 onBack={props.onBack}
166 leading={
167 props.leading ??
168 // Only a real button: any leading content takes the bar's navigation slot.
169 (sidebarToggle && !props.sheet ? (
170 <AndroidHeaderIconButton
171 accessibilityLabel={sidebarToggle.accessibilityLabel}
172 icon={sidebarToggle.icon}
173 onPress={sidebarToggle.onPress}
174 />
175 ) : undefined)
176 }
177 embedded={props.sheet}
178 hideBottomBorder={props.hideBottomBorder}
179 compact={props.compact}
180 actions={
181 search && !inline
182 ? [
183 {
184 accessibilityLabel: searchLabel,
185 icon: "magnifyingglass",
186 onPress: openSearch,
187 },
188 ...(actions ?? []),
189 ]
190 : actions
191 }
192 trailing={
193 menuView?.length || props.trailing || closeInspector ? (
194 <>
195 {menuView}
196 {props.trailing}
197 {closeInspector ? (
198 <AndroidHeaderIconButton
199 accessibilityLabel="Close inspector"
200 icon="xmark"
201 onPress={closeInspector}
202 />
203 ) : null}
204 </>
205 ) : undefined
206 }
207 />
208 );
209 if (!search) {
210 return (
211 <>
212 {options}
213 {header}
214 </>
215 );
216 }
217 return (
218 <>
219 {options}
220 <View>
221 <View
222 pointerEvents={searching ? "none" : "auto"}
223 accessibilityElementsHidden={searching}
224 importantForAccessibility={searching ? "no-hide-descendants" : "auto"}
225 style={{ opacity: searching ? 0 : 1 }}
226 >
227 {header}
228 </View>
229 {searching ? (
230 <View
231 className="absolute inset-0 bg-header"
232 // Match Material TopAppBar's 4dp icon inset so buttons stay where the bar drew them.
233 style={{ paddingTop, paddingBottom, paddingHorizontal: 4 }}
234 >
235 <View className="flex-1 flex-row items-center gap-1">
236 {inline ? (
237 props.onBack ? (
238 <ScreenHeaderButton
239 accessibilityLabel="Navigate up"
240 icon="arrow.left"
241 onPress={props.onBack}
242 />
243 ) : null
244 ) : (
245 <ScreenHeaderButton
246 accessibilityLabel={search.closeAccessibilityLabel ?? "Close search"}
247 icon="arrow.left"
248 onPress={closeSearch}
249 />
250 )}
251 {searchField}
252 {inline ? menuView : null}
253 </View>
254 </View>
255 ) : null}
256 </View>
257 </>
258 );