|
| 1 | +import { cn } from "@/lib/utils"; |
| 2 | +import { cva, type VariantProps } from "class-variance-authority"; |
1 | 3 | import * as React from "react"; |
2 | 4 | import { |
3 | 5 | Pressable, |
4 | | - PressableProps as RNPressableProps, |
| 6 | + type PressableStateCallbackType, |
| 7 | + type PressableProps as RNPressableProps, |
5 | 8 | View, |
6 | | - ViewStyle, |
7 | | - PressableStateCallbackType, |
| 9 | + type ViewStyle, |
8 | 10 | } from "react-native"; |
9 | | -import { cn } from "@/lib/utils"; |
10 | | - |
11 | | -import { cva, type VariantProps } from "class-variance-authority"; |
12 | 11 |
|
13 | 12 | export const buttonVariants = cva( |
14 | | - "flex-row items-center justify-center rounded-md", |
15 | | - { |
16 | | - variants: { |
17 | | - variant: { |
18 | | - default: |
19 | | - "bg-primary text-primary-foreground dark:bg-primary dark:text-primary-foreground shadow", |
20 | | - destructive: |
21 | | - "bg-destructive text-destructive-foreground dark:bg-destructive dark:text-destructive-foreground shadow-sm", |
22 | | - outline: |
23 | | - "border border-input bg-background text-foreground dark:border-input dark:bg-background dark:text-foreground shadow-sm", |
24 | | - secondary: |
25 | | - "bg-secondary text-secondary-foreground dark:bg-secondary dark:text-secondary-foreground shadow-sm", |
26 | | - ghost: "text-foreground dark:text-foreground", |
27 | | - link: "text-primary dark:text-primary underline", |
28 | | - }, |
29 | | - size: { |
30 | | - default: "h-12 px-6", |
31 | | - sm: "h-10 px-4", |
32 | | - lg: "h-14 px-8", |
33 | | - icon: "h-12 w-12", |
34 | | - }, |
35 | | - }, |
36 | | - defaultVariants: { |
37 | | - variant: "default", |
38 | | - size: "default", |
39 | | - }, |
40 | | - } |
| 13 | + "flex-row items-center justify-center rounded-lg", |
| 14 | + { |
| 15 | + variants: { |
| 16 | + variant: { |
| 17 | + default: "bg-primary text-primary-foreground shadow-sm", |
| 18 | + destructive: "bg-destructive text-destructive-foreground shadow-sm", |
| 19 | + outline: "border-2 border-border bg-background text-foreground", |
| 20 | + secondary: "bg-secondary text-secondary-foreground shadow-sm", |
| 21 | + ghost: "text-foreground", |
| 22 | + link: "text-primary underline", |
| 23 | + selection: "border-2 border-border bg-background", |
| 24 | + }, |
| 25 | + size: { |
| 26 | + default: "h-12 px-4", |
| 27 | + sm: "h-10 px-3", |
| 28 | + lg: "h-14 px-6", |
| 29 | + icon: "h-12 w-12", |
| 30 | + }, |
| 31 | + selected: { |
| 32 | + true: "", |
| 33 | + false: "", |
| 34 | + }, |
| 35 | + }, |
| 36 | + compoundVariants: [ |
| 37 | + { |
| 38 | + variant: "selection", |
| 39 | + selected: true, |
| 40 | + className: "border-primary bg-primary/5", |
| 41 | + }, |
| 42 | + { |
| 43 | + variant: "outline", |
| 44 | + selected: true, |
| 45 | + className: "border-primary ring-1 ring-primary/20", |
| 46 | + }, |
| 47 | + ], |
| 48 | + defaultVariants: { |
| 49 | + variant: "default", |
| 50 | + size: "default", |
| 51 | + selected: false, |
| 52 | + }, |
| 53 | + }, |
41 | 54 | ); |
42 | 55 |
|
43 | 56 | export interface ButtonProps |
44 | | - extends Omit<RNPressableProps, "style">, |
45 | | - VariantProps<typeof buttonVariants> { |
46 | | - className?: string; |
47 | | - style?: ViewStyle; |
48 | | - asChild?: boolean; |
| 57 | + extends Omit<RNPressableProps, "style">, |
| 58 | + VariantProps<typeof buttonVariants> { |
| 59 | + className?: string; |
| 60 | + style?: ViewStyle; |
| 61 | + asChild?: boolean; |
| 62 | + selected?: boolean; |
49 | 63 | } |
50 | 64 |
|
51 | 65 | const Button = React.forwardRef<View, ButtonProps>( |
52 | | - ({ className, variant, size, asChild = false, children, ...props }, ref) => { |
53 | | - return ( |
54 | | - <Pressable |
55 | | - className={cn(buttonVariants({ variant, size, className }))} |
56 | | - ref={ref} |
57 | | - {...props} |
58 | | - > |
59 | | - {(state: PressableStateCallbackType) => ( |
60 | | - <View |
61 | | - className={`flex-row items-center justify-center gap-2 ${state.pressed ? "opacity-80" : "" |
62 | | - }`} |
63 | | - > |
64 | | - {typeof children === "function" ? children(state) : children} |
65 | | - </View> |
66 | | - )} |
67 | | - </Pressable> |
68 | | - ); |
69 | | - } |
| 66 | + ( |
| 67 | + { className, variant, size, selected, asChild = false, children, ...props }, |
| 68 | + ref, |
| 69 | + ) => { |
| 70 | + const [isPressed, setIsPressed] = React.useState(false); |
| 71 | + |
| 72 | + return ( |
| 73 | + <Pressable |
| 74 | + className={cn(buttonVariants({ variant, size, selected, className }))} |
| 75 | + ref={ref} |
| 76 | + onPressIn={() => setIsPressed(true)} |
| 77 | + onPressOut={() => setIsPressed(false)} |
| 78 | + {...props} |
| 79 | + > |
| 80 | + {(state: PressableStateCallbackType) => ( |
| 81 | + <View |
| 82 | + className={`flex-row items-center justify-center gap-2 ${ |
| 83 | + state.pressed || isPressed ? "opacity-80" : "" |
| 84 | + }`} |
| 85 | + > |
| 86 | + {typeof children === "function" ? children(state) : children} |
| 87 | + </View> |
| 88 | + )} |
| 89 | + </Pressable> |
| 90 | + ); |
| 91 | + }, |
70 | 92 | ); |
71 | 93 |
|
72 | 94 | Button.displayName = "Button"; |
|
0 commit comments