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

ControlPill.tsx

141 lines4.5 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} from "react";
10import { Platform, Pressable, useColorScheme, View } from "react-native";
11import { useThemeColor } from "../lib/useThemeColor";
12​
13import { cn } from "../lib/cn";
14import { AndroidAnchoredMenu } from "./AndroidAnchoredMenu";
15import { SymbolView } from "./AppSymbol";
16import { AppText as Text } from "./AppText";
17​
18export function ControlPill(props: {
19 readonly icon?: ComponentProps<typeof SymbolView>["name"];
20 readonly iconNode?: ReactNode;
21 readonly label?: string;
22 readonly accessibilityLabel?: string;
23 readonly onPress?: () => void;
24 readonly variant?: "circle" | "pill" | "primary" | "danger";
25 readonly disabled?: boolean;
26}) {
27 const variant = props.variant ?? "circle";
28​
29 const iconColor = useThemeColor("--color-icon");
30 const iconSubtle = useThemeColor("--color-icon-subtle");
31 const primaryFg = useThemeColor("--color-primary-foreground");
32 const dangerFg = useThemeColor("--color-danger-foreground");
33 const iconTintColor =
34 variant === "primary"
35 ? props.disabled
36 ? iconSubtle
37 : primaryFg
38 : variant === "danger"
39 ? dangerFg
40 : iconColor;
41​
42 const isCircle =
43 variant === "circle" || variant === "danger" || (variant === "primary" && !props.label);
44 const containerClassName = cn(
45 isCircle
46 ? "h-11 w-11 items-center justify-center rounded-full"
47 : variant === "primary"
48 ? "h-11 flex-row items-center justify-center gap-2 rounded-full px-5"
49 : "h-11 flex-row items-center justify-center gap-2 rounded-full px-3.5",
50 variant === "primary"
51 ? props.disabled
52 ? "bg-subtle-strong"
53 : "bg-primary"
54 : variant === "danger"
55 ? "bg-danger"
56 : "bg-subtle",
57 );
58 const labelClassName = cn(
59 "text-center text-xs font-v12-bold",
60 variant === "primary"
61 ? props.disabled
62 ? "text-foreground-muted"
63 : "text-primary-foreground"
64 : "",
65 );
66​
67 return (
68 <Pressable
69 accessibilityLabel={props.accessibilityLabel ?? props.label}
70 accessibilityRole="button"
71 onPress={props.onPress}
72 disabled={props.disabled}
73 className={containerClassName}
74 >
75 {props.iconNode ? (
76 <View className="h-4 w-4 items-center justify-center">{props.iconNode}</View>
77 ) : props.icon ? (
78 <SymbolView name={props.icon} size={16} tintColor={iconTintColor} type="monochrome" />
79 ) : null}
80 {props.label ? <Text className={labelClassName}>{props.label}</Text> : null}
81 </Pressable>
82 );
84​
85// iOS renders the native UIMenu (standard checkmark for `state: "on"`);
86// Android renders the token-styled AndroidAnchoredMenu, since the native
87// AppCompat popup can't be themed past its stock animation, metrics, and
88// submenu chrome.
89export function ControlPillMenu(
90 props: Omit<ComponentProps<typeof MenuView>, "children" | "themeVariant"> & {
91 readonly children: ReactNode;
92 readonly className?: string;
93 },
94) {
95 const isDarkMode = useColorScheme() === "dark";
96​
97 if (Platform.OS === "android") {
98 // Long-press menus keep their child interactive: the child element gets
99 // an injected onLongPress (mirroring the iOS context-menu interaction)
100 // so its own tap handling still works.
101 if (props.shouldOpenOnLongPress && isValidElement(props.children)) {
102 const child = props.children as ReactElement<{ onLongPress?: () => void }>;
103 return (
104 <AndroidAnchoredMenu
105 actions={props.actions}
106 className={props.className}
107 title={props.title}
108 style={props.style}
109 onPressAction={props.onPressAction}
110 >
111 {(open) =>
112 cloneElement(child, {
113 onLongPress: () => {
114 void Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
115 open();
116 },
117 })
118 }
119 </AndroidAnchoredMenu>
120 );
121 }
122 return (
123 <AndroidAnchoredMenu
124 actions={props.actions}
125 className={props.className}
126 title={props.title}
127 style={props.style}
128 onPressAction={props.onPressAction}
129 >
130 {props.children}
131 </AndroidAnchoredMenu>
132 );
133 }
134​
135 const { className: _className, ...menuProps } = props;
136 return (
137 <MenuView {...menuProps} themeVariant={isDarkMode ? "dark" : "light"}>
138 {menuProps.children}
139 </MenuView>
140 );