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

Chip.tsx

91 lines2.8 KB

1import { GlassView } from "expo-glass-effect";
2import { Pressable, View } from "react-native";
3import { withUniwind } from "uniwind";
4​
5import { cn } from "../../lib/cn";
6import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass";
7import { AppText } from "./AppText";
8import { SymbolView, type AppSymbolName } from "./AppSymbol";
9​
10const ChipGlass = withUniwind(GlassView, {
11 style: { fromClassName: "className" },
12 tintColor: { fromClassName: "tintColorClassName", styleProperty: "accentColor" },
13});
14​
15/** Compact labels and filter triggers. Menu owners can wrap the non-pressable form. */
16export function Chip(props: {
17 readonly label: string;
18 readonly selected?: boolean;
19 readonly icon?: AppSymbolName;
20 readonly trailingIcon?: AppSymbolName;
21 readonly size?: "small" | "default";
22 readonly glass?: boolean;
23 readonly colorScheme?: "light" | "dark";
24 readonly onPress?: () => void;
25 readonly accessibilityLabel?: string;
26 readonly disabled?: boolean;
27}) {
28 const small = props.size === "small";
29 const layout = cn(
30 "flex-row items-center gap-1.5 rounded-2xl px-3",
31 small ? "py-1" : "min-h-8 py-1.5",
32 );
33 const iconColor = props.selected ? "accent-primary-foreground" : "accent-icon-muted";
34 const content = (
35 <>
36 {props.icon ? (
37 <SymbolView name={props.icon} size={15} tintColorClassName={iconColor} />
38 ) : null}
39 <AppText
40 textRole={small ? "caption" : "footnote"}
41 numberOfLines={small ? undefined : 1}
42 className={cn(
43 "shrink",
44 !small && "max-w-[180px]",
45 !small && "font-t3-medium",
46 props.selected ? "text-primary-foreground" : "text-foreground-muted",
47 )}
48 >
49 {props.label}
50 </AppText>
51 {props.trailingIcon ? (
52 <SymbolView name={props.trailingIcon} size={11} tintColorClassName={iconColor} />
53 ) : null}
54 </>
55 );
56 const body =
57 props.glass && NATIVE_LIQUID_GLASS_SUPPORTED ? (
58 <ChipGlass
59 isInteractive
60 colorScheme={props.colorScheme}
61 tintColorClassName={props.selected ? "accent-primary" : undefined}
62 className={layout}
63 >
64 {content}
65 </ChipGlass>
66 ) : (
67 <View
68 className={cn(
69 layout,
70 props.selected ? "bg-primary" : small ? "bg-grouped-card" : "bg-subtle",
71 )}
72 >
73 {content}
74 </View>
75 );
76 return props.onPress ? (
77 <Pressable
78 accessibilityRole="button"
79 accessibilityLabel={props.accessibilityLabel ?? props.label}
80 accessibilityState={{ selected: props.selected, disabled: Boolean(props.disabled) }}
81 disabled={props.disabled}
82 onPress={props.onPress}
83 hitSlop={6}
84 className={props.disabled ? "opacity-45" : "active:opacity-70"}
85 >
86 {body}
87 </Pressable>
88 ) : (
89 body
90 );