Skip to content

Instantly share code, notes, and snippets.

@Maruz
Last active November 20, 2019 12:33
Show Gist options
  • Save Maruz/0dd7daaad10a9b139fbfc36b610f803f to your computer and use it in GitHub Desktop.
Save Maruz/0dd7daaad10a9b139fbfc36b610f803f to your computer and use it in GitHub Desktop.
React FontAwesomeIcon
import React, { FC } from 'react';
import cx from 'clsx';
export interface FontAwesomeIconProps {
animate?: 'spin' | 'pulse' | undefined;
border?: boolean;
className?: string | undefined;
color?: string | undefined;
fixedWidth?: boolean;
flip?: 'vertical' | 'horizontal' | 'both' | undefined;
fontWeight?: 900 | 400 | 300 | undefined;
inverse?: boolean | undefined;
mask?: string;
name: string;
prefix?: 'fas' | 'far' | 'fal' | 'fad' | 'fab';
pull?: 'left' | 'right' | undefined;
rotate?: 90 | 180 | 270 | undefined;
size?:
| 'xs'
| 'sm'
| 'md'
| 'lg'
| '2x'
| '3x'
| '4x'
| '5x'
| '6x'
| '7x'
| '8x'
| '9x'
| '10x';
stack?: boolean | '1x' | '2x';
style?: Object;
swapOpacity?: boolean;
transform?: string;
[key: string]: any; // so the rest spread works properly with TS
}
const FontAwesomeIcon: FC<FontAwesomeIconProps> = ({
animate,
border,
className,
color,
fixedWidth,
flip,
fontWeight,
inverse,
mask,
name,
prefix = 'fas',
pull,
rotate,
size = 'md',
stack,
style,
swapOpacity,
transform,
...rest
}) => (
<i
className={cx(
prefix,
`fa-${name}`,
`fa-${size}`,
{
'data-fa-mask': mask,
'data-fa-transform': transform,
'fa-border': border,
'fa-fw': fixedWidth,
'fa-inverse': inverse,
'fa-pulse': animate === 'pulse',
'fa-spin': animate === 'spin',
'fa-swap-opacity': swapOpacity,
[`fa-flip-${flip}`]: flip,
[`fa-pull-${pull}`]: pull,
[`fa-rotate-${rotate}`]: rotate,
[`fa-stack${stack === true ? '' : `-${stack}`}`]: stack,
},
className,
)}
style={{ ...style, color, fontWeight }}
{...rest}
/>
);
export default FontAwesomeIcon;
.my-error--error {
&:--error {
color: red;
}
}
import React, { FC } from 'react';
import './my-icon.less';
const MyErrorIcon: FC = () => (
<FontAwesomeIcon name="exclamation-triangle" className="my-icon--error" />
);
export default MyErrorIcon;
@Maruz
Copy link
Author

Maruz commented Nov 20, 2019

See official documentation for how these props work: https://fontawesome.com/how-to-use/on-the-web/referencing-icons/basic-use

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment