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.android";
12import { ScreenHeaderButton } from "./ScreenHeaderButton.android";
13import { Menu } from "./Menu";
14import { Button } from "./Button";
15import { SearchField } from "./SearchField";
16import { findAppBarMenuItem, menuActionsFromAppBarItems } from "./menuActionItems";
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 = findAppBarMenuItem(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: menuActionsFromAppBarItems(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={menuActionsFromAppBarItems(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 <View className="self-center">
159 <Button
160 label={props.confirm.label}
161 tone="text"
162 loading={props.confirm.loading}
163 disabled={props.confirm.disabled}
164 onPress={props.confirm.onPress}
165 />
166 </View>
167 ) : null;
168 const options =
169 props.embedded || props.sheet ? null : (
170 <NativeStackScreenOptions
171 options={{
172 ...(props.matchSearchSurface
173 ? { contentStyle: { backgroundColor: themeVariables["--color-header"] } }
174 : undefined),
175 headerShown: false,
176 title: props.title,
177 }}
178 optionsVersion={props.optionsVersion}
179 />
180 );
181 const header = (
182 <AndroidScreenHeader
183 title={props.title}
184 titleContent={props.titleContent}
185 subtitle={props.subtitle}
186 onBack={props.onClose ? undefined : props.onBack}
187 leading={
188 (props.onClose ? (
189 <AndroidHeaderIconButton
190 accessibilityLabel={props.closeAccessibilityLabel ?? "Close"}
191 icon="xmark"
192 onPress={props.onClose}
193 />
194 ) : undefined) ??
195 props.leading ??
196 // Only a real button: any leading content takes the bar's navigation slot.
197 (sidebarToggle && !props.sheet ? (
198 <AndroidHeaderIconButton
199 accessibilityLabel={sidebarToggle.accessibilityLabel}
200 icon={sidebarToggle.icon}
201 onPress={sidebarToggle.onPress}
202 />
203 ) : undefined)
204 }
205 embedded={props.sheet}
206 hideBottomBorder={props.hideBottomBorder}
207 compact={props.compact}
208 actions={
209 search && !inline
210 ? [
211 {
212 accessibilityLabel: searchLabel,
213 icon: "magnifyingglass",
214 onPress: openSearch,
215 },
216 ...(actions ?? []),
217 ]
218 : actions
219 }
220 trailing={
221 menuView?.length || props.trailing || props.confirm || closeInspector ? (
222 <>
223 {menuView}
224 {props.trailing}
225 {confirmButton}
226 {closeInspector ? (
227 <AndroidHeaderIconButton
228 accessibilityLabel="Close inspector"
229 icon="xmark"
230 onPress={closeInspector}
231 />
232 ) : null}
233 </>
234 ) : undefined
235 }
236 />
237 );
238 if (!search) {
239 return (
240 <>
241 {options}
242 {header}
243 </>
244 );
245 }
246 return (
247 <>
248 {options}
249 <View>
250 <View
251 pointerEvents={searching ? "none" : "auto"}
252 accessibilityElementsHidden={searching}
253 importantForAccessibility={searching ? "no-hide-descendants" : "auto"}
254 style={{ opacity: searching ? 0 : 1 }}
255 >
256 {header}
257 </View>
258 {searching ? (
259 <View
260 className="absolute inset-0 bg-header"
261 // Match Material TopAppBar's 4dp icon inset so buttons stay where the bar drew them.
262 style={{ paddingTop, paddingBottom, paddingHorizontal: 4 }}
263 >
264 <View className="flex-1 flex-row items-center gap-1">
265 {inline ? (
266 props.onClose ? (
267 <ScreenHeaderButton
268 accessibilityLabel={props.closeAccessibilityLabel ?? "Close"}
269 icon="xmark"
270 onPress={props.onClose}
271 />
272 ) : props.onBack ? (
273 <ScreenHeaderButton
274 accessibilityLabel="Navigate up"
275 icon="arrow.left"
276 onPress={props.onBack}
277 />
278 ) : null
279 ) : (
280 <ScreenHeaderButton
281 accessibilityLabel={search.closeAccessibilityLabel ?? "Close search"}
282 icon="arrow.left"
283 onPress={closeSearch}
284 />
285 )}
286 {searchField}
287 {inline ? menuView : null}
288 {inline ? confirmButton : null}
289 </View>
290 </View>
291 ) : null}
292 </View>
293 </>
294 );
295}