Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to list all available custom icons #222

Open
Rednas83 opened this issue Jul 29, 2024 · 7 comments
Open

How to list all available custom icons #222

Rednas83 opened this issue Jul 29, 2024 · 7 comments

Comments

@Rednas83
Copy link

Usually I fetched the different icons with something like.

var icons = import.meta.glob(`.././assets/*.svg`, { as: "raw", eager: true })

for (const item in icons) {
  body.innerHTML = icons[item]
  let name = item.formatx(`../assets/$V.svg`)
  let color = body.querySelector("#color")?.getAttributeNS(null, "fill") || "black"
  let size = undefined // TODO

  result.push(await icon(`${name} c-${color}`, body.querySelector("svg") || undefined))
}

But this returns
image

Is there a better way?

@antfu
Copy link
Member

antfu commented Oct 28, 2024

We might need to add a DevTools tab for it. Or do you mean to get them from at the runtime?

@antfu
Copy link
Member

antfu commented Oct 28, 2024

@Rednas83
Copy link
Author

I meant to list the custom svg files from the customCollections

export default defineNuxtConfig({
  modules: [
    '@nuxt/icon'
  ],
  ssr: false,
  icon: {
    provider: 'server', // <-- this
    customCollections: [
      {
        prefix: 'my-icon',
        dir: './assets/my-icons'
      },
    ],
  },
})

Is there a convenient way to do that?

@oleg-savenok
Copy link

Any update on this?

@Rednas83
Copy link
Author

Rednas83 commented Feb 23, 2025

Just tried it and it actually also works for custom collections.
Unfortunate thing is that it only works on server level🤔

server

import { listIcons } from "@iconify/vue"
var icons = listIcons() as `${string}:${string}`[] // Returns [uil:github, icons:unicorn, etc...] on server level and returns [] on client level

nuxt.config.ts

 icon: {
    clientBundle: {
      icons: ["uil:github"],
      scan: true,
      includeCustomCollections: true,
      sizeLimitKb: 256,
    },
    serverBundle: {
      collections: ["uil"],
    },
    customCollections: [
      { prefix: "icons", dir: "./assets/icons" },
    ],
  },

How to get listIcons to work on client level?

Perhaps add support for a native solution and put it in the docs?

@cyberalien
Copy link

That function lists icons that are loaded. If it is ran after rendering all pages, which should load icon data, it might work for your use case.

Docs: https://iconify.design/docs/icon-components/vue/list-icons.html

@Rednas83
Copy link
Author

Rednas83 commented Feb 23, 2025

You can easily reproduce by
pages/icons.vue

<template>
  <pre>{{ listIcons() }}</pre>
</template>
<script setup lang="ts">
import { listIcons } from "@iconify/vue"
</script>

console
Image

I also created an iconify issue for tracking this issue
iconify/iconify#365

And a reproduction
https://stackblitz.com/edit/nuxt-icon-playground-k5wvp3bq?file=app.vue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants