fontawesome.macro icon indicating copy to clipboard operation
fontawesome.macro copied to clipboard

MacroError: Only string literals are supported when referencing icons (use a string here instead)

Open dance-cmdr opened this issue 3 years ago • 3 comments
trafficstars

I understand that this might be a Typescript issue rather than a macro issue, but I wonder if you could be of help.

I'm trying to make a basic utility to conditionally render "chevron-up" OR "chevron-down".

Doing this...

const chevronIconName: IconName = isExpanded ? "chevron-up" : "chevron-down";
const chevronIcon = solid(chevronIconName) ;

Throws this error and of course it breaks the webpack build.

MacroError: Only string literals are supported when referencing icons (use a string here instead)

dance-cmdr avatar Oct 05 '22 17:10 dance-cmdr

Hey @dance-cmdr did you find any solution for above error?

@javascripter can you please help us on this?

shreyas098 avatar Jan 20 '23 07:01 shreyas098

It seems like the macro is less useful if you need a dynamic solution. https://stackoverflow.com/questions/69472624/font-awesome-6-beta-and-dynamic-icon-name-in-next-js

rj-wowza avatar Jan 26 '23 20:01 rj-wowza

@dance-cmdr cc @shreyas098

const chevronIconName: IconName = isExpanded ? "chevron-up" : "chevron-down";
const chevronIcon = solid(chevronIconName) ;

Since solid() is a macro instead of an actual function, you can only use a string literal inside solid() as arguments, and cannot use something dynamic like a variable inside solid() (as the name of the icon to import has to be known at compile time).

So, you need to inline the variable and use it like this.

const chevronIcon = isExpanded ? solid('chevron-up') ? solid('chevron-down') ;

javascripter avatar Jan 27 '23 16:01 javascripter