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