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 if you are bundling SVG framework with your scripts.

Examples of using loadIcons function:

<script src="https://code.iconify.design/2/2.0.4/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:

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.4"

Getting icons

There are several functions in this section:

Adding icons

Functions for adding icons to SVG framework:

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:

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:

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 developers that need more control over component. For example, it is used in 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.