Open History to see the last commit
1import { MenuView } from "@react-native-menu/menu";
2import {
3 cloneElement,
4 isValidElement,
5 useMemo,
6 useRef,
7 type ComponentProps,
8 type ReactElement,
9} from "react";
10import type { ColorValue, PressableProps } from "react-native";
11import { withUniwind } from "uniwind";
12import { useAppearancePreferences } from "../../features/settings/appearance/AppearancePreferencesProvider";
13import { withMenuActionIconColors } from "../../lib/menu-action-colors";
14import { perfProbe } from "../../features/development/perf-probe-hooks";
15import type { MenuProps } from "./Menu.types";
16
17const ThemedMenuView = withUniwind(
18 function NativeMenuView({
19 iconColor,
20 destructiveIconColor,
21 ...props
22 }: ComponentProps<typeof MenuView> & {
23 readonly iconColor?: ColorValue;
24 readonly destructiveIconColor?: ColorValue;
25 }) {
26 const actions = useMemo(
27 () =>
28 withMenuActionIconColors(props.actions, {
29 icon: iconColor,
30 destructiveIcon: destructiveIconColor,
31 }),
32 [props.actions, iconColor, destructiveIconColor],
33 );
34 return <MenuView {...props} actions={actions} />;
35 },
36 {
37 iconColor: { fromClassName: "iconColorClassName", styleProperty: "accentColor" },
38 destructiveIconColor: {
39 fromClassName: "destructiveIconColorClassName",
40 styleProperty: "accentColor",
41 },
42 },
43);
44
45export function Menu(props: MenuProps) {
46 const { themeAppearance } = useAppearancePreferences();
47 const isDarkMode = themeAppearance === "dark";
48 const menuPress = useRef({ isPreparing: false, isOpen: false, suppressPress: false });
49 const pendingPress = useRef<(() => void) | null>(null);
50
51 const { className: _className, ...menuProps } = props;
52 let children = menuProps.children;
53 if (props.shouldOpenOnLongPress && isValidElement(children)) {
54 const child = children as ReactElement<Pick<PressableProps, "onTouchStart" | "onPress">>;
55 /* oxlint-disable react/refs -- Cloning the trigger installs event callbacks; the refs are accessed when those callbacks run. */
56 children = cloneElement(child, {
57 onTouchStart: (event) => {
58 // onPressIn can repeat within one gesture when a finger leaves and re-enters the row.
59 menuPress.current.isPreparing = false;
60 menuPress.current.suppressPress = menuPress.current.isOpen;
61 pendingPress.current = null;
62 child.props.onTouchStart?.(event);
63 },
64 onPress: (event) => {
65 // Accessibility clicks lack touch identifiers and must ignore previous touch cancellation.
66 const isTouch = typeof event.nativeEvent.identifier === "number";
67 if (isTouch ? menuPress.current.suppressPress : menuPress.current.isOpen) {
68 return;
69 }
70 if (isTouch && menuPress.current.isPreparing) {
71 // A release can precede native display; let UIKit's display/cancel callback decide its outcome.
72 event.persist();
73 pendingPress.current = () => child.props.onPress?.(event);
74 return;
75 }
76 child.props.onPress?.(event);
77 },
78 });
79 /* oxlint-enable react/refs */
80 menuProps.onMenuInteractionStart = () => {
81 menuPress.current.isPreparing = true;
82 props.onMenuInteractionStart?.();
83 };
84 menuProps.onOpenMenu = () => {
85 menuPress.current.isPreparing = false;
86 menuPress.current.isOpen = true;
87 menuPress.current.suppressPress = true;
88 pendingPress.current = null;
89 props.onOpenMenu?.();
90 };
91 menuProps.onCloseMenu = () => {
92 menuPress.current.isPreparing = false;
93 menuPress.current.isOpen = false;
94 // Keep cancellation through finger-up even if the menu dismisses first.
95 const press = pendingPress.current;
96 pendingPress.current = null;
97 props.onCloseMenu?.();
98 if (!menuPress.current.suppressPress) {
99 press?.();
100 }
101 };
102 }
103 const { onPressAction } = props;
104 const probe = perfProbe;
105 if (probe && onPressAction)
106 menuProps.onPressAction = (event) => {
107 probe.menuAction();
108 onPressAction(event);
109 };
110 return (
111 <ThemedMenuView
112 {...menuProps}
113 iconColorClassName="accent-icon"
114 destructiveIconColorClassName="accent-danger-foreground"
115 themeVariant={isDarkMode ? "dark" : "light"}
116 >
117 {children}
118 </ThemedMenuView>
119 );
120}