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

50 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-02-06 10:01:03 -08:00
import clsx from 'clsx';
2022-03-21 11:09:01 -07:00
import React from 'react';
import SvgIcon from '../icon/svg-icon';
2022-03-21 11:09:01 -07:00
import Text from '../text/text';
2022-04-02 11:03:12 -07: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
/** Don't render a background behind the icon. */
transparent?: boolean
2022-08-12 08:42:26 -07:00
/** Predefined styles to display for the button. */
theme?: 'seamless' | 'outlined'
/** Override the data-testid */
'data-testid'?: string
2022-03-21 11:09:01 -07:00
}
/** A clickable icon. */
2022-03-21 11:09:01 -07:00
const IconButton = React.forwardRef((props: IIconButton, ref: React.ForwardedRef<HTMLButtonElement>): JSX.Element => {
2022-08-12 08:42:26 -07:00
const { src, className, iconClassName, text, transparent = false, theme = 'seamless', ...filteredProps } = props;
2022-03-21 11:09:01 -07:00
return (
<button
ref={ref}
type='button'
2023-02-06 10:06:44 -08: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', {
2022-03-23 17:18:37 -07:00
'bg-white dark:bg-transparent': !transparent,
2022-08-12 08:42:26 -07: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',
2022-07-06 11:19:42 -07:00
'opacity-50': filteredProps.disabled,
}, className)}
2022-03-21 11:09:01 -07:00
{...filteredProps}
data-testid={filteredProps['data-testid'] || 'icon-button'}
2022-03-21 11:09:01 -07:00
>
<SvgIcon src={src} className={iconClassName} />
2022-03-21 11:09:01 -07:00
{text ? (
<Text tag='span' theme='inherit' size='sm'>
2022-03-21 11:09:01 -07:00
{text}
</Text>
) : null}
</button>
);
});
export default IconButton;