sass-extras
Useful utilities for working with Sass
It includes functions for using SVG inline, generating random colors, system font stack, PRNG, and more…
Install
npm install sass-extras
Supported browsers
Latest Chrome, Firefox, and Safari.
Usage
@import './node_modules/sass-extras/index';
Or with sass-loader
:
@import '~sass-extras/index';
Docs
Related
- react-extras - Useful components and utilities for working with React
- modern-normalize - Normalize browsers' default style
Color
functions
tint
@function tint($color, $percentage) { ... }
Description
Lighten a color by mixing it with white.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$color | Color to lighten | Color | — none |
$percentage | Amount of white color to mix in | Number | — none |
Returns
Color
Example
a {
color: tint(pink, 10%);
}
Output CSS
a {
color: #ffc6d0;
}
shade
@function shade($color, $percentage) { ... }
Description
Darken a color by mixing it with black.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$color | Color to darken | Color | — none |
$percentage | Amount of black color to mix in | Number | — none |
Returns
Color
Example
a {
color: shade(pink, 10%);
}
Output CSS
a {
color: #e6adb7;
}
black
@function black($opacity) { ... }
Description
Get the color black with a given $opacity
.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$opacity | Opacity percentage | Number | — none |
Returns
Color
Example
a {
color: black(10%);
}
Output CSS
a {
color: rgba(0, 0, 0, 0.1);
}
white
@function white($opacity) { ... }
Description
Get the color white with a given $opacity
.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$opacity | Opacity percentage | Number | — none |
Returns
Color
Example
a {
color: white(10%);
}
Output CSS
a {
color: rgba(255, 255, 255, 0.1);
}
List
functions
list-first
@function list-first($list) { ... }
Description
Get the first element of $list
.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$list | — none | List | — none |
Returns
Example
@debug list-first((1, 2, 3));
//=> 1
list-last
@function list-last($list) { ... }
Description
Get the last element of $list
.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$list | — none | List | — none |
Returns
Example
@debug list-last((1, 2, 3));
//=> 3
Number
functions
strip-unit
@function strip-unit($number) { ... }
Description
Strip the unit from a number.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$number | — none | Number | — none |
Returns
Number
Example
@debug strip-unit(5px);
//=> 5
number-clamp
@function number-clamp($number, $min, $max) { ... }
Description
Clamp $number
between $min
and $max
.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$number | Number to clamp | Number | — none |
$min | Minimum number | Number | — none |
$max | Maximum number | Number | — none |
Returns
Number
Example
@debug math-clamp(1, 2, 4);
//=> 2
number-invert
@function number-invert($number) { ... }
Description
Invert a number.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$number | Number to invert | Number | — none |
Returns
Number
Example
@debug number-invert(5);
//=> -5
Random
functions
seed-random-integer
@function seed-random-integer($seed) { ... }
Description
Generate a pseudorandom integer. Uses the Park-Miller
algorithm.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$seed | The same | Number | — none |
Returns
Number
Example
@debug seed-random-integer(5);
//=> 84035
Used by
- [function]
seed-random-float
- [function]
seed-random-boolean
seed-random-float
@function seed-random-float($seed) { ... }
Description
Generate a pseudorandom float. Uses the Park-Miller
algorithm.
Note: You'll need to increase the default Sass precision to at least 10 for this method to be useful.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$seed | The same | Number | — none |
Returns
Number
Example
@debug seed-random-float(5);
//=> 0.0000391314
Requires
- [function]
seed-random-integer
seed-random-boolean
@function seed-random-boolean($seed) { ... }
Description
Generate a pseudorandom boolean. Uses the Park-Miller
algorithm.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$seed | The same | Number | — none |
Returns
Boolean
Example
@debug seed-random-boolean(5);
//=> false
Requires
- [function]
seed-random-integer
random-color
@function random-color($saturation: 0.5, $lightness: 0.5) { ... }
Description
Get a random aesthetically pleasing color.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$saturation | Color saturation in the range | Number | 0.5 |
$lightness | Color lightness in the range | Number | 0.5 |
Returns
Color
Example
.foo {
background-color: random-color();
}
Output CSS
.foo {
background-color: random-color();
}
String
functions
string-contains
@function string-contains($string, $substring) { ... }
Description
Check if $string
contains the given $substring
.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$string | String to check | String | — none |
$substring | Substring to search for in | String | — none |
Returns
Boolean
Example
@debug string-contains('foo bar baz', $substring: 'bar');
//=> true
string-starts-with
@function string-starts-with($string, $substring) { ... }
Description
Check if $string
starts with the given $substring
.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$string | String to check | String | — none |
$substring | Substring to search for at the start of | String | — none |
Returns
Boolean
Example
@debug string-starts-with('foo bar', $substring: 'foo');
//=> true
string-ends-with
@function string-ends-with($string, $substring) { ... }
Description
Check if $string
ends with the given $substring
.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$string | String to check | String | — none |
$substring | Substring to search for at the end of | String | — none |
Returns
Boolean
Example
@debug string-ends-with('foo bar', $substring: 'bar');
//=> true
string-replace
@function string-replace($string, $search, $replacement) { ... }
Description
Replace substring $search
in $string
with $replacement
.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$string | String to search | String | — none |
$search | Substring to search for in | String | — none |
$replacement | Replacement for | String | — none |
Returns
String
Example
@debug string-replace('foo bar baz', $search: 'bar', $replacement: 'unicorn');
//=> 'foo unicorn baz'
Used by
- [function]
url-encode
- [function]
svg-url
General
functions
url-encode
@function url-encode($string) { ... }
Description
Encode URL-unsafe characters in $string
.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$string | — none | String | — none |
Returns
String
Example
@debug url-encode('#foo@bar');
//=> '%23foo%40bar'
Requires
- [function]
string-replace
Used by
- [function]
svg-url
svg-url
@function svg-url($svg) { ... }
Description
Use SVG anywhere a url()
is accepted, like in a background
property.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$svg | SVG string. The | String | — none |
Example
body {
background: svg-url('<svg>…</svg>');
}
Requires
- [function]
string-replace
- [function]
url-encode
Used by
- [mixin]
background-svg
mixins
background-svg
@mixin background-svg($svg) { ... }
Description
Set SVG as background-image
.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$svg | SVG string. The | String | — none |
Example
body {
@include background-svg('<svg>…</svg>');
}
Requires
- [function]
svg-url
context
@mixin context($changed, $to) { ... }
Description
Include a block with a changed context.
Parameters
parameter Name | parameter Description | parameter Type | parameter Default value |
---|---|---|---|
$changed | Old parent selector. Any parent. | String | — none |
$to | New parent selector. Any parent. | String | — none |
Example
.tabs {
.tab {
background: pink;
.icon {
color: gray;
@include context($changed: '.tab', $to: '.tab:hover') {
color: blue;
}
}
}
}
Output CSS
.tabs .tab {
background: pink;
}
.tabs .tab .icon {
color: gray;
}
.tabs .tab:hover .icon {
color: blue;
}
variables
system-fonts
$system-fonts: 'system-ui',
'-apple-system',
'Segoe UI',
'Roboto',
'Helvetica',
'Arial',
'sans-serif',
'Apple Color Emoji',
'Segoe UI Emoji' !default;
Description
System font stack.
Type
List
Example
body {
font-family: $system-fonts;
}