Skip to content

Instantly share code, notes, and snippets.

@EvanLovely
Last active January 6, 2018 02:47
Show Gist options
  • Save EvanLovely/91b4e22dc6af816e67e144dc72ce6e45 to your computer and use it in GitHub Desktop.
Save EvanLovely/91b4e22dc6af816e67e144dc72ce6e45 to your computer and use it in GitHub Desktop.

Import Google Fonts Sass Mixin

@include google-font-import($variants: ("300", "400", "400i", "700"), $family: "Source+Sans+Pro");

This would output this CSS:

@import url("//fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700");
/// List to String (like JS's `join`)
/// Converts Sass list to string using `$glue` separator.
/// @param {list} $list
/// @param {string} $glue [''] - What join list items with (ex `,`)
/// @param {bool} $is-nested [false]
/// @return {string}
/// @link http://hugogiraudel.com/2013/08/08/advanced-sass-list-functions/ Source
@function to-string($list, $glue: '', $is-nested: false) {
$result: null;
@for $i from 1 through length($list) {
$e: nth($list, $i);
@if type-of($e) == list {
$result: $result#{to-string($e, $glue, true)};
}
@else {
$result: if($i != length($list) or $is-nested, $result#{$e}#{$glue}, $result#{$e});
}
}
@return $result;
}
/// Import Google Font
/// @param {list} $variants - Font Weight and Style variants (i.e. `("300", "400", "400i")`) - `i` = italic
/// @param {string} $family - Font Family with spaces as `+` (i.e. `Source+Sans+Pro`)
/// @output `@import()` statement
/// @example scss
/// @include google-font-import($variants: ("300", "400", "400i", "700"), $family: "Source+Sans+Pro");
/// // would output `@import url("//fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700");`
@mixin google-font-import($variants, $family) {
@import url('//fonts.googleapis.com/css?family=#{$family}:#{to-string($variants, ",")}');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment