soapbox/app/soapbox/components/ui/icon-button/icon-button.tsx

49 wiersze
1.9 KiB
TypeScript
Czysty Zwykły widok Historia

2023-02-06 18:01:03 +00:00
import clsx from 'clsx';
2022-03-21 18:09:01 +00:00
import React from 'react';
import SvgIcon from '../icon/svg-icon';
2022-03-21 18:09:01 +00:00
import Text from '../text/text';
2022-04-02 18:03:12 +00:00
interface IIconButton extends React.ButtonHTMLAttributes<HTMLButtonElement> {
/** Class name for the <svg> icon. */
iconClassName?: string
/** URL to the svg icon. */
src: string
/** Text to display next ot the button. */
text?: string
2022-08-12 15:42:26 +00:00
/** Predefined styles to display for the button. */
2023-04-04 10:29:09 +00:00
theme?: 'seamless' | 'outlined' | 'secondary' | 'transparent'
/** Override the data-testid */
'data-testid'?: string
2022-03-21 18:09:01 +00:00
}
/** A clickable icon. */
2022-03-21 18:09:01 +00:00
const IconButton = React.forwardRef((props: IIconButton, ref: React.ForwardedRef<HTMLButtonElement>): JSX.Element => {
2023-04-04 10:29:09 +00:00
const { src, className, iconClassName, text, theme = 'seamless', ...filteredProps } = props;
2022-03-21 18:09:01 +00:00
return (
<button
ref={ref}
type='button'
2023-02-06 18:06:44 +00:00
className={clsx('flex items-center space-x-2 rounded-full p-1 focus:outline-none focus:ring-2 focus:ring-primary-500 focus:ring-offset-2 dark:ring-offset-0', {
2023-04-04 10:29:09 +00:00
'bg-white dark:bg-transparent': theme === 'seamless',
2022-08-12 15:42:26 +00:00
'border border-solid bg-transparent border-gray-400 dark:border-gray-800 hover:border-primary-300 dark:hover:border-primary-700 focus:border-primary-500 text-gray-900 dark:text-gray-100 focus:ring-primary-500': theme === 'outlined',
2023-03-22 17:56:32 +00:00
'border-transparent bg-primary-100 dark:bg-primary-800 hover:bg-primary-50 dark:hover:bg-primary-700 focus:bg-primary-100 dark:focus:bg-primary-800 text-primary-500 dark:text-primary-200': theme === 'secondary',
2022-07-06 18:19:42 +00:00
'opacity-50': filteredProps.disabled,
}, className)}
2022-03-21 18:09:01 +00:00
{...filteredProps}
data-testid={filteredProps['data-testid'] || 'icon-button'}
2022-03-21 18:09:01 +00:00
>
<SvgIcon src={src} className={iconClassName} />
2022-03-21 18:09:01 +00:00
{text ? (
<Text tag='span' theme='inherit' size='sm'>
2022-03-21 18:09:01 +00:00
{text}
</Text>
) : null}
</button>
);
});
export default IconButton;