Skip to content

Instantly share code, notes, and snippets.

@pascalduez
Last active June 28, 2017 20:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pascalduez/10003922 to your computer and use it in GitHub Desktop.
Save pascalduez/10003922 to your computer and use it in GitHub Desktop.
Sass IE filters helper.
// ----
// Sass (v3.3.8)
// Compass (v1.0.0.alpha.18)
// ----
//
// Sass IE filters helper.
//
// Join `$list` into a string.
// -----------------------------------------------------------------------------
// @param [list] $list
// @param [string] $glue
// -----------------------------------------------------------------------------
// @return [string]
@function _join($list, $glue: "") {
$result: "";
$length: length($list);
@if $length < 1 {
@return $result;
}
@for $i from 1 through $length {
$result: $result
+ nth($list, $i)
+ if($i != $length, $glue, "");
}
@return $result;
}
// Capitalize string
// --------------------------------------------------------------------------------
// @param [string] $string
// --------------------------------------------------------------------------------
// @return [string]
@function capitalize($string) {
@return to-upper-case(str-slice($string, 1, 1)) + str-slice($string, 2);
}
// IE filter formatting helper
// -----------------------------------------------------------------------------
// @param [filter] $string: filter name
// @param [arglist] $args: filter properties
// -----------------------------------------------------------------------------
// @dependencies `_join()`, `capitalize()`
// -----------------------------------------------------------------------------
// @return [string]
@function _ie-filter(
$filter,
$args...
) {
$properties: ();
$parts: (
"progid:DXImageTransform.Microsoft.",
"#{capitalize($filter)}",
"(",
"properties",
")"
);
@each $key, $value in keywords($args) {
@if type-of($value) == color {
$value: "'#{ie-hex-str($value)}'";
}
$properties: append($properties, "#{$key}=#{$value}");
}
$parts: set-nth(
$parts,
index($parts, "properties"),
_join($properties, ", ")
);
@return _join($parts);
}
@mixin ie-filter($filter, $args...) {
-ms-filter: quote(_ie-filter($filter, $args...)); // IE 8+
filter: unquote(_ie-filter($filter, $args...)); // IE < 8
}
// Test
sass {
@include ie-filter(Light);
@include ie-filter(Blur, $pixelRadius: 5);
@include ie-filter(DropShadow, $OffX: 5px, $OffY: 5px, $Color: rgba(0, 0, 0, .5));
}
sass {
-ms-filter: "progid:DXImageTransform.Microsoft.Light()";
filter: progid:DXImageTransform.Microsoft.Light();
-ms-filter: "progid:DXImageTransform.Microsoft.Blur(pixelRadius=5)";
filter: progid:DXImageTransform.Microsoft.Blur(pixelRadius=5);
-ms-filter: "progid:DXImageTransform.Microsoft.DropShadow(OffX=5px, OffY=5px, Color='#80000000')";
filter: progid:DXImageTransform.Microsoft.DropShadow(OffX=5px, OffY=5px, Color='#80000000');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment