Skip to content

Instantly share code, notes, and snippets.

@JawsomeJason
Created September 15, 2016 00:48
Show Gist options
  • Save JawsomeJason/c189aeb8c48ce0c2af8b1fe5f83d91f6 to your computer and use it in GitHub Desktop.
Save JawsomeJason/c189aeb8c48ce0c2af8b1fe5f83d91f6 to your computer and use it in GitHub Desktop.
Handoff sets of Sass variables to mixin's content block
// handoff mixin variables for use inside content
$batons: () !default;
/// get the last set of variables handed-off
/// @param $baton=() map of variables
/// @example calling from inside a handoff-enabled mixin with content block
/// @mixin foo($foo, $free) {
///
/// // send handoff right before `@content`
/// @include handoff((
/// foo: bar,
/// free: beer
/// ));
/// @content
/// }
///
/// @include foo() {
/// // get handoff first thing
/// $handoff: handoff();
///
/// foo: map-get($handoff, foo);
/// free: map-get($handoff, beer);
/// }
@function handoff($baton) {
// pop the last set of variables and return and return
@if($baton == null) {
$batons-except-last: ();
@for $i from 1 through length($batons) {
@if $i == length($handoff) {
$last: nth($handoff, length($handoff));
}
@else {
$batons-except-last: append($batons-except-last, nth($batons, $i));
}
}
$batons: $batons-except-last !global;
@return $last;
}
// add to relay list
@if type-of($baton) == map {
$batons: append($batons, $baton) !global;
}
@else {
@error 'handoff parameter must be a map';
}
}
@mixin handoff($baton) {
@if type-of($baton) != map {
@error 'handoff parameter must be a map';
}
$_: handoff($baton);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment