I'm trying to make a horizontal rainbow stripe background gradient mixin, but I feel like this is way too verbose. How can it be better?
Goals:
// You could set individual variables for each color as well.
// You would still pass them all as a single argument,
// or join them into a single variable before passing, as you see fit.
$mycolors: red orange yellow green blue indigo violet;
// Returns a striped gradient for use anywhere gradients are accepted.
// - $position: the starting position or angle of the gradient.
// - $colors: a list of all the colors to be used.
@function rainbow($position, $colors) {
$colors: if(type-of($colors) != 'list', compact($colors), $colors);
$gradient: compact();
$width: 100% / length($colors);
@for $i from 1 through length($colors) {
$pop: nth($colors,$i);
$new: $pop ($width * ($i - 1)), $pop ($width * $i);
$gradient: join($gradient, $new, comma);
}
@return linear-gradient($position, $gradient);
}
.rainbow {
@include background-image(rainbow(left, $colors));
}
$list:
(
theme1: (color: red, background: green, font: (family: Arial, size: 20px)),
theme2: (color: black, background: white, font: (family: Verdana, size: 30px))
);
$oldList:
(
1: (red, blue),
2: (brown, gold)
);
@each $index, $row in $oldList
{
// $color: map-get($row, color) !global;
// $background: map-get($row, background) !global;
// $font: map-get($row, font);
// $fontFamily: map-get($font, family);
.theme-#{$index}
{
$c1: nth($row, 1);
$c2: nth($row, 2);
// font-family: $fontFamily;
color: $c1;
background: $c2;
}
}
/*! 1 zpusob pres lists a funkci nth */
.success {
background-image: url(../images/success.png);
}
.error {
background-image: url(../images/error.png);
}
/* 2 zpusob pomoci tzv. multiple assignment */
.puma-icon {
background-image: url("/images/puma.png");
}
.sea-slug-icon {
background-image: url("/images/sea-slug.png");
}
.egret-icon {
background-image: url("/images/egret.png");
}
$placeholders: '-webkit-input-placeholder',
'-moz-placeholder',
'-ms-input-placeholder';
@mixin placeholder {
@each $placeholder in $placeholders {
@if $placeholder == "-webkit-input-placeholder" {
&::#{$placeholder} {
@content;
}
}
@else if $placeholder == "-moz-placeholder" {
// FF 18-
&:#{$placeholder} {
@content;
}
// FF 19+
&::#{$placeholder} {
@content;
}
}
@else {
&:#{$placeholder} {
@content;
}
}
}
}
// _mixins.scss
@mixin module($color: grey, $duration: 0s, $border: null) {
color: $color;
transition: $duration;
border: $border;
}
// _component.scss
.element {
@include module((
duration: .15s,
color: pink
)...);
}
// _mixins.scss
@mixin module2($options: ()) {
$configuration: map-merge((
color: grey,
duration: 0s,
border: null
), $options);
color: map-get($configuration, color);
transition: map-get($configuration, duration);
border: map-get($configuration, border);
}
// _component.scss
.element {
@include module2((
color: pink,
duration: .15s
));
}
// http://www.sitepoint.com/using-sass-maps/