2023-02-06 10:01:03 -08:00
|
|
|
import clsx from 'clsx';
|
2022-03-21 11:09:01 -07:00
|
|
|
import React from 'react';
|
|
|
|
|
2022-04-07 11:47:06 -07:00
|
|
|
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> {
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Class name for the <svg> icon. */
|
2023-02-15 13:26:27 -08:00
|
|
|
iconClassName?: string
|
2022-04-30 21:39:58 -07:00
|
|
|
/** URL to the svg icon. */
|
2023-02-15 13:26:27 -08:00
|
|
|
src: string
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Text to display next ot the button. */
|
2023-02-15 13:26:27 -08:00
|
|
|
text?: string
|
2022-04-30 21:39:58 -07:00
|
|
|
/** Don't render a background behind the icon. */
|
2023-02-15 13:26:27 -08:00
|
|
|
transparent?: boolean
|
2022-08-12 08:42:26 -07:00
|
|
|
/** Predefined styles to display for the button. */
|
2023-02-15 13:26:27 -08:00
|
|
|
theme?: 'seamless' | 'outlined'
|
2023-02-08 09:58:01 -08:00
|
|
|
/** Override the data-testid */
|
|
|
|
'data-testid'?: string
|
2022-03-21 11:09:01 -07:00
|
|
|
}
|
|
|
|
|
2022-04-30 21:39:58 -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,
|
2022-03-24 12:27:27 -07:00
|
|
|
}, className)}
|
2022-03-21 11:09:01 -07:00
|
|
|
{...filteredProps}
|
2023-02-08 09:58:01 -08:00
|
|
|
data-testid={filteredProps['data-testid'] || 'icon-button'}
|
2022-03-21 11:09:01 -07:00
|
|
|
>
|
2022-04-07 11:47:06 -07:00
|
|
|
<SvgIcon src={src} className={iconClassName} />
|
2022-03-21 11:09:01 -07:00
|
|
|
|
|
|
|
{text ? (
|
2022-07-22 10:30:16 -07:00
|
|
|
<Text tag='span' theme='inherit' size='sm'>
|
2022-03-21 11:09:01 -07:00
|
|
|
{text}
|
|
|
|
</Text>
|
|
|
|
) : null}
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default IconButton;
|