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