版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 1 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. # unplugin-icons
  2. [![NPM version](https://img.shields.io/npm/v/unplugin-icons?color=a1b858&label=)](https://www.npmjs.com/package/unplugin-icons)
  3. Access thousands of icons as components **on-demand** universally.
  4. ### Features
  5. - 🌏 Universal
  6. - 🤹 **Any** icon sets - 100+ popular sets with over 10,000 icons, logos, emojis, etc. Powered by [Iconify](https://github.com/iconify/iconify).
  7. - 📦 **Major** build tools - Vite, Webpack, Rollup, Nuxt, etc. Powered by [unplugin](https://github.com/unjs/unplugin).
  8. - 🪜 **Major** frameworks - Vanilla, Web Components, React, Vue 3, Vue 2, Solid, Svelte, and more. [Contribute](./src/core/compiles).
  9. - 🍱 **Any** combinations of them!
  10. - ☁️ On-demand - Only bundle the icons you really use, while having all the options.
  11. - 🖨 SSR / SSG friendly - Ship the icons with your page, no more FOUC.
  12. - 🌈 Stylable - Change size, color, or even add animations as you would with styles and classes.
  13. - 📥 [Custom icons](#custom-icons) - load your custom icons to get universal integrations at ease.
  14. - 📲 [Auto Importing](#auto-importing) - Use icons as components directly in your template.
  15. - 🦾 TypeScript support.
  16. - 🔍 [Browse Icons](https://icones.js.org/)
  17. <table><td><br>
  18. &nbsp;&nbsp;&nbsp;💡 **Story behind this tool**: [Journey with Icons Continues](https://antfu.me/posts/journey-with-icons-continues) - a blog post by Anthony&nbsp;&nbsp;&nbsp;
  19. </td></table>
  20. > **`vite-plugin-icons` has been renamed to `unplugin-icons`**, see the [migration guide](#migrate-from-vite-plugin-icons)
  21. ## Usage
  22. Import icons names with the convention `~icons/{collection}/{icon}` and use them directly as components. [Auto importing is also possible](#auto-importing).
  23. ###### React
  24. ```jsx
  25. import IconAccessibility from '~icons/carbon/accessibility'
  26. import IconAccountBox from '~icons/mdi/account-box'
  27. function App() {
  28. return (
  29. <div>
  30. <IconAccessibility />
  31. <IconAccountBox style={{ fontSize: '2em', color: 'red' }}/>
  32. </div>
  33. )
  34. }
  35. ```
  36. ###### Vue
  37. ```html
  38. <script setup>
  39. import IconAccessibility from '~icons/carbon/accessibility'
  40. import IconAccountBox from '~icons/mdi/account-box'
  41. </script>
  42. <template>
  43. <icon-accessibility/>
  44. <icon-account-box style="font-size: 2em; color: red"/>
  45. </template>
  46. ```
  47. ## Install
  48. ### Plugin
  49. ```bash
  50. npm i -D unplugin-icons
  51. ```
  52. ### Icons Data
  53. We use [Iconify](https://iconify.design/) as the icons data source (supports 100+ iconsets).
  54. You have two ways to install them:
  55. ###### Install Full Collection
  56. ```bash
  57. npm i -D @iconify/json
  58. ```
  59. `@iconify/json` (~120MB) includes all the iconsets from Iconify so you can install once and use any of them as you want (only the icons you actually use will be bundle into the production build).
  60. ###### Install by Icon Set
  61. If you only want to use a few of the icon sets and don't want to download the entire collection, you can also install them individually with `@iconify-json/[collection-id]`.
  62. For example, to install [Material Design Icons](https://icon-sets.iconify.design/mdi/), you can do:
  63. ```bash
  64. npm i -D @iconify-json/mdi
  65. ```
  66. To boost your workflow, it's also possible to let `unplugin-icons` handle that installation by enabling the `autoInstall` option.
  67. ```ts
  68. Icons({
  69. // experimental
  70. autoInstall: true,
  71. })
  72. ```
  73. It will install the icon set when you import them. The right package manager will be auto-detected (`npm`, `yarn` or `pnpm`).
  74. ## Configuration
  75. ###### Build Tools
  76. <details>
  77. <summary>Vite</summary><br>
  78. ```ts
  79. // vite.config.ts
  80. import Icons from 'unplugin-icons/vite'
  81. export default defineConfig({
  82. plugins: [
  83. Icons({ /* options */ }),
  84. ],
  85. })
  86. ```
  87. <br></details>
  88. <details>
  89. <summary>Rollup</summary><br>
  90. ```ts
  91. // rollup.config.js
  92. import Icons from 'unplugin-icons/rollup'
  93. export default {
  94. plugins: [
  95. Icons({ /* options */ }),
  96. ],
  97. }
  98. ```
  99. <br></details>
  100. <details>
  101. <summary>Webpack</summary><br>
  102. ```ts
  103. // webpack.config.js
  104. module.exports = {
  105. /* ... */
  106. plugins: [
  107. require('unplugin-icons/webpack')({ /* options */ }),
  108. ],
  109. }
  110. ```
  111. <br></details>
  112. <details>
  113. <summary>Nuxt</summary><br>
  114. Nuxt 2 and [Nuxt Bridge](https://github.com/nuxt/bridge)
  115. ```ts
  116. // nuxt.config.js
  117. export default {
  118. buildModules: [
  119. ['unplugin-icons/nuxt', { /* options */ }],
  120. ],
  121. }
  122. ```
  123. Nuxt 3
  124. ```ts
  125. // nuxt.config.js
  126. export default defineNuxtConfig({
  127. modules: [
  128. ['unplugin-icons/nuxt', { /* options */ }]
  129. ],
  130. })
  131. ```
  132. <br></details>
  133. <details>
  134. <summary>Vue CLI</summary><br>
  135. ```ts
  136. // vue.config.js
  137. module.exports = {
  138. configureWebpack: {
  139. plugins: [
  140. require('unplugin-icons/webpack')({ /* options */ }),
  141. ],
  142. },
  143. }
  144. ```
  145. <br></details>
  146. <details>
  147. <summary>SvelteKit</summary><br>
  148. The `unplugin-icons` plugin should be configured in the `vite.config.js` configuration file:
  149. ```ts
  150. // vite.config.js
  151. import { defineConfig } from 'vite'
  152. import { sveltekit } from '@sveltejs/kit/vite'
  153. import Icons from 'unplugin-icons/vite'
  154. export default defineConfig({
  155. plugins: [
  156. sveltekit(),
  157. Icons({
  158. compiler: 'svelte',
  159. })
  160. ]
  161. })
  162. ```
  163. Check instructions in the `Frameworks -> Svelte` section below if you faced module import errors.
  164. <br></details>
  165. <details>
  166. <summary>Svelte + Vite</summary><br>
  167. Svelte support requires the `@sveltejs/vite-plugin-svelte` plugin:
  168. ```shell
  169. npm i -D @sveltejs/vite-plugin-svelte
  170. ```
  171. The `unplugin-icons` plugin should be configured in the `vite.config.js` configuration file:
  172. ```ts
  173. // vite.config.js
  174. import { defineConfig } from 'vite'
  175. import { svelte } from '@sveltejs/vite-plugin-svelte'
  176. import Icons from 'unplugin-icons/vite'
  177. export default defineConfig({
  178. plugins: [
  179. svelte(),
  180. Icons({
  181. compiler: 'svelte',
  182. }),
  183. ],
  184. })
  185. ```
  186. Check instructions in the `Frameworks -> Svelte` section below if you faced module import errors.
  187. <br></details>
  188. <details>
  189. <summary>Next.js</summary><br>
  190. The `unplugin-icons` plugin should be configured on `next.config.js` configuration file:
  191. ```js
  192. /** @type {import('next').NextConfig} */
  193. module.exports = {
  194. reactStrictMode: true,
  195. webpack(config) {
  196. config.plugins.push(
  197. require('unplugin-icons/webpack')({
  198. compiler: 'jsx',
  199. jsx: 'react',
  200. }),
  201. )
  202. return config
  203. },
  204. }
  205. ```
  206. Check instructions in the `Frameworks -> React` section below if you faced module import errors.
  207. ⚠️ **Warning:** to import an icon is necessary to explicitly add the `.jsx` extension to the import path, so that Next.js knows how to load it, by example:
  208. <!-- eslint-skip -->
  209. ```ts
  210. import IconArrowRight from '~icons/dashicons/arrow-right.jsx';
  211. // ^-- write `.jsx` to avoid
  212. // https://github.com/antfu/unplugin-icons/issues/103
  213. // ...some code later
  214. <IconArrowRight />
  215. ```
  216. See inside of `examples/next` for a working example project.
  217. <br></details>
  218. <details>
  219. <summary>esbuild</summary><br>
  220. ```ts
  221. // esbuild.config.js
  222. import { build } from 'esbuild'
  223. build({
  224. /* ... */
  225. plugins: [
  226. require('unplugin-icons/esbuild')({
  227. /* options */
  228. }),
  229. ],
  230. })
  231. ```
  232. <br></details>
  233. <details>
  234. <summary>Astro</summary><br>
  235. ```ts
  236. // astro.config.mjs
  237. import { defineConfig } from 'astro/config'
  238. import Icons from 'unplugin-icons/vite'
  239. // https://astro.build/config
  240. export default defineConfig({
  241. vite: {
  242. plugins: [
  243. Icons({
  244. compiler: 'astro',
  245. }),
  246. ],
  247. },
  248. })
  249. ```
  250. <br></details>
  251. ###### Frameworks
  252. <details>
  253. <summary>Vue 3 / Vue 2.7+</summary><br>
  254. Vue 3 / Vue 2.7+ support requires peer dependency `@vue/compiler-sfc`:
  255. ```bash
  256. npm i -D @vue/compiler-sfc
  257. ```
  258. ```ts
  259. Icons({ compiler: 'vue3' })
  260. ```
  261. Type Declarations
  262. <!-- eslint-skip -->
  263. ```jsonc
  264. // tsconfig.json
  265. {
  266. "compilerOptions": {
  267. "types": [
  268. "unplugin-icons/types/vue",
  269. ]
  270. }
  271. }
  272. ```
  273. <br></details>
  274. <details>
  275. <summary>Vue 2 (only for versions < 2.7)</summary><br>
  276. Vue 2 support requires peer dependency `vue-template-compiler`:
  277. ```bash
  278. npm i -D vue-template-compiler
  279. ```
  280. ```ts
  281. Icons({ compiler: 'vue2' })
  282. ```
  283. Type Declarations
  284. <!-- eslint-skip -->
  285. ```jsonc
  286. // tsconfig.json
  287. {
  288. "compilerOptions": {
  289. "types": [
  290. "unplugin-icons/types/vue",
  291. ]
  292. }
  293. }
  294. ```
  295. <br></details>
  296. <details>
  297. <summary>React</summary><br>
  298. JSX support requires peer dependency `@svgr/core`:
  299. ```bash
  300. npm i -D @svgr/core
  301. ```
  302. ```ts
  303. Icons({ compiler: 'jsx', jsx: 'react' })
  304. ```
  305. Type Declarations
  306. <!-- eslint-skip -->
  307. ```jsonc
  308. // tsconfig.json
  309. {
  310. "compilerOptions": {
  311. "types": [
  312. "unplugin-icons/types/react",
  313. ]
  314. }
  315. }
  316. ```
  317. <br></details>
  318. <details>
  319. <summary>Preact</summary><br>
  320. JSX support requires peer dependency `@svgr/core`:
  321. ```bash
  322. npm i -D @svgr/core
  323. ```
  324. ```ts
  325. Icons({ compiler: 'jsx', jsx: 'preact' })
  326. ```
  327. Type Declarations
  328. <!-- eslint-skip -->
  329. ```jsonc
  330. // tsconfig.json
  331. {
  332. "compilerOptions": {
  333. "types": [
  334. "unplugin-icons/types/preact",
  335. ]
  336. }
  337. }
  338. ```
  339. <br></details>
  340. <details>
  341. <summary>Solid</summary><br>
  342. ```ts
  343. Icons({ compiler: 'solid' })
  344. ```
  345. Type Declarations
  346. <!-- eslint-skip -->
  347. ```jsonc
  348. // tsconfig.json
  349. {
  350. "compilerOptions": {
  351. "types": [
  352. "unplugin-icons/types/solid",
  353. ]
  354. }
  355. }
  356. ```
  357. <br></details>
  358. <details>
  359. <summary>Svelte</summary><br>
  360. ```ts
  361. Icons({ compiler: 'svelte' })
  362. ```
  363. Type Declarations
  364. For SvelteKit, in the `src/app.d.ts` file:
  365. ```js
  366. /// <reference types="@sveltejs/kit" />
  367. /// <reference types="unplugin-icons/types/svelte" />
  368. ```
  369. For Svelte + Vite, in the `src/vite-env.d.ts` file:
  370. ```js
  371. /// <reference types="svelte" />
  372. /// <reference types="vite/client" />
  373. /// <reference types="unplugin-icons/types/svelte" />
  374. ```
  375. <br></details>
  376. <details>
  377. <summary>Astro</summary><br>
  378. Type Declarations
  379. <!-- eslint-skip -->
  380. ```jsonc
  381. // tsconfig.json
  382. {
  383. "compilerOptions": {
  384. "types": [
  385. "unplugin-icons/types/astro",
  386. ]
  387. }
  388. }
  389. ```
  390. <br></details>
  391. <details>
  392. <summary>Qwik</summary><br>
  393. ```ts
  394. Icons({ compiler: 'qwik' })
  395. ```
  396. Type Declarations
  397. <!-- eslint-skip -->
  398. ```jsonc
  399. // tsconfig.json
  400. {
  401. "compilerOptions": {
  402. "types": [
  403. "unplugin-icons/types/qwik",
  404. ]
  405. }
  406. }
  407. ```
  408. <br></details>
  409. ## Use RAW compiler from query params
  410. From `v0.13.2` you can also use `raw` compiler to access the `svg` icon and use it on your html templates, just add `raw` to the icon query param.
  411. For example, using `vue3`:
  412. ```vue
  413. <script setup lang='ts'>
  414. import RawMdiAlarmOff from '~icons/mdi/alarm-off?raw&width=4em&height=4em'
  415. import RawMdiAlarmOff2 from '~icons/mdi/alarm-off?raw&width=1em&height=1em'
  416. </script>
  417. <template>
  418. <!-- raw example -->
  419. <pre>
  420. import RawMdiAlarmOff from '~icons/mdi/alarm-off?raw&width=4em&height=4em'
  421. {{ RawMdiAlarmOff }}
  422. import RawMdiAlarmOff2 from '~icons/mdi/alarm-off?raw&width=1em&height=1em'
  423. {{ RawMdiAlarmOff2 }}
  424. </pre>
  425. <!-- svg example -->
  426. <span v-html="RawMdiAlarmOff" />
  427. <span v-html="RawMdiAlarmOff2" />
  428. </template>
  429. ```
  430. ## Custom Icons
  431. From `v0.11`, you can now load your own icons!
  432. From `v0.13` you can also provide a transform callback to `FileSystemIconLoader`.
  433. ```ts
  434. import { promises as fs } from 'fs'
  435. // loader helpers
  436. import { FileSystemIconLoader } from 'unplugin-icons/loaders'
  437. Icons({
  438. customCollections: {
  439. // key as the collection name
  440. 'my-icons': {
  441. account: '<svg><!-- ... --></svg>',
  442. // load your custom icon lazily
  443. settings: () => fs.readFile('./path/to/my-icon.svg', 'utf-8'),
  444. /* ... */
  445. },
  446. 'my-other-icons': async (iconName) => {
  447. // your custom loader here. Do whatever you want.
  448. // for example, fetch from a remote server:
  449. return await fetch(`https://example.com/icons/${iconName}.svg`).then(res => res.text())
  450. },
  451. // a helper to load icons from the file system
  452. // files under `./assets/icons` with `.svg` extension will be loaded as it's file name
  453. // you can also provide a transform callback to change each icon (optional)
  454. 'my-yet-other-icons': FileSystemIconLoader(
  455. './assets/icons',
  456. svg => svg.replace(/^<svg /, '<svg fill="currentColor" '),
  457. ),
  458. },
  459. })
  460. ```
  461. Then use as
  462. ```ts
  463. import IconAccount from '~icons/my-icons/account'
  464. import IconFoo from '~icons/my-other-icons/foo'
  465. import IconBar from '~icons/my-yet-other-icons/bar'
  466. ```
  467. > 💡 SVG Authoring Tips:
  468. > - To make your icons color adaptable, set `fill="currentColor"` or `stroke="currentColor"` in your SVG.
  469. > - Leave the `height` and `width` unspecified, we will set them for you.
  470. ### Use with Resolver
  471. When using with resolvers for auto-importing, you will need to tell it your custom collection names:
  472. ```ts
  473. IconResolver({
  474. customCollections: [
  475. 'my-icons',
  476. 'my-other-icons',
  477. 'my-yet-other-icons',
  478. ],
  479. })
  480. ```
  481. See the [Vue 3 + Vite example](./examples/vite-vue3/vite.config.ts).
  482. ## Icon customizer
  483. From `v0.13` you can also customize each icon using `iconCustomizer` configuration option or using query params when importing them.
  484. The `query` param will take precedence over `iconCustomizer` and `iconCustomizer` over `configuration`.
  485. The `iconCustomizer` and `query` params will be applied to any collection, that is, for each icon from `custom` loader, `inlined` on `customCollections` or from `@iconify`.
  486. For example, you can configure `iconCustomizer` to change all icons for a collection or individual icons on a collection:
  487. ```ts
  488. import { promises as fs } from 'fs'
  489. // loader helpers
  490. import { FileSystemIconLoader } from 'unplugin-icons/loaders'
  491. Icons({
  492. customCollections: {
  493. // key as the collection name
  494. 'my-icons': {
  495. account: '<svg><!-- ... --></svg>',
  496. // load your custom icon lazily
  497. settings: () => fs.readFile('./path/to/my-icon.svg', 'utf-8'),
  498. /* ... */
  499. },
  500. 'my-other-icons': async (iconName) => {
  501. // your custom loader here. Do whatever you want.
  502. // for example, fetch from a remote server:
  503. return await fetch(`https://example.com/icons/${iconName}.svg`).then(res => res.text())
  504. },
  505. // a helper to load icons from the file system
  506. // files under `./assets/icons` with `.svg` extension will be loaded as it's file name
  507. // you can also provide a transform callback to change each icon (optional)
  508. 'my-yet-other-icons': FileSystemIconLoader(
  509. './assets/icons',
  510. svg => svg.replace(/^<svg /, '<svg fill="currentColor" '),
  511. ),
  512. },
  513. iconCustomizer(collection, icon, props) {
  514. // customize all icons in this collection
  515. if (collection === 'my-other-icons') {
  516. props.width = '4em'
  517. props.height = '4em'
  518. }
  519. // customize this icon in this collection
  520. if (collection === 'my-icons' && icon === 'account') {
  521. props.width = '6em'
  522. props.height = '6em'
  523. }
  524. // customize this @iconify icon in this collection
  525. if (collection === 'mdi' && icon === 'account') {
  526. props.width = '2em'
  527. props.height = '2em'
  528. }
  529. },
  530. })
  531. ```
  532. or you can use `query` params to apply to individual icons:
  533. <!-- eslint-skip -->
  534. ```vue
  535. <script setup lang='ts'>
  536. import MdiAlarmOff from 'virtual:icons/mdi/alarm-off?width=4em&height=4em'
  537. import MdiAlarmOff2 from 'virtual:icons/mdi/alarm-off?width=1em&height=1em'
  538. </script>
  539. <template>
  540. <!-- width=4em and height=4em -->
  541. <mdi-alarm-off />
  542. <!-- width=4em and height=4em -->
  543. <MdiAlarmOff />
  544. <!-- width=1em and height=1em -->
  545. <MdiAlarmOff2 />
  546. </template>
  547. ```
  548. See `src/App.vue` component and `vite.config.ts` configuration on `vite-vue3` example project.
  549. ## Global Custom Icon Transformation
  550. From version `0.14.2`, when loading your custom icons, you can transform them, for example adding `fill` attribute with `currentColor`:
  551. ```ts
  552. Icons({
  553. customCollections: {
  554. // key as the collection name
  555. 'my-icons': {
  556. account: '<svg><!-- ... --></svg>',
  557. /* ... */
  558. },
  559. },
  560. transform(svg, collection, icon) {
  561. // apply fill to this icon on this collection
  562. if (collection === 'my-icons' && icon === 'account')
  563. return svg.replace(/^<svg /, '<svg fill="currentColor" ')
  564. return svg
  565. },
  566. })
  567. ```
  568. ## Advanced Custom Icon Set Cleanup
  569. When using this plugin with your custom icons, consider using a cleanup process similar to that done by [Iconify](https://iconify.design/) for any icons sets. All the tools you need are available in [Iconify Tools](https://docs.iconify.design/tools/tools2/).
  570. You can check this repo, using `unplugin-icons` on a `SvelteKit` project: https://github.com/iconify/tools/tree/main/%40iconify-demo/unplugin-svelte.
  571. Read [Cleaning up icons](https://docs.iconify.design/articles/cleaning-up-icons/) article from [Iconify](https://iconify.design/) for more details.
  572. ## Migrate from `vite-plugin-icons`
  573. `package.json`
  574. ```diff
  575. {
  576. "devDependencies": {
  577. - "vite-plugin-icons": "*",
  578. + "unplugin-icons": "^0.7.0",
  579. }
  580. }
  581. ```
  582. `vite.config.json`
  583. ```diff
  584. import Components from 'unplugin-vue-components/vite'
  585. - import Icons, { ViteIconsResolver } from 'vite-plugin-icons'
  586. + import Icons from 'unplugin-icons/vite'
  587. + import IconsResolver from 'unplugin-icons/resolver'
  588. export default {
  589. plugins: [
  590. Vue(),
  591. Components({
  592. resolvers: [
  593. IconsResolver()
  594. ],
  595. }),
  596. Icons(),
  597. ],
  598. }
  599. ```
  600. `*` - imports usage
  601. ```diff
  602. - import IconComponent from 'virtual:vite-icons/collection/name'
  603. + import IconComponent from '~icons/collection/name'
  604. ```
  605. > You can still use `virtual:icons` prefix in Vite if you prefer, but it's not yet supported in Webpack, we are unifying it as a workaround in the docs.
  606. ## Options
  607. You can set default styling for all icons.
  608. The following config shows the default values of each option:
  609. ```ts
  610. Icons({
  611. scale: 1.2, // Scale of icons against 1em
  612. defaultStyle: '', // Style apply to icons
  613. defaultClass: '', // Class names apply to icons
  614. compiler: null, // 'vue2', 'vue3', 'jsx'
  615. jsx: 'react', // 'react' or 'preact'
  616. })
  617. ```
  618. ## Auto Importing
  619. <details>
  620. <summary>Vue 2 & 3</summary><br>
  621. Use with [`unplugin-vue-components`](https://github.com/antfu/unplugin-vue-components)
  622. For example in Vite:
  623. ```js
  624. // vite.config.js
  625. import Vue from '@vitejs/plugin-vue'
  626. import Icons from 'unplugin-icons/vite'
  627. import IconsResolver from 'unplugin-icons/resolver'
  628. import Components from 'unplugin-vue-components/vite'
  629. export default {
  630. plugins: [
  631. Vue(),
  632. Components({
  633. resolvers: [
  634. IconsResolver(),
  635. ],
  636. }),
  637. Icons(),
  638. ],
  639. }
  640. ```
  641. Then you can use any icons as you want without explicit importing. Only the used icons will be bundled.
  642. ```html
  643. <template>
  644. <i-carbon-accessibility/>
  645. <i-mdi-account-box style="font-size: 2em; color: red"/>
  646. </template>
  647. ```
  648. </details>
  649. <details>
  650. <summary>React & Solid</summary><br>
  651. Use with [`unplugin-auto-import`](https://github.com/antfu/unplugin-auto-import)
  652. For example in Vite:
  653. ```js
  654. // vite.config.js
  655. import Icons from 'unplugin-icons/vite'
  656. import IconsResolver from 'unplugin-icons/resolver'
  657. import AutoImport from 'unplugin-auto-import/vite'
  658. export default {
  659. plugins: [
  660. /* ... */
  661. AutoImport({
  662. resolvers: [
  663. IconsResolver({
  664. prefix: 'Icon',
  665. extension: 'jsx',
  666. }),
  667. ],
  668. }),
  669. Icons({
  670. compiler: 'jsx', // or 'solid'
  671. }),
  672. ],
  673. }
  674. ```
  675. Then you can use any icons with the prefix `Icon` as you want without explicit importing. Type declarations will be generated on the fly.
  676. <!-- eslint-disable react/jsx-no-undef -->
  677. ```js
  678. export function Component() {
  679. return (
  680. <div>
  681. <IconCarbonApps />
  682. <IconMdiAccountBox style="font-size: 2em; color: red"/>
  683. </div>
  684. )
  685. }
  686. ```
  687. </details>
  688. ### Name Conversion
  689. When using component resolver, you have to follow the name conversion for icons to be properly inferred.
  690. ```
  691. {prefix}-{collection}-{icon}
  692. ```
  693. The `collection` field follows [Iconify's collection IDs](https://iconify.design/icon-sets/).
  694. By default, the prefix is set to `i` while you can customize via config
  695. ```ts
  696. IconsResolver({
  697. prefix: 'icon', // <--
  698. })
  699. ```
  700. ```html
  701. <template>
  702. <icon-mdi-account />
  703. </template>
  704. ```
  705. Non-prefix mode is also supported
  706. ```ts
  707. IconsResolver({
  708. prefix: false, // <--
  709. // this is optional, default enabling all the collections supported by Iconify
  710. enabledCollections: ['mdi'],
  711. })
  712. ```
  713. ```vue
  714. <template>
  715. <mdi-account />
  716. </template>
  717. ```
  718. ### Collection Aliases
  719. When using component resolver, you have to use the name of the collection that can be long or redundant: for example,
  720. when using `icon-park` collection you need to use it like this `<icon-icon-park-abnormal />`.
  721. You can add an alias for any collection to the `IconResolver` plugin:
  722. ```ts
  723. IconsResolver({
  724. alias: {
  725. park: 'icon-park',
  726. fas: 'fa-solid',
  727. // ...
  728. }
  729. })
  730. ```
  731. You can use the alias or the collection name, the plugin will resolve both.
  732. Following with the example and configuring the plugin with previous `alias` entry, you can now use
  733. `<icon-park-abnormal />` or `<icon-icon-park-abnormal />`.
  734. ## Sponsors
  735. This project is part of my <a href='https://github.com/antfu-sponsors'>Sponsor Program</a>
  736. <p align="center">
  737. <a href="https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg">
  738. <img src='https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg'/>
  739. </a>
  740. </p>
  741. ## License
  742. MIT License © 2020-PRESENT [Anthony Fu](https://github.com/antfu)