From dd5f3c463fab336d694f426dcad11a1783590fc9 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Sat, 5 Feb 2022 07:52:13 +0100 Subject: Ported from import syntax to modules --- src/_contexts.scss | 114 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 67 insertions(+), 47 deletions(-) (limited to 'src/_contexts.scss') diff --git a/src/_contexts.scss b/src/_contexts.scss index 9fe0e8c..8542056 100644 --- a/src/_contexts.scss +++ b/src/_contexts.scss @@ -5,16 +5,18 @@ /// It's an essential part for the BEM-related mixins. /// /// If you want to create a new context, the easiest pattern is to create a new mixin and wrap -/// the @content between a pair of iro-context-push and iro-context-pop. -/// From within the @content, you can access the context's data with iro-context-get. +/// the @content between a pair of push and pop. +/// From within the @content, you can access the context's data with get. /// To make the compilation fail if a certain nesting order is violated, use -/// iro-context-assert-stack-must-contain and iro-context-assert-stack-must-not-contain. +/// assert-stack-must-contain and assert-stack-must-not-contain. /// /// @group Contexts /// /// @access public //// +@use './functions'; + /// /// Map of all context stacks. /// @@ -22,7 +24,7 @@ /// /// @access private /// -$iro-context-stacks: (); +$stacks: (); /// /// Create a new context stack. @@ -31,8 +33,8 @@ $iro-context-stacks: (); /// /// @throw If context stack already exists /// -@mixin iro-context-stack-create($stack-id) { - $noop: iro-context-stack-create($stack-id); +@mixin create($stack-id) { + $noop: create($stack-id); } /// @@ -40,12 +42,12 @@ $iro-context-stacks: (); /// /// @param {string} $stack-id - ID of context stack /// -@function iro-context-stack-create($stack-id) { - @if map-has-key($iro-context-stacks, $stack-id) { +@function create($stack-id) { + @if map-has-key($stacks, $stack-id) { @error 'Context stack "#{inspect($stack-id)}" does not exist.'; } - $iro-context-stacks: map-merge($iro-context-stacks, ($stack-id: ())) !global; + $stacks: map-merge($stacks, ($stack-id: ())) !global; @return null; } @@ -55,8 +57,8 @@ $iro-context-stacks: (); /// /// @param {string} $stack-id - ID of context stack /// -@mixin iro-context-stack-clear($stack-id) { - $noop: iro-context-stack-clear($stack-id); +@mixin clear($stack-id) { + $noop: clear($stack-id); } /// @@ -64,13 +66,13 @@ $iro-context-stacks: (); /// /// @param {string} $stack-id - ID of context stack /// -@function iro-context-stack-clear($stack-id) { - @if not map-has-key($iro-context-stacks, $stack-id) { +@function clear($stack-id) { + @if not map-has-key($stacks, $stack-id) { @error 'Context stack "#{inspect($stack-id)}" does not exist.'; } $context-stack: (); - $iro-context-stacks: map-merge($iro-context-stacks, ($stack-id: $context-stack)) !global; + $stacks: map-merge($stacks, ($stack-id: $context-stack)) !global; @return null; } @@ -80,8 +82,8 @@ $iro-context-stacks: (); /// /// @param {string} $stack-id - ID of context stack /// -@mixin iro-context-stack-delete($stack-id) { - $noop: iro-context-stack-delete($stack-id); +@mixin delete($stack-id) { + $noop: delete($stack-id); } /// @@ -89,12 +91,12 @@ $iro-context-stacks: (); /// /// @param {string} $stack-id - ID of context stack /// -@function iro-context-stack-delete($stack-id) { - @if not map-has-key($iro-context-stacks, $stack-id) { +@function delete($stack-id) { + @if not map-has-key($stacks, $stack-id) { @error 'Context stack "#{inspect($stack-id)}" does not exist.'; } - $iro-context-stacks: map-remove($iro-context-stacks, $stack-id) !global; + $stacks: map-remove($stacks, $stack-id) !global; @return null; } @@ -106,8 +108,8 @@ $iro-context-stacks: (); /// @param {string} $id - ID of new context /// @param {any} $data [()] - Data that belongs to the context /// -@mixin iro-context-push($stack-id, $id, $data: ()) { - $noop: iro-context-push($stack-id, $id, $data); +@mixin push($stack-id, $id, $data: ()) { + $noop: push($stack-id, $id, $data); } /// @@ -119,15 +121,15 @@ $iro-context-stacks: (); /// /// @return {list} A list with two items: 1 = context id, 2 = context data /// -@function iro-context-push($stack-id, $id, $data: ()) { - @if not map-has-key($iro-context-stacks, $stack-id) { +@function push($stack-id, $id, $data: ()) { + @if not map-has-key($stacks, $stack-id) { @error 'Context stack "#{inspect($stack-id)}" does not exist.'; } $context: $id $data; - $context-stack: map-get($iro-context-stacks, $stack-id); + $context-stack: map-get($stacks, $stack-id); $context-stack: append($context-stack, $context); - $iro-context-stacks: map-merge($iro-context-stacks, ($stack-id: $context-stack)) !global; + $stacks: map-merge($stacks, ($stack-id: $context-stack)) !global; @return $context; } @@ -139,8 +141,8 @@ $iro-context-stacks: (); /// /// @throw If context stack doesn't exist /// -@mixin iro-context-pop($stack-id) { - $noop: iro-context-pop($stack-id); +@mixin pop($stack-id) { + $noop: pop($stack-id); } /// @@ -150,12 +152,12 @@ $iro-context-stacks: (); /// /// @return {list} A list with two items: 1 = context id, 2 = context data /// -@function iro-context-pop($stack-id) { - @if not map-has-key($iro-context-stacks, $stack-id) { +@function pop($stack-id) { + @if not map-has-key($stacks, $stack-id) { @error 'Context stack "#{inspect($stack-id)}" does not exist.'; } - $context-stack: map-get($iro-context-stacks, $stack-id); + $context-stack: map-get($stacks, $stack-id); @if length($context-stack) == 0 { @error 'Context stack "#{inspect($stack-id)}" is already empty.'; @@ -166,10 +168,10 @@ $iro-context-stacks: (); @if length($context-stack) == 1 { $context-stack: (); } @else { - $context-stack: iro-list-slice($context-stack, 1, length($context-stack) - 1); + $context-stack: functions.list-slice($context-stack, 1, length($context-stack) - 1); } - $iro-context-stacks: map-merge($iro-context-stacks, ($stack-id: $context-stack)) !global; + $stacks: map-merge($stacks, ($stack-id: $context-stack)) !global; @return $popped-context; } @@ -183,10 +185,16 @@ $iro-context-stacks: (); /// /// @throw If assertion fails /// -@mixin iro-context-assert-stack-must-contain($stack-id, $context-ids, $check-head-only: false) { - @if not iro-context-stack-contains($stack-id, $context-ids, $check-head-only) { +@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)}".'; } + + @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) } /// @@ -198,10 +206,16 @@ $iro-context-stacks: (); /// /// @throw If assertion fails /// -@mixin iro-context-assert-stack-must-not-contain($stack-id, $context-ids, $check-head-only: false) { - @if iro-context-stack-contains($stack-id, $context-ids, $check-head-only) { +@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)}".'; } + + @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); } /// @@ -213,12 +227,12 @@ $iro-context-stacks: (); /// /// @return {bool} `true` if the context stack contains one of the context IDs, otherwise `false` /// -@function iro-context-stack-contains($stack-id, $context-ids, $check-head-only: false) { - @if not map-has-key($iro-context-stacks, $stack-id) { +@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.'; } - $context-stack: map-get($iro-context-stacks, $stack-id); + $context-stack: map-get($stacks, $stack-id); @if length($context-stack) == 0 { @return false; @@ -247,10 +261,16 @@ $iro-context-stacks: (); /// /// @throw If assertion fails /// -@mixin iro-context-assert-stack-count($stack-id, $max-count) { - @if iro-context-stack-count($stack-id) > $max-count { +@function assert-stack-count($stack-id, $max-count) { + @if count($stack-id) > $max-count { @error 'Maximum context count "#{inspect($max-count)}" exceeded.'; } + + @return null; +} + +@mixin assert-stack-count($stack-id, $max-count) { + $noop: assert-stack-count($stack-id, $max-count); } /// @@ -260,12 +280,12 @@ $iro-context-stacks: (); /// /// @return {number} The number of contexts /// -@function iro-context-stack-count($stack-id) { - @if not map-has-key($iro-context-stacks, $stack-id) { +@function count($stack-id) { + @if not map-has-key($stacks, $stack-id) { @error 'Context stack "#{inspect($stack-id)}" does not exist.'; } - $context-stack: map-get($iro-context-stacks, $stack-id); + $context-stack: map-get($stacks, $stack-id); @return length($context-stack); } @@ -278,12 +298,12 @@ $iro-context-stacks: (); /// /// @return {list} Null if no match was found, otherwise a list with two items: 1. context ID, 2. context data. /// -@function iro-context-get($stack-id, $type-or-level: null) { - @if not map-has-key($iro-context-stacks, $stack-id) { +@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($iro-context-stacks, $stack-id); + $context-stack: map-get($stacks, $stack-id); @if length($context-stack) == 0 { @return null; -- cgit v1.2.3-54-g00ecf