Open History to see the last commit
1import type { MenuAction } from "@react-native-menu/menu";
2import type { AppBarMenuItem } from "./AppBar.types";
3
4export function androidHeaderMenuActions(items: ReadonlyArray<AppBarMenuItem>): MenuAction[] {
5 return items.flatMap((item): MenuAction[] =>
6 "items" in item
7 ? item.inline && !item.title
8 ? androidHeaderMenuActions(item.items)
9 : [
10 {
11 id: item.id,
12 title: item.title ?? "",
13 image: item.icon,
14 subactions: androidHeaderMenuActions(item.items),
15 },
16 ]
17 : [
18 {
19 id: item.id,
20 title: item.title,
21 subtitle: item.subtitle,
22 image: item.icon,
23 imageColor: item.iconColor,
24 state: item.selected ? "on" : undefined,
25 attributes: item.disabled ? { disabled: true } : undefined,
26 },
27 ],
28 );
29}
30
31export function findHeaderMenuAction(
32 items: ReadonlyArray<AppBarMenuItem>,
33 id: string,
34): Extract<AppBarMenuItem, { readonly onPress: () => void }> | undefined {
35 for (const item of items) {
36 if ("items" in item) {
37 const action = findHeaderMenuAction(item.items, id);
38 if (action) return action;
39 } else if (item.id === id) {
40 return item;
41 }
42 }
43 return undefined;
44}
45
46export function splitAndroidHeaderActions<Action extends { readonly overflowOnly?: boolean }>(
47 actions: ReadonlyArray<Action>,
48 wide: boolean,
49) {
50 const inline = actions.filter((action) => !action.overflowOnly);
51 const directCount = actions.length > 2 ? (wide ? 3 : 1) : inline.length;
52 const visibleActions = inline.slice(0, directCount);
53 return {
54 visibleActions,
55 overflowActions: actions.filter((action) => !visibleActions.includes(action)),
56 };
57}