Skip to content

Instantly share code, notes, and snippets.

@chrisburnell
Last active December 1, 2015 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisburnell/890e30941098cd1b945f to your computer and use it in GitHub Desktop.
Save chrisburnell/890e30941098cd1b945f to your computer and use it in GitHub Desktop.
SCSS @mixin for choosing a `font-family` and optional `font-display`
/// @type List
$font-stack-primary: Georgia, serif !default;
/// @type List
$font-stack-secondary: Arial, sans-serif !default;
/// @type List
$font-stack-tertiary: Tahoma, sans-serif !default;
/// @type Map
/// @prop {String} key - Font Family name
/// @prop {Number} value - Font Family value
$font-families: (
"primary": $font-stack-primary,
"secondary": $font-stack-secondary,
"tertiary": $font-stack-tertiary
}
///
/// Specify font-family and associated font-display
///
/// @param {String} $family - Font Family value
/// @param {String} $display - Font Display value
///
/// @require {Variable} $font-families
///
/// @example scss
/// @include font-family(primary);
/// // font-family: Georgia, serif;
///
/// @example scss
/// @include font-family(secondary, swap);
/// // font-family: Arial, sans-serif;
/// // font-display: swap;
///
/// @throw Error if no parameters are passed.
///
@mixin font-family($family, $display) {
@if( length($family) == 0 ) {
@error "font-family() expects at least one parameter."
}
@if not map-has-key($font-families, $family) {
@error "There is no font-family named `#{$family}` in $font-families. Font Family should be one of #{map-keys($font-families)}.";
}
font-family: map-get($font-families, $family);
@if( length($display) != 0 ) {
font-display: $display;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment