Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Less CSS : Conditional @imports using mixins - Variable scope

Tags:

less

What I'm attempting to do is @import specific files into my Less project by setting checking for specific variables. This is what I have so far:

@import "shared/variables";
@import "shared/global";
@import "shared/hero";
@import "shared/listings";

//
// Color Variations
// Based on @bt-color-variation
// Decides when to load the light/dark theme
//
@bt-color-variation: dark; // Default color variation
.bt-color-variation(dark) {
    @import "color-variations/dark/global";

    @import "color-variations/dark/variables";
}

.bt-color-variation(dark) {
    @import "color-variations/light/global";
    @import "color-variations/light/buttons.less";

    @import "color-variations/light/variables";
}

.bt-color-variation(@bt-color-variation);

This does in-fact load the appropriate color variations based on that variable. The issue is that any variables set within that color variation's variable.less file are not overriding what is done outside of it in the shared/variables.less file. It seems like there's an issue with either "when" mixins are run compared to the regular @imports or a scope issue. I was hoping the mixins would run after and use Less's "last-win" case for variables.

Any ideas with how I'd conditionally load files based on a Less variable (and not deal with variable scope) would be EXTREMELY helpful. Thanks as always.

like image 497
Zach Avatar asked Oct 30 '14 22:10

Zach


People also ask

What can be passed to a LESS mixin?

Mixins are a group of CSS properties that allow you to use properties of one class for another class and includes class name as its properties. In LESS, you can declare a mixin in the same way as CSS style using class or id selector. It can store multiple values and can be reused in the code whenever necessary.

What is the correct syntax of a variable in LESS?

LESS allows variables to be defined with an @ symbol. The Variable assignment is done with a colon(:).

How do you write LESS in CSS?

Create a stylesheet with . less extension and link it in your document using the rel="stylesheet/less" attribute. You are all set and can compose styles within the . less .

What are CSS LESS files?

Less (Leaner Style Sheets; sometimes stylized as LESS) is a dynamic preprocessor style sheet language that can be compiled into Cascading Style Sheets (CSS) and run on the client side or server side.


1 Answers

Variables from within mixins do not override those in the caller scope (see Mixins as Functions).

For this use-case I'd suggest something more simple:

@bt-color-variation: dark;

@import "color-variations/@{bt-color-variation}/global";
@import "color-variations/@{bt-color-variation}/buttons";
@import "color-variations/@{bt-color-variation}/variables";

See Variabes > Import statements.

like image 149
seven-phases-max Avatar answered Jan 01 '23 13:01

seven-phases-max