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

NativeHeaderToolbar.tsx

305 lines9.5 KB

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>>();
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("");
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 };
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;
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;
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;
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 === "NativeHeaderToolbarCustom" && isValidElement(child.props.children)) {
122 return { type: "custom", element: child.props.children };
123 }
124​
125 if (typeName === "NativeHeaderToolbarButton") {
126 return {
127 type: "button",
128 label: typeof child.props.label === "string" ? child.props.label : "",
129 accessibilityLabel:
130 typeof child.props.accessibilityLabel === "string"
131 ? child.props.accessibilityLabel
132 : undefined,
133 disabled: Boolean(child.props.disabled),
134 icon: iconFromProp(child.props.icon),
135 onPress:
136 typeof child.props.onPress === "function"
137 ? (child.props.onPress as () => void)
138 : () => undefined,
139 sharesBackground: !child.props.separateBackground,
140 tintColor: child.props.tintColor as ColorValue | undefined,
141 variant: "plain",
142 };
143 }
144​
145 if (typeName === "NativeHeaderToolbarMenu") {
146 return {
147 type: "menu",
148 label: typeof child.props.title === "string" ? child.props.title : "",
149 accessibilityLabel:
150 typeof child.props.accessibilityLabel === "string"
151 ? child.props.accessibilityLabel
152 : undefined,
153 disabled: Boolean(child.props.disabled),
154 icon: iconFromProp(child.props.icon),
155 menu: {
156 title: typeof child.props.title === "string" ? child.props.title : undefined,
157 items: collectMenuItems(child.props.children),
158 },
159 sharesBackground: !child.props.separateBackground,
160 tintColor: child.props.tintColor as ColorValue | undefined,
161 variant: "plain",
162 };
163 }
164​
165 if (typeName === "NativeHeaderToolbarSpacer") {
166 return {
167 type: "spacing",
168 spacing: typeof child.props.width === "number" ? child.props.width : 8,
169 flexible: Boolean(child.props.flexible),
170 } as NativeStackHeaderItem;
171 }
172​
173 return null;
175​
176function collectToolbarItems(children: ReactNode): NativeStackHeaderItem[] {
177 const items: NativeStackHeaderItem[] = [];
178 Children.forEach(children, (child) => {
179 const item = convertToolbarChild(child);
180 if (item) {
181 if (item.type === "spacing") {
182 // Native inserts spacing items at `index`, treating a missing index
183 // as 0 — which would move the spacer in front of earlier siblings.
184 (item as { index?: number }).index = items.length;
185 }
186 items.push(item);
187 }
188 });
189 return items;
191​
192function NativeHeaderToolbarRoot(props: {
193 readonly placement?: "left" | "right" | "bottom";
194 readonly children?: ReactNode;
195}) {
196 const navigation = useNativeStackNavigation();
197 const items = useMemo(() => collectToolbarItems(props.children), [props.children]);
198​
199 // Swap toolbar owners before paint so split and compact headers cannot clear each other.
200 useLayoutEffect(() => {
201 if (!navigation) {
202 return;
203 }
204 if (props.placement === "bottom") {
205 navigation.setOptions({
206 unstable_headerToolbarItems: () => items,
207 } as NativeStackOptionsWithToolbar);
208 return () => {
209 navigation.setOptions({
210 unstable_headerToolbarItems: () => [],
211 } as NativeStackOptionsWithToolbar);
212 };
213 }
214 if (props.placement === "left") {
215 navigation.setOptions({ unstable_headerLeftItems: () => items });
216 return () => {
217 navigation.setOptions({ unstable_headerLeftItems: () => [] });
218 };
219 }
220 navigation.setOptions({ unstable_headerRightItems: () => items });
221 return () => {
222 navigation.setOptions({ unstable_headerRightItems: () => [] });
223 };
224 }, [items, navigation, props.placement]);
225​
226 return null;
228​
229function NativeHeaderToolbarButton(_props: {
230 readonly accessibilityLabel?: string;
231 readonly disabled?: boolean;
232 readonly icon?: string;
233 readonly label?: string;
234 readonly onPress?: () => void;
235 readonly separateBackground?: boolean;
236 readonly tintColor?: ColorValue;
237}) {
238 return null;
240NativeHeaderToolbarButton.displayName = "NativeHeaderToolbarButton";
241​
242function NativeHeaderToolbarMenu(_props: {
243 readonly accessibilityLabel?: string;
244 readonly children?: ReactNode;
245 readonly disabled?: boolean;
246 readonly icon?: string;
247 readonly inline?: boolean;
248 readonly separateBackground?: boolean;
249 readonly tintColor?: ColorValue;
250 readonly title?: string;
251}) {
252 return null;
254NativeHeaderToolbarMenu.displayName = "NativeHeaderToolbarMenu";
255​
256function NativeHeaderToolbarMenuAction(_props: {
257 readonly children?: ReactNode;
258 readonly destructive?: boolean;
259 readonly disabled?: boolean;
260 readonly discoverabilityLabel?: string;
261 readonly icon?: string;
262 readonly isOn?: boolean;
263 readonly onPress?: () => void;
264 readonly subtitle?: string;
265}) {
266 return null;
268NativeHeaderToolbarMenuAction.displayName = "NativeHeaderToolbarMenuAction";
269​
270/** Renders an arbitrary element as a bar item, e.g. a spinner standing in for a button. */
271function NativeHeaderToolbarCustom(_props: { readonly children: ReactElement }) {
272 return null;
274NativeHeaderToolbarCustom.displayName = "NativeHeaderToolbarCustom";
275​
276function NativeHeaderToolbarLabel(_props: { readonly children?: ReactNode }) {
277 return null;
279NativeHeaderToolbarLabel.displayName = "NativeHeaderToolbarLabel";
280​
281function NativeHeaderToolbarSpacer(_props: {
282 readonly flexible?: boolean;
283 readonly sharesBackground?: boolean;
284 readonly width?: number;
285}) {
286 return null;
288NativeHeaderToolbarSpacer.displayName = "NativeHeaderToolbarSpacer";
289​
290function NativeHeaderToolbarSearchBarSlot() {
291 return null;
293NativeHeaderToolbarSearchBarSlot.displayName = "NativeHeaderToolbarSearchBarSlot";
294​
295export const NativeHeaderToolbar = Object.assign(NativeHeaderToolbarRoot, {
296 Button: NativeHeaderToolbarButton,
297 Custom: NativeHeaderToolbarCustom,
298 Label: NativeHeaderToolbarLabel,
299 Menu: Object.assign(NativeHeaderToolbarMenu, {
300 Action: NativeHeaderToolbarMenuAction,
301 }),
302 MenuAction: NativeHeaderToolbarMenuAction,
303 SearchBarSlot: NativeHeaderToolbarSearchBarSlot,
304 Spacer: NativeHeaderToolbarSpacer,
305});