aboutsummaryrefslogtreecommitdiffstats
path: root/src/_props.scss
diff options
context:
space:
mode:
Diffstat (limited to 'src/_props.scss')
-rw-r--r--src/_props.scss182
1 files changed, 158 insertions, 24 deletions
diff --git a/src/_props.scss b/src/_props.scss
index 86530f7..86a2215 100644
--- a/src/_props.scss
+++ b/src/_props.scss
@@ -41,26 +41,71 @@ $iro-props-default-tree: 'default' !default;
41/// 41///
42$iro-props-trees: (); 42$iro-props-trees: ();
43 43
44///
45/// Default context name used for the namespace context.
46///
47/// @type string
48///
49$iro-props-namespace-context-id: 'namespace' !default;
50
51///
52/// Declare a namespace, meaning that all variables declared and accessed.
53///
54/// @param {string} $name - Name of the namespace
55///
56@mixin iro-props-namespace($name) {
57 $key: '--#{$name}';
58
59 $ns-key: iro-props-get-ns-key();
60
61 @if $ns-key != null {
62 $key: append($ns-key, $key);
63 } @else {
64 $key: ($key);
65 }
66
67 @include iro-context-push($iro-props-namespace-context-id, 'namespace', (
68 'name': $name,
69 'key': $key
70 ));
71
72 @content;
73
74 @include iro-context-pop($iro-props-namespace-context-id);
75}
76
77///
78/// Get the current namespace name.
79///
80@function iro-props-ns-name() {
81 $noop: iro-context-assert-stack-must-contain($iro-props-namespace-context-id, 'namespace');
82
83 $data: nth(iro-context-get($iro-props-namespace-context-id, 'namespace'), 2);
84 $name: map-get($data, 'name');
85
86 @return $name;
87}
88
44/// 89///
45/// Save a property tree. If a tree with the sane name already exists, the trees 90/// Save a property tree. If a tree with the sane name already exists, the trees
46/// will be merged. 91/// will be merged.
47/// 92///
48/// @param {map} $map - Map containing properties 93/// @param {map} $map - Map containing properties
49/// @param {string} $tree [$iro-props-default-tree] - ID the map is saved as 94/// @param {string} $tree [$iro-props-default-tree] - ID the map is saved as
50/// @param {bool} $merge [false] - If a tree named $tree already exists and this value is set to true, they will be merged. Otherwise an error will be emitted. 95/// @param {bool} $merge [false] - If a tree named $tree already exists and this value is set to true, they will be merged. Otherwise an error will be emitted.
51/// 96///
52@mixin iro-props-save($map, $tree: $iro-props-default-tree, $merge: false) { 97@mixin iro-props-store($map, $tree: $iro-props-default-tree, $merge: false, $global: false) {
53 $noop: iro-props-save($map, $tree, $merge); 98 $noop: iro-props-store($map, $tree, $merge, $global);
54} 99}
55 100
56/// 101///
57/// Save a property tree. 102/// Save a property tree.
58/// 103///
59/// @param {map} $map - Map containing properties 104/// @param {map} $map - Map containing properties
60/// @param {string} $tree [$iro-props-default-tree] - ID the map is saved as 105/// @param {string} $tree [$iro-props-default-tree] - ID the map is saved as
61/// @param {bool} $merge [false] - If a tree named $tree already exists and this value is set to true, they will be merged. Otherwise an error will be emitted. 106/// @param {bool} $merge [false] - If a tree named $tree already exists and this value is set to true, they will be merged. Otherwise an error will be emitted.
62/// 107///
63@function iro-props-save($map, $tree: $iro-props-default-tree, $merge: false) { 108@function iro-props-store($map, $tree: $iro-props-default-tree, $merge: false, $global: false) {
64 $prop-map: null; 109 $prop-map: null;
65 110
66 @if $iro-props-enforce-double-dashes { 111 @if $iro-props-enforce-double-dashes {
@@ -69,6 +114,14 @@ $iro-props-trees: ();
69 } 114 }
70 } 115 }
71 116
117 @if not $global {
118 $ns-key: iro-props-get-ns-key();
119
120 @if $ns-key != null {
121 $map: ($ns-key: $map)
122 }
123 }
124
72 @if map-has-key($iro-props-trees, $tree) { 125 @if map-has-key($iro-props-trees, $tree) {
73 @if $merge { 126 @if $merge {
74 $map: iro-map-merge-recursive(map-get($iro-props-trees, $tree), $map); 127 $map: iro-map-merge-recursive(map-get($iro-props-trees, $tree), $map);
@@ -87,18 +140,18 @@ $iro-props-trees: ();
87/// 140///
88/// @param {string} $tree [$iro-props-default-tree] - ID of the tree to be deleted 141/// @param {string} $tree [$iro-props-default-tree] - ID of the tree to be deleted
89/// 142///
90@mixin iro-props-delete($tree: $iro-props-default-tree) { 143@mixin iro-props-clear($tree: $iro-props-default-tree) {
91 $noop: iro-props-delete($tree); 144 $noop: iro-props-clear($tree);
92} 145}
93 146
94/// 147///
95/// Unset a property tree. 148/// Delete a property tree.
96/// 149///
97/// @param {string} $tree [$iro-props-default-tree] - ID of the tree to be deleted 150/// @param {string} $tree [$iro-props-default-tree] - ID of the tree to be deleted
98/// 151///
99/// @throw If the property tree does not exist 152/// @throw If the property tree does not exist
100/// 153///
101@function iro-props-delete($tree: $iro-props-default-tree) { 154@function iro-props-clear($tree: $iro-props-default-tree) {
102 @if not map-has-key($iro-props-trees, $tree) { 155 @if not map-has-key($iro-props-trees, $tree) {
103 @error 'Property tree "#{inspect($tree)}" does not exist.'; 156 @error 'Property tree "#{inspect($tree)}" does not exist.';
104 } 157 }
@@ -111,33 +164,50 @@ $iro-props-trees: ();
111/// 164///
112/// Access a whole property or a subsection (i.e. value) of it. 165/// Access a whole property or a subsection (i.e. value) of it.
113/// 166///
114/// @param {string | list} $key [null] - Key of the property to read. If this is a list of keys, the map will be traversed in that order. 167/// @param {string | list} $key [null] - Key of the property to read. If this is a list of keys, the map will be traversed in that order.
115/// @param {string} $tree [$iro-props-default-tree] - ID of the property tree to use 168/// @param {string} $tree [$iro-props-default-tree] - ID of the property tree to use
116/// @param {any} $default [null] - Default value to return of no match was found. If null, this function will throw an error instead. 169/// @param {any} $default [null] - Default value to return of no match was found. If null, this function will throw an error instead.
117/// 170///
118/// @return {any} Value assigned to property or $default 171/// @return {any} Value assigned to property or $default
119/// 172///
120/// @throw If there was no match for $key and $default is null 173/// @throw If there was no match for $key and $default is null
121/// 174///
122@function iro-props-get($key: (), $tree: $iro-props-default-tree, $default: null) { 175@function iro-props-get($key: (), $tree: $iro-props-default-tree, $default: null, $global: false) {
123 @if not map-has-key($iro-props-trees, $tree) { 176 @if not map-has-key($iro-props-trees, $tree) {
124 @error 'Unknown tree "#{$tree}".'; 177 @error 'Unknown tree "#{$tree}".';
125 } 178 }
126 179
127 $result: map-get($iro-props-trees, $tree); 180 $result: map-get($iro-props-trees, $tree);
128 181
182 @if not $global {
183 $ns-key: iro-props-get-ns-key();
184
185 @if $ns-key != null {
186 $orig-key: $key;
187 $key: $ns-key;
188
189 @if type-of($orig-key) == list {
190 @each $subkey in $orig-key {
191 $key: append($key, $subkey);
192 }
193 } @else {
194 $key: append($key, $orig-key);
195 }
196 }
197 }
198
129 @if type-of($key) == list { 199 @if type-of($key) == list {
130 $stop: false; 200 $stop: false;
131 201
132 @each $k in $key { 202 @each $k in $key {
133 @if map-has-key($result, $k) and not $stop { 203 @if not $stop and map-has-key($result, $k) {
134 $result: map-get($result, $k); 204 $result: map-get($result, $k);
135 205
136 @if type-of($result) == list and nth($result, 1) == 'iro-prop-ref' { 206 @if type-of($result) == list and nth($result, 1) == 'iro-prop-ref' {
137 @if length($result) == 2 { 207 @if length($result) == 2 {
138 $result: iro-props-get($tree: nth($result, 2)); 208 $result: iro-props-get($tree: nth($result, 2), $global: true);
139 } @else { 209 } @else {
140 $result: iro-props-get(nth($result, 3), nth($result, 2)); 210 $result: iro-props-get(nth($result, 3), nth($result, 2), $global: true);
141 } 211 }
142 } 212 }
143 } @else { 213 } @else {
@@ -153,9 +223,9 @@ $iro-props-trees: ();
153 223
154 @if type-of($result) == list and nth($result, 1) == 'iro-prop-ref' { 224 @if type-of($result) == list and nth($result, 1) == 'iro-prop-ref' {
155 @if length($result) == 2 { 225 @if length($result) == 2 {
156 $result: iro-props-get($tree: nth($result, 2)); 226 $result: iro-props-get($tree: nth($result, 2), $global: true);
157 } @else { 227 } @else {
158 $result: iro-props-get(nth($result, 3), nth($result, 2)); 228 $result: iro-props-get(nth($result, 3), nth($result, 2), $global: true);
159 } 229 }
160 } 230 }
161 } 231 }
@@ -180,11 +250,28 @@ $iro-props-trees: ();
180/// 250///
181/// @return {string} var() 251/// @return {string} var()
182/// 252///
183@function iro-props-get-native($key, $tree: null, $default: null) { 253@function iro-props-get-native($key, $tree: null, $default: null, $global: false) {
184 @if $tree != null { 254 @if $tree != null {
185 $noop: iro-props-get($key, $tree, $default); 255 $noop: iro-props-get($key, $tree, $default);
186 } 256 }
187 257
258 @if not $global {
259 $ns-key: iro-props-get-ns-key();
260
261 @if $ns-key != null {
262 $orig-key: $key;
263 $key: $ns-key;
264
265 @if type-of($orig-key) == list {
266 @each $subkey in $orig-key {
267 $key: append($key, $subkey);
268 }
269 } @else {
270 $key: append($key, $orig-key);
271 }
272 }
273 }
274
188 $native-var: ''; 275 $native-var: '';
189 276
190 @if type-of($key) == list { 277 @if type-of($key) == list {
@@ -206,9 +293,9 @@ $iro-props-trees: ();
206/// Generate assignments for native CSS custom properties with the values from the specified tree. 293/// Generate assignments for native CSS custom properties with the values from the specified tree.
207/// 294///
208/// @param {string} $tree [$iro-props-default-tree] - ID of the property tree to use 295/// @param {string} $tree [$iro-props-default-tree] - ID of the property tree to use
209/// @param {string} $root [()] - Sub-tree to use for assignment 296/// @param {string} $root [()] - Sub-tree to use for assignment
210/// 297///
211@mixin iro-props-assign-native($tree: $iro-props-default-tree, $root: (), $skip: (), $prefix: '') { 298@mixin iro-props-assign-native($tree: $iro-props-default-tree, $root: (), $skip: (), $prefix: '', $global: false) {
212 $map: iro-props-get($root, $tree); 299 $map: iro-props-get($root, $tree);
213 $map: map-remove($map, $skip...); 300 $map: map-remove($map, $skip...);
214 301
@@ -216,6 +303,14 @@ $iro-props-trees: ();
216 $prefix: iro-str-implode($prefix) 303 $prefix: iro-str-implode($prefix)
217 } 304 }
218 305
306 @if not $global {
307 $ns-key: iro-props-get-ns-key();
308
309 @if $ns-key != null {
310 $prefix: $prefix + iro-str-implode($ns-key);
311 }
312 }
313
219 @include iro-props-assign-native-internal($map, $prefix); 314 @include iro-props-assign-native-internal($map, $prefix);
220} 315}
221 316
@@ -270,16 +365,55 @@ $iro-props-trees: ();
270/// Generate a reference to another tree. Dereferencing is lazy, so you may specify a tree that hasn't been created yet. 365/// Generate a reference to another tree. Dereferencing is lazy, so you may specify a tree that hasn't been created yet.
271/// 366///
272/// @param {string} $tree [$iro-props-default-tree] - ID of the property tree to use 367/// @param {string} $tree [$iro-props-default-tree] - ID of the property tree to use
273/// @param {string | list} $key - Key of the property to read. If this is a list of keys, the map will be traversed in that order. 368/// @param {string | list} $key - Key of the property to read. If this is a list of keys, the map will be traversed in that order.
274/// 369///
275/// @return {list} A special list that let's Ignis know that this is a lazy value. 370/// @return {list} A special list that let's Ignis know that this is a lazy value.
276/// 371///
277/// @throw If there was no match for $key and $default is null 372/// @throw If there was no match for $key and $default is null
278/// 373///
279@function iro-props-ref($tree: $iro-props-default-tree, $key: null) { 374@function iro-props-ref($tree: $iro-props-default-tree, $key: null, $global: false) {
375 @if not $global {
376 $ns-key: iro-props-get-ns-key();
377
378 @if $ns-key != null {
379 $orig-key: $key;
380 $key: $ns-key;
381
382 @if $orig-key != null {
383 @if type-of($orig-key) == list {
384 @each $subkey in $orig-key {
385 $key: append($key, $subkey);
386 }
387 } @else {
388 $key: append($key, $orig-key);
389 }
390 }
391 }
392 }
393
280 @if $key == null { 394 @if $key == null {
281 @return ('iro-prop-ref' $tree); 395 @return ('iro-prop-ref' $tree);
282 } @else { 396 } @else {
283 @return ('iro-prop-ref' $tree $key); 397 @return ('iro-prop-ref' $tree $key);
284 } 398 }
285} 399}
400
401///
402/// Get the current namespace key.
403///
404/// @access private
405///
406@function iro-props-get-ns-key() {
407 $ctx: iro-context-get($iro-props-namespace-context-id, 'namespace');
408
409 @if $ctx == null {
410 @return null;
411 }
412
413 $data: nth($ctx, 2);
414 $key: map-get($data, 'key');
415
416 @return $key;
417}
418
419@include iro-context-stack-create($iro-props-namespace-context-id);