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