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';
|
2022-11-28 02:00:28 +00:00
|
|
|
import * as lucideIcons from 'lucide-react';
|
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>;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
className?: string;
|
2022-08-01 07:13:58 +00:00
|
|
|
size?: IconSize;
|
|
|
|
mode?: IconMode;
|
2023-07-13 20:55:52 +00:00
|
|
|
spin?: boolean;
|
2022-06-23 06:32:18 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 20:55:52 +00:00
|
|
|
export function Icon({ icon, className, mode, size, spin }: Props) {
|
|
|
|
const classes = clsx(className, 'icon inline-flex', {
|
|
|
|
[`icon-${mode}`]: mode,
|
|
|
|
[`icon-${size}`]: size,
|
|
|
|
'animate-spin-slow': spin,
|
|
|
|
});
|
2022-07-06 14:05:00 +00:00
|
|
|
|
2022-06-23 06:32:18 +00:00
|
|
|
if (typeof icon !== 'string') {
|
|
|
|
const Icon = isValidElementType(icon) ? icon : null;
|
|
|
|
|
2022-11-28 02:00:28 +00:00
|
|
|
if (Icon) {
|
|
|
|
return <Icon className={classes} aria-hidden="true" role="img" />;
|
|
|
|
}
|
|
|
|
|
2022-06-23 06:32:18 +00:00
|
|
|
return (
|
2022-07-06 14:05:00 +00:00
|
|
|
<span className={classes} aria-hidden="true" role="img">
|
2022-11-28 02:00:28 +00:00
|
|
|
{icon}
|
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-', '');
|
2022-08-10 04:12:20 +00:00
|
|
|
return (
|
|
|
|
<Svg
|
|
|
|
icon={svgIcon as keyof typeof SvgIcons}
|
2022-11-28 02:00:28 +00:00
|
|
|
className={classes}
|
|
|
|
aria-hidden="true"
|
2022-08-10 04:12:20 +00:00
|
|
|
/>
|
|
|
|
);
|
2022-07-14 01:44:56 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 02:00:28 +00:00
|
|
|
const iconName = icon
|
|
|
|
.split('-')
|
|
|
|
.map((s) => s.slice(0, 1).toUpperCase() + s.slice(1))
|
|
|
|
.join('') as keyof typeof lucideIcons;
|
|
|
|
const IconComponent = lucideIcons[iconName] as React.FC<
|
|
|
|
React.SVGProps<SVGSVGElement>
|
|
|
|
>;
|
|
|
|
if (!IconComponent) {
|
|
|
|
// console error so that the error is logged but no functionality is broken
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(`Icon not found: '${icon}'`);
|
|
|
|
return null;
|
2022-06-23 06:32:18 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 02:00:28 +00:00
|
|
|
return <IconComponent className={classes} aria-hidden="true" />;
|
2022-06-23 06:32:18 +00:00
|
|
|
}
|