@@ -4,104 +4,128 @@ Object.defineProperty(exports, "__esModule", {
4
4
value : true
5
5
} ) ;
6
6
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" ) ; } } ; } ( ) ;
8
8
9
9
var _react = require ( 'react' ) ;
10
10
11
- var _react2 = _interopRequireDefault ( _react ) ;
12
-
13
11
var _propTypes = require ( 'prop-types' ) ;
14
12
15
13
var _propTypes2 = _interopRequireDefault ( _propTypes ) ;
16
14
17
15
function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
18
16
19
- function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
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 ) ; } }
20
18
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 ; }
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 ( ',' ) ;
22
23
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
+ return [ ] . concat ( _toConsumableArray ( acc ) , [ family + ( weights && ':' + weights ) ] ) ;
25
+ } , [ ] ) . join ( '|' ) ;
24
26
25
- var GoogleFontLoader = function ( _React$PureComponent ) {
26
- _inherits ( GoogleFontLoader , _React$PureComponent ) ;
27
+ var link = document . createElement ( 'link' ) ;
28
+ link . rel = 'stylesheet' ;
29
+ link . href = 'https://fonts.googleapis.com/css?family=' + families ;
27
30
28
- function GoogleFontLoader ( ) {
29
- var _ref ;
31
+ if ( subsets && Array . isArray ( subsets ) && subsets . length > 0 ) {
32
+ link . href += '&subset=' + subsets . join ( ',' ) ;
33
+ }
30
34
31
- var _temp , _this , _ret ;
35
+ if ( display ) {
36
+ link . href += '&display=' + display ;
37
+ }
32
38
33
- _classCallCheck ( this , GoogleFontLoader ) ;
39
+ return link ;
40
+ } ;
34
41
35
- for ( var _len = arguments . length , args = Array ( _len ) , _key = 0 ; _key < _len ; _key ++ ) {
36
- args [ _key ] = arguments [ _key ] ;
37
- }
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 ;
38
47
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 ;
48
+ var _useState = ( 0 , _react . useState ) ( createLink ( fonts , subsets , display ) ) ,
49
+ _useState2 = _slicedToArray ( _useState , 2 ) ,
50
+ link = _useState2 [ 0 ] ,
51
+ setLink = _useState2 [ 1 ] ;
43
52
53
+ ( 0 , _react . useEffect ) ( function ( ) {
54
+ document . head . appendChild ( link ) ;
44
55
45
- var families = fonts . reduce ( function ( acc , font ) {
46
- var family = font . font . replace ( / + / g, '+' ) ;
47
- var weights = ( font . weights || [ ] ) . join ( ',' ) ;
56
+ return function ( ) {
57
+ return document . head . removeChild ( link ) ;
58
+ } ;
59
+ } , [ link ] ) ;
48
60
49
- acc . push ( family + ( weights && ':' + weights ) ) ;
61
+ ( 0 , _react . useEffect ) ( function ( ) {
62
+ setLink ( createLink ( fonts , subsets , display ) ) ;
63
+ } , [ fonts , subsets , display ] ) ;
50
64
51
- return acc ;
52
- } , [ ] ) . join ( '|' ) ;
65
+ return null ;
66
+ } ;
53
67
54
- var link = document . createElement ( 'link' ) ;
55
- link . rel = 'stylesheet' ;
56
- link . href = 'https://fonts.googleapis.com/css?family=' + families ;
68
+ // class GoogleFontLoader extends React.PureComponent {
69
+ // link = null;
57
70
58
- if ( subsets && Array . isArray ( subsets ) && subsets . length > 0 ) {
59
- link . href += '&subset=' + subsets . join ( ',' ) ;
60
- }
71
+ // createLink = () => {
72
+ // const { fonts, subsets } = this.props;
61
73
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
- }
74
+ // const families = fonts.reduce((acc, font) => {
75
+ // const family = font.font.replace(/ +/g, '+');
76
+ // const weights = (font.weights || []).join(',');
77
+
78
+ // acc.push(family + (weights && `:${weights}`));
79
+
80
+ // return acc;
81
+ // }, []).join('|');
82
+
83
+ // const link = document.createElement('link');
84
+ // link.rel = 'stylesheet';
85
+ // link.href = `https://fonts.googleapis.com/css?family=${families}`;
86
+
87
+ // if (subsets && Array.isArray(subsets) && subsets.length > 0) {
88
+ // link.href += `&subset=${subsets.join(',')}`;
89
+ // }
90
+
91
+ // return link;
92
+ // }
93
+
94
+ // appendLink = () => document.head.appendChild(this.link);
95
+
96
+ // removeLink = () => document.head.removeChild(this.link);
97
+
98
+ // replaceLink = () => {
99
+ // this.removeLink();
100
+ // this.link = this.createLink();
101
+ // this.appendLink();
102
+ // }
103
+
104
+ // componentDidMount () {
105
+ // this.link = this.createLink();
106
+ // this.appendLink();
107
+ // }
108
+
109
+ // componentDidUpdate (prevProps) {
110
+ // if (JSON.stringify(prevProps.fonts) !== JSON.stringify(this.props.fonts)) {
111
+ // this.replaceLink();
112
+ // }
113
+ // }
114
+
115
+ // componentWillUnmount () {
116
+ // this.removeLink();
117
+ // }
75
118
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 ) ;
119
+ // render = () => null;
120
+ // }
98
121
99
122
GoogleFontLoader . propTypes = {
100
123
fonts : _propTypes2 . default . arrayOf ( _propTypes2 . default . shape ( {
101
124
font : _propTypes2 . default . string . isRequired ,
102
125
weights : _propTypes2 . default . arrayOf ( _propTypes2 . default . oneOfType ( [ _propTypes2 . default . string , _propTypes2 . default . number ] ) )
103
126
} ) ) . isRequired ,
104
- subsets : _propTypes2 . default . arrayOf ( _propTypes2 . default . string )
127
+ subsets : _propTypes2 . default . arrayOf ( _propTypes2 . default . string ) ,
128
+ display : _propTypes2 . default . string
105
129
} ;
106
130
107
131
exports . default = GoogleFontLoader ;
0 commit comments