@@ -4,104 +4,74 @@ Object.defineProperty(exports, "__esModule", {
44 value : true
55} ) ;
66
7- var _createClass = function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ( ) ;
7+ var _slicedToArray = function ( ) { function sliceIterator ( arr , i ) { var _arr = [ ] ; var _n = true ; var _d = false ; var _e = undefined ; try { for ( var _i = arr [ Symbol . iterator ] ( ) , _s ; ! ( _n = ( _s = _i . next ( ) ) . done ) ; _n = true ) { _arr . push ( _s . value ) ; if ( i && _arr . length === i ) break ; } } catch ( err ) { _d = true ; _e = err ; } finally { try { if ( ! _n && _i [ "return" ] ) _i [ "return" ] ( ) ; } finally { if ( _d ) throw _e ; } } return _arr ; } return function ( arr , i ) { if ( Array . isArray ( arr ) ) { return arr ; } else if ( Symbol . iterator in Object ( arr ) ) { return sliceIterator ( arr , i ) ; } else { throw new TypeError ( "Invalid attempt to destructure non-iterable instance" ) ; } } ; } ( ) ;
88
99var _react = require ( 'react' ) ;
1010
11- var _react2 = _interopRequireDefault ( _react ) ;
12-
1311var _propTypes = require ( 'prop-types' ) ;
1412
1513var _propTypes2 = _interopRequireDefault ( _propTypes ) ;
1614
1715function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
1816
19- function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
20-
21- function _possibleConstructorReturn ( self , call ) { if ( ! self ) { throw new ReferenceError ( "this hasn't been initialised - super() hasn't been called" ) ; } return call && ( typeof call === "object" || typeof call === "function" ) ? call : self ; }
22-
23- function _inherits ( subClass , superClass ) { if ( typeof superClass !== "function" && superClass !== null ) { throw new TypeError ( "Super expression must either be null or a function, not " + typeof superClass ) ; } subClass . prototype = Object . create ( superClass && superClass . prototype , { constructor : { value : subClass , enumerable : false , writable : true , configurable : true } } ) ; if ( superClass ) Object . setPrototypeOf ? Object . setPrototypeOf ( subClass , superClass ) : subClass . __proto__ = superClass ; }
24-
25- var GoogleFontLoader = function ( _React$PureComponent ) {
26- _inherits ( GoogleFontLoader , _React$PureComponent ) ;
27-
28- function GoogleFontLoader ( ) {
29- var _ref ;
17+ 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 ) ; } }
3018
31- var _temp , _this , _ret ;
19+ var createLink = function createLink ( fonts , subsets , display ) {
20+ var families = fonts . reduce ( function ( acc , font ) {
21+ var family = font . font . replace ( / + / g, '+' ) ;
22+ var weights = ( font . weights || [ ] ) . join ( ',' ) ;
3223
33- _classCallCheck ( this , GoogleFontLoader ) ;
24+ return [ ] . concat ( _toConsumableArray ( acc ) , [ family + ( weights && ':' + weights ) ] ) ;
25+ } , [ ] ) . join ( '|' ) ;
3426
35- for ( var _len = arguments . length , args = Array ( _len ) , _key = 0 ; _key < _len ; _key ++ ) {
36- args [ _key ] = arguments [ _key ] ;
37- }
27+ var link = document . createElement ( 'link' ) ;
28+ link . rel = 'stylesheet' ;
29+ link . href = 'https://fonts.googleapis.com/css?family=' + families ;
3830
39- return _ret = ( _temp = ( _this = _possibleConstructorReturn ( this , ( _ref = GoogleFontLoader . __proto__ || Object . getPrototypeOf ( GoogleFontLoader ) ) . call . apply ( _ref , [ this ] . concat ( args ) ) ) , _this ) , _this . link = null , _this . createLink = function ( ) {
40- var _this$props = _this . props ,
41- fonts = _this$props . fonts ,
42- subsets = _this$props . subsets ;
31+ if ( subsets && Array . isArray ( subsets ) && subsets . length > 0 ) {
32+ link . href += '&subset=' + subsets . join ( ',' ) ;
33+ }
4334
35+ if ( display ) {
36+ link . href += '&display=' + display ;
37+ }
4438
45- var families = fonts . reduce ( function ( acc , font ) {
46- var family = font . font . replace ( / + / g, '+' ) ;
47- var weights = ( font . weights || [ ] ) . join ( ',' ) ;
39+ return link ;
40+ } ;
4841
49- acc . push ( family + ( weights && ':' + weights ) ) ;
42+ var GoogleFontLoader = function GoogleFontLoader ( _ref ) {
43+ var fonts = _ref . fonts ,
44+ subsets = _ref . subsets ,
45+ _ref$display = _ref . display ,
46+ display = _ref$display === undefined ? null : _ref$display ;
5047
51- return acc ;
52- } , [ ] ) . join ( '|' ) ;
48+ var _useState = ( 0 , _react . useState ) ( createLink ( fonts , subsets , display ) ) ,
49+ _useState2 = _slicedToArray ( _useState , 2 ) ,
50+ link = _useState2 [ 0 ] ,
51+ setLink = _useState2 [ 1 ] ;
5352
54- var link = document . createElement ( 'link' ) ;
55- link . rel = 'stylesheet' ;
56- link . href = 'https://fonts.googleapis.com/css?family=' + families ;
53+ ( 0 , _react . useEffect ) ( function ( ) {
54+ document . head . appendChild ( link ) ;
5755
58- if ( subsets && Array . isArray ( subsets ) && subsets . length > 0 ) {
59- link . href += '&subset=' + subsets . join ( ',' ) ;
60- }
56+ return function ( ) {
57+ return document . head . removeChild ( link ) ;
58+ } ;
59+ } , [ link ] ) ;
6160
62- return link ;
63- } , _this . appendLink = function ( ) {
64- return document . head . appendChild ( _this . link ) ;
65- } , _this . removeLink = function ( ) {
66- return document . head . removeChild ( _this . link ) ;
67- } , _this . replaceLink = function ( ) {
68- _this . removeLink ( ) ;
69- _this . link = _this . createLink ( ) ;
70- _this . appendLink ( ) ;
71- } , _this . render = function ( ) {
72- return null ;
73- } , _temp ) , _possibleConstructorReturn ( _this , _ret ) ;
74- }
61+ ( 0 , _react . useEffect ) ( function ( ) {
62+ setLink ( createLink ( fonts , subsets , display ) ) ;
63+ } , [ fonts , subsets , display ] ) ;
7564
76- _createClass ( GoogleFontLoader , [ {
77- key : 'componentDidMount' ,
78- value : function componentDidMount ( ) {
79- this . link = this . createLink ( ) ;
80- this . appendLink ( ) ;
81- }
82- } , {
83- key : 'componentDidUpdate' ,
84- value : function componentDidUpdate ( prevProps ) {
85- if ( JSON . stringify ( prevProps . fonts ) !== JSON . stringify ( this . props . fonts ) ) {
86- this . replaceLink ( ) ;
87- }
88- }
89- } , {
90- key : 'componentWillUnmount' ,
91- value : function componentWillUnmount ( ) {
92- this . removeLink ( ) ;
93- }
94- } ] ) ;
95-
96- return GoogleFontLoader ;
97- } ( _react2 . default . PureComponent ) ;
65+ return null ;
66+ } ;
9867
9968GoogleFontLoader . propTypes = {
10069 fonts : _propTypes2 . default . arrayOf ( _propTypes2 . default . shape ( {
10170 font : _propTypes2 . default . string . isRequired ,
10271 weights : _propTypes2 . default . arrayOf ( _propTypes2 . default . oneOfType ( [ _propTypes2 . default . string , _propTypes2 . default . number ] ) )
10372 } ) ) . isRequired ,
104- subsets : _propTypes2 . default . arrayOf ( _propTypes2 . default . string )
73+ subsets : _propTypes2 . default . arrayOf ( _propTypes2 . default . string ) ,
74+ display : _propTypes2 . default . string
10575} ;
10676
10777exports . default = GoogleFontLoader ;
0 commit comments