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

Updated detached rulesets and mixins as functions #201

Merged
merged 6 commits into from
Aug 31, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 164 additions & 1 deletion content/features/detached-rulesets.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,29 @@

Released [v1.7.0]({{ less.master }}CHANGELOG.md)

You may want to define a mixin that will abstract out either wrapping a piece of code in a media query or a non supported browser class name. You can now pass rulesets to mixins so that the mixin can wrap the content. E.g.
Detached ruleset is a group of css properties, nested rulestes, media declarations or anything else stored in a variable. You can include it into a ruleset or another structure and all its properties are going to be copied there. You can also use it as a mixin argument and pass it around as any other variable.

Simple example:
````less
// declare detached ruleset
@detached-ruleset: { background: red; };

// use detached ruleset
.top {
@detached-ruleset();
}
````

compiles into:
````css
.top {
background: red;
}
````

Parentheses after detached ruleset call are mandatory. The `@detached-ruleset;` call would NOT work.

It is usefull when you want to define a mixin that abstracts out either wrapping a piece of code in a media query or a non supported browser class name. The rulesets can be passed to mixin so that the mixin can wrap the content. E.g.

```less
.desktop-and-old-ie(@rules) {
Expand Down Expand Up @@ -69,3 +91,144 @@ which will output
}
}
```

Detached ruleset call unlocks (returns) all its mixins into caller the same way as mixin calls do. However, it does NOT return variables.

Returned mixin:
````less
// detached ruleset with a mixin
@detached-ruleset: {
.mixin() {
color:blue;
}
};
// call detached ruleset
.caller {
@detached-ruleset();
.mixin();
}
````

Results in:
````css
.caller {
color: blue;
}
````

Private variables:
````less
detached-ruleset: {
@color:blue; //this variable is private
};
.caller {
color: @color; //syntax error
}
````

## Scoping
Detached ruleset can use all variables and mixins accessible on place where it is *defined* and where it is *called*. Otherwise said, both definition and caller scopes are available to it. If both scopes contains the same variable or mixin, declaration scope value takes precedence.

*Declaration scope* is the one where detached ruleset body is defined. Copying detached ruleset from one variable into another can not modify its scope. Ruleset does not gain access to new scopes just by being referenced there.

Lastly, detached ruleset can gain access to scope by being unlocked (imported) into it.

#### Definition and Caller Scope Visibility
Detached mixin sees callers variables and mixins:
````less
@detached-ruleset: {
caller-variable: @callerVariable; // variable is undefined here
.callerMixin(); // mixin is undefined here
};

selector {
// use detached ruleset
@detached-ruleset();

// define variable and mixin needed inside the detached ruleset
@callerVariable: value;
.callerMixin() {
variable: declaration;
}
}
````

compiles into:
````css
selector {
caller-variable: value;
variable: declaration;
}
````

Variable and mixins accessible form definition win over those available in caller:
````less
@variable: global;
@detached-ruleset: {
//will use global variable, because it is accessible
//from detached-ruleset definition
variable: @variable;
};

selector {
@detached-ruleset();
@variable: value; //variable defined in caller - will be ignored
}
````

compiles into:
````css
selector {
variable: global;
}
````

#### Referencing WONT Modify Detached Ruleset Scope
Ruleset does not gain access to new scopes just by being referenced there:
````less
@detached-1: { scope-detached: @one @two; };
.one {
@one: visible;
.two {
@detached-2: @detached-1; // copying/renaming ruleset
@two: visible; // ruleset can not see this variable
}
}

.usePlace {
.one > .two();
@detached-2();
}
````

throws an error:
````
ERROR 1:32 The variable "@one" was not declared.
````

#### Unlocking WILL Modify Detached Ruleset Scope
Detached ruleset gains access by being unlocked (imported) inside a scope:
````less
#space {
.importer1() {
@detached: { scope-detached: @variable; }; // define detached ruleset
}
}

.importer2() {
@variable: value; // unlocked detached ruleset CAN see this variable
#space > .importer1(); // unlock/import detached ruleset
}

.usePlace {
.importer2(); // unlock/import detached ruleset second time
@detached();
}
````

compiles into:
````css
.usePlace {
scope-detached: value;
}
````
51 changes: 49 additions & 2 deletions content/features/mixins-as-functions.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
> Return variables from mixins
> Return variables or mixins from mixins

All variables defined in a mixin are visible and can be used in caller's scope (unless the caller scope already has a variable with same name defined, including previously defined by another mixin call).
Variables and mixins defined in a mixin are visible and can be used in caller's scope. There is only one exception, variable is not copied if the caller contains a variable with the same name (that includes variables defined by another mixin call). Only variables present in callers local scope are protected. Variables inherited from parent scopes are overridden.

Example:

Expand Down Expand Up @@ -48,3 +48,50 @@ div {
padding: 33px;
}
```
<<<<<<< HEAD
=======

Variable defined directly in callers scope can not be overriden. However, variable defined in callers parent scope is not protected and will be overriden:
````less
.mixin() {
@size: in-mixin;
@definedOnlyInMixin: in-mixin;
}

.class {
margin: @size @definedOnlyInMixin;
.mixin();
}

@size: globaly-defined-value; // callers parent scope - no protection
````

Results in:
````css
.class {
margin: in-mixin in-mixin;
}
````

Finally, mixin defined in mixin acts as return value too:
````less
.unlock(@value) { // outer mixin
.doSomething() { // nested mixin
declaration: @value;
}
}

#namespace {
.unlock(5); // unlock doSomething mixin
.doSomething(); //nested mixin was copied here and is usable
}
````

Results in:
````css
#namespace {
declaration: 5;
}
````

>>>>>>> f945fa6948c69b5a84e402bc8eb8b2ead2735cfa