SVG framework functions
This tutorial is part of Iconify SVG Framework tutorial.
Iconify SVG framework has many functions that you can use to use SVG framework in your scripts.
Usage
There are two ways of using SVG framework functions:
- By using Iconify global.
- By importing Iconify from @iconify/iconify@beta if you are bundling SVG framework with your scripts.
Examples of using loadIcons function:
<script src="https://code.iconify.design/2/2.0.0-rc.6/iconify.min.js"></script>
<script>
Iconify.loadIcons(['mdi:home'], () => {
console.log('Loaded home icon!');
});
</script>
import Iconify from '@iconify/iconify';
Iconify.loadIcons(['mdi:home'], () => {
console.log('Loaded 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. "2.0.0-rc.6"
Getting icons
There are several functions in this section:
- iconExists(name). Checks if an icon exists, returns boolean.
- listIcons(). Lists available icons, returns string[].
- getIcon(name). Returns icon data, returns IconifyIcon object.
Adding icons
Functions for adding icons to SVG framework:
- addIcon(). Adds one icon.
- addCollection(). Adds an icon set.
Note: icons added to SVG framework with these functions are not stored in cache. SVG framework 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.
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
These functions are not available in module without API.
- loadIcons(icons, callback?). Loads icons from API, calls optional callback when either all or part of icons have been loaded.
- enableCache(). Enables caching in localStorage and sessionStorage.
- disableCache(). Disables caching in localStorage and sessionStorage.
- addAPIProvider(). Adds custom API provider. This is experimental function. API provider functionality is in development.
Internal API functions
There are several internal API functions that are exposed. They are intended to be used by implementations that want more control over SVG framework, such as Iconify Icon Finder. 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.