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 useRef,
10} from "react";
11import { Platform, Pressable, useColorScheme, View } from "react-native";
12import { useThemeColor } from "../lib/useThemeColor";
13
14import { cn } from "../lib/cn";
15import { AndroidAnchoredMenu } from "./AndroidAnchoredMenu";
16import { SymbolView } from "./AppSymbol";
17import { AppText as Text } from "./AppText";
18
19export function ControlPill(props: {
20 readonly icon?: ComponentProps<typeof SymbolView>["name"];
21 readonly iconNode?: ReactNode;
22 readonly label?: string;
23 readonly accessibilityLabel?: string;
24 readonly onPress?: () => void;
25 readonly activateOnPressIn?: boolean;
26 readonly variant?: "circle" | "pill" | "primary" | "danger";
27 readonly disabled?: boolean;
28 readonly className?: string;
29}) {
30 const variant = props.variant ?? "circle";
31 const activatedOnPressInRef = useRef(false);
32
33 const handlePressIn = () => {
34 activatedOnPressInRef.current = true;
35 props.onPress?.();
36 };
37 const handlePressOut = () => {
38 // Pressability invokes onPressOut immediately before onPress on release.
39 // Defer the reset so onPress can identify the same physical gesture.
40 setTimeout(() => {
41 activatedOnPressInRef.current = false;
42 }, 0);
43 };
44 const handlePress = () => {
45 if (activatedOnPressInRef.current) {
46 return;
47 }
48 props.onPress?.();
49 };
50
51 const iconColor = useThemeColor("--color-icon");
52 const iconSubtle = useThemeColor("--color-icon-subtle");
53 const primaryFg = useThemeColor("--color-primary-foreground");
54 const dangerFg = useThemeColor("--color-danger-foreground");
55 const iconTintColor =
56 variant === "primary"
57 ? props.disabled
58 ? iconSubtle
59 : primaryFg
60 : variant === "danger"
61 ? dangerFg
62 : iconColor;
63
64 const isCircle =
65 variant === "circle" || variant === "danger" || (variant === "primary" && !props.label);
66 const containerClassName = cn(
67 isCircle
68 ? "h-11 w-11 items-center justify-center rounded-full"
69 : variant === "primary"
70 ? "h-11 flex-row items-center justify-center gap-2 rounded-full px-5"
71 : "h-11 flex-row items-center justify-center gap-2 rounded-full px-3.5",
72 variant === "primary"
73 ? props.disabled
74 ? "bg-subtle-strong"
75 : "bg-primary"
76 : variant === "danger"
77 ? "bg-danger"
78 : "bg-subtle",
79 props.className,
80 );
81 const labelClassName = cn(
82 "text-center text-xs font-t3-bold",
83 variant === "primary"
84 ? props.disabled
85 ? "text-foreground-muted"
86 : "text-primary-foreground"
87 : "",
88 );
89
90 return (
91 <Pressable
92 accessibilityLabel={props.accessibilityLabel ?? props.label}
93 accessibilityRole="button"
94 onPress={props.activateOnPressIn ? handlePress : props.onPress}
95 onPressIn={props.activateOnPressIn ? handlePressIn : undefined}
96 onPressOut={props.activateOnPressIn ? handlePressOut : undefined}
97 disabled={props.disabled}
98 className={containerClassName}
99 >
100 {props.iconNode ? (
101 <View className="h-4 w-4 items-center justify-center">{props.iconNode}</View>
102 ) : props.icon ? (
103 <SymbolView name={props.icon} size={16} tintColor={iconTintColor} type="monochrome" />
104 ) : null}
105 {props.label ? <Text className={labelClassName}>{props.label}</Text> : null}
106 </Pressable>
107 );
108}
109
110// iOS renders the native UIMenu (standard checkmark for `state: "on"`);
111// Android renders the token-styled AndroidAnchoredMenu, since the native
112// AppCompat popup can't be themed past its stock animation, metrics, and
113// submenu chrome.
114export function ControlPillMenu(
115 props: Omit<ComponentProps<typeof MenuView>, "children" | "themeVariant"> & {
116 readonly children: ReactNode;
117 readonly className?: string;
118 },
119) {
120 const isDarkMode = useColorScheme() === "dark";
121
122 if (Platform.OS === "android") {
123 // Long-press menus keep their child interactive: the child element gets
124 // an injected onLongPress (mirroring the iOS context-menu interaction)
125 // so its own tap handling still works.
126 if (props.shouldOpenOnLongPress && isValidElement(props.children)) {
127 const child = props.children as ReactElement<{ onLongPress?: () => void }>;
128 return (
129 <AndroidAnchoredMenu
130 actions={props.actions}
131 className={props.className}
132 title={props.title}
133 style={props.style}
134 onPressAction={props.onPressAction}
135 >
136 {(open) =>
137 cloneElement(child, {
138 onLongPress: () => {
139 void Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
140 open();
141 },
142 })
143 }
144 </AndroidAnchoredMenu>
145 );
146 }
147 return (
148 <AndroidAnchoredMenu
149 actions={props.actions}
150 className={props.className}
151 title={props.title}
152 style={props.style}
153 onPressAction={props.onPressAction}
154 >
155 {props.children}
156 </AndroidAnchoredMenu>
157 );
158 }
159
160 const { className: _className, ...menuProps } = props;
161 let children = menuProps.children;
162 // In long-press mode the wrapped pressable still receives the touch (the
163 // patched MenuView button is touch-transparent) and RN's Fabric touch
164 // handler is never cancelled by the in-tree UIContextMenuInteraction, so a
165 // bare onPress would fire on finger-up even after the menu opened — and
166 // also on a long press released just under the menu threshold. A dispatched
167 // onLongPress makes Pressability swallow the release, so holds past 350ms
168 // (below the ~500ms context-menu threshold) can only open the menu, never
169 // tap through.
170 if (props.shouldOpenOnLongPress && isValidElement(children)) {
171 const child = children as ReactElement<{ onLongPress?: () => void; delayLongPress?: number }>;
172 children = cloneElement(child, {
173 onLongPress: child.props.onLongPress ?? (() => undefined),
174 delayLongPress: child.props.delayLongPress ?? 350,
175 });
176 }
177 return (
178 <MenuView {...menuProps} themeVariant={isDarkMode ? "dark" : "light"}>
179 {children}
180 </MenuView>
181 );
182}