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