import React from 'react'; import { Link } from '@tanstack/react-router'; import Spinner from '@/components/elements/Spinner'; import { getSkin, replaceable, useSkin } from '@/themes/skins'; export type ButtonColor = 'green' | 'red' | 'orange' | 'primary' | 'grey'; export type ButtonSize = 'xsmall' | 'small' | 'large' | 'xlarge'; interface Props { isLoading?: boolean; size?: ButtonSize; color?: ButtonColor; isSecondary?: boolean; } type StyleableProps = Omit; const buttonState = ({ color, size, isSecondary }: StyleableProps, loading = false) => ({ color: color ?? 'primary', size: size ?? 'small', secondary: !!isSecondary, loading, }); /** The root classes of a button, for elements styled as one outside a component. */ const buttonClassName = ({ className, ...props }: StyleableProps & { className?: string }): string => getSkin('Button', buttonState(props)).classes('root', className); type ButtonStyleProps = { as?: E; ref?: React.ComponentPropsWithRef['ref']; } & StyleableProps & Omit, keyof StyleableProps | 'as'>; /** Any element, styled as a button. */ const ButtonStyle = ({ as, color, size, isSecondary, className, ref, ...props }: ButtonStyleProps) => { const Component = (as || 'button') as React.ElementType; const ui = useSkin('Button', buttonState({ color, size, isSecondary })); return ; }; export type ButtonProps = Omit, keyof Props> & Props; function CoreButton({ children, isLoading, disabled, type = 'button', color, size, isSecondary, className, ref, ...props }: ButtonProps) { const ui = useSkin('Button', buttonState({ color, size, isSecondary }, !!isLoading)); return ( ); } /** Defaults to `type="button"`; `isLoading` shows a spinner and disables the button. */ const Button = replaceable('Button', CoreButton); const TextButton = ({ ref, ...props }: ButtonProps) =>