Guest User

Untitled

a guest
Dec 15th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. import React from 'react';
  2. import { loadCSS } from 'fg-loadcss';
  3. import FontfaceObserver from 'fontfaceobserver';
  4.  
  5. class FontLoader extends React.Component {
  6. static defaultProps = {
  7. fonts: [],
  8. render: () => {}
  9. };
  10.  
  11. state = {
  12. loading: true,
  13. error: null,
  14. success: false,
  15. };
  16.  
  17. componentDidMount() {
  18. this.props.fonts.map(font => loadCSS(font.url));
  19.  
  20. Promise.all(
  21. this.props.fonts
  22. .map(font => font.font)
  23. .map(font => new FontfaceObserver(font).load())
  24. ).then(
  25. () => this.setState({ loading: false, success: true }),
  26. error => this.setState({ loading: false, error })
  27. );
  28. }
  29.  
  30. render() {
  31. return this.props.render(this.state);
  32. }
  33. }
  34.  
  35. export default FontLoader;
Add Comment
Please, Sign In to add comment