From dc73e0a578d600ee2a20bb87cc9b23112dcd89b4 Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Fri, 4 Aug 2017 09:19:17 -0400 Subject: [PATCH] Add extract-match mixin Works similar to apply-match but exports the @__match variable to the parent scope instead of accepting a ruleset. I have a suspicion that this will generally be easier to wield and produce more predictable results than all the crazy extra scoping bullshit I always have to try and do when using the "closure"-ish approach I use with apply-match. --- src/generators/_matching.less | 14 ++++++++++++++ src/utilities/misc.less | 9 ++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/generators/_matching.less b/src/generators/_matching.less index 9de841308..cea975260 100644 --- a/src/generators/_matching.less +++ b/src/generators/_matching.less @@ -14,3 +14,17 @@ @ruleset(); } } + +.extract-match(@map; @search) { + @current: extract(extract(@map, 1), 1); + .extract-match(@map; @search; @current; 1); +} + +.extract-match(@map; @search; @current; @i) when not (@search = @current) and (@i <= length(@map)) { + @current: extract(extract(@map, @i + 1), 1); + .extract-match(@map; @search; @current; @i + 1); +} + +.extract-match(@map; @search; @current; @i) when (@search = @current) and (@i <= length(@map)) { + @__match: extract(extract(@map, @i), 2); +} diff --git a/src/utilities/misc.less b/src/utilities/misc.less index 55a06a7f9..861b4e3a8 100644 --- a/src/utilities/misc.less +++ b/src/utilities/misc.less @@ -16,11 +16,10 @@ } .screen(@screen, @ruleset) { - .apply-match(@screens; @screen; { - @media (min-width: @__match) { - @ruleset(); - } - }); + .extract-match(@screens; @screen); + @media (min-width: @__match) { + @ruleset(); + } } .no-focus {