Created
October 8, 2020 15:26
-
-
Save mikehwagz/fa835bb06d7cdece851d73e8cedb7fef to your computer and use it in GitHub Desktop.
font loading
This file contains 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 characters
const FontFaceObserver = require('fontfaceobserver') | |
export default function loadFonts(fontManifest) { | |
return new Promise((resolve, reject) => { | |
const observers = fontManifest.map( | |
(entry) => new FontFaceObserver(entry.family, entry.options), | |
) | |
Promise.all(observers.map((font) => font.load())) | |
.then((res) => { | |
// if (process.env.NODE_ENV !== 'production') { | |
// console.group('FontFaceObserver') | |
// res.forEach((font) => | |
// console.log(`Loaded ${font.family} (${font.style})`), | |
// ) | |
// console.groupEnd() | |
// } | |
resolve() | |
}) | |
.catch(() => { | |
// if (process.env.NODE_ENV !== 'production') { | |
// console.warn('FFO: 3s loading timeout exceeded.') | |
// } | |
reject() | |
}) | |
}) | |
} |
This file contains 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 characters
loadFonts([ | |
{ | |
family: 'Lorem Ipsum', | |
options: { | |
weight: 700, | |
}, | |
}, | |
]).then(() => { | |
// do stuff | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment