Open History to see the last commit
1import { MenuView } from "@react-native-menu/menu";
2import * as Haptics from "expo-haptics";
3import {
4 cloneElement,
5 isValidElement,
6 type ComponentProps,
7 type ReactElement,
8 type ReactNode,
9 useMemo,
10 useRef,
11} from "react";
12import {
13 Platform,
14 Pressable,
15 View,
16 type ColorValue,
17 type PressableProps,
18 type AccessibilityProps,
19} from "react-native";
20import { withUniwind } from "uniwind";
21import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
22
23import { cn } from "../lib/cn";
24import { withMenuActionIconColors } from "../lib/menu-action-colors";
25import { AndroidAnchoredMenu } from "./AndroidAnchoredMenu";
26import { SymbolView } from "./AppSymbol";
27import { AppText as Text } from "./AppText";
28
29const ThemedMenuView = withUniwind(
30 function NativeMenuView({
31 iconColor,
32 destructiveIconColor,
33 ...props
34 }: ComponentProps<typeof MenuView> & {
35 readonly iconColor?: ColorValue;
36 readonly destructiveIconColor?: ColorValue;
37 }) {
38 const actions = useMemo(
39 () =>
40 withMenuActionIconColors(props.actions, {
41 icon: iconColor,
42 destructiveIcon: destructiveIconColor,
43 }),
44 [props.actions, iconColor, destructiveIconColor],
45 );
46 return <MenuView {...props} actions={actions} />;
47 },
48 {
49 iconColor: { fromClassName: "iconColorClassName", styleProperty: "accentColor" },
50 destructiveIconColor: {
51 fromClassName: "destructiveIconColorClassName",
52 styleProperty: "accentColor",
53 },
54 },
55);
56
57export function ControlPill(props: {
58 readonly icon?: ComponentProps<typeof SymbolView>["name"];
59 readonly iconNode?: ReactNode;
60 readonly label?: string;
61 readonly accessibilityLabel?: string;
62 readonly onPress?: () => void;
63 readonly activateOnPressIn?: boolean;
64 readonly variant?: "circle" | "pill" | "primary" | "danger";
65 readonly disabled?: boolean;
66 readonly className?: string;
67}) {
68 const variant = props.variant ?? "circle";
69 const activatedOnPressInRef = useRef(false);
70
71 const handlePressIn = () => {
72 activatedOnPressInRef.current = true;
73 props.onPress?.();
74 };
75 const handlePressOut = () => {
76 // Pressability invokes onPressOut immediately before onPress on release.
77 // Defer the reset so onPress can identify the same physical gesture.
78 setTimeout(() => {
79 activatedOnPressInRef.current = false;
80 }, 0);
81 };
82 const handlePress = () => {
83 if (activatedOnPressInRef.current) {
84 return;
85 }
86 props.onPress?.();
87 };
88
89 const iconTintClassName =
90 variant === "primary"
91 ? props.disabled
92 ? "accent-icon-subtle"
93 : "accent-primary-foreground"
94 : variant === "danger"
95 ? "accent-danger-foreground"
96 : "accent-icon";
97
98 const isCircle =
99 variant === "circle" || variant === "danger" || (variant === "primary" && !props.label);
100 const containerClassName = cn(
101 isCircle
102 ? "h-11 w-11 items-center justify-center rounded-full"
103 : variant === "primary"
104 ? "h-11 flex-row items-center justify-center gap-2 rounded-full px-5"
105 : "h-11 flex-row items-center justify-center gap-2 rounded-full px-3.5",
106 variant === "primary"
107 ? props.disabled
108 ? "bg-subtle-strong"
109 : "bg-primary"
110 : variant === "danger"
111 ? "bg-danger"
112 : "bg-subtle",
113 props.className,
114 );
115 const labelClassName = cn(
116 "text-center text-xs font-t3-bold",
117 variant === "primary"
118 ? props.disabled
119 ? "text-foreground-muted"
120 : "text-primary-foreground"
121 : "",
122 );
123
124 return (
125 <Pressable
126 accessibilityLabel={props.accessibilityLabel ?? props.label}
127 accessibilityRole="button"
128 onPress={props.activateOnPressIn ? handlePress : props.onPress}
129 onPressIn={props.activateOnPressIn ? handlePressIn : undefined}
130 onPressOut={props.activateOnPressIn ? handlePressOut : undefined}
131 disabled={props.disabled}
132 className={containerClassName}
133 >
134 {props.iconNode ? (
135 <View className="h-4 w-4 items-center justify-center">{props.iconNode}</View>
136 ) : props.icon ? (
137 <SymbolView
138 name={props.icon}
139 size={16}
140 tintColorClassName={iconTintClassName}
141 type="monochrome"
142 />
143 ) : null}
144 {props.label ? <Text className={labelClassName}>{props.label}</Text> : null}
145 </Pressable>
146 );
147}
148
149// iOS renders the native UIMenu (standard checkmark for `state: "on"`);
150// Android renders the token-styled AndroidAnchoredMenu, since the native
151// AppCompat popup can't be themed past its stock animation, metrics, and
152// submenu chrome.
153export function ControlPillMenu(
154 props: Omit<ComponentProps<typeof MenuView>, "children" | "themeVariant"> &
155 Pick<AccessibilityProps, "accessible" | "accessibilityLabel" | "accessibilityRole"> & {
156 readonly children: ReactNode;
157 readonly className?: string;
158 },
159) {
160 const { themeAppearance } = useAppearancePreferences();
161 const isDarkMode = themeAppearance === "dark";
162 const menuPress = useRef({ isPreparing: false, isOpen: false, suppressPress: false });
163 const pendingPress = useRef<(() => void) | null>(null);
164
165 if (Platform.OS === "android") {
166 // Long-press menus keep their child interactive: the child element gets
167 // an injected onLongPress (mirroring the iOS context-menu interaction)
168 // so its own tap handling still works.
169 if (props.shouldOpenOnLongPress && isValidElement(props.children)) {
170 const child = props.children as ReactElement<{ onLongPress?: () => void }>;
171 return (
172 <AndroidAnchoredMenu
173 actions={props.actions}
174 className={props.className}
175 title={props.title}
176 style={props.style}
177 onPressAction={props.onPressAction}
178 >
179 {(open) =>
180 cloneElement(child, {
181 onLongPress: () => {
182 void Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
183 open();
184 },
185 })
186 }
187 </AndroidAnchoredMenu>
188 );
189 }
190 return (
191 <AndroidAnchoredMenu
192 actions={props.actions}
193 className={props.className}
194 title={props.title}
195 style={props.style}
196 onPressAction={props.onPressAction}
197 >
198 {props.children}
199 </AndroidAnchoredMenu>
200 );
201 }
202
203 const { className: _className, ...menuProps } = props;
204 let children = menuProps.children;
205 if (props.shouldOpenOnLongPress && isValidElement(children)) {
206 const child = children as ReactElement<Pick<PressableProps, "onTouchStart" | "onPress">>;
207 children = cloneElement(child, {
208 onTouchStart: (event) => {
209 // Reset for a new touch, not onPressIn, which also fires when a
210 // finger moves out of the row and back during the same gesture.
211 menuPress.current.isPreparing = false;
212 menuPress.current.suppressPress = menuPress.current.isOpen;
213 pendingPress.current = null;
214 child.props.onTouchStart?.(event);
215 },
216 onPress: (event) => {
217 // Accessibility clicks have no touch identifier and must not inherit
218 // cancellation from a previous physical gesture.
219 const isTouch = typeof event.nativeEvent.identifier === "number";
220 if (isTouch ? menuPress.current.suppressPress : menuPress.current.isOpen) {
221 return;
222 }
223 if (isTouch && menuPress.current.isPreparing) {
224 // A release can arrive between native menu preparation and display.
225 // Let UIKit's display/cancel callback decide this press's outcome.
226 event.persist();
227 pendingPress.current = () => child.props.onPress?.(event);
228 return;
229 }
230 child.props.onPress?.(event);
231 },
232 });
233 menuProps.onMenuInteractionStart = () => {
234 menuPress.current.isPreparing = true;
235 props.onMenuInteractionStart?.();
236 };
237 menuProps.onOpenMenu = () => {
238 menuPress.current.isPreparing = false;
239 menuPress.current.isOpen = true;
240 menuPress.current.suppressPress = true;
241 pendingPress.current = null;
242 props.onOpenMenu?.();
243 };
244 menuProps.onCloseMenu = () => {
245 menuPress.current.isPreparing = false;
246 menuPress.current.isOpen = false;
247 // Keep this gesture cancelled even if dismissal precedes finger-up.
248 // A separate JS long-press timer would also swallow holds that never
249 // open the native menu.
250 const press = pendingPress.current;
251 pendingPress.current = null;
252 props.onCloseMenu?.();
253 if (!menuPress.current.suppressPress) {
254 press?.();
255 }
256 };
257 }
258 return (
259 <ThemedMenuView
260 {...menuProps}
261 iconColorClassName="accent-icon"
262 destructiveIconColorClassName="accent-danger-foreground"
263 themeVariant={isDarkMode ? "dark" : "light"}
264 >
265 {children}
266 </ThemedMenuView>
267 );
268}