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

Is there way to let javascript to pass value to less? #2932

Closed
maplewf opened this issue Jul 22, 2016 · 7 comments
Closed

Is there way to let javascript to pass value to less? #2932

maplewf opened this issue Jul 22, 2016 · 7 comments

Comments

@maplewf
Copy link

maplewf commented Jul 22, 2016

Hi,

I'm very new for LESS, I think it's good way to organize the style sheets, but I have a doubt about how to make js interact with less. Here is my story: I want to provide user the ability to switch theme in my web page, when user choose the theme with detailed settings through UI, js will get all style data for this theme, next is to use these style settings to change CSS, but if I integrate with LESS, how can i pass these settings to LESS? I roughly go through the guide in http://lesscss.org/features/, but not found the proper way to do it. did I miss something, or just LESS don't support it? anyone can give me a hint?

By the way, all actions I described above just happened on client-side, no server interaction.

thanks in advance.

best regards.

@rjgotten
Copy link
Contributor

You can pass an additional set of variables to the LESS compiler and have it recompile the stylesheets with the new values:

http://lesscss.org/usage/#using-less-in-the-browser-modify-variables

@stramel
Copy link

stramel commented Aug 3, 2016

Could push for the use of CSS Variables that could be set using Javascript. This would allow you to serve up CSS and dynamically change the theme.

#2715

@rjgotten
Copy link
Contributor

rjgotten commented Aug 4, 2016

Could push for the use of CSS Variables that could be set using Javascript.

While more performant to do it native, LESS can already take an externally populated set of variables and recompile when their values change.

Again:

http://lesscss.org/usage/#using-less-in-the-browser-modify-variables

@stramel
Copy link

stramel commented Aug 4, 2016

@rjgotten I agree, LESS by itself at this given time can handle it. However, it does require compiling at runtime for that solution, so I brought up an alternative way that did not.

@matthew-dean
Copy link
Member

@stramel I think you're focusing on the wrong place. If you've used CSS variables (or "custom properties" as they are actually defined) in your Less style sheet, you can use JavaScript to just set new values for those custom properties directly by injecting new styles into your document that sets them. You don't have to manipulate the Less-generated styles at all to do so. The browser will combine your new style values with the existing generated styles using the normal CSS cascade and re-paint the page. Nothing new should be needed on the Less side.

@stramel
Copy link

stramel commented Aug 4, 2016

@matthew-dean Correct, you could use javascript to inject a CSS stylesheet with updated CSS variables/properties or use javascript to directly set the CSS variables/properties.

@rjgotten had proposed serving LESS stylesheets and compiling them in the user's browser at runtime or recompiling them using less.modifyVars.

I was simply posting an alternative for a different perspective.

@seven-phases-max
Copy link
Member

Closing as answered.

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

5 participants