IconSet class
IconSet class in Iconify Tools represents an icon set.
Usage
To create an instance, use this code to import existing IconifyJSON data:
ts
import { IconSet } from '@iconify/tools';
const iconSet = new IconSet({
prefix: 'codicon',
icons: {
'add': {
body: '<g fill="currentColor"><path d="M14 7v1H8v6H7V8H1V7h6V1h1v6h6z"/></g>',
},
'chrome-maximize': {
body: '<g fill="currentColor"><path d="M3 3v10h10V3H3zm9 9H4V4h8v8z"/></g>',
},
'chrome-minimize': {
body: '<g fill="currentColor"><path d="M14 8v1H3V8h11z"/></g>',
},
},
});
or this to create an empty icon set:
ts
import { blankIconSet } from '@iconify/tools';
const iconSet = blankIconSet('some-prefix');
Constructor does not validate the icon set. If you are not sure about the source, you need to validate it using validateIconSet() from Iconify Utils.
Functions
Working with icons:
- list() lists all icons.
- forEach() runs callback for all icons. Supports asynchronous callbacks.
- exists(name) checks if icon exists.
- count() counts number of icons in the icon set.
- remove(name) removes icon.
- rename(oldName, newName) renames icon.
- setItem(name, item) adds/updates item in entries property.
- setIcon(name, icon) adds/updates icon, using IconifyIcon data.
- setVariation(name, parent, props) creates a variation (alias with customisations) for icon.
- setAlias(name, parent) creates an alias for icon.
- getTree() returns list of parent icons for each icon, null if icon is invalid.
- entries property contains data for all icons and aliases. You can access it directly, but most changes can be done using functions listed above.
Functions for importing/exporting icon set:
- prefix property contains icon set prefix, which is used when exporting icon set. To change prefix, write to property.
- resolve(name) returns ResolvedIconifyIcon object for icon, null on failure.
- load(data) loads data from IconifyJSON type. This is identical to creating new IconSet instance, but it changes current instance instead of making new one.
- export() exports icon set as IconifyJSON.
- toSVG(name) returns SVG instance for icon, null on failure.
- fromSVG(name, svg) adds/updates icon from SVG instance.
- toString(name) exports icon as SVG string, returns null on failure.
Functions for working with metadata:
- info property contains icon set info in IconifyInfo type (or null if info is not available). To update info, write to property.
- chars() returns characters map, where key is character (as hexadecimal code) and value is icon name.
- toggleCharacter(name, char, add) adds or removes character for icon.
- listCategory(category) lists all icons in category, excluding aliases and hidden icons.
- toggleCategory(name, category, add) adds or removes category for icon.
- categories property contains data for categories. You can access it directly if needed.
- checkTheme() checks prefixes or suffixes, returning list of icons that belong to each theme and list of icons that do not belong to any theme.
- suffixes and prefixes properties contain prefixes and suffixes. Access properties directly to update themes.
Other:
- mergeIconSets() merges two IconSet instances, returning new instance. This function is intended to be used to update the icon set.
Working with icons
All icon optimisation and parsing functions work with SVG instances. How to apply those functions to an entire icon set?
It can be done by icons using forEach() method:
ts
iconSet.forEach(async (name, type) => {
if (type !== 'icon') {
// Ignore aliases and variations: they inherit content from parent icon, so there is nothing to change
return;
}
const svg = iconSet.toSVG(name);
if (svg) {
// Change colors to red
parseColors(svg, {
defaultColor: 'red',
callback: (attr, colorStr, color) => {
return !color || isEmptyColor(color) ? colorStr : 'red';
},
});
// Update icon from SVG instance
iconSet.fromSVG(name, svg);
}
});
// The rest of code here