A python script is provided to quickly and easily generate a React component that displays a Material UI Icon, an SVG of a Material UI icon for use in the Jinja templates or both.
To generate both a React component and an SVG of a Material UI icon, run the
following command from inside the tools/resources/icons
directory:
./run.sh {icon_name}
For example:
./run.sh arrow_forward
This will download the SVG from the Material UI repository, process it and generate two files:
- An SVG for use in Jinja templates in the
server/templates/resources/icons
directory. - A React component in
static/js/components/elements/icons
.
To generate only the SVG for the templates, run either of the following commands:
./run.sh -f arrow_forward
./run.sh --flask arrow_forward
To generate only the React component run either of the following commands:
./run.sh -r arrow_forward
./run.sh --react arrow_forward
It is recommended to run the prettier on the generated .tsx
file.
If for any reason you need to generate an icon manually (for example, if an icon is not available from the repository via the script), you can do so with these directions. Note that this should only rarely be required.
- Download the SVG. If it is a Material UI font, it will likely come from https://fonts.google.com/icons
- Change the height to "1em". Change the fill to "currentColor". Remove the width.
- For use in the Jinja templates, copy this SVG into
server/templates/resources/icons
. - For use as a React component
- open an existing component in
static/js/components/elements/icons
, and save it with the new icon's name. - Update the source to indicate the source of the SVG you are using. This might be https://fonts.google.com/icons.
- Update the name in the comments.
- Paste the SVG over top of the old SVG.
- Add {...props} as the final prop in the SVG before the
>
.
- open an existing component in
To use a generated icon in a Jinja template
{% from 'macros/icons.html' import inline_svg %}
{{ inline_svg('arrow_forward') }}
You can use the React component directly inside the JSX:
<ArrowForward />
If done so without explicitly providing color and size, the icon will inherit its color and size from the enclosing CSS, just as a font icon would. For example:
span.big-red-icon {
font-size: 50px;
color: red;
}
<span className="big-red-icon">
<ArrowForward />
</span>
This allows you to use the React component icons much as you would the Material Icons provided through Google Fonts, with the added advantage that they are inline and not subject to flashes of unstyled content.
You can also style the icons directly with its props. Each added icon component can take any prop that you can send into an SVG.
<ArrowForward
fill="red"
height="50px"
/>