Skip to content

Commit

Permalink
feat: implement googleFont addon
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 18, 2018
1 parent 7fe0952 commit 9083919
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions addon/googleFont.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

function createUrl (font, weights, subsets) {
var params = 'family=' + encodeURIComponent(font);

if (weights) {
if (!(weights instanceof Array))
weights = [weights];

params += ':' + weights.join(',');
}

if (subsets) {
if (!(subsets instanceof Array))
subsets = [subsets];

params += '&subset=' + subsets.join(',');
}

return 'https://fonts.googleapis.com/css' + params;
}

exports.addon = function (renderer) {
if (process.env.NODE_ENV !== 'production') {
require('./__dev__/warnOnMissingDependencies')('hydrate', renderer, ['put']);
}

if (renderer.client) {
renderer.googleFont = function (font, weights, subsets) {
var el = document.createElement('link');

el.href = createUrl(font, weights, subsets);
el.rel = 'stylesheet';
el.type = 'text/css';

document.head.appendChild(el);
};
} else {
renderer.googleFont = function (font, weights, subsets) {
renderer.raw = "@import url('" + createUrl(font, weights, subsets) + "');" + renderer.raw;
};
}
};

0 comments on commit 9083919

Please sign in to comment.