chinwagsocial/app/javascript/mastodon/components/button.tsx

63 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

2024-03-28 21:06:25 +11:00
import type { PropsWithChildren } from 'react';
import { useCallback } from 'react';
import classNames from 'classnames';
2024-03-28 21:06:25 +11:00
interface BaseProps
extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'children'> {
block?: boolean;
secondary?: boolean;
dangerous?: boolean;
}
2024-03-28 21:06:25 +11:00
interface PropsChildren extends PropsWithChildren<BaseProps> {
text?: undefined;
}
interface PropsWithText extends BaseProps {
2024-03-28 21:06:25 +11:00
text: JSX.Element | string;
children?: undefined;
}
2024-03-28 21:06:25 +11:00
type Props = PropsWithText | PropsChildren;
export const Button: React.FC<Props> = ({
type = 'button',
onClick,
disabled,
block,
secondary,
dangerous,
className,
title,
2024-03-28 21:06:25 +11:00
text,
children,
...props
}) => {
const handleClick = useCallback<React.MouseEventHandler<HTMLButtonElement>>(
(e) => {
if (!disabled && onClick) {
onClick(e);
}
},
[disabled, onClick],
);
return (
<button
className={classNames('button', className, {
'button-secondary': secondary,
'button--block': block,
'button--dangerous': dangerous,
})}
disabled={disabled}
onClick={handleClick}
title={title}
type={type}
{...props}
>
{text ?? children}
</button>
);
};