Skip to content

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:

Browser:
html<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>
Node.js:
js// 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:

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:

Adding icons

Functions for adding icons to the SVG framework:

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:

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

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.

Released under the Apache 2.0 License.