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

Menu.tsx

110 lines3.8 KB

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 type { MenuProps } from "./Menu.types";
15​
16const ThemedMenuView = withUniwind(
17 function NativeMenuView({
18 iconColor,
19 destructiveIconColor,
20 ...props
21 }: ComponentProps<typeof MenuView> & {
22 readonly iconColor?: ColorValue;
23 readonly destructiveIconColor?: ColorValue;
24 }) {
25 const actions = useMemo(
26 () =>
27 withMenuActionIconColors(props.actions, {
28 icon: iconColor,
29 destructiveIcon: destructiveIconColor,
30 }),
31 [props.actions, iconColor, destructiveIconColor],
32 );
33 return <MenuView {...props} actions={actions} />;
34 },
35 {
36 iconColor: { fromClassName: "iconColorClassName", styleProperty: "accentColor" },
37 destructiveIconColor: {
38 fromClassName: "destructiveIconColorClassName",
39 styleProperty: "accentColor",
40 },
41 },
42);
43​
44export function Menu(props: MenuProps) {
45 const { themeAppearance } = useAppearancePreferences();
46 const isDarkMode = themeAppearance === "dark";
47 const menuPress = useRef({ isPreparing: false, isOpen: false, suppressPress: false });
48 const pendingPress = useRef<(() => void) | null>(null);
49​
50 const { className: _className, ...menuProps } = props;
51 let children = menuProps.children;
52 if (props.shouldOpenOnLongPress && isValidElement(children)) {
53 const child = children as ReactElement<Pick<PressableProps, "onTouchStart" | "onPress">>;
54 children = cloneElement(child, {
55 onTouchStart: (event) => {
56 // onPressIn can repeat within one gesture when a finger leaves and re-enters the row.
57 menuPress.current.isPreparing = false;
58 menuPress.current.suppressPress = menuPress.current.isOpen;
59 pendingPress.current = null;
60 child.props.onTouchStart?.(event);
61 },
62 onPress: (event) => {
63 // Accessibility clicks lack touch identifiers and must ignore previous touch cancellation.
64 const isTouch = typeof event.nativeEvent.identifier === "number";
65 if (isTouch ? menuPress.current.suppressPress : menuPress.current.isOpen) {
66 return;
67 }
68 if (isTouch && menuPress.current.isPreparing) {
69 // A release can precede native display; let UIKit's display/cancel callback decide its outcome.
70 event.persist();
71 pendingPress.current = () => child.props.onPress?.(event);
72 return;
73 }
74 child.props.onPress?.(event);
75 },
76 });
77 menuProps.onMenuInteractionStart = () => {
78 menuPress.current.isPreparing = true;
79 props.onMenuInteractionStart?.();
80 };
81 menuProps.onOpenMenu = () => {
82 menuPress.current.isPreparing = false;
83 menuPress.current.isOpen = true;
84 menuPress.current.suppressPress = true;
85 pendingPress.current = null;
86 props.onOpenMenu?.();
87 };
88 menuProps.onCloseMenu = () => {
89 menuPress.current.isPreparing = false;
90 menuPress.current.isOpen = false;
91 // Keep cancellation through finger-up even if the menu dismisses first.
92 const press = pendingPress.current;
93 pendingPress.current = null;
94 props.onCloseMenu?.();
95 if (!menuPress.current.suppressPress) {
96 press?.();
97 }
98 };
99 }
100 return (
101 <ThemedMenuView
102 {...menuProps}
103 iconColorClassName="accent-icon"
104 destructiveIconColorClassName="accent-danger-foreground"
105 themeVariant={isDarkMode ? "dark" : "light"}
106 >
107 {children}
108 </ThemedMenuView>
109 );