import React from 'react'; import { cva, type VariantProps } from 'class-variance-authority'; import { Link } from '@tanstack/react-router'; import { cn } from '@/lib/cn'; import Spinner from '@/components/elements/Spinner'; interface Props { isLoading?: boolean; size?: 'xsmall' | 'small' | 'large' | 'xlarge'; color?: 'green' | 'red' | 'orange' | 'primary' | 'grey'; isSecondary?: boolean; } const buttonStyle = cva( 'relative inline-block rounded-sm border p-2 text-sm uppercase tracking-wide transition-colors duration-150 disabled:cursor-default disabled:opacity-50', { variants: { color: { primary: 'bg-primary border-primary text-primary-foreground [&:hover:not(:disabled)]:bg-primary/80 [&:hover:not(:disabled)]:border-primary', grey: 'border-border bg-popover text-foreground [&:hover:not(:disabled)]:bg-secondary [&:hover:not(:disabled)]:border-secondary', green: 'border-success bg-success text-success-foreground [&:hover:not(:disabled)]:bg-success/80 [&:hover:not(:disabled)]:border-success', red: 'border-destructive bg-destructive text-destructive-foreground [&:hover:not(:disabled)]:bg-destructive/80 [&:hover:not(:disabled)]:border-destructive', orange: 'border-caution bg-caution text-caution-foreground [&:hover:not(:disabled)]:bg-caution/80 [&:hover:not(:disabled)]:border-caution', }, size: { xsmall: 'px-2 py-1 text-xs', small: 'px-4 py-2', large: 'p-4 text-sm', xlarge: 'p-4 w-full', }, isSecondary: { true: 'border-border bg-transparent text-foreground [&:hover:not(:disabled)]:border-popover [&:hover:not(:disabled)]:bg-popover [&:hover:not(:disabled)]:text-foreground', false: '', }, }, compoundVariants: [ { isSecondary: true, color: 'primary', class: '[&:hover:not(:disabled)]:bg-primary [&:hover:not(:disabled)]:border-primary [&:hover:not(:disabled)]:text-primary-foreground', }, { isSecondary: true, color: 'red', class: '[&:hover:not(:disabled)]:bg-destructive [&:hover:not(:disabled)]:border-destructive [&:hover:not(:disabled)]:text-destructive-foreground', }, { isSecondary: true, color: 'green', class: '[&:hover:not(:disabled)]:bg-success [&:hover:not(:disabled)]:border-success [&:hover:not(:disabled)]:text-success-foreground', }, { isSecondary: true, color: 'orange', class: '[&:hover:not(:disabled)]:bg-caution [&:hover:not(:disabled)]:border-caution [&:hover:not(:disabled)]:text-caution-foreground', }, { isSecondary: false, color: 'primary', class: '[&:active:not(:disabled)]:bg-primary/70 [&:active:not(:disabled)]:border-primary/70', }, { isSecondary: false, color: 'green', class: '[&:active:not(:disabled)]:bg-success/70 [&:active:not(:disabled)]:border-success', }, { isSecondary: false, color: 'red', class: '[&:active:not(:disabled)]:bg-destructive/70 [&:active:not(:disabled)]:border-destructive', }, { isSecondary: false, color: 'orange', class: '[&:active:not(:disabled)]:bg-caution/70 [&:active:not(:disabled)]:border-caution', }, ], defaultVariants: { color: 'primary', size: 'small', isSecondary: false, }, } ); type StyleProps = VariantProps; const buttonClassName = ({ color, size, isSecondary, className, }: Omit & { className?: string }): string => cn( buttonStyle({ // A secondary button without a colour skips the default primary fill. color: isSecondary && !color ? undefined : (color as StyleProps['color']), size: (size as StyleProps['size']) ?? 'small', isSecondary: !!isSecondary, }), className ); type StyleableProps = Omit; type ButtonStyleProps = { as?: E; ref?: React.ComponentPropsWithRef['ref']; } & StyleableProps & Omit, keyof StyleableProps | 'as'>; const ButtonStyle = ({ as, color, size, isSecondary, className, ref, ...props }: ButtonStyleProps) => { const Component = (as || 'button') as React.ElementType; return ; }; type ComponentProps = Omit, keyof Props> & Props; /** Defaults to `type="button"`; `isLoading` shows a spinner and disables the button. */ const Button = ({ children, isLoading, disabled, type = 'button', ref, ...props }: ComponentProps) => ( {isLoading && ( )} {children} ); Button.displayName = 'Button'; const TextButton = ({ ref, ...props }: ComponentProps) =>