You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<style jsx> doesn't allow us to create dinamic styles for a component.
The className toggling or inline style options available are not useful if you don't know previously all the elements you want to style.
I mean, in my case, I want to style some social network icons. The data of the icons is in an object that we import.
So, the logic way (to me) of styling this was, well, generate the classes for each icon (each of whose have unique styles) and then, add the classes inside <style jsx>.
Just because we can't do it inside <style jsx>:
Only constants defined outside of the component scope are allowed here.
So it's the only way I could think that seems reasonable right now.
And yes: we could add every class manually, but that isn't a very good solution. We have four icons now, but we don't know if tomorrow we will be adding more or deleting others. Having to manually add or remove classes manually for every change we made isn't ideal.
So... It's there any reason why doesn't this work at all? Example of what I'm trying:
/* * socialIcons is a imported constant object, with a signature as * { socialName1: {color: ..., url: ..., ...}} */letsocialIconsClasses='';// generate the classes for the social iconsObject.keys(socialIcons).forEach((socialName)=>{socialIconsClasses=`${socialIconsClasses} .SocialIconList-icon--${socialName}:hover { fill: ${socialIcons[socialName].color}; } `;});// generate the markup for the social iconsconstgenerateSocialIcons=Object.keys(socialIcons).map(socialName=>(<a...><svgclassName={`SocialIconList-icon SocialIconList-icon--${socialName}`}><usexlinkHref={`#icon-${socialName}`}/></svg><stylejsx>{` /* some other classes*/${socialIconsClasses} /* THIS DOESN'T WORK (and doesn't throw any error either) */ `}</style></a>));constSocialIconList=()=>(<divclassName="SocialIconList">
...
<divclassName="SocialIconList-icons">{generateSocialIcons} // here will insert the social icons markup in our component
<stylejsx>{` some other styles `}</style></div></div>);
I also try importing the classes (as a string) from other module. And some combinations. Nothing works :(. I'm out of ideas.
Thanks for your time 👍.
The text was updated successfully, but these errors were encountered:
Currently this is not possible because CSS is static.
That's why we throw the "Only constants defined outside of the component scope are allowed here" error.
We are planning on adding support for dynamic values in the future but we want to make sure that whatever solution we come up with is fast – I will probably tackle this after #148 ships. You can subscribe to #81 if you want to stay updated.
For now I would use inline styles onMouseEnter it requires a bit of extra work but it is not complicated.
FWIW in your generateSocialIcons you are looping and outputting ${socialIconsClasses} which leads to duplication since that var contains all of the styles :)
<style jsx>
doesn't allow us to create dinamic styles for a component.The
className
toggling or inline style options available are not useful if you don't know previously all the elements you want to style.I mean, in my case, I want to style some social network icons. The data of the icons is in an object that we import.
So, the logic way (to me) of styling this was, well, generate the classes for each icon (each of whose have unique styles) and then, add the classes inside
<style jsx>
.Just because we can't do it inside
<style jsx>
:So it's the only way I could think that seems reasonable right now.
And yes: we could add every class manually, but that isn't a very good solution. We have four icons now, but we don't know if tomorrow we will be adding more or deleting others. Having to manually add or remove classes manually for every change we made isn't ideal.
So... It's there any reason why doesn't this work at all? Example of what I'm trying:
I also try importing the classes (as a string) from other module. And some combinations. Nothing works :(. I'm out of ideas.
Thanks for your time 👍.
The text was updated successfully, but these errors were encountered: