Skip to content

Commit

Permalink
Font Loading Strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
sherakama committed Apr 7, 2017
1 parent 31277b4 commit b374a4c
Show file tree
Hide file tree
Showing 42 changed files with 2,060 additions and 2,318 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = function(grunt) {
sourceMap: true,
// This controls the compiled css and can be changed to nested, compact or compressed.
outputStyle: 'expanded',
precision: 10
precision: 10,
},
dist: {
files: {
Expand Down
30 changes: 15 additions & 15 deletions core/base/_fonts.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
/// @group base
////

// @import '../utilities/settings/settings';
/// Default fonts
///
$decanter-default-fonts: (
'mono': (
$monospace-source,
),
'sans': (
$sans-serif-source,
),
'serif': (
$serif-source,
)
) !default;

/// Import declared monospace font
@if(global-variable-exists(monospace-source)) {
@import url($monospace-source);
}

/// Import declared Sans Serif font
@if(global-variable-exists(sans-serif-source)) {
@import url($sans-serif-source);
}

/// Import declared Serif font
@if(global-variable-exists(serif-source)) {
@import url($serif-source);
}
//// Load the font-face declarations.
@include load-fonts($decanter-default-fonts);
4 changes: 2 additions & 2 deletions core/utilities/functions/_decanter-functions.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
@import
'color',
'unitconversion',
'layout/set-container-padding',
'layout/adjust-container-margin';
'layout/set-container-padding',
'layout/adjust-container-margin';
1 change: 1 addition & 0 deletions core/utilities/mixins/_decanter-mixins.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
, 'material-shadow'
, 'font-size'
, 'on-event'
, 'load-fonts'
;
72 changes: 72 additions & 0 deletions core/utilities/mixins/_load-fonts.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
@charset 'UTF-8';

////
/// Font loading function.
/// @group functions
////

/// Global default fonts.
$fonts: () !default;

/// Keeps track of already loaded fonts.
$loaded-fonts: ();

/// Load a collection of @font-face fonts
///
@mixin load-fonts($load: $fonts) {

// Lists.
@if type-of($load) == list {
// Loop through each item in the passed in list.
@each $_item in $load {
// Cast the item to a string to be sure we don't pass
// strange variables to the loader.
$_item_string: #{$_item};
@if (_is-loaded_font($_item_string) == false) {
$_tmp: _log-loaded-font($_item_string);
$_item_string: quote($_item_string);
@import url($_item_string);
}
@else {
@debug "Font: #{$_item_string} already loaded.";
}
}
}

// Maps.
@if type-of($load) == map {
// Loop through each item in the passed in map.
@each $_index, $_item in $load {
@each $_font in $_item {
// Cast the item to a string to be sure we don't pass
// strange variables to the loader.
$_item_string: #{$_font};
@if (_is-loaded_font($_item_string) == false) {
$_tmp: _log-loaded-font($_item_string);
$_item_string: quote($_item_string);
@import url($_item_string);
}
@else {
@debug "Font: #{$_item_string} already loaded.";
}
}
}
}
}

/// Internal function to keep track of loaded fonts
/// so we don't duplicate calls.
@function _log-loaded-font($url) {
$loaded-fonts: append($loaded-fonts, $url, comma) !global;
@return true;
}


/// Checks the global variable for loaded fonts
@function _is-loaded_font($url) {
$_value: index($loaded-fonts, $url);
@if type-of($_value) == number {
@return true;
}
@return false;
}
114 changes: 52 additions & 62 deletions examples/css/index.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b374a4c

Please # to comment.