Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically changing less variables

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'}) 
like image 746
ahmetunal Avatar asked Oct 19 '11 14:10

ahmetunal


People also ask

Can I use CSS variables in less?

Because less is a great tool to compile css in a programming way and css variable has the feature of dynamtic value and is a good way to switch theme. It's true that is less can't use css variables in less function.

Can you change scss variable value dynamically?

CSS Variables to the Rescue CSS variables is a standard which enables you to have variables in CSS. SCSS is compiled to CSS during compile time, and SCSS variables are replaced with resolved value during compile time, which means there is no way to change the variable during run time.

How do you declare a variable in less?

LESS allows variables to be defined with an @ symbol.


1 Answers

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'
});
like image 184
Hakan Bilgin Avatar answered Oct 11 '22 08:10

Hakan Bilgin