question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamic icon components

See original GitHub issue

Is it possible to dynamically use icons?

I’m using the vitesse template and would previously use something like <Icon :icon="myIconVariable" /> but now it seems like that no longer exists so I was wondering if it is still possible to do something like that.

I tried something like this without much luck

<template>
  <component :is="myIconVariable.replace(':', '-')" />
</template>

<script setup>
const myIconVariable = 'mdi:vuejs'
</script>

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
husaytcommented, Jan 19, 2022

Along with autoImport one can achieve even more elegant solution. Use the same IconResolver you used in unplugin components:

  AutoImport({
      imports: ["vue"],
      resolvers: [
        IconsResolver({
          componentPrefix: "icon",
          enabledCollections: ["carbon", "mdi"],
        }),
      ],
      dts: "src/auto-imports.d.ts",
    }),

Then you don’t need to even import and the above will look like this:

<script setup>
const items = [
{
  name: 'Alarm',
  icon: IconMdiAlarm,
},
{
  name: 'Account',
  icon: IconMdiAccount,
}
]
</script>

<template>
  <div v-for="i of items">
    <component :is="i.icon"/>
  </div>
</template>
6reactions
antfucommented, Jan 5, 2022

Due to the mechanism of how unplugin-vue-components works, there is no way to infer a component from a dynamic string. (unplugin-icons only resolved icons at build time)

The workarounds for this is:

<IconA v-if="cond1"/> 
<IconB v-else-if="cond2"/>
<IconC v-else-if="cond3"/>
<IconD v-else />

Or

<script setup>
import MdiAlarm from '~icons/mdi/alarm'
import MdiAccount from '~icons/mdi/account'

const items = [
{
  name: 'Alarm',
  icon: MdiAlarm,
},
{
  name: 'Account',
  icon: MdiAccount,
}
]
</script>

<template>
  <div v-for="i of items">
    <component :is="i.icon"/>
  </div>
</template>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic Icons - Lightning Design System
Dynamic Icons. Share. Lightning Component. HTML/CSS: ...
Read more >
React Dynamic Icon Component - CodeSandbox
CodeSandbox is an online editor tailored for web applications.
Read more >
lightning:dynamicIcon | Lightning Components Developer Guide
Represents various animated icons with different states. This component requires API version 41.0 and later. A lightning:dynamicIcon component visually displays ...
Read more >
Dynamic import of react-icons - Stack Overflow
You have to replace the strings values of your icons in the iconsList array with the Icon component itself. Just change :
Read more >
Dynamic SVG Icons Component For React
Dynamic -svg-icons-component. Using this component, you can easily use any number of svg files in your project just with one component.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found