Skip to content

Instantly share code, notes, and snippets.

@chrislambe
Created August 31, 2020 18:35
Show Gist options
  • Save chrislambe/062bfc12aeecd7502b39c8dafe19f185 to your computer and use it in GitHub Desktop.
Save chrislambe/062bfc12aeecd7502b39c8dafe19f185 to your computer and use it in GitHub Desktop.
Font Awesome icon component for Material UI
import { IconDefinition } from '@fortawesome/fontawesome-svg-core';
import SvgIcon, { SvgIconProps } from '@material-ui/core/SvgIcon';
import makeStyles from '@material-ui/styles/makeStyles';
import React, { forwardRef } from 'react';
type Props = Omit<SvgIconProps, 'viewBox'> & {
icon: IconDefinition;
};
const useStyles = makeStyles({
root: {},
colorSecondary: {},
colorAction: {},
colorDisabled: {},
colorError: {},
colorPrimary: {},
fontSizeInherit: {},
fontSizeSmall: {},
fontSizeLarge: {},
pathPrimary: {},
pathSecondary: {
opacity: 0.4,
},
});
export const FontAwesomeSvgIcon = forwardRef<SVGSVGElement, Props>((props, ref) => {
const {
icon: {
icon: [width, height, , , svgPathData],
},
classes,
...svgIconProps
} = props;
const {
pathPrimary: pathPrimaryClassName,
pathSecondary: pathSecondaryClassName,
...svgIconClasses
} = useStyles(props);
return (
<SvgIcon
ref={ref}
{...svgIconProps}
classes={svgIconClasses}
viewBox={`0 0 ${width} ${height}`}>
{typeof svgPathData === 'string' ? (
<path d={svgPathData} />
) : (
/**
* For whatever reason the 0th path is considered "secondary"
*/
svgPathData.map((d, i) => (
<path className={i === 0 ? pathSecondaryClassName : pathPrimaryClassName} d={d} />
))
)}
</SvgIcon>
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment