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.

Possibility to generate SVG/access body instead of component

See original GitHub issue

Hello,

and first of all: thank you for this wonderful software which leverages the usage of the already marvellous iconify even more.

I have a problem currently, where I need to use the icons in the JavaScript-code of a SvelteKit-application in order to generate some html. However I couldn’t find any possibility to easily generate the SVG out of an imported icon. Instead I get a ProxyComponent. I guess that I could switch the compiler-setting to 'none' or 'raw', but as I am using the icons in 100 different places as icon-components, I can’t just turn it off.

I scanned the documentation multiple times and also took a look at the sources and methods of the ProxyComponent, but couldn’t find a way to generate the SVG or access the body/data of the icon. Is there a method I just missed? As said, I use it in a lot of Svelte-components to load icons.

The (simplified) usage I imagine would be as follows (instead I’m currently just saving the whole SVG as string):

import IonSearchIcon from '~icons/ion/search';

let searchIcon = `<div><svg>${IonSearchIcon.body}</svg></div>`;

// Some further processing

Could you help me put me on the right path?

Again, thanks a lot for the library, it simplified my development a lot.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
userquincommented, Feb 11, 2022

@taffit I’ll add an example on svelte/sveltekit examples (also for each fw)

EDIT: using for example {@html} https://github.com/antfu/unplugin-icons/pull/166/files#diff-bef351e1c884739a05a30bf3534e87f0de0ae52e9745b29651dfde86ef6403e0R17

1reaction
userquincommented, Feb 11, 2022

@taffit I only add the raw compiler, since you can use only one compiler from configuration options, and none will be only usable from internal compilers.

Yes, the example is on vue3 example but you can also apply the same code to your svelte/sveltekit sfc, the iimport will just export the svg icon as default, so you can use it where you want.

EDIT: you can check for example the custom compiler added on vite-svelte example here: https://github.com/antfu/unplugin-icons/blob/main/examples/vite-svelte/vite.config.js#L21. You can manipulate the svg and use it as you want.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to access SVG elements with Javascript - Stack Overflow
At the moment, I've resorted to using Illustrator to create the SVG file, and I'm using Raphaël JS to create paths and simply...
Read more >
The Best Way to Embed SVG on HTML (2021) - Vecta.io
Ideally our SVG should be easy to maintain, just create and embed without the need to ensure their classes and IDs do not...
Read more >
Generating SVG With React - Smashing Magazine
React is one of today's most popular ways to create a component-based UI. It helps to organize an application into small, human-digestible ...
Read more >
Accessible SVGs | CSS-Tricks
This example is fine to use as just a basic image replacement, but it doesn't allow for us to access to contents of...
Read more >
Creating dynamic SVG elements with JavaScript - Motion Tricks
Let's get back to it with dynamic element creation. This time there is a SVG in the HTML, but it has no viewBox,...
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