Open History to see the last commit
1import { useNavigation, type ParamListBase } from "@react-navigation/native";
2import type {
3 NativeStackHeaderItem,
4 NativeStackHeaderItemMenu,
5 NativeStackNavigationOptions,
6 NativeStackNavigationProp,
7} from "@react-navigation/native-stack";
8import {
9 Children,
10 isValidElement,
11 useLayoutEffect,
12 useMemo,
13 type ReactElement,
14 type ReactNode,
15} from "react";
16import type { ColorValue } from "react-native";
17
18function useNativeStackNavigation(): NativeStackNavigationProp<ParamListBase> | null {
19 return useNavigation<NativeStackNavigationProp<ParamListBase>>();
20}
21
22function labelFromChildren(children: ReactNode): string {
23 const parts: string[] = [];
24 Children.forEach(children, (child) => {
25 if (typeof child === "string" || typeof child === "number") {
26 parts.push(String(child));
27 } else if (isValidElement<{ children?: ReactNode }>(child)) {
28 parts.push(labelFromChildren(child.props.children));
29 }
30 });
31 return parts.join("");
32}
33
34type NativeStackHeaderIcon = NonNullable<
35 Extract<NativeStackHeaderItem, { type: "button" }>["icon"]
36>;
37type NativeStackOptionsWithToolbar = NativeStackNavigationOptions & {
38 unstable_headerToolbarItems?: () => NativeStackHeaderItem[];
39};
40
41function iconFromProp(icon: unknown): NativeStackHeaderIcon | undefined {
42 if (typeof icon !== "string") {
43 return undefined;
44 }
45 return { type: "sfSymbol", name: icon as never };
46}
47
48type ToolbarElementProps = Record<string, unknown> & { readonly children?: ReactNode };
49
50function elementTypeName(element: ReactElement): string | undefined {
51 const type = element.type;
52 if (typeof type === "function") {
53 return (type as { displayName?: string; name?: string }).displayName ?? type.name;
54 }
55 return undefined;
56}
57
58function convertMenuAction(
59 element: ReactElement<ToolbarElementProps>,
60): NativeStackHeaderItemMenu["menu"]["items"][number] | null {
61 const typeName = elementTypeName(element);
62 if (typeName === "NativeHeaderToolbarMenuAction") {
63 const label = labelFromChildren(element.props.children);
64 return {
65 type: "action",
66 label,
67 description: typeof element.props.subtitle === "string" ? element.props.subtitle : undefined,
68 disabled: Boolean(element.props.disabled),
69 icon: iconFromProp(element.props.icon),
70 onPress:
71 typeof element.props.onPress === "function"
72 ? (element.props.onPress as () => void)
73 : () => undefined,
74 state: element.props.isOn === true ? "on" : undefined,
75 destructive: Boolean(element.props.destructive),
76 discoverabilityLabel:
77 typeof element.props.discoverabilityLabel === "string"
78 ? element.props.discoverabilityLabel
79 : undefined,
80 };
81 }
82
83 if (typeName === "NativeHeaderToolbarMenu") {
84 return {
85 type: "submenu",
86 label:
87 typeof element.props.title === "string"
88 ? element.props.title
89 : labelFromChildren(element.props.children),
90 icon: iconFromProp(element.props.icon),
91 inline: Boolean(element.props.inline),
92 items: collectMenuItems(element.props.children),
93 };
94 }
95
96 return null;
97}
98
99function collectMenuItems(children: ReactNode): NativeStackHeaderItemMenu["menu"]["items"] {
100 const items: NativeStackHeaderItemMenu["menu"]["items"] = [];
101 Children.forEach(children, (child) => {
102 if (!isValidElement<ToolbarElementProps>(child)) {
103 return;
104 }
105 const item = convertMenuAction(child);
106 if (item) {
107 items.push(item);
108 return;
109 }
110 items.push(...collectMenuItems(child.props.children));
111 });
112 return items;
113}
114
115function convertToolbarChild(child: ReactNode): NativeStackHeaderItem | null {
116 if (!isValidElement<ToolbarElementProps>(child)) {
117 return null;
118 }
119
120 const typeName = elementTypeName(child);
121 if (typeName === "NativeHeaderToolbarButton") {
122 return {
123 type: "button",
124 label: typeof child.props.label === "string" ? child.props.label : "",
125 accessibilityLabel:
126 typeof child.props.accessibilityLabel === "string"
127 ? child.props.accessibilityLabel
128 : undefined,
129 disabled: Boolean(child.props.disabled),
130 icon: iconFromProp(child.props.icon),
131 onPress:
132 typeof child.props.onPress === "function"
133 ? (child.props.onPress as () => void)
134 : () => undefined,
135 sharesBackground: !child.props.separateBackground,
136 tintColor: child.props.tintColor as ColorValue | undefined,
137 variant: "plain",
138 };
139 }
140
141 if (typeName === "NativeHeaderToolbarMenu") {
142 return {
143 type: "menu",
144 label: typeof child.props.title === "string" ? child.props.title : "",
145 accessibilityLabel:
146 typeof child.props.accessibilityLabel === "string"
147 ? child.props.accessibilityLabel
148 : undefined,
149 disabled: Boolean(child.props.disabled),
150 icon: iconFromProp(child.props.icon),
151 menu: {
152 title: typeof child.props.title === "string" ? child.props.title : undefined,
153 items: collectMenuItems(child.props.children),
154 },
155 sharesBackground: !child.props.separateBackground,
156 tintColor: child.props.tintColor as ColorValue | undefined,
157 variant: "plain",
158 };
159 }
160
161 if (typeName === "NativeHeaderToolbarSpacer") {
162 return {
163 type: "spacing",
164 spacing: typeof child.props.width === "number" ? child.props.width : 8,
165 flexible: Boolean(child.props.flexible),
166 } as NativeStackHeaderItem;
167 }
168
169 return null;
170}
171
172function collectToolbarItems(children: ReactNode): NativeStackHeaderItem[] {
173 const items: NativeStackHeaderItem[] = [];
174 Children.forEach(children, (child) => {
175 const item = convertToolbarChild(child);
176 if (item) {
177 if (item.type === "spacing") {
178 // Native inserts spacing items at `index`, treating a missing index
179 // as 0 — which would move the spacer in front of earlier siblings.
180 (item as { index?: number }).index = items.length;
181 }
182 items.push(item);
183 }
184 });
185 return items;
186}
187
188function NativeHeaderToolbarRoot(props: {
189 readonly placement?: "left" | "right" | "bottom";
190 readonly children?: ReactNode;
191}) {
192 const navigation = useNativeStackNavigation();
193 const items = useMemo(() => collectToolbarItems(props.children), [props.children]);
194
195 // Swap toolbar owners before paint so split and compact headers cannot clear each other.
196 useLayoutEffect(() => {
197 if (!navigation) {
198 return;
199 }
200 if (props.placement === "bottom") {
201 navigation.setOptions({
202 unstable_headerToolbarItems: () => items,
203 } as NativeStackOptionsWithToolbar);
204 return () => {
205 navigation.setOptions({
206 unstable_headerToolbarItems: () => [],
207 } as NativeStackOptionsWithToolbar);
208 };
209 }
210 if (props.placement === "left") {
211 navigation.setOptions({ unstable_headerLeftItems: () => items });
212 return () => {
213 navigation.setOptions({ unstable_headerLeftItems: () => [] });
214 };
215 }
216 navigation.setOptions({ unstable_headerRightItems: () => items });
217 return () => {
218 navigation.setOptions({ unstable_headerRightItems: () => [] });
219 };
220 }, [items, navigation, props.placement]);
221
222 return null;
223}
224
225function NativeHeaderToolbarButton(_props: {
226 readonly accessibilityLabel?: string;
227 readonly disabled?: boolean;
228 readonly icon?: string;
229 readonly label?: string;
230 readonly onPress?: () => void;
231 readonly separateBackground?: boolean;
232 readonly tintColor?: ColorValue;
233}) {
234 return null;
235}
236NativeHeaderToolbarButton.displayName = "NativeHeaderToolbarButton";
237
238function NativeHeaderToolbarMenu(_props: {
239 readonly accessibilityLabel?: string;
240 readonly children?: ReactNode;
241 readonly disabled?: boolean;
242 readonly icon?: string;
243 readonly inline?: boolean;
244 readonly separateBackground?: boolean;
245 readonly tintColor?: ColorValue;
246 readonly title?: string;
247}) {
248 return null;
249}
250NativeHeaderToolbarMenu.displayName = "NativeHeaderToolbarMenu";
251
252function NativeHeaderToolbarMenuAction(_props: {
253 readonly children?: ReactNode;
254 readonly destructive?: boolean;
255 readonly disabled?: boolean;
256 readonly discoverabilityLabel?: string;
257 readonly icon?: string;
258 readonly isOn?: boolean;
259 readonly onPress?: () => void;
260 readonly subtitle?: string;
261}) {
262 return null;
263}
264NativeHeaderToolbarMenuAction.displayName = "NativeHeaderToolbarMenuAction";
265
266function NativeHeaderToolbarLabel(_props: { readonly children?: ReactNode }) {
267 return null;
268}
269NativeHeaderToolbarLabel.displayName = "NativeHeaderToolbarLabel";
270
271function NativeHeaderToolbarSpacer(_props: {
272 readonly flexible?: boolean;
273 readonly sharesBackground?: boolean;
274 readonly width?: number;
275}) {
276 return null;
277}
278NativeHeaderToolbarSpacer.displayName = "NativeHeaderToolbarSpacer";
279
280function NativeHeaderToolbarSearchBarSlot() {
281 return null;
282}
283NativeHeaderToolbarSearchBarSlot.displayName = "NativeHeaderToolbarSearchBarSlot";
284
285export const NativeHeaderToolbar = Object.assign(NativeHeaderToolbarRoot, {
286 Button: NativeHeaderToolbarButton,
287 Label: NativeHeaderToolbarLabel,
288 Menu: Object.assign(NativeHeaderToolbarMenu, {
289 Action: NativeHeaderToolbarMenuAction,
290 }),
291 MenuAction: NativeHeaderToolbarMenuAction,
292 SearchBarSlot: NativeHeaderToolbarSearchBarSlot,
293 Spacer: NativeHeaderToolbarSpacer,
294});