-
Notifications
You must be signed in to change notification settings - Fork 355
Expand file tree
/
Copy pathindex.tsx
More file actions
131 lines (129 loc) · 6.1 KB
/
Copy pathindex.tsx
File metadata and controls
131 lines (129 loc) · 6.1 KB
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
import { clsx } from 'clsx';
import { Loader2 } from 'lucide-react';
import { ComponentPropsWithoutRef } from 'react';
export interface ButtonProps extends ComponentPropsWithoutRef<'button'> {
variant?: 'primary' | 'secondary' | 'tertiary' | 'ghost' | 'danger';
size?: 'large' | 'medium' | 'small' | 'x-small';
shape?: 'pill' | 'rounded' | 'square' | 'circle';
loading?: boolean;
}
// eslint-disable-next-line valid-jsdoc
/**
* This component supports various CSS variables for theming. Here's a comprehensive list, along
* with their default values:
*
* ```css
* :root {
* --button-focus: hsl(var(--primary));
* --button-font-family: var(--font-family-body);
* --button-primary-background: hsl(var(--primary));
* --button-primary-background-hover: color-mix(in oklab, hsl(var(--primary)), white 75%);
* --button-primary-foreground: hsl(var(--foreground));
* --button-primary-border: hsl(var(--primary));
* --button-secondary-background: hsl(var(--foreground));
* --button-secondary-background-hover: hsl(var(--background));
* --button-secondary-foreground: hsl(var(--background));
* --button-secondary-border: hsl(var(--foreground));
* --button-tertiary-background: hsl(var(--background));
* --button-tertiary-background-hover: hsl(var(--contrast-100));
* --button-tertiary-foreground: hsl(var(--foreground));
* --button-tertiary-border: hsl(var(--contrast-200));
* --button-ghost-background: transparent;
* --button-ghost-background-hover: hsl(var(--foreground) / 5%);
* --button-ghost-foreground: hsl(var(--foreground));
* --button-ghost-border: transparent;
* --button-loader-icon: hsl(var(--foreground));
* --button-danger-background: color-mix(in oklab, hsl(var(--error)), white 30%);
* --button-danger-background-hover: color-mix(in oklab, hsl(var(--error)), white 75%);
* --button-danger-foreground: hsl(var(--foreground));
* --button-danger-border: color-mix(in oklab, hsl(var(--error)), white 30%);
* }
* ```
*/
export function Button({
variant = 'primary',
size = 'large',
shape = 'pill',
loading = false,
type = 'button',
disabled = false,
className,
children,
...props
}: ButtonProps) {
return (
<button
{...props}
aria-busy={loading}
className={clsx(
'relative z-0 inline-flex h-fit cursor-pointer select-none items-center justify-center overflow-hidden border text-center font-[family-name:var(--button-font-family,var(--font-family-body))] font-semibold leading-normal after:absolute after:inset-0 after:-z-10 after:-translate-x-[105%] after:duration-300 after:[animation-timing-function:cubic-bezier(0,0.25,0,1)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--button-focus,hsl(var(--primary)))] focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-30',
{
primary:
'border-[var(--button-primary-border,hsl(var(--primary)))] bg-[var(--button-primary-background,hsl(var(--primary)))] text-[var(--button-primary-foreground,hsl(var(--foreground)))] after:bg-[var(--button-primary-background-hover,color-mix(in_oklab,hsl(var(--primary)),white_75%))]',
secondary:
'border-[var(--button-secondary-border,hsl(var(--foreground)))] bg-[var(--button-secondary-background,hsl(var(--foreground)))] text-[var(--button-secondary-foreground,hsl(var(--background)))] after:bg-[var(--button-secondary-background-hover,hsl(var(--background)))]',
tertiary:
'border-[var(--button-tertiary-border,hsl(var(--contrast-200)))] bg-[var(--button-tertiary-background,hsl(var(--background)))] text-[var(--button-tertiary-foreground,hsl(var(--foreground)))] after:bg-[var(--button-tertiary-background-hover,hsl(var(--contrast-100)))]',
ghost:
'border-[var(--button-ghost-border,transparent)] bg-[var(--button-ghost-background,transparent)] text-[var(--button-ghost-foreground,hsl(var(--foreground)))] after:bg-[var(--button-ghost-background-hover,hsl(var(--foreground)/5%))]',
danger:
'border-[var(--button-danger-border,color-mix(in_oklab,hsl(var(--error)),white_30%))] bg-[var(--button-danger-background,color-mix(in_oklab,hsl(var(--error)),white_30%))] text-[var(--button-danger-foreground)] after:bg-[var(--button-danger-background-hover,color-mix(in_oklab,hsl(var(--error)),white_75%))]',
}[variant],
{
pill: 'rounded-full after:rounded-full',
rounded: 'rounded-lg after:rounded-lg',
square: 'rounded-none after:rounded-none',
circle: 'rounded-full after:rounded-full',
}[shape],
!loading && !disabled && 'hover:after:translate-x-0',
loading && 'pointer-events-none',
className,
)}
disabled={disabled}
type={type}
>
<span
className={clsx(
'inline-flex items-center justify-center transition-all duration-300 ease-in-out',
loading ? '-translate-y-10 opacity-0' : 'translate-y-0 opacity-100',
{
'x-small': 'min-h-8 text-xs',
small: 'min-h-10 text-sm',
medium: 'min-h-12 text-base',
large: 'min-h-14 text-base',
}[size],
shape === 'circle' &&
{
'x-small': 'min-w-8',
small: 'min-w-10',
medium: 'min-w-12',
large: 'min-w-14',
}[size],
shape !== 'circle' &&
{
'x-small': 'gap-x-2 px-3 py-1.5',
small: 'gap-x-2 px-4 py-2.5',
medium: 'gap-x-2.5 px-5 py-3',
large: 'gap-x-3 px-6 py-4',
}[size],
variant === 'secondary' && 'mix-blend-difference',
)}
>
{children}
</span>
<span
className={clsx(
'absolute inset-0 grid place-content-center transition-all duration-300 ease-in-out',
loading ? 'translate-y-0 opacity-100' : 'translate-y-10 opacity-0',
)}
>
<Loader2
className={clsx(
'animate-spin',
variant === 'tertiary' && 'text-[var(--button-loader-icon,hsl(var(--foreground)))]',
)}
/>
</span>
</button>
);
}