15

I want to change a less variable on client side. Say I have a less file

@color1: #123456;
@color2: @color1 + #111111;

.title { color: @color1; }
.text { color: @color2; }

I want that user yo pick a color and change the value of @color1 and recompile css without reloading the page.

Basically I'm looking for a js function, something like this

less_again({color1: '#ff0000'}) 
ahmetunal
  • 3,930
  • 1
  • 23
  • 26

3 Answers3

25

Marvin, I wrote a function that does exactly what you're looking for, last night.

I have created a fork on Github; https://github.com/hbi99/less.js/commit/6508fe89a6210ae3cd8fffb8e998334644e7dcdc

Take a look at it. Since this is a recent addition, I'd like to hear your comments on the addition. This solution fits my needs perfectly and I think it will do the same for you.

Here is a basic sample;

Sample LESS:

@bgColor: black;
@textColor: yellow;
body {background: @bgColor; color: @textColor;}

From JS:

less.modifyVars({
  '@bgColor': 'blue',
  '@textColor': 'red'
});
Hakan Bilgin
  • 1,113
  • 12
  • 11
  • This looks really cool Hakan, looking forward to trying it out. Is there a reason you haven't sent a pull request yet? I would be interested to see what cloudhead says about the approach. – studgeek Feb 28 '12 at 22:42
  • Also see this post where I suggest your patch could create valid use cases for using less.js in production. http://stackoverflow.com/a/9491074/255961 – studgeek Feb 28 '12 at 22:53
  • My thought is that it could be tried out by others first, perhaps refined, and after that send a pull request. I'll send a pull request soon. Thank you for the feedback :) – Hakan Bilgin Mar 18 '12 at 12:03
  • Hakan, need your help! not sure if i am missing something over here. I tried this with bootstrap and its not working. What i did is created a button and on click of that button, called ur method expecting it to change the page's css. But instead of rewritting the css the existing css got removed. Here is the jsbin link i created [link]http://jsbin.com/avaqox – hellojava May 20 '12 at 06:00
  • Kindly help me out mate. or any pointers b4 i start tweaking the original code. – hellojava May 20 '12 at 06:03
  • Forgot to mention, i already added ur function to the less js – hellojava May 20 '12 at 06:05
  • @hellojava: sorry, I haven't seen you comment until now. I'll ake a closer look at your problem as soon as I can and get back to you. – Hakan Bilgin May 24 '12 at 15:27
  • @hellojava - your page seems not correctly set up. Look at this test-page: http://run73.com/test.htm – Hakan Bilgin May 27 '12 at 13:23
  • Many Thanks Hakan. I ll have a look at this page.:) – hellojava May 29 '12 at 19:22
  • 3
    Looks like modifyVars is included in actual less now. Works great! – NotDan Nov 27 '14 at 01:29
  • Yeah...sorry. I haven't comment that here...it was added ~18 months ago. – Hakan Bilgin Dec 12 '14 at 12:54
  • thnaks, it helped me. – atilkan Jul 22 '15 at 02:34
  • 1
    Link to `modifyVars` docs: http://lesscss.org/usage/#using-less-in-the-browser-modify-variables – Panagiotis Panagi Oct 15 '15 at 11:48
6

The creator of less.js added some features that should allow you to do something like this. Read the comments and the answers here: Load less.js rules dynamically

Community
  • 1
  • 1
Moin Zaman
  • 25,281
  • 6
  • 70
  • 74
0

This less:

@color1: #123456;
@color2: @color1 + #111111;

.title { color: @color1; }
.text { color: @color2; }

compiles to this CSS and this is all the browser knows about:

.title { color: #123456; }
.text { color: #234567; }

So, now you're effectively saying you want to change dynamically to this:

.title { color: #ff0000; }

You can do that by reaching into the existing stylesheet with JS and changing the rule for .title. Or, you can define an alternate rule in your original CSS:

.alternate.title { color: #ff0000; }

And, find all the objects with .title and add .alternate to them. In jQuery, this would be as simple as:

$(".title").addClass(".alternate");

In plain JS, you'd need to use a shim to provide getElementsByClassName in a cross browser fashion and then use:

var list = document.getElementsByClassName("title");
for (var i = 0, len = list.length; i < len; i++) {
    list[i].className += " alternate";
}
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • this is not useful, because i use different versions of @color1, like `saturate(@color1, 10%); darken(@color1, 60%); fadeout(desaturate(@color1, 80%), 50%);` etc. so "#ff0000" changes to different values for each. – ahmetunal Oct 19 '11 at 15:17
  • Good luck then. If you want to change the value of `@color1`, you'll have to dynamically reload all your CSS stylesheets (after modification) because `@color1` is not known to the browser in any way as it has been removed when less was compiled. – jfriend00 Oct 19 '11 at 15:21
  • That is basically the issue, i'm searching a way to recompile less without reloading with the new value of color1. The thread that Moin redirected looks promising, right now I'm working on that. Thanks anyway. – ahmetunal Oct 19 '11 at 15:46