Last active
November 28, 2018 23:19
-
-
Save jakewtaylor/ad2ef4dcb5548ac716f0faacf8c2c74d to your computer and use it in GitHub Desktop.
Revisions
-
jakewtaylor revised this gist
Nov 28, 2018 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,8 +20,8 @@ const App = () => ( }, ]} /> <p style={{ fontFamily: 'Roboto Mono, monospaced' }}>This will be in Roboto Mono!</p> <p style={{ fontFamily: 'Roboto, sans-serif' }}>This will be in Roboto!</p> </> ); ``` -
jakewtaylor revised this gist
Nov 28, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ You simply pass it a config array and it will load the fonts for you by appendin Example usage: ```JavaScript const App = () => ( <> <GoogleFontLoader fonts={[ -
jakewtaylor created this gist
Nov 28, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,51 @@ import React from 'react'; class GoogleFontLoader extends React.PureComponent { link = null; createLink = () => { const { fonts } = this.props; const families = fonts.reduce((acc, font) => { const family = font.font.replace(/ +/g, '+'); const weights = font.weights.join(','); acc.push(`${family}:${weights}`); return acc; }, []).join('|'); const link = document.createElement('link'); link.rel = 'stylesheet'; link.href = `https://fonts.googleapis.com/css?family=${families}`; return link; } appendLink = () => document.head.appendChild(this.link); removeLink = () => document.head.removeChild(this.link); replaceLink = () => { this.removeLink(); this.link = this.createLink(); this.appendLink(); } componentDidMount () { this.link = this.createLink(); this.appendLink(); } componentDidUpdate (prevProps) { if (JSON.stringify(prevProps.fonts) !== JSON.stringify(this.props.fonts)) { this.replaceLink(); } } componentWillUnmount () { this.removeLink(); } render = () => null; }; export default GoogleFontLoader; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ # GoogleFontLoader This is a _really_ simple component that can automatically handle loading Google fonts for you. You simply pass it a config array and it will load the fonts for you by appending a `<link />` tag to the document head. It will update itself if the config changes, and will remove itself on unmount. Example usage: ``` const App = () => ( <> <GoogleFontLoader fonts={[ { font: 'Roboto', weights: [400], }, { font: 'Roboto Mono', weights: [400, 700], }, ]} /> <p style={{ fontFamily: 'Roboto Mono, monospaced' }}>I'll be in Roboto Mono!</p> <p style={{ fontFamily: 'Roboto, sans-serif' }}>I'll be in Roboto!</p> </> ); ```