unplugin-icons
Universal on-demand icons solution.
Works for
For Any I mean (almost) any 😅
vite-plugin-icons
has been renamed to unplugin-icons
, see the migration guide.
Usage
Import icons names with the prefix ~icons/
and use them directly as components. Auto importing is also possible.
React
import IconAccessibility from '~icons/carbon/accessibility'
import IconAccountBox from '~icons/mdi/account-box'
function App() {
return (
<div>
<IconAccessibility />
<IconAccountBox style={{ fontSize: '2em', color: 'red' }}/>
</div>
)
}
Vue
<script setup>
import IconAccessibility from '~icons/carbon/accessibility'
import IconAccountBox from '~icons/mdi/account-box'
</script>
<template>
<icon-accessibility/>
<icon-account-box style="font-size: 2em; color: red"/>
</template>
Install
Install the plugin and peer dependency @iconify/json
npm i -D unplugin-icons @iconify/json
Build Tools
Vite
import Icons from 'unplugin-icons/vite'
export default defineConfig({
plugins: [
Icons({ }),
],
})
Rollup
import Icons from 'unplugin-icons/rollup'
export default {
plugins: [
Icons({ }),
],
}
Webpack
module.exports = {
plugins: [
require('unplugin-icons/webpack')({ })
]
}
Nuxt
export default {
buildModules: [
['unplugin-icons/nuxt', { }],
],
}
This module works for both Nuxt 2 and Nuxt Vite
Vue CLI
module.exports = {
configureWebpack: {
plugins: [
require('unplugin-icons/webpack')({ }),
],
},
}
Frameworks
Vue 3
Vue 3 support requires peer dependency @vue/compiler-sfc
:
npm i -D @vue/compiler-sfc
Icons({ compiler: 'vue3' })
Type Declarations
// tsconfig.json
{
"compilerOptions": {
"types": [
"unplugin-icons/types/vue",
]
}
}
Vue 2
Vue 2 support requires peer dependency vue-template-compiler
:
npm i -D vue-template-compiler
Icons({ compiler: 'vue2' })
Type Declarations
// tsconfig.json
{
"compilerOptions": {
"types": [
"unplugin-icons/types/vue",
]
}
}
React
JSX support requires peer dependency @svgr/core
:
npm i -D @svgr/core
Icons({ compiler: 'jsx', jsx: 'react' })
Type Declarations
// tsconfig.json
{
"compilerOptions": {
"types": [
"unplugin-icons/types/react",
]
}
}
Preact
JSX support requires peer dependency @svgr/core
:
npm i -D @svgr/core
Icons({ compiler: 'jsx', jsx: 'preact' })
Type Declarations
// tsconfig.json
{
"compilerOptions": {
"types": [
"unplugin-icons/types/preact",
]
}
}
Migrate from vite-plugin-icons
package.json
{
"devDependencies": {
- "vite-plugin-icons": "*",
+ "unplugin-icons": "^0.6.0",
}
}
vite.config.json
import Components from 'vite-plugin-components'
- import Icons, { ViteIconsResolver } from 'vite-plugin-icons'
+ import Icons from 'unplugin-icons/vite'
+ import IconsResolver from 'unplugin-icons/resolver'
export default {
plugins: [
Vue(),
Components({
customComponentResolvers: IconsResolver(),
}),
Icons(),
],
}
*
- imports usage
- import IconComponent from 'virtual:vite-icons/collection/name'
+ import IconComponent from '~icons/collection/name'
Options
You can set default styling for all icons.
The following config shows the default values of each option:
Icons({
scale: 1.2,
defaultStyle: '',
defaultClass: '',
compiler: null,
jsx: 'react'
})
Auto Importing
Use with vite-plugin-components
(>= v0.5.5
)
import Vue from '@vitejs/plugin-vue'
import Components from 'vite-plugin-components'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
export default {
plugins: [
Vue(),
Components({
customComponentResolvers: IconsResolver(),
}),
Icons(),
],
}
Then you can use any icons as you want without explicit importing (only the used icons will be bundled)
<template>
<i-carbon-accessibility/>
<i-mdi-account-box style="font-size: 2em; color: red"/>
</template>
Name Conversion
When using component resolver, you have to follow the name conversion for icons to be properly inferred.
{prefix}-{collection}-{icon}
The collection
field follows Iconify's collection IDs.
By default, the prefix is set to i
while you can customize via config
export default {
plugins: [
Vue(),
Components({
customComponentResolvers: IconsResolver({
componentPrefix: 'icon'
}),
}),
Icons(),
],
}
<template>
<icon-mdi-account />
</template>
Non-prefix mode is also supported
IconsResolver({
componentPrefix: '',
enabledCollections: ['mdi']
})
<template>
<mdi-account />
</template>
This project is part of my Sponsor Program
License
MIT License © 2020-PRESENT Anthony Fu