Iconify for Svelte
In addition to SVG framework, Iconify offers native components for several popular UI frameworks. Iconify for Svelte is one of such components.
Yet another icon component? What are advantages over other icon components?
- One syntax for over 100,000 icons from 100+ icon sets.
- Renders SVG. Many components simply render icon fonts, which look ugly. Iconify renders pixel perfect SVG.
- Loads icons on demand. No need to bundle icons, component will automatically load icon data for icons that you use from Iconify API.
If you are using NPM:
npm install --save-dev @iconify/svelte
If you are using Yarn:
yarn add --dev @iconify/svelte
Installand import component from it (component is exported as default export):
import Icon from '@iconify/svelte';
Then in template use Icon component with icon name as icon parameter:
<Icon icon="mdi-light:home" />
Retrieving icon data from Iconify API requires visitor to be online. What if you want to use component offline or on local network?
There are several options:
- You can build icon bundles for icons that are used in your application, making icon data available offline.
- You can use individual icon packages, which is similar to icon bundles, but import icons one by one and are easier to use.
See icon bundles for Iconify for Svelte documentation.
Component is compatible with Sapper and SvelteKit. Syntax is the same as with Svelte.
Component does not retrieve icon data until it is mounted. For server side rendering it means HTML will not include SVGs, they will be dynamically added only when hydrating DOM on client side.
If you do want to render SVGs on server side, use either offline bundle or provide icon data as parameter instead of icon name.
You can pass any custom properties to Icon.
- icon, icon name or icon data.
- inline, toggles inline or block mode.
- width, icon width.
- height, icon height.
- hFlip, flips icon horizontally.
- vFlip, flips icon vertically.
- flip, alternative to hFlip and vFlip.
- rotate, rotates icon.
- color, changes icon color.
- hAlign horizontally aligns content inside icon.
- vAlign vertically aligns content inside icon.
- slice alignment behaviour: "meet" or "slice".
- align, aligns content inside icon, alternative to hAlign, vAlign and slice.
- onLoad, is a callback that is called when icon data has been loaded. See below.
See below for more information on each optional property.
In addition to the properties mentioned above, the icon component accepts any other properties. All other properties will be passed to generated SVG element, so you can do stuff like setting the inline style, add title and so on.
Unlike React component, Svelte component does not support events.
Icon name is a string, which has 3 parts:
Examples of valid icon names:
- voice-presentation" from Flat Color Icons icon set, from public Iconify API. - icon is "
- home" from Material Design Light icon set, from public Iconify API. - icon is "
- If API provider is empty, it can be skipped (like in examples above).
- If prefix does not contain "-", prefix and icon name can be separated with dash. This is to support people migrating from icon fonts. For example, and are identical because "fa" does not contain dash.
There are over 100,000 icons available from 100+ icon sets. Browse icons sets to see all available icons.
You can also add custom API providers for more icon choices. See API providers documentation.
You can only change color of monotone icons. Some icons, such as emoji, have a hardcoded palette that cannot be changed.
To add color to a monotone icon simply change text color.
<Icon icon="mdi:home" style="color: red" />
For various ways to set color, see how to change icon color in Iconify for Svelte.
By default, icon height is set to "1em", icon width is changed dynamically based on the icon's width to height ratio. This makes it easy to change icon size by changing font-size in the stylesheet, just like icon fonts.
There are several ways to change icon dimensions:
- Setting font-size in style (or fontSize if you are using inline style).
- Setting width and/or height property.
Values for width and height can be numbers or strings.
If you set only one dimension, another dimension will be calculated using the icon's width to height ratio. For example, if the icon size is 16 x 24, you set the height to 48, the width will be set to 32. Calculations work not only with numbers, but also with string values.
<Icon icon="mdi:home" style="font-size: 24px;" />
For various ways to change icon dimensions and alignment, see how to change icon dimensions in Iconify for Svelte.
An icon can be rotated and flipped horizontally and/or vertically. All transformations are done relative to the center of the icon.
These are not CSS transformations, transformations are applied inside SVG.
For more details see how to transform icon in Iconify for Svelte.
onLoad property is an optional callback function. It is called when icon data has been loaded.
It is not an event, such as click event for links, it is a simple callback function.
When onLoad is called:
- If value of icon property is an object, onLoad is not called.
- If value of icon property is a string and icon data is available, onLoad is called on first render.
- If value of icon property is a string and icon data is not available, onLoad is called on first re-render after icon data is retrieved from API.
What is the purpose of onLoad? To let you know when Icon component renders an icon and when it does not render anything. This allows you to do things like adding class name for parent element, such as "container--with-icon" that modify layout if icon is being displayed.
Component exports various functions, which developers can use to control icons.
Functions are split in several groups (click function name to see more details and examples):
There are several functions in this section:
Functions for adding icons to component:
Note: icons added to component with these functions are not stored in cache. Component caches only icons retrieved from API.
- . Loads icons from API, calls optional callback when either all or part of icons have been loaded.
- . Enables caching in localStorage and sessionStorage.
- . Disables caching in localStorage and sessionStorage.
- . Adds custom API provider. See API providers documentation.
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 _api object:
- 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 that use custom module for retrieving data from API.