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

48 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-03-21 11:09:01 -07:00
import classNames from 'classnames';
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. */
2022-03-21 11:09:01 -07:00
iconClassName?: string,
/** URL to the svg icon. */
2022-03-21 11:09:01 -07:00
src: string,
/** Text to display next ot the button. */
2022-03-21 11:09:01 -07:00
text?: string,
/** Don't render a background behind the icon. */
2022-08-12 08:42:26 -07:00
transparent?: boolean,
/** Predefined styles to display for the button. */
theme?: 'seamless' | 'outlined',
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'
2022-03-23 17:18:37 -07:00
className={classNames('flex items-center space-x-2 p-1 rounded-full focus:outline-none focus:ring-2 focus:ring-offset-2 dark:ring-offset-0 focus:ring-primary-500', {
'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}
2022-04-07 08:01:38 -07:00
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;