forked from mrzachnugent/react-native-reusables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.tsx
138 lines (132 loc) · 3.75 KB
/
button.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { cva, type VariantProps } from 'class-variance-authority';
import * as React from 'react';
import { useColorScheme } from 'nativewind';
import { Platform, Pressable, Text, View } from 'react-native';
import { cn, isTextChildren } from '~/lib/utils';
import * as Slot from '~/lib/rn-primitives/slot/slot-native';
const buttonVariants = cva(
'flex-row items-center justify-center rounded-md web:ring-offset-background web:transition-colors web:focus-visible:outline-none web:focus-visible:ring-2 web:focus-visible:ring-ring web:focus-visible:ring-offset-2',
{
variants: {
variant: {
default: 'bg-primary',
destructive: 'bg-destructive',
outline: 'border border-input bg-background',
secondary: 'bg-secondary',
ghost: '',
link: '',
},
size: {
default: 'px-4 py-2 native:px-6 native:py-3.5',
sm: 'px-3 py-1 native:py-2',
lg: 'px-8 py-1.5 native:py-4',
},
},
defaultVariants: {
variant: 'default',
size: 'default',
},
}
);
const buttonTextVariants = cva('font-medium', {
variants: {
variant: {
default: 'text-primary-foreground',
destructive: 'text-destructive-foreground',
outline: 'text-foreground',
secondary: 'text-secondary-foreground',
ghost: 'text-foreground',
link: 'text-primary underline',
},
size: {
default: 'text-sm native:text-xl',
sm: 'text-xs native:text-lg',
lg: 'text-base native:text-2xl',
},
},
defaultVariants: {
variant: 'default',
size: 'default',
},
});
const rippleColor = (isThemeDark: boolean) => {
const secondary = isThemeDark ? 'hsl(240 4% 16%)' : 'hsl(240 5% 96%)';
return {
default: isThemeDark ? '#d4d4d8' : '#3f3f46',
destructive: isThemeDark ? '#b91c1c' : '#f87171',
outline: secondary,
secondary: isThemeDark ? '#3f3f46' : '#e4e4e7',
ghost: secondary,
link: secondary,
};
};
const Button = React.forwardRef<
React.ElementRef<typeof Pressable>,
React.ComponentPropsWithoutRef<typeof Pressable> &
VariantProps<typeof buttonVariants> & {
textClass?: string;
androidRootClass?: string;
}
>(
(
{
className,
textClass,
variant = 'default',
size,
children,
androidRootClass,
disabled,
...props
},
ref
) => {
const { colorScheme } = useColorScheme();
const Root = Platform.OS === 'android' ? View : Slot.Pressable;
return (
<Root
className={cn(
Platform.OS === 'android' && 'flex-row rounded-md overflow-hidden',
Platform.OS === 'android' && androidRootClass
)}
>
<Pressable
className={cn(
buttonVariants({
variant,
size,
className: cn(
className,
disabled && 'opacity-50 web:cursor-default'
),
})
)}
ref={ref}
android_ripple={{
color: rippleColor(colorScheme === 'dark')[variant as 'default'],
borderless: false,
}}
disabled={disabled}
{...props}
>
{isTextChildren(children)
? ({ pressed, hovered }) => (
<Text
className={cn(
hovered && 'opacity-90',
pressed && 'opacity-70',
buttonTextVariants({ variant, size, className: textClass }),
disabled && 'opacity-100'
)}
>
{children as string | string[]}
</Text>
)
: children}
</Pressable>
</Root>
);
}
);
Button.displayName = 'Button';
export { Button, buttonTextVariants, buttonVariants };