|
1 | 1 | import React from "react"; |
2 | | -import { BaseButtonProps } from "./base"; |
3 | | -import PrimaryButton from "./primary"; |
4 | | -import SecondaryButton from "./secondary"; |
5 | | -import TertiaryButton from "./tertiary"; |
6 | | -import KlerosSymbol from "../../assets/svgs/kleros.svg"; |
| 2 | +import { cn } from "../../utils"; |
| 3 | +import { |
| 4 | + Button as AriaButton, |
| 5 | + type ButtonProps as AriaButtonProps, |
| 6 | +} from "react-aria-components"; |
| 7 | +import ButtonText from "./ButtonText"; |
| 8 | +import KlerosSymbol from "./KlerosSymbol"; |
| 9 | +import ButtonIcon from "./ButtonIcon"; |
7 | 10 |
|
8 | | -interface ButtonProps extends Omit<BaseButtonProps, "$loading"> { |
9 | | - disabled?: boolean; |
| 11 | +export interface BaseButtonProps { |
| 12 | + /** @default primary */ |
| 13 | + variant?: "primary" | "secondary" | "tertiary"; |
| 14 | + /** |
| 15 | + * Reduce the paddings on button |
| 16 | + * @default false |
| 17 | + */ |
| 18 | + small?: boolean; |
| 19 | + /** |
| 20 | + * Indicate if button is in loading state |
| 21 | + * @default false |
| 22 | + */ |
| 23 | + isLoading?: boolean; |
| 24 | +} |
| 25 | + |
| 26 | +export interface ButtonProps |
| 27 | + extends AriaButtonProps, |
| 28 | + Omit<BaseButtonProps, "$loading"> { |
10 | 29 | text: string; |
| 30 | + /** A React svg element to show as icon on button */ |
11 | 31 | Icon?: React.FC<React.SVGAttributes<SVGElement>>; |
| 32 | + /** A React node element to show as is on button */ |
12 | 33 | icon?: React.ReactNode; |
13 | | - onClick?: React.MouseEventHandler<HTMLButtonElement>; |
| 34 | + className?: string; |
14 | 35 | } |
15 | 36 |
|
16 | | -const Button: React.FC<ButtonProps> = ({ |
| 37 | +function Button({ |
17 | 38 | text, |
18 | 39 | variant, |
19 | 40 | Icon, |
20 | 41 | icon, |
21 | | - onClick, |
22 | 42 | isLoading, |
| 43 | + className, |
| 44 | + isDisabled, |
23 | 45 | ...props |
24 | | -}) => { |
25 | | - let ButtonType = PrimaryButton; |
26 | | - if (variant === "secondary") ButtonType = SecondaryButton; |
27 | | - if (variant === "tertiary") ButtonType = TertiaryButton; |
28 | | - |
| 46 | +}: Readonly<ButtonProps>): React.ReactElement { |
| 47 | + const isPrimary = variant === "primary" || variant === undefined; |
| 48 | + const isSecondary = variant === "secondary"; |
| 49 | + const isTertiary = variant === "tertiary"; |
29 | 50 | return ( |
30 | | - <ButtonType {...{ variant, onClick, isLoading, ...props }}> |
31 | | - {isLoading && <KlerosSymbol className="button-loading" />} |
32 | | - {icon ?? (Icon && <Icon className="button-svg" />)} |
33 | | - <p className="button-text">{text}</p> |
34 | | - </ButtonType> |
| 51 | + <AriaButton |
| 52 | + className={cn( |
| 53 | + "relative box-border h-fit w-fit", |
| 54 | + "flex flex-row items-center justify-center", |
| 55 | + "rounded-base hover:cursor-pointer", |
| 56 | + "ease-ease transition-[background] duration-(--klerosUIComponentsTransitionSpeed)", |
| 57 | + |
| 58 | + props.small ? "px-6 py-1.5" : "px-8 py-[11.5px]", |
| 59 | + isPrimary && [ |
| 60 | + "bg-klerosUIComponentsPrimaryBlue hover:bg-klerosUIComponentsSecondaryBlue", |
| 61 | + ], |
| 62 | + isSecondary && [ |
| 63 | + "bg-klerosUIComponentsWhiteBackground hover:bg-klerosUIComponentsMediumBlue", |
| 64 | + "border-klerosUIComponentsPrimaryBlue border", |
| 65 | + props.small ? "px-[23px] py-[5px]" : "px-[31px] py-[10.5px]", |
| 66 | + ], |
| 67 | + isTertiary && [ |
| 68 | + "bg-klerosUIComponentsSecondaryPurple hover:bg-klerosUIComponentsPrimaryPurple", |
| 69 | + ], |
| 70 | + isDisabled && [ |
| 71 | + "bg-klerosUIComponentsLightGrey hover:bg-klerosUIComponentsLightGrey hover:cursor-not-allowed", |
| 72 | + isSecondary && "border-klerosUIComponentsStroke border", |
| 73 | + ], |
| 74 | + className, |
| 75 | + )} |
| 76 | + {...{ variant, isLoading, isDisabled, ...props }} |
| 77 | + > |
| 78 | + {isLoading && <KlerosSymbol {...{ isDisabled, variant }} />} |
| 79 | + <ButtonIcon {...{ icon, Icon, isDisabled, isLoading, variant }} /> |
| 80 | + <ButtonText {...{ isLoading, isDisabled, variant, text }} /> |
| 81 | + </AriaButton> |
35 | 82 | ); |
36 | | -}; |
| 83 | +} |
37 | 84 |
|
38 | 85 | export default Button; |
0 commit comments