@uxf/icons-generator
A well configurable tool for generating and managing custom SVG icons and sprites in web projects.
Quick Start
Installation
Use npm or yarn to install:
npm i -D @uxf/icons-generator
yarn add -D @uxf/icons-generator
Configuration
Create a icons.config.js
file in your project's root:
module.exports = {
icons: {
test: {
width: 24,
height: 24,
data: `<path fill="#fecd09" d="M33.03 17.44c-1.68 0-3.12-.99-3.79-2.42h7.58a4.181 4.181 0 01-3.79 2.42z" />`
}
}
}
See the full configuration options here.
Generating Icons
Run the generator:
icons-gen
For a custom config file name:
icons-gen --configFile=yourConfigFileName.js
Configuration Details
Key | Type | Default | Required | Description |
---|
typescript | boolean | true | No | - |
configDirectory | string | "/src/config/" | No | - |
generatedDirectory | string | "/public/icons-generated/" | No | - |
spriteFileName | string | _icon-sprite.svg | No | Specify custom sprite file name |
typeName | string | IconsSet | No | - |
customDefinitionContent | string | - | No | - |
icons | Record<string, SimpleIcon | SizedIcon | IconFromAdapterFunction> | - | Yes | More details here |
fallbackFilesDirectory | - | - | No | More details here |
Icon Types
SimpleIcon
type SimpleIcon = {
data: string;
height: number;
width: number;
};
Example:
test: {
width: 50,
height: 60,
data: `<path fill="#fecd09" d="M33.03 17.44c-1.68 0-3.12-.99-3.79-2.42h7.58a4.181 4.181 0 01-3.79 2.42z" />`,
}
SizedIcon
type SizedIcon = Record<number, string>;
Example:
test: {
24: `<path fill="#fecd09" d="M33.03 17.44c-1.68 0-3.12-.99-3.79-2.42h7.58a4.181 4.181 0 01-3.79 2.42z" />`,
44: `<path fill="#fecd09" d="M33.03 17.44c-1.68 0-3.12-.99-3.79-2.42h7.58a4.181 4.181 0 01-3.79 2.42z" />`,
}
Predefined providers
Font Awesome Pro Adapter
Local Development Setup
// Set registry and token
npm config set "@fortawesome:registry" https://npm.fontawesome.com/
npm config set "//npm.fontawesome.com/:_authToken" YOUR_TOKEN
// Install package
npm install --save-dev @fortawesome/fontawesome-pro
Usage
const { faPro } = require('@uxf/icons-generator/providers/fa-pro');
icons: {
...faPro.adapter(["brands.linkedin"]),
twitter: faPro.icon("brands.twitter"),
}
Without Private Key
If your project already contains generated icons, installing the Font Awesome Pro package is not necessary.
The package automatically generates a 'fallback file' for the icons in use, ensuring functionality even without
Font Awesome Pro. However, while this setup allows for the continued use of existing icons, adding new icons
from the adapter will not be possible.
Recommended Usage (Step-by-Step)
1. Configuration
Create and configure your icon settings in a config file at the project's root.
2. Icon Generation
Execute icons-gen
to produce:
- SVG sprite and individual files.
- Definition file with icon list and sizes.
- Config file with icon version and sprite path.
3. Icon Component Setup
Incorporate the generated output into your project. Example for TypeScript and React:
import { ICONS, IconsSet } from "src/config/icons";
import { ICON_SPRITE } from "src/config/icons-config";
type IconProps = {
name: IconsSet;
};
export const Icon: FC<IconProps> = ({ name }) => {
const sizes = ICONS[name];
return (
<svg
height={sizes.h}
width={sizes.w}
preserveAspectRatio="xMidYMid meet"
role="img"
viewBox={`0 0 ${sizes.w} ${sizes.h}`}
>
<use xlinkHref={`${ICON_SPRITE}#icon-sprite--${name}`} />
</svg>
);
};
4. Sprite Preloading (Optional)
import { ICON_SPRITE, ICONS_VERSION } from "src/config/icons-config";
<Head>
<link
as="image"
href={`${ICON_SPRITE}?v=${ICONS_VERSION}`}
rel="preload"
type="image/svg+xml"
/>
</Head>
5. Verification
Run icons-check
to ensure icon consistency with the config. It's recommended to integrate this check into your CI process.