Open History to see the last commit
1import { useGT } from "gt-react-native";
2import { Box, Icon, IconButton, RNHostView } from "@expo/ui/jetpack-compose";
3import { semantics, size } from "@expo/ui/jetpack-compose/modifiers";
4import type { MenuAction } from "@react-native-menu/menu";
5import {
6 createContext,
7 use,
8 useLayoutEffect,
9 useState,
10 useSyncExternalStore,
11 type ReactNode,
12} from "react";
13import { Pressable, View } from "react-native";
14
15import { AndroidAnchoredMenu } from "./AndroidAnchoredMenu.android";
16import { AndroidHeaderIconButton } from "./AndroidScreenHeader.android";
17import { AppText } from "./AppText";
18import { NativeAppBar } from "./NativeAppBar.android";
19import { useControlTheme } from "./useControlTheme";
20import { useToolbarLayout } from "./useToolbarLayout";
21
22const SEARCH_ICON = require("../../../assets/icons/search.xml");
23const FILTER_ICON = require("../../../assets/icons/filter.xml");
24const FILTER_FILLED_ICON = require("../../../assets/icons/filter-filled.xml");
25// Material TopAppBar: 48dp icon buttons, the last one 4dp from the end.
26const ACTION_SIZE = 48;
27const ACTION_END_INSET = 4;
28
29export interface WorkspaceAppBarProps {
30 readonly title: string;
31 readonly titleContent?: ReactNode;
32 readonly search?: {
33 readonly accessibilityLabel: string;
34 readonly active: boolean;
35 readonly field: ReactNode;
36 readonly onOpen: () => void;
37 readonly onClose: () => void;
38 };
39 /** Stays trailing while searching, so it never moves and still narrows the results. */
40 readonly filter?: {
41 readonly accessibilityLabel: string;
42 readonly customized: boolean;
43 readonly actions: MenuAction[];
44 readonly onSelect: (id: string) => void;
45 };
46}
47
48function createWorkspaceAppBarStore() {
49 let owner: object | null = null;
50 let content: WorkspaceAppBarProps | null = null;
51 const listeners = new Set<() => void>();
52 const emit = () => {
53 for (const listener of listeners) listener();
54 };
55 return {
56 subscribe: (listener: () => void) => {
57 listeners.add(listener);
58 return () => {
59 listeners.delete(listener);
60 };
61 },
62 get: () => content,
63 set: (nextOwner: object, next: WorkspaceAppBarProps) => {
64 owner = nextOwner;
65 content = next;
66 emit();
67 },
68 clear: (previousOwner: object) => {
69 if (owner !== previousOwner) return;
70 owner = null;
71 content = null;
72 emit();
73 },
74 };
75}
76
77const WorkspaceAppBarStoreContext = createContext<ReturnType<
78 typeof createWorkspaceAppBarStore
79> | null>(null);
80
81/**
82 * Hosts one app bar above a set of workspace tab panes. Hidden panes lay out at zero size, and a
83 * Compose bar inside one takes frames to measure again when it returns, so the bar lives here and
84 * the visible tab projects its contents into it.
85 */
86export function WorkspaceAppBarScope(props: {
87 readonly children: ReactNode;
88 /** Off, panes draw their own bars; the children keep their place so toggling keeps their state. */
89 readonly enabled?: boolean;
90}) {
91 const [store] = useState(createWorkspaceAppBarStore);
92 const enabled = props.enabled ?? true;
93 return (
94 <WorkspaceAppBarStoreContext value={enabled ? store : null}>
95 {enabled ? <WorkspaceAppBarHost /> : null}
96 {props.children}
97 </WorkspaceAppBarStoreContext>
98 );
99}
100
101function WorkspaceAppBarHost() {
102 const store = use(WorkspaceAppBarStoreContext)!;
103 const content = useSyncExternalStore(store.subscribe, store.get, store.get);
104 return <WorkspaceAppBarView {...(content ?? { title: "" })} />;
105}
106
107export function useHasWorkspaceAppBar() {
108 return use(WorkspaceAppBarStoreContext) !== null;
109}
110
111/** Shows a tab's header in the enclosing scope's bar, or inline outside one. */
112export function WorkspaceAppBar(props: WorkspaceAppBarProps) {
113 const store = use(WorkspaceAppBarStoreContext);
114 const [owner] = useState(() => ({}));
115 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- projects this tab's header into the out-of-tree workspace bar after every commit
116 useLayoutEffect(() => {
117 store?.set(owner, props);
118 });
119 // Hidden panes destroy their effects, so the tab leaving the screen releases the bar.
120 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- releases the workspace bar when this tab hides or unmounts
121 useLayoutEffect(() => (store ? () => store.clear(owner) : undefined), [store, owner]);
122 return store ? null : <WorkspaceAppBarView {...props} />;
123}
124
125/**
126 * Every tab draws the same bar, so a switch only changes its contents. The actions are Compose nodes
127 * and the title host keeps one height: React Native content hosted in Compose lags Compose's layout
128 * by a frame whenever its size changes, which showed as the filter jumping between tabs.
129 */
130function WorkspaceAppBarView(props: WorkspaceAppBarProps) {
131 const gt = useGT();
132 const { search, filter } = props;
133 const searching = search?.active === true;
134 const { colors } = useControlTheme();
135 const tint = colors["--color-header-foreground"];
136 const { height, paddingTop, paddingBottom } = useToolbarLayout();
137 const filterMenu = filter ? (
138 <AndroidAnchoredMenu
139 actions={filter.actions}
140 onPressAction={({ nativeEvent }) => filter.onSelect(nativeEvent.event)}
141 >
142 {(open) =>
143 searching ? (
144 <AndroidHeaderIconButton
145 accessibilityLabel={filter.accessibilityLabel}
146 icon={
147 filter.customized
148 ? "line.3.horizontal.decrease.circle.fill"
149 : "line.3.horizontal.decrease.circle"
150 }
151 onPress={open}
152 />
153 ) : (
154 // The bar draws the icon; this takes the touch so the menu can anchor to the slot.
155 <Pressable
156 accessibilityLabel={filter.accessibilityLabel}
157 accessibilityRole="button"
158 android_ripple={{ borderless: true, radius: 20 }}
159 onPress={open}
160 style={{ width: ACTION_SIZE, height: ACTION_SIZE }}
161 />
162 )
163 }
164 </AndroidAnchoredMenu>
165 ) : null;
166 return (
167 <View className="bg-header" style={{ paddingTop, paddingBottom }}>
168 {searching ? (
169 <View
170 className="flex-row items-center gap-1"
171 style={{ height, paddingHorizontal: ACTION_END_INSET }}
172 >
173 <AndroidHeaderIconButton
174 accessibilityLabel={gt("Close search")}
175 icon="arrow.left"
176 onPress={search.onClose}
177 />
178 {search.field}
179 {filterMenu}
180 </View>
181 ) : (
182 <View>
183 <NativeAppBar
184 title={
185 <RNHostView matchContents={{ vertical: true }}>
186 <View
187 collapsable={false}
188 className="justify-center"
189 style={{ height: ACTION_SIZE }}
190 >
191 {props.titleContent ?? (
192 <AppText textRole="title" numberOfLines={1} className="text-header-foreground">
193 {props.title}
194 </AppText>
195 )}
196 </View>
197 </RNHostView>
198 }
199 // Both slots always exist: Expo UI applies a child insert or removal a frame before the
200 // other children's new props, so changing the slot count drew the wrong glyph mid-switch.
201 nativeActions={
202 <>
203 <IconButton
204 enabled={search !== undefined}
205 onClick={search?.onOpen}
206 modifiers={[
207 size(ACTION_SIZE, ACTION_SIZE),
208 ...(search
209 ? [semantics({ contentDescription: search.accessibilityLabel })]
210 : []),
211 ]}
212 >
213 <Icon source={SEARCH_ICON} size={24} tint={search ? tint : "transparent"} />
214 </IconButton>
215 <Box contentAlignment="center" modifiers={[size(ACTION_SIZE, ACTION_SIZE)]}>
216 <Icon
217 source={filter?.customized ? FILTER_FILLED_ICON : FILTER_ICON}
218 size={24}
219 tint={filter ? tint : "transparent"}
220 />
221 </Box>
222 </>
223 }
224 />
225 {filter ? (
226 <View
227 className="absolute"
228 style={{ right: ACTION_END_INSET, top: (height - ACTION_SIZE) / 2 }}
229 >
230 {filterMenu}
231 </View>
232 ) : null}
233 </View>
234 )}
235 </View>
236 );
237}