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

AppBar.android.tsx

294 lines9.7 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 { 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();
24​
25interface SearchState {
26 readonly searching: boolean;
27 readonly open: () => void;
28 readonly close: () => void;
29 readonly inputRef: RefObject<TextInputInstance | null>;
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 );
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 }} />;
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 actions={
208 search && !inline
209 ? [
210 {
211 accessibilityLabel: searchLabel,
212 icon: "magnifyingglass",
213 onPress: openSearch,
214 },
215 ...(actions ?? []),
216 ]
217 : actions
218 }
219 trailing={
220 menuView?.length || props.trailing || props.confirm || closeInspector ? (
221 <>
222 {menuView}
223 {props.trailing}
224 {confirmButton}
225 {closeInspector ? (
226 <AndroidHeaderIconButton
227 accessibilityLabel="Close inspector"
228 icon="xmark"
229 onPress={closeInspector}
230 />
231 ) : null}
232 </>
233 ) : undefined
234 }
235 />
236 );
237 if (!search) {
238 return (
239 <>
240 {options}
241 {header}
242 </>
243 );
244 }
245 return (
246 <>
247 {options}
248 <View>
249 <View
250 pointerEvents={searching ? "none" : "auto"}
251 accessibilityElementsHidden={searching}
252 importantForAccessibility={searching ? "no-hide-descendants" : "auto"}
253 style={{ opacity: searching ? 0 : 1 }}
254 >
255 {header}
256 </View>
257 {searching ? (
258 <View
259 className="absolute inset-0 bg-header"
260 // Match Material TopAppBar's 4dp icon inset so buttons stay where the bar drew them.
261 style={{ paddingTop, paddingBottom, paddingHorizontal: 4 }}
262 >
263 <View className="flex-1 flex-row items-center gap-1">
264 {inline ? (
265 props.onClose ? (
266 <ScreenHeaderButton
267 accessibilityLabel={props.closeAccessibilityLabel ?? "Close"}
268 icon="xmark"
269 onPress={props.onClose}
270 />
271 ) : props.onBack ? (
272 <ScreenHeaderButton
273 accessibilityLabel="Navigate up"
274 icon="arrow.left"
275 onPress={props.onBack}
276 />
277 ) : null
278 ) : (
279 <ScreenHeaderButton
280 accessibilityLabel={search.closeAccessibilityLabel ?? "Close search"}
281 icon="arrow.left"
282 onPress={closeSearch}
283 />
284 )}
285 {searchField}
286 {inline ? menuView : null}
287 {inline ? confirmButton : null}
288 </View>
289 </View>
290 ) : null}
291 </View>
292 </>
293 );