sassy-namespaces 0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +45 -38
- data/lib/sassy-namespaces.rb +1 -1
- data/stylesheets/{_sassy-maps.scss → _sassy-stylesheets.scss} +29 -7
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2769b569ed18c8110272fd03140e32b5e902fba1
|
4
|
+
data.tar.gz: 68e75d1b46cb7cf79fc494197e9e86d87d68043b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aae3d39c3b2604fef1328e96a98073949a10f76f813fb0d022d7b6106b1ad52492fde07a0060ac62f2836248fd454794d38aa4621de5229fd9a7536b40cf860b
|
7
|
+
data.tar.gz: e2dac496bc5263370bdad64dd5034dbffd52b8b124b5270260ff787ae80e4e25f606f1a2aa8aeae94b1f980d6db89592c52043813e6009ca68951fd904651ba2
|
data/README.md
CHANGED
@@ -1,32 +1,39 @@
|
|
1
1
|
Sassy Namespaces
|
2
2
|
================
|
3
3
|
|
4
|
-
Sass 3.3 brought us maps, which are great for
|
4
|
+
Sass 3.3 brought us maps, which are great for grouping related values, but the syntax is a bit tedious. Sassy Namespaces offers a pattern for creating and accessing namespaces using Sass maps.
|
5
5
|
|
6
6
|
Yes, it's essentially a wrapper around the excellent [Sassy Maps](https://github.com/Team-Sass/Sassy-Maps) extension.
|
7
7
|
|
8
8
|
The problem
|
9
9
|
-----------
|
10
10
|
|
11
|
-
In vanilla Sass, even single-level namespaces are a bit of
|
11
|
+
In vanilla Sass, even single-level namespaces are a bit of a chore:
|
12
12
|
|
13
13
|
```scss
|
14
|
+
// Create "color" namespace
|
14
15
|
$color: ();
|
16
|
+
// Add key-value pair
|
15
17
|
$color: $map-merge($color, (primary: red));
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
+
output {
|
20
|
+
foo: map-get($color, primary); // foo: red
|
19
21
|
}
|
20
22
|
```
|
21
23
|
|
22
|
-
|
24
|
+
A partial solution is to define wrapper functions/mixins that abstract away the ugliness:
|
23
25
|
|
24
26
|
```scss
|
27
|
+
// Create "color" namespace
|
25
28
|
$color: ();
|
26
29
|
|
30
|
+
@function color-set($key, $value) {
|
31
|
+
@return $map-merge($color, ($key: $value));
|
32
|
+
}
|
33
|
+
|
27
34
|
// Setter
|
28
|
-
@mixin set
|
29
|
-
$color:
|
35
|
+
@mixin color-set($key, $value) {
|
36
|
+
$color: color-set($key, $value);
|
30
37
|
}
|
31
38
|
|
32
39
|
// Getter
|
@@ -34,43 +41,47 @@ $color: ();
|
|
34
41
|
@return map-get($color, $key);
|
35
42
|
}
|
36
43
|
|
37
|
-
|
38
|
-
|
39
|
-
|
44
|
+
// Add key-value pair
|
45
|
+
@include color-set(primary, red);
|
46
|
+
output {
|
47
|
+
foo: color(primary); // foo: red
|
40
48
|
}
|
41
49
|
```
|
42
50
|
|
43
51
|
But this quickly gets tiresome, especially once you try to create namespaces with more than one level of hierarchy.
|
44
52
|
|
45
|
-
|
53
|
+
Namespaces, minus the headaches
|
46
54
|
------------------------------------
|
47
55
|
|
48
|
-
|
56
|
+
With Sassy Namespaces, maps are created and used internally. You don't ever have to deal with them directly. Easy as:
|
49
57
|
|
50
58
|
```scss
|
51
|
-
|
59
|
+
// Create a "color" namespace
|
60
|
+
@include namespace-create(color); // (This step can be skipped)
|
61
|
+
// Add key-value pair
|
52
62
|
@include namespace-set(color, primary, red);
|
53
|
-
|
54
|
-
|
63
|
+
ouput {
|
64
|
+
foo: namespace-get(color, primary); // foo: red;
|
55
65
|
}
|
56
66
|
```
|
57
67
|
|
58
|
-
|
68
|
+
It's possible streamline to this even further. `namespace-set()` will create a namespace if it doesn't exist, so we can skip `namespace-create()`.
|
59
69
|
|
60
|
-
|
70
|
+
Also, instead of calling `namespace-set()` and `namespace-get()`, we can replace both with the wrapper function/mixin `namespace()`, which will either get or set as is appropriate. This comes in handy when creating wrapper functions/mixins:
|
61
71
|
|
62
72
|
```scss
|
63
|
-
|
64
|
-
|
73
|
+
// Alias color() to namespace(color)
|
74
|
+
@mixin color($args...) {
|
75
|
+
$color: namespace(color, $args...);
|
65
76
|
}
|
66
77
|
|
67
78
|
@function color($args...) {
|
68
|
-
@return namespace
|
79
|
+
@return namespace(color, $args...);
|
69
80
|
}
|
70
81
|
|
71
|
-
@include
|
72
|
-
|
73
|
-
|
82
|
+
@include color(primary, red);
|
83
|
+
output {
|
84
|
+
foo: color(primary); // foo: red
|
74
85
|
}
|
75
86
|
```
|
76
87
|
|
@@ -81,18 +92,18 @@ If you want to set multiple values at once, without calling `namespace-set()` ov
|
|
81
92
|
$map: (primary: red, secondary: green)
|
82
93
|
|
83
94
|
@include namespace-set(color, $map);
|
84
|
-
|
85
|
-
|
86
|
-
|
95
|
+
output {
|
96
|
+
foo: namespace-get(color, primary); // foo: red
|
97
|
+
bar: namespace-get(color, secondary); // bar: green
|
87
98
|
}
|
88
99
|
```
|
89
100
|
|
90
|
-
And, since Sassy Namespaces uses Sassy Maps internally, hierarchical namespaces are
|
101
|
+
And, since Sassy Namespaces uses Sassy Maps internally, hierarchical namespaces are easy, too:
|
91
102
|
|
92
103
|
```scss
|
93
104
|
@include namespace-set(color, text link hover, green);
|
94
|
-
|
95
|
-
|
105
|
+
output {
|
106
|
+
foo: namespace-get(color, text link hover); // foo: green
|
96
107
|
}
|
97
108
|
```
|
98
109
|
|
@@ -100,23 +111,19 @@ At any time, you can return the underlying namespace map:
|
|
100
111
|
|
101
112
|
```scss
|
102
113
|
@include namespace-set(color, primary, red);
|
103
|
-
$namespace-1: namespace(color);
|
114
|
+
$namespace-1: namespace-get(color);
|
104
115
|
|
105
116
|
@include namespace-set(color, secondary, green);
|
106
|
-
$namespace-2: namespace(color);
|
117
|
+
$namespace-2: namespace-get(color);
|
107
118
|
|
108
119
|
/*
|
109
120
|
$namespace-1: (
|
110
|
-
|
111
|
-
primary: red
|
112
|
-
)
|
121
|
+
primary: red
|
113
122
|
)
|
114
123
|
|
115
124
|
$namespace-2: (
|
116
|
-
|
117
|
-
|
118
|
-
secondary: green
|
119
|
-
)
|
125
|
+
primary: red,
|
126
|
+
secondary: green
|
120
127
|
)
|
121
128
|
*/
|
122
129
|
```
|
data/lib/sassy-namespaces.rb
CHANGED
@@ -1,20 +1,19 @@
|
|
1
1
|
@import "sassy-maps";
|
2
2
|
|
3
|
+
// Private map that holds the namespaces
|
3
4
|
$__sassy-namespaces: ();
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
}
|
8
|
-
|
6
|
+
// Add a key-value pair to an existing namespace, or to a new one if it doesn't already exist
|
7
|
+
// Returns the updated namespace map
|
9
8
|
@function namespace-set($name, $args...) {
|
10
9
|
$length: length($args);
|
11
|
-
$namespace: map-get($__sassy-namespaces, $name);
|
10
|
+
$namespace: map-get($__sassy-namespaces, $name) or ($name: ());
|
12
11
|
|
13
12
|
@if length($args) == 1 {
|
14
13
|
@if type-of(nth($args, 1)) == map {
|
15
14
|
$namespace: map-merge($namespace, nth($args, 1));
|
16
15
|
} @else {
|
17
|
-
@warn "Must pass either a map or
|
16
|
+
@warn "Must pass either a map or a key and a value.";
|
18
17
|
}
|
19
18
|
}
|
20
19
|
@else {
|
@@ -31,10 +30,17 @@ $__sassy-namespaces: ();
|
|
31
30
|
@return $namespace;
|
32
31
|
}
|
33
32
|
|
33
|
+
|
34
|
+
// Return value for key of a namespace
|
35
|
+
// Returns null if key or namespace doesn't exist
|
34
36
|
@function namespace-get($name, $args...) {
|
35
37
|
$length: length($args);
|
36
38
|
$namespace: map-get($__sassy-namespaces, $name);
|
37
39
|
|
40
|
+
@if not $namespace {
|
41
|
+
@return null;
|
42
|
+
}
|
43
|
+
|
38
44
|
@if $length > 0 {
|
39
45
|
$keys: nth($args, 1);
|
40
46
|
|
@@ -48,6 +54,22 @@ $__sassy-namespaces: ();
|
|
48
54
|
}
|
49
55
|
}
|
50
56
|
|
51
|
-
@
|
57
|
+
@function namespace($args...) {
|
58
|
+
@return namespace-get($args...) or namespace-set($args...);
|
59
|
+
}
|
60
|
+
|
61
|
+
@mixin namespace-get($args...) {
|
52
62
|
$namespace: namespace-set($name, $args...);
|
63
|
+
}
|
64
|
+
|
65
|
+
@mixin namespace-get($args...) {
|
66
|
+
$namespace: namespace-get($name, $args...);
|
67
|
+
}
|
68
|
+
|
69
|
+
@mixin namespace($name, $args...) {
|
70
|
+
$namespace: namespace($name, $args...);
|
71
|
+
}
|
72
|
+
|
73
|
+
@mixin namespace-create($name, $args...) {
|
74
|
+
@include namespace-set($name, $args...);
|
53
75
|
}
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sassy-namespaces
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Clark
|
@@ -61,8 +61,8 @@ extra_rdoc_files: []
|
|
61
61
|
files:
|
62
62
|
- README.md
|
63
63
|
- lib/sassy-namespaces.rb
|
64
|
-
- stylesheets/_sassy-
|
65
|
-
homepage:
|
64
|
+
- stylesheets/_sassy-stylesheets.scss
|
65
|
+
homepage: https://github.com/acdlite/sassy-namespaces
|
66
66
|
licenses: []
|
67
67
|
metadata: {}
|
68
68
|
post_install_message:
|