2022-06-23 06:32:18 +00:00
|
|
|
import clsx from 'clsx';
|
2022-07-25 08:11:48 +00:00
|
|
|
import { ComponentType, ReactNode } from 'react';
|
|
|
|
import * as featherIcons from 'react-feather';
|
2022-06-23 06:32:18 +00:00
|
|
|
import { isValidElementType } from 'react-is';
|
|
|
|
|
2022-07-14 01:44:56 +00:00
|
|
|
import Svg, { SvgIcons } from './Svg';
|
|
|
|
|
2022-06-23 06:32:18 +00:00
|
|
|
export interface IconProps {
|
|
|
|
icon: ReactNode | ComponentType<unknown>;
|
|
|
|
featherIcon?: boolean;
|
|
|
|
}
|
|
|
|
|
2022-08-01 07:13:58 +00:00
|
|
|
export type IconMode =
|
|
|
|
| 'alt'
|
|
|
|
| 'primary'
|
|
|
|
| 'primary-alt'
|
|
|
|
| 'secondary'
|
|
|
|
| 'secondary-alt'
|
|
|
|
| 'warning'
|
|
|
|
| 'warning-alt'
|
|
|
|
| 'danger'
|
|
|
|
| 'danger-alt'
|
|
|
|
| 'success'
|
|
|
|
| 'success-alt';
|
|
|
|
|
|
|
|
export type IconSize = 'xs' | 'sm' | 'md' | 'lg' | 'xl';
|
|
|
|
|
2022-06-23 06:32:18 +00:00
|
|
|
interface Props {
|
2022-07-06 14:05:00 +00:00
|
|
|
icon: ReactNode | ComponentType<{ size?: string | number }>;
|
2022-06-23 06:32:18 +00:00
|
|
|
feather?: boolean;
|
|
|
|
className?: string;
|
2022-08-01 07:13:58 +00:00
|
|
|
size?: IconSize;
|
|
|
|
mode?: IconMode;
|
2022-06-23 06:32:18 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 02:11:13 +00:00
|
|
|
export function Icon({ icon, feather, className, mode, size }: Props) {
|
2022-07-06 14:05:00 +00:00
|
|
|
const classes = clsx(
|
|
|
|
className,
|
|
|
|
'icon',
|
|
|
|
{ [`icon-${mode}`]: mode },
|
|
|
|
{ [`icon-${size}`]: size }
|
|
|
|
);
|
|
|
|
|
2022-06-23 06:32:18 +00:00
|
|
|
if (typeof icon !== 'string') {
|
|
|
|
const Icon = isValidElementType(icon) ? icon : null;
|
|
|
|
|
|
|
|
return (
|
2022-07-06 14:05:00 +00:00
|
|
|
<span className={classes} aria-hidden="true" role="img">
|
|
|
|
{Icon == null ? <>{icon}</> : <Icon size="1em" />}
|
2022-06-23 06:32:18 +00:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-14 01:44:56 +00:00
|
|
|
if (icon.indexOf('svg-') === 0) {
|
|
|
|
const svgIcon = icon.replace('svg-', '');
|
|
|
|
return <Svg icon={svgIcon as keyof typeof SvgIcons} className={classes} />;
|
|
|
|
}
|
|
|
|
|
2022-06-23 06:32:18 +00:00
|
|
|
if (feather) {
|
2022-07-25 08:11:48 +00:00
|
|
|
const iconName = icon
|
|
|
|
.split('-')
|
|
|
|
.map((s) => s.slice(0, 1).toUpperCase() + s.slice(1))
|
|
|
|
.join('') as keyof typeof featherIcons;
|
|
|
|
const IconComponent = featherIcons[iconName];
|
|
|
|
return <IconComponent className={classes} />;
|
2022-06-23 06:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-07-06 14:05:00 +00:00
|
|
|
<i className={clsx('fa', icon, classes)} aria-hidden="true" role="img" />
|
2022-06-23 06:32:18 +00:00
|
|
|
);
|
|
|
|
}
|