Files

114 lines
3.5 KiB
TypeScript
Raw Permalink Normal View History

2019-07-27 15:17:50 -07:00
import React from 'react';
2026-10-05 18:10:18 -04:00
import { Link } from '@tanstack/react-router';
2020-07-04 14:34:43 -07:00
import Spinner from '@/components/elements/Spinner';
2026-10-12 09:04:03 -04:00
import { getSkin, replaceable, useSkin } from '@/themes/skins';
export type ButtonColor = 'green' | 'red' | 'orange' | 'primary' | 'grey';
export type ButtonSize = 'xsmall' | 'small' | 'large' | 'xlarge';
2019-07-27 15:17:50 -07:00
interface Props {
isLoading?: boolean;
2026-10-12 09:04:03 -04:00
size?: ButtonSize;
color?: ButtonColor;
isSecondary?: boolean;
}
2019-07-27 15:17:50 -07:00
2026-10-05 18:10:18 -04:00
type StyleableProps = Omit<Props, 'isLoading'>;
2026-10-12 09:04:03 -04:00
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);
2026-10-05 18:10:18 -04:00
type ButtonStyleProps<E extends React.ElementType = 'button'> = {
as?: E;
ref?: React.ComponentPropsWithRef<E>['ref'];
} & StyleableProps &
Omit<React.ComponentPropsWithoutRef<E>, keyof StyleableProps | 'as'>;
2026-10-12 09:04:03 -04:00
/** Any element, styled as a button. */
2026-10-05 18:10:18 -04:00
const ButtonStyle = <E extends React.ElementType = 'button'>({
as,
color,
size,
isSecondary,
className,
ref,
...props
}: ButtonStyleProps<E>) => {
const Component = (as || 'button') as React.ElementType;
2026-10-12 09:04:03 -04:00
const ui = useSkin('Button', buttonState({ color, size, isSecondary }));
2026-10-05 18:10:18 -04:00
2026-10-12 09:04:03 -04:00
return <Component ref={ref} {...ui('root', className)} {...props} />;
2026-10-05 18:10:18 -04:00
};
2026-10-12 09:04:03 -04:00
export type ButtonProps = Omit<React.ComponentPropsWithRef<'button'>, keyof Props> & Props;
2026-10-05 18:10:18 -04:00
2026-10-12 09:04:03 -04:00
function CoreButton({
children,
isLoading,
disabled,
type = 'button',
color,
size,
isSecondary,
className,
ref,
...props
}: ButtonProps) {
const ui = useSkin('Button', buttonState({ color, size, isSecondary }, !!isLoading));
return (
<button
ref={ref}
{...ui('root', className)}
type={type}
disabled={disabled || isLoading}
aria-busy={isLoading || undefined}
{...props}
>
{isLoading && (
<span {...ui('spinner')}>
<Spinner size='small' />
</span>
)}
<span {...ui('label')}>{children}</span>
</button>
);
}
2026-10-08 16:53:20 -04:00
2026-10-12 09:04:03 -04:00
/** Defaults to `type="button"`; `isLoading` shows a spinner and disables the button. */
const Button = replaceable('Button', CoreButton);
2026-10-05 18:10:18 -04:00
2026-10-12 09:04:03 -04:00
const TextButton = ({ ref, ...props }: ButtonProps) => <Button ref={ref} color='grey' {...props} />;
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
TextButton.displayName = 'Button.Text';
2026-10-12 09:04:03 -04:00
const DangerButton = ({ ref, ...props }: ButtonProps) => <Button ref={ref} color='red' {...props} />;
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
DangerButton.displayName = 'Button.Danger';
// Assigned as static properties; Object.assign would hide the component from Fast Refresh.
const ButtonWithVariants = Button as typeof Button & {
Text: typeof TextButton;
Danger: typeof DangerButton;
};
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
ButtonWithVariants.Text = TextButton;
ButtonWithVariants.Danger = DangerButton;
type LinkProps = Omit<React.JSX.IntrinsicElements['a'], 'ref' | keyof Props> & Props;
2026-10-08 16:53:20 -04:00
const LinkButton = (props: LinkProps) => <ButtonStyle as='a' {...props} />;
2020-07-04 15:58:14 -07:00
2026-10-05 18:10:18 -04:00
const RouterLinkButton = (props: ButtonStyleProps<typeof Link>) => <ButtonStyle as={Link} {...props} />;
2020-07-04 15:58:14 -07:00
2026-10-05 18:10:18 -04:00
export { LinkButton, RouterLinkButton, ButtonStyle, buttonClassName };
export default ButtonWithVariants;