SVG framework functions
This tutorial is part of Iconify SVG Framework tutorial.
Iconify SVG framework has many functions that you can use to use the SVG framework in your scripts.
Usage
There are two ways of using SVG framework functions:
- By using Iconify global.
- By importing Iconify (or named functions you want to import) from @iconify/iconify if you are bundling SVG framework with your scripts.
Examples of using loadIcon function:
<script src="https://code.iconify.design/3/3.1.1/iconify.min.js"></script>
<script>
Iconify.loadIcon('mdi:home')
.then((data) => {
console.log('Loaded home icon!');
})
.catch((err) => {
console.error('Failed to load home icon');
});
</script>
// Default import for everything
// import Iconify from '@iconify/iconify';
// Named import for specific function
import { loadIcon } from '@iconify/iconify';
loadIcon('mdi:home')
.then((data) => {
console.log('Loaded home icon!');
})
.catch((err) => {
console.error('Failed to load home icon');
});
Even if you use bundle (method shown in second example), Iconify global is also available because SVG framework exports functions and creates a global variable regardless of how you use it. That means you can use method shown in first example regardless of how you import SVG framework.
Functions
Functions are split in several groups:
- General functions.
- Getting icons.
- Adding icons.
- Rendering icons.
- Scanning and observing DOM.
- API functions.
- Internal API functions.
Click function name to see more details and examples.
General functions
In this section there is only one function:
- getVersion(). This function returns SVG framework version string. "3.1.1"
Getting icons
There are several functions in this section:
- iconExists(name). Checks if icon data is available, returns boolean.
- listIcons(). Lists available icons, returns string[].
- getIcon(name). Returns icon data, returns IconifyIcon object.
Adding icons
Functions for adding icons to the SVG framework:
- addIcon(). Adds one icon.
- addCollection(). Adds an icon set.
Note: icons added to the SVG framework with these functions are not stored in the icon data cache. Component caches only icons retrieved from API.
Rendering icons
Functions that generate SVG or data:
- renderSVG(name, customisations?). Creates <svg> element.
- renderHTML(name, customisations?). Returns <svg> string.
- renderIcon(name, customisations?). Generates data used by functions above. This can be used if you prefer to generate <svg> yourself. Data includes attributes for <svg> and inner HTML.
- buildIcon(data, customisations) is identical to renderIcon(), but uses icon data as first parameter
Scanning and observing DOM
SVG framework automatically scans DOM whenever something changes. However, there are some limitations:
- Observer can observe only child elements of document.body.
- SVG framework scans DOM after every change (though scans are throttled to avoid scanning too often).
In some instances you might want to temporarily disable observer or scan an element that is not part of DOM, such as Shadow DOM. There are functions that you can use:
- scan(root?). Scans DOM or custom element for placeholder elements.
- observe(root). Observes custom root element.
- stopObserving(root). Stops observing custom root element. You can call it with document.body as parameter to stop observing document.body.
- pauseObserver(root?). Pauses observer.
- resumeObserver(root?). Resumes observer.
Helper functions
Few helper functions that are exposed because they might be useful when creating things such as icon picker:
- calculateSize(). Helper function to calculates icon size. It is used to calculate width if only height is set and vice versa.
- replaceIDs(html). Randomizes IDs in generated string. This should be used when rendering icon based on data returned by renderIcon() or getIcon to make sure elements inside each icon have unique IDs. This function is not needed for icons generated by renderSVG() and renderHTML().
API functions
- loadIcons(icons, callback?). Loads icons from API, calls optional callback when either all or part of icons have been loaded.
- loadIcon(icon). Loads one icon from API, returns Promise.
- enableCache(). Enables caching in localStorage and sessionStorage.
- disableCache(). Disables caching in localStorage and sessionStorage.
- addAPIProvider(). Adds custom API provider. See API providers documentation.
Internal API functions
There are several internal API functions that are exposed. They are intended to be used by developers that need more control over the component. For example, it is used in Sketch and Figma plug-ins. Use them carefully.
All internal API functions are exposed as properties of Iconify._api object and are available only when API is included:
- getAPI(). Returns internal API module.
- getAPIConfig(). Returns API configuration.
- setAPIModule(provider). Sets API module for provider. This is experimental function intended for custom API providers. API provider functionality is in development.