Icon
An Icon component to display various types of icons from Nuxt Mdi or Nuxt Icon module.
Component
Setup
The Icon component uses the Nuxt Icon module to render icons.
Please, make sure you have the Nuxt Icon module installed and configured in your Nuxt project.
In order to use icon collections, decide whether you want to use local setup or server-served icons.
Local setup
Install the desired icon collection:
npm install @iconify-json/collection-name
Server-served icons
Configure the server bundle to include the desired icon collections:
export default defineNuxtConfig({
icon: {
serverBundle: {
collections: ['mdi']
}
}
})
Props
| Props | Default | Type |
|---|---|---|
name | 'mdi:help' | string |
mode | IconMode.CSS | IconMode |
size | IconSize.MD | IconSize |
color | ColorAccent.NEUTRAL | ColorAccent |
svgCustomize | — | CollectionCustomizeCallback |
iconClass | — | string | string[] |
Usage
name
Sets the name of the icon to be displayed. Uses the collection-name:icon-name format.
<template>
<Icon name="mdi:help" />
</template>
The Nuxt Icon module component is renamed to 'NuxtIcon' (original name: 'Icon') by the design system to avoid conflicts. So take this into account when reviewing the docs pages and if you are using it directly without the design system 'Icon' component.
Custom icon collections
To use custom icon collections, you need to configure them in your nuxt.config.ts file. Here's an example of how to add a custom icon collection:
export default defineNuxtConfig({
modules: [
'@nuxt/icon'
],
icon: {
customCollections: [
{
prefix: 'my-icon',
dir: './assets/my-icons',
// if you want to include all the icons in nested directories:
// recursive: true,
},
],
},
})
To ensure that the custom icons use the correct colors, make sure to set the `fill` and/or `stroke` attributes in your SVG files to `currentColor`.
For further information, please refer to the Nuxt Icon module documentation.
mode
Sets the rendering mode of the icon. It uses the IconMode enum.
Only works when using IconType.COLLECTION.
<template>
<Icon
:mode="IconMode.CSS"
icon="mdi:help"
/>
</template>
Options
| Value | Description |
|---|---|
CSS | Renders the icon using CSS background-image |
SVG | Renders the icon as inline SVG |
size
Sets the size of the icon. Uses the IconSize enum.
<template>
<Icon :size="IconSize.LG" />
</template>
Options
| Value | Description |
|---|---|
XS | Extra Small |
SM | Small |
MD | Medium |
LG | Large |
XL | Extra Large |
2XL | 2 Extra Large |
color
Sets the color of the icon. Uses the ColorAccent enum.
<template>
<Icon :color="ColorAccent.SUCCESS" />
</template>
Options
| Value | Description |
|---|---|
NEUTRAL | Neutral |
SUCCESS | Success |
WARNING | Warning |
DANGER | Danger |
INFO | Info |
PRIMARY_BRAND | Primary Brand |
SECONDARY_BRAND | Secondary Brand |
svgCustomize
A callback function that allows you to customize the SVG content of the icon before it is rendered. This function receives the SVG string as an argument and should return the modified SVG string.
It only works when using IconType.COLLECTION with mode="svg".
<template>
<Icon
icon="mdi:account"
:svgCustomize="customizeSVG"
type="collection"
mode="svg"
/>
</template>
<script setup lang="ts">
// Define the customize function to modify SVG content
const customizeSVG = (content: string, name: string, prefix: string, provider: string) => {
if (prefix !== 'tabler') return content // Ignore Prefix
return content
.replace(/stroke-width="[^"]*"/g, `stroke-width="2"`) // Change stroke width to 2
.replace(/stroke="[^"]*"/g, `stroke="#FF5733"`) // Change stroke color to red
.replace(/fill="[^"]*"/g, `fill="#FF5733"`) // Change fill color to red
.replace(/animation-duration="[^"]*"/g, `animation-duration="1s"`) // Change animation duration to 1s (for animated icons)
.replace(/opacity="[^"]*"/g, `opacity="0.8"`);// Change opacity to 0.8
}
</script>
CollectionCustomizeCallback type
type CollectionCustomizeCallback = (
svg: string,
name?: string,
prefix?: string,
provider?: string,
) => string
iconClass
Adds custom CSS classes to the icon element.
<template>
<Icon icon="mdi:account" iconClass="custom-icon-class" />
</template>