Created
June 8, 2020 21:24
-
-
Save felquis/3ccc90d95dd55a4770eae48d300c0f8b to your computer and use it in GitHub Desktop.
Yet Another Snippet to load global stylesheets
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 characters
/* | |
Usage: loadStyles([{ | |
href: | |
'https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons', | |
}, | |
{ | |
href: | |
'https://unpkg.com/[email protected]/dist/css/bootstrap-material-design.min.css', | |
integrity: | |
'sha384', | |
}, | |
{ | |
href: 'https://unpkg.com/normalize.css@^4.1.1', | |
}, | |
]) | |
*/ | |
const loadStyles = list => { | |
return Promise.all(list.map((file) => { | |
return new Promise((resolve, reject) => { | |
const link = document.createElement('link'); | |
link.rel = 'stylesheet'; | |
Object.keys(file).forEach(propertyName => { | |
link[propertyName] = file[propertyName]; | |
}); | |
link.onload = () => resolve(); | |
link.onerror = () => reject(); | |
document.body.appendChild(link); | |
}) | |
})) | |
}; | |
module exports loadStyles |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment