Last active
August 29, 2015 14:07
-
-
Save whizark/344cd819e8a45f2acb4a to your computer and use it in GitHub Desktop.
Sass: Creating new scope & instance variable/method #sass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---- | |
// Sass (v3.4.4) | |
// Compass (v1.0.1) | |
// ---- | |
// Sass: Creating new scope & instance variable/method in Sass | |
// | |
// Other ideas: https://github.com/whizark/xass#ideas | |
// This is pseudo code | |
// The FILO stack to keep scope & the property values | |
$-scope: ( | |
// <scope-id>: ( | |
// <name>: <value>, | |
// ... | |
// ), | |
// ... | |
); | |
// Setter function | |
@function set($name, $value) { | |
// 1. Get the $-scope value by current scope ID | |
// 2. Set the $value to the $name property in the current scope | |
// 3. Merge back the value(1) to $-scope | |
@return $value; | |
} | |
// Getter function | |
@function get($name) { | |
$value: null; | |
// 1. Get the $-scope value by current scope ID | |
// 2. Get the value of $name property | |
@return $value; | |
} | |
// The mixin to create new scope | |
@mixin new() { | |
// 1. Create a new scope ID | |
// 2. Push the scope ID and empty Map into $-scope | |
// 3. Do something | |
// e.g. call constructor function by using call() | |
// or set default values etc. | |
@content; | |
// 4. Pop the current scope ID and Map(2) from $-scope | |
} | |
// Component Definition | |
@function method ($arg) { | |
$value: null; | |
// 1. Get property value(s) | |
$value: get('property'); | |
// 2. Do something | |
$value: $arg; | |
@return $value; | |
} | |
// Use case | |
.instance { | |
// 1. Create new scope. | |
@include new() { | |
// Scope begins | |
// Set property | |
$property: set('property', 1); | |
// Call a method | |
$value: method(1); | |
property: $value; | |
// Scope ends | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.instance { | |
property: 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment