Skip to content

Commit

Permalink
Font Loading Strategy (#43)
Browse files Browse the repository at this point in the history
* Font Loading Strategy

* default the loaded fonts (in case we load multiple instances)
check for “not true” instead of “false” (default or un-instanced states can be true, false, null, default or a string). I specifically want to check for the absence of a “true” boolean setting)
add a case for when we are passing this mixin a straight string or variable.

Separate internal functions into stand-alone files (shea, we may need some discussion if a component contains variables, strings and functions, maybe we can instance this inside a parent folder that has it’s own structure?

flatten out the list
  • Loading branch information
sherakama authored and kgcreative committed Apr 8, 2017
1 parent 31277b4 commit 6a4d951
Show file tree
Hide file tree
Showing 44 changed files with 2,071 additions and 2,320 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
24 changes: 9 additions & 15 deletions core/base/_fonts.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@
/// @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);
9 changes: 5 additions & 4 deletions core/utilities/functions/_decanter-functions.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
////

@import
'color',
'unitconversion',
'layout/set-container-padding',
'layout/adjust-container-margin';
'color'
, 'unitconversion'
, 'font/log-loaded-font'
, 'layout/set-container-padding'
, 'layout/adjust-container-margin';
11 changes: 11 additions & 0 deletions core/utilities/functions/fonts/_check-loaded-font.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@charset "UTF-8";

/// @access private
/// Checks the global variable for loaded fonts
@function _check-loaded-font($url) {
$_value: index($loaded-fonts, $url);
@if type-of($_value) == number {
@return true;
}
@return false;
}
9 changes: 9 additions & 0 deletions core/utilities/functions/fonts/_log-loaded-font.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@charset "UTF-8";

/// @access private
/// 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;
}
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'
;
66 changes: 66 additions & 0 deletions core/utilities/mixins/_load-fonts.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
@charset 'UTF-8';

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

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

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

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

// String
@if type-of($load) == string {
// Cast the item to a string to be sure we don't pass
// strange variables to the loader.
$_item_string: '#{$load}';
@if (_is-loaded_font($_item_string) != true) {
$_tmp: _log-loaded-font($_item_string);
$_item_string: quote($_item_string);
@import url($_item_string);
}
@else {
@debug "Font: #{$_item_string} already loaded.";
}
// List
} @else 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) != true) {
$_tmp: _log-loaded-font($_item_string);
$_item_string: quote($_item_string);
@import url($_item_string);
}
@else {
@debug "Font: #{$_item_string} already loaded.";
}
}
// Map
} @else 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) != true) {
$_tmp: _log-loaded-font($_item_string);
$_item_string: quote($_item_string);
@import url($_item_string);
}
@else {
@debug "Font: #{$_item_string} already loaded.";
}
}
}
}
}
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 6a4d951

Please sign in to comment.