diff options
author | Volpeon <git@volpeon.ink> | 2021-03-17 17:32:14 +0100 |
---|---|---|
committer | Volpeon <git@volpeon.ink> | 2021-03-17 17:32:14 +0100 |
commit | 8f2ce6e4491d630a35dca9612daedf95b4a2954b (patch) | |
tree | 037ca6c1fd40a8c424328ad3392fe6edfaad175c | |
parent | Expose prefix parameter for iro-props-assign-native (diff) | |
download | iro-sass-8f2ce6e4491d630a35dca9612daedf95b4a2954b.tar.gz iro-sass-8f2ce6e4491d630a35dca9612daedf95b4a2954b.tar.bz2 iro-sass-8f2ce6e4491d630a35dca9612daedf95b4a2954b.zip |
Added namespaces and shortcodes for property trees
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | package.json | 6 | ||||
-rw-r--r-- | src/_contexts.scss | 6 | ||||
-rw-r--r-- | src/_props.scss | 182 | ||||
-rw-r--r-- | src/props-shortcodes.scss | 77 | ||||
-rw-r--r-- | test/_props.scss | 166 | ||||
-rw-r--r-- | yarn.lock | 286 |
7 files changed, 475 insertions, 250 deletions
@@ -214,7 +214,7 @@ It's a very simple feature, but it makes managing large sets of structured data | |||
214 | Example usage: | 214 | Example usage: |
215 | 215 | ||
216 | ```scss | 216 | ```scss |
217 | @include iro-props-save(( | 217 | @include iro-props-store(( |
218 | --accent: #f00, | 218 | --accent: #f00, |
219 | --accent-text: #fff, | 219 | --accent-text: #fff, |
220 | 220 | ||
diff --git a/package.json b/package.json index d4eb18b..c93a3b3 100644 --- a/package.json +++ b/package.json | |||
@@ -25,9 +25,9 @@ | |||
25 | }, | 25 | }, |
26 | "homepage": "https://git.vulpes.one/Feuerfuchs/iro-sass", | 26 | "homepage": "https://git.vulpes.one/Feuerfuchs/iro-sass", |
27 | "devDependencies": { | 27 | "devDependencies": { |
28 | "mocha": "^8.2.0", | 28 | "mocha": "^8.3.2", |
29 | "nodemon": "^2.0.6", | 29 | "nodemon": "^2.0.7", |
30 | "sass": "^1.28.0", | 30 | "sass": "^1.32.8", |
31 | "sass-lint": "^1.12.1", | 31 | "sass-lint": "^1.12.1", |
32 | "sass-true": "^6.0.1", | 32 | "sass-true": "^6.0.1", |
33 | "sassdoc": "^2.7.3" | 33 | "sassdoc": "^2.7.3" |
diff --git a/src/_contexts.scss b/src/_contexts.scss index 556fde3..9fe0e8c 100644 --- a/src/_contexts.scss +++ b/src/_contexts.scss | |||
@@ -124,9 +124,9 @@ $iro-context-stacks: (); | |||
124 | @error 'Context stack "#{inspect($stack-id)}" does not exist.'; | 124 | @error 'Context stack "#{inspect($stack-id)}" does not exist.'; |
125 | } | 125 | } |
126 | 126 | ||
127 | $context: $id $data; | 127 | $context: $id $data; |
128 | $context-stack: map-get($iro-context-stacks, $stack-id); | 128 | $context-stack: map-get($iro-context-stacks, $stack-id); |
129 | $context-stack: append($context-stack, $context); | 129 | $context-stack: append($context-stack, $context); |
130 | $iro-context-stacks: map-merge($iro-context-stacks, ($stack-id: $context-stack)) !global; | 130 | $iro-context-stacks: map-merge($iro-context-stacks, ($stack-id: $context-stack)) !global; |
131 | 131 | ||
132 | @return $context; | 132 | @return $context; |
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 |