2023-05-01 00:51:00 +02:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
2024-01-12 21:16:48 +01:00
|
|
|
import CheckBoxOutlineBlankIcon from 'mastodon/../material-icons/400-24px/check_box_outline_blank.svg?react';
|
2023-10-31 17:05:44 +01:00
|
|
|
import { isProduction } from 'mastodon/utils/environment';
|
|
|
|
|
2023-10-24 19:45:08 +02:00
|
|
|
interface SVGPropsWithTitle extends React.SVGProps<SVGSVGElement> {
|
|
|
|
title?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type IconProp = React.FC<SVGPropsWithTitle>;
|
|
|
|
|
|
|
|
interface Props extends React.SVGProps<SVGSVGElement> {
|
2023-05-01 00:51:00 +02:00
|
|
|
children?: never;
|
2023-10-24 19:45:08 +02:00
|
|
|
id: string;
|
|
|
|
icon: IconProp;
|
|
|
|
title?: string;
|
2023-05-10 12:59:29 +02:00
|
|
|
}
|
|
|
|
|
2023-05-09 19:02:12 +02:00
|
|
|
export const Icon: React.FC<Props> = ({
|
|
|
|
id,
|
2023-10-24 19:45:08 +02:00
|
|
|
icon: IconComponent,
|
2023-05-09 19:02:12 +02:00
|
|
|
className,
|
2023-10-24 19:45:08 +02:00
|
|
|
title: titleProp,
|
2023-05-09 19:02:12 +02:00
|
|
|
...other
|
2023-10-24 19:45:08 +02:00
|
|
|
}) => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
|
|
if (!IconComponent) {
|
2023-10-31 17:05:44 +01:00
|
|
|
if (!isProduction()) {
|
2023-10-24 22:43:06 +02:00
|
|
|
throw new Error(
|
|
|
|
`<Icon id="${id}" className="${className}"> is missing an "icon" prop.`,
|
|
|
|
);
|
2023-10-24 19:45:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
IconComponent = CheckBoxOutlineBlankIcon;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ariaHidden = titleProp ? undefined : true;
|
|
|
|
const role = !ariaHidden ? 'img' : undefined;
|
|
|
|
|
|
|
|
// Set the title to an empty string to remove the built-in SVG one if any
|
|
|
|
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
|
|
const title = titleProp || '';
|
|
|
|
|
|
|
|
return (
|
|
|
|
<IconComponent
|
|
|
|
className={classNames('icon', `icon-${id}`, className)}
|
|
|
|
title={title}
|
|
|
|
aria-hidden={ariaHidden}
|
|
|
|
role={role}
|
|
|
|
{...other}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|