Skip to content

Add a prop for a custom document (good for iframes). #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface GoogleFontLoaderProps {
fonts: Font[];
subsets?: string[];
display?: 'auto' | 'block' | 'swap' | 'fallback' | 'optional';
contentDocument?: object;
}

declare const GoogleFontLoader: React.FC<GoogleFontLoaderProps>;
Expand Down
19 changes: 11 additions & 8 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

var createLink = function createLink(fonts, subsets, display) {
var createLink = function createLink(fonts, subsets, display, contentDocument) {
var families = fonts.reduce(function (acc, font) {
var family = font.font.replace(/ +/g, '+');
var weights = (font.weights || []).join(',');

return [].concat(_toConsumableArray(acc), [family + (weights && ':' + weights)]);
}, []).join('|');

var link = document.createElement('link');
var link = contentDocument.createElement('link');
link.rel = 'stylesheet';
link.href = 'https://fonts.googleapis.com/css?family=' + families;

Expand All @@ -43,23 +43,25 @@ var GoogleFontLoader = function GoogleFontLoader(_ref) {
var fonts = _ref.fonts,
subsets = _ref.subsets,
_ref$display = _ref.display,
display = _ref$display === undefined ? null : _ref$display;
display = _ref$display === undefined ? null : _ref$display,
_ref$contentDocument = _ref.contentDocument,
contentDocument = _ref$contentDocument === undefined ? document : _ref$contentDocument;

var _useState = (0, _react.useState)(createLink(fonts, subsets, display)),
var _useState = (0, _react.useState)(createLink(fonts, subsets, display, contentDocument)),
_useState2 = _slicedToArray(_useState, 2),
link = _useState2[0],
setLink = _useState2[1];

(0, _react.useEffect)(function () {
document.head.appendChild(link);
contentDocument.head.appendChild(link);

return function () {
return document.head.removeChild(link);
return contentDocument.head.removeChild(link);
};
}, [link]);

(0, _react.useEffect)(function () {
setLink(createLink(fonts, subsets, display));
setLink(createLink(fonts, subsets, display, contentDocument));
}, [fonts, subsets, display]);

return null;
Expand All @@ -71,7 +73,8 @@ GoogleFontLoader.propTypes = {
weights: _propTypes2.default.arrayOf(_propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number]))
})).isRequired,
subsets: _propTypes2.default.arrayOf(_propTypes2.default.string),
display: _propTypes2.default.string
display: _propTypes2.default.string,
contentDocument: _propTypes2.default.object
};

exports.default = GoogleFontLoader;
Loading