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

ControlPill.tsx

184 lines6.3 KB

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