Skip to content

Instantly share code, notes, and snippets.

@Lendar
Last active October 22, 2021 20:38
Show Gist options
  • Save Lendar/75a16245907b82a9a301 to your computer and use it in GitHub Desktop.
Save Lendar/75a16245907b82a9a301 to your computer and use it in GitHub Desktop.
Using FontFaceObserver for React app loader
import React from 'react'
import 'fontfaceobserver'
import IndexPage from './IndexPage'
import Loader from './Loader'
import { fetchUser } from '../apiclient'
import 'font-awesome-webpack'
import './Lato-font.css'
import './Roboto-font.css'
export default class App extends React.Component {
constructor () {
super()
this.state = {
isLoading: true
}
Promise.all([
fetchUser(),
new FontFaceObserver('Roboto').check(),
new FontFaceObserver('Lato').check(),
new FontFaceObserver('FontAwesome').check('\u{F135}'),
new FontFaceObserver('Glyphicons Halflings').check('\u{E104}') // included here for demonstration purposes
]).then(([user]) => {
this.setState({isLoading: false, user})
}, err => {
console.error('Failed to load fonts!', err)
this.setState({isLoading: false})
})
}
render () {
const { isLoading, user } = this.state
return (
isLoading
? <Loader/>
: <IndexPage user={user}/>
)
}
}
{
"private": true,
"dependencies": {
"font-awesome": "^4.4.0",
"font-awesome-webpack": "0.0.4",
"fontfaceobserver": "^1.5.4",
"react": "^0.14.1",
"react-dom": "^0.14.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment