Skip to content

Instantly share code, notes, and snippets.

@mikehwagz
Created October 8, 2020 15:26
Show Gist options
  • Save mikehwagz/fa835bb06d7cdece851d73e8cedb7fef to your computer and use it in GitHub Desktop.
Save mikehwagz/fa835bb06d7cdece851d73e8cedb7fef to your computer and use it in GitHub Desktop.
font loading
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()
})
})
}
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