1+ import { cva } from "class-variance-authority" ;
2+ import clsx from "clsx" ;
13import type { AnchorHTMLAttributes , ButtonHTMLAttributes } from "react" ;
24
3- import { cn } from "../../utils/classnames" ;
45import { Spinner } from "./Spinner" ;
56
67type BaseProps = {
7- variant ?: "primary" | "secondary" | "tertiary" ;
8+ variant ?: "primary" | "secondary" ;
89 isLoading ?: boolean ;
910} ;
1011
@@ -20,31 +21,48 @@ type AsLinkProps = BaseProps &
2021
2122type Props = AsButtonProps | AsLinkProps ;
2223
24+ const buttonClassName = cva (
25+ [
26+ "tdk-rounded-lg tdk-px-4 tdk-py-1 tdk-transition-colors tdk-text-sm tdk-border tdk-border-solid tdk-min-h-9 disabled:tdk-cursor-not-allowed disabled:tdk-opacity-90" ,
27+ ] ,
28+ {
29+ variants : {
30+ intent : {
31+ primary : [
32+ "tdk-text-cream tdk-border-ruby-600 tdk-bg-ruby-700 hover:tdk-border-ruby-400 hover:tdk-bg-ruby-500 active:tdk-border-ruby-700 active:tdk-bg-ruby-800 disabled:hover:tdk-border-ruby-600 disabled:hover:tdk-bg-ruby-700" ,
33+ ] ,
34+ secondary : [
35+ "tdk-text-black tdk-border-honey-400 tdk-bg-honey-500 hover:tdk-border-honey-300 hover:tdk-bg-honey-400 active:tdk-border-honey-500 active:tdk-bg-honey-800 disabled:hover:tdk-border-honey-400 disabled:hover:tdk-bg-honey-500" ,
36+ ] ,
37+ } ,
38+ isLoading : {
39+ false : "tdk-cursor-pointer" ,
40+ true : "tdk-flex tdk-items-center tdk-justify-center tdk-cursor-wait" ,
41+ } ,
42+ } ,
43+ compoundVariants : [
44+ {
45+ intent : "primary" ,
46+ isLoading : true ,
47+ class :
48+ "tdk-border-ruby-700 tdk-bg-ruby-800 hover:tdk-border-ruby-700 hover:tdk-bg-ruby-800" ,
49+ } ,
50+ {
51+ intent : "secondary" ,
52+ isLoading : true ,
53+ class :
54+ "tdk-border-honey-500 tdk-bg-honey-800 hover:tdk-border-honey-500 hover:tdk-bg-honey-800" ,
55+ } ,
56+ ] ,
57+ defaultVariants : {
58+ intent : "primary" ,
59+ isLoading : false ,
60+ } ,
61+ } ,
62+ ) ;
63+
2364export const Button = ( props : Props ) => {
2465 const { variant = "primary" , isLoading = false } = props ;
25- const className = cn (
26- "tdk-rounded-lg tdk-px-4 tdk-py-1 tdk-transition-colors tdk-text-sm tdk-border tdk-border-solid tdk-min-h-9" ,
27- isLoading
28- ? "tdk-flex tdk-items-center tdk-justify-center tdk-cursor-wait"
29- : "tdk-cursor-pointer" ,
30- variant === "primary" &&
31- "tdk-text-cream tdk-border-ruby-600 tdk-bg-ruby-700 hover:tdk-border-ruby-400 hover:tdk-bg-ruby-500 active:tdk-border-ruby-700 active:tdk-bg-ruby-800" ,
32- variant === "primary" &&
33- isLoading &&
34- "tdk-border-ruby-700 tdk-bg-ruby-800 hover:tdk-border-ruby-700 hover:tdk-bg-ruby-800" ,
35- variant === "secondary" &&
36- "tdk-text-black tdk-border-honey-400 tdk-bg-honey-500 hover:tdk-border-honey-300 hover:tdk-bg-honey-400 active:tdk-border-honey-500 active:tdk-bg-honey-800" ,
37- variant === "secondary" &&
38- isLoading &&
39- "tdk-border-honey-500 tdk-bg-honey-800 hover:tdk-border-honey-500 hover:tdk-bg-honey-800" ,
40- variant === "tertiary" &&
41- "tdk-text-cream tdk-border-night-400 tdk-bg-night-500 hover:tdk-border-night-200 hover:tdk-bg-night-300 active:tdk-border-night-400 active:tdk-bg-night-600" ,
42- variant === "tertiary" &&
43- isLoading &&
44- "tdk-border-night-400 tdk-bg-night-600 hover:tdk-border-night-400 hover:tdk-bg-night-600" ,
45- "disabled:tdk-cursor-not-allowed disabled:tdk-text-cream disabled:tdk-border-night-600 disabled:tdk-bg-night-900" ,
46- props . className ,
47- ) ;
4866
4967 const children = isLoading ? (
5068 < Spinner className = "tdk-w-3.5 tdk-h-3.5 tdk-mx-auto" />
@@ -55,7 +73,13 @@ export const Button = (props: Props) => {
5573 if ( props . as === "link" ) {
5674 const { as : _ , variant : __ , isLoading : ___ , ...linkProps } = props ;
5775 return (
58- < a { ...linkProps } className = { className } >
76+ < a
77+ { ...linkProps }
78+ className = { clsx (
79+ buttonClassName ( { intent : variant , isLoading } ) ,
80+ props . className ,
81+ ) }
82+ >
5983 { children }
6084 </ a >
6185 ) ;
@@ -65,7 +89,13 @@ export const Button = (props: Props) => {
6589 return (
6690 < button
6791 { ...buttonProps }
68- className = { className }
92+ className = { clsx (
93+ buttonClassName ( {
94+ intent : variant ,
95+ isLoading,
96+ } ) ,
97+ buttonProps . className ,
98+ ) }
6999 onClick = { isLoading ? undefined : props . onClick }
70100 >
71101 { children }
0 commit comments