Skip to content

Instantly share code, notes, and snippets.

@Manoz
Last active August 11, 2018 20:38
Show Gist options
  • Save Manoz/46ec3f269dec200af195e9b1f9c256e1 to your computer and use it in GitHub Desktop.
Save Manoz/46ec3f269dec200af195e9b1f9c256e1 to your computer and use it in GitHub Desktop.
React font-face utility
/**
* FontFace utility function that generates the font-face for you
* @param {string} name font-family name
* @param {string} src file name
* @param {string} fontWeight any font-weight (default: normal)
* @param {string} fontStyle any font-style (default: normal)
* @return {string} Return the complete css
*/
export function fontFace(name, src, fontWeight = 'normal', fontStyle = 'normal') {
return `
@font-face{
font-family: "${name}";
src: url(${require(`./fonts/${src}.eot`)});
src: url(${require(`./fonts/${src}.eot`)}?#iefix) format("embedded-opentype"),
url(${require(`./fonts/${src}.woff`)}) format("woff"),
url(${require(`./fonts/${src}.ttf`)}) format("truetype"),
url(${require(`./fonts/${src}.svg`)}#${name}) format("svg");
font-style: ${fontStyle};
font-weight: ${fontWeight};
}
`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment