From f0f84513f8efe533b6ee670a6f1a0c074387b2ec Mon Sep 17 00:00:00 2001 From: Volpeon Date: Wed, 13 Aug 2025 12:01:46 +0200 Subject: Make use of SASS modules --- src/_contexts.scss | 231 +++++++++++++++++++++++++++-------------------------- 1 file changed, 117 insertions(+), 114 deletions(-) (limited to 'src/_contexts.scss') diff --git a/src/_contexts.scss b/src/_contexts.scss index 7ffbb4e..ed376a2 100644 --- a/src/_contexts.scss +++ b/src/_contexts.scss @@ -15,6 +15,9 @@ /// @access public //// +@use 'sass:list'; +@use 'sass:map'; +@use 'sass:meta'; @use './functions'; /// @@ -34,7 +37,7 @@ $stacks: (); /// @throw If context stack already exists /// @mixin create($stack-id) { - $noop: create($stack-id); + $noop: create($stack-id); } /// @@ -43,13 +46,13 @@ $stacks: (); /// @param {string} $stack-id - ID of context stack /// @function create($stack-id) { - @if map-has-key($stacks, $stack-id) { - @error 'Context stack "#{inspect($stack-id)}" does not exist.'; - } + @if map.has-key($stacks, $stack-id) { + @error 'Context stack "#{inspect($stack-id)}" does not exist.'; + } - $stacks: map-merge($stacks, ($stack-id: ())) !global; + $stacks: map.merge($stacks, ($stack-id: ())) !global; - @return null; + @return null; } /// @@ -58,7 +61,7 @@ $stacks: (); /// @param {string} $stack-id - ID of context stack /// @mixin clear($stack-id) { - $noop: clear($stack-id); + $noop: clear($stack-id); } /// @@ -67,14 +70,14 @@ $stacks: (); /// @param {string} $stack-id - ID of context stack /// @function clear($stack-id) { - @if not map-has-key($stacks, $stack-id) { - @error 'Context stack "#{inspect($stack-id)}" does not exist.'; - } + @if not map.has-key($stacks, $stack-id) { + @error 'Context stack "#{inspect($stack-id)}" does not exist.'; + } - $context-stack: (); - $stacks: map-merge($stacks, ($stack-id: $context-stack)) !global; + $context-stack: (); + $stacks: map.merge($stacks, ($stack-id: $context-stack)) !global; - @return null; + @return null; } /// @@ -83,7 +86,7 @@ $stacks: (); /// @param {string} $stack-id - ID of context stack /// @mixin delete($stack-id) { - $noop: delete($stack-id); + $noop: delete($stack-id); } /// @@ -92,13 +95,13 @@ $stacks: (); /// @param {string} $stack-id - ID of context stack /// @function delete($stack-id) { - @if not map-has-key($stacks, $stack-id) { - @error 'Context stack "#{inspect($stack-id)}" does not exist.'; - } + @if not map.has-key($stacks, $stack-id) { + @error 'Context stack "#{inspect($stack-id)}" does not exist.'; + } - $stacks: map-remove($stacks, $stack-id) !global; + $stacks: map.remove($stacks, $stack-id) !global; - @return null; + @return null; } /// @@ -109,7 +112,7 @@ $stacks: (); /// @param {any} $data [()] - Data that belongs to the context /// @mixin push($stack-id, $id, $data: ()) { - $noop: push($stack-id, $id, $data); + $noop: push($stack-id, $id, $data); } /// @@ -122,16 +125,16 @@ $stacks: (); /// @return {list} A list with two items: 1 = context id, 2 = context data /// @function push($stack-id, $id, $data: ()) { - @if not map-has-key($stacks, $stack-id) { - @error 'Context stack "#{inspect($stack-id)}" does not exist.'; - } + @if not map.has-key($stacks, $stack-id) { + @error 'Context stack "#{inspect($stack-id)}" does not exist.'; + } - $context: $id $data; - $context-stack: map-get($stacks, $stack-id); - $context-stack: append($context-stack, $context); - $stacks: map-merge($stacks, ($stack-id: $context-stack)) !global; + $context: $id $data; + $context-stack: map.get($stacks, $stack-id); + $context-stack: list.append($context-stack, $context); + $stacks: map.merge($stacks, ($stack-id: $context-stack)) !global; - @return $context; + @return $context; } /// @@ -142,7 +145,7 @@ $stacks: (); /// @throw If context stack doesn't exist /// @mixin pop($stack-id) { - $noop: pop($stack-id); + $noop: pop($stack-id); } /// @@ -153,27 +156,27 @@ $stacks: (); /// @return {list} A list with two items: 1 = context id, 2 = context data /// @function pop($stack-id) { - @if not map-has-key($stacks, $stack-id) { - @error 'Context stack "#{inspect($stack-id)}" does not exist.'; - } + @if not map.has-key($stacks, $stack-id) { + @error 'Context stack "#{inspect($stack-id)}" does not exist.'; + } - $context-stack: map-get($stacks, $stack-id); + $context-stack: map.get($stacks, $stack-id); - @if length($context-stack) == 0 { - @error 'Context stack "#{inspect($stack-id)}" is already empty.'; - } + @if list.length($context-stack) == 0 { + @error 'Context stack "#{inspect($stack-id)}" is already empty.'; + } - $popped-context: nth($context-stack, -1); + $popped-context: list.nth($context-stack, -1); - @if length($context-stack) == 1 { - $context-stack: (); - } @else { - $context-stack: functions.list-slice($context-stack, 1, length($context-stack) - 1); - } + @if list.length($context-stack) == 1 { + $context-stack: (); + } @else { + $context-stack: functions.list-slice($context-stack, 1, list.length($context-stack) - 1); + } - $stacks: map-merge($stacks, ($stack-id: $context-stack)) !global; + $stacks: map.merge($stacks, ($stack-id: $context-stack)) !global; - @return $popped-context; + @return $popped-context; } /// @@ -186,15 +189,15 @@ $stacks: (); /// @throw If assertion fails /// @function assert-stack-must-contain($stack-id, $context-ids, $check-head-only: false) { - @if not contains($stack-id, $context-ids, $check-head-only) { - @error 'Must be called inside of contexts "#{inspect($context-ids)}".'; - } + @if not contains($stack-id, $context-ids, $check-head-only) { + @error 'Must be called inside of contexts "#{inspect($context-ids)}".'; + } - @return null; + @return null; } @mixin assert-stack-must-contain($stack-id, $context-ids, $check-head-only: false) { - $noop: assert-stack-must-contain($stack-id, $context-ids, $check-head-only); + $noop: assert-stack-must-contain($stack-id, $context-ids, $check-head-only); } /// @@ -207,15 +210,15 @@ $stacks: (); /// @throw If assertion fails /// @function assert-stack-must-not-contain($stack-id, $context-ids, $check-head-only: false) { - @if contains($stack-id, $context-ids, $check-head-only) { - @error 'Must not be called inside of contexts "#{inspect($context-ids)}".'; - } + @if contains($stack-id, $context-ids, $check-head-only) { + @error 'Must not be called inside of contexts "#{inspect($context-ids)}".'; + } - @return null; + @return null; } @mixin assert-stack-must-not-contain($stack-id, $context-ids, $check-head-only: false) { - $noop: assert-stack-must-not-contain($stack-id, $context-ids, $check-head-only); + $noop: assert-stack-must-not-contain($stack-id, $context-ids, $check-head-only); } /// @@ -228,29 +231,29 @@ $stacks: (); /// @return {bool} `true` if the context stack contains one of the context IDs, otherwise `false` /// @function contains($stack-id, $context-ids, $check-head-only: false) { - @if not map-has-key($stacks, $stack-id) { - @error 'Context stack "#{inspect($stack-id)}" does not exist.'; - } + @if not map.has-key($stacks, $stack-id) { + @error 'Context stack "#{inspect($stack-id)}" does not exist.'; + } - $context-stack: map-get($stacks, $stack-id); + $context-stack: map.get($stacks, $stack-id); - @if length($context-stack) == 0 { - @return false; - } + @if list.length($context-stack) == 0 { + @return false; + } - $end-idx: if($check-head-only, length($context-stack), 1); + $end-idx: if($check-head-only, list.length($context-stack), 1); - @for $i from length($context-stack) through $end-idx { - $context: nth($context-stack, $i); + @for $i from list.length($context-stack) through $end-idx { + $context: list.nth($context-stack, $i); - @each $chk-context in $context-ids { - @if nth($context, 1) == $chk-context { - @return true; - } - } - } + @each $chk-context in $context-ids { + @if list.nth($context, 1) == $chk-context { + @return true; + } + } + } - @return false; + @return false; } /// @@ -262,15 +265,15 @@ $stacks: (); /// @throw If assertion fails /// @function assert-stack-count($stack-id, $max-count) { - @if count($stack-id) > $max-count { - @error 'Maximum context count "#{inspect($max-count)}" exceeded.'; - } + @if count($stack-id) > $max-count { + @error 'Maximum context count "#{inspect($max-count)}" exceeded.'; + } - @return null; + @return null; } @mixin assert-stack-count($stack-id, $max-count) { - $noop: assert-stack-count($stack-id, $max-count); + $noop: assert-stack-count($stack-id, $max-count); } /// @@ -281,13 +284,13 @@ $stacks: (); /// @return {number} The number of contexts /// @function count($stack-id) { - @if not map-has-key($stacks, $stack-id) { - @error 'Context stack "#{inspect($stack-id)}" does not exist.'; - } + @if not map.has-key($stacks, $stack-id) { + @error 'Context stack "#{inspect($stack-id)}" does not exist.'; + } - $context-stack: map-get($stacks, $stack-id); + $context-stack: map.get($stacks, $stack-id); - @return length($context-stack); + @return list.length($context-stack); } /// @@ -299,37 +302,37 @@ $stacks: (); /// @return {list} Null if no match was found, otherwise a list with two items: 1. context ID, 2. context data. /// @function get($stack-id, $type-or-level: null) { - @if not map-has-key($stacks, $stack-id) { - @error 'Context stack "#{inspect($stack-id)}" does not exist.'; - } - - $context-stack: map-get($stacks, $stack-id); - - @if length($context-stack) == 0 { - @return null; - } - - @if type-of($type-or-level) == number { - $context: nth($context-stack, -$type-or-level); - - @return $context; - } @else { - @for $i from -1 through -(length($context-stack)) { - $context: nth($context-stack, $i); - - @if type-of($type-or-level) == list { - @for $j from 1 through length($type-or-level) { - $ctx: nth($type-or-level, $j); - - @if nth($context, 1) == $ctx { - @return $context; - } - } - } @else if nth($context, 1) == $type-or-level { - @return $context; - } - } - } - - @return null; + @if not map.has-key($stacks, $stack-id) { + @error 'Context stack "#{inspect($stack-id)}" does not exist.'; + } + + $context-stack: map.get($stacks, $stack-id); + + @if list.length($context-stack) == 0 { + @return null; + } + + @if meta.type-of($type-or-level) == number { + $context: list.nth($context-stack, -$type-or-level); + + @return $context; + } @else { + @for $i from -1 through -(list.length($context-stack)) { + $context: list.nth($context-stack, $i); + + @if meta.type-of($type-or-level) == list { + @for $j from 1 through list.length($type-or-level) { + $ctx: list.nth($type-or-level, $j); + + @if list.nth($context, 1) == $ctx { + @return $context; + } + } + } @else if list.nth($context, 1) == $type-or-level { + @return $context; + } + } + } + + @return null; } -- cgit v1.2.3-70-g09d2