18

I'm using https://github.com/styled-components/styled-components.

I'm trying to work out the best strategy for components that require @font-face. I want to make sure each component is independent of its context, so I'm defining font-family styles on each them. But if I use injectGlobal in multiple components, I get multiple @font-face rules for the same font.

Should I just define the @font-face rules in my ThemeProvider entry-point component and live with the fact that the desired font might not be loaded by the browser?

4 Answers 4

29

That's exactly why we made injectGlobal. If you take a look at our docs they say:

We do not encourage the use of this. Use once per app at most, contained in a single file. This is an escape hatch. Only use it for the rare @font-face definition or body styling.

So what I'd do is create a JS file called global-styles.js which contains this code:

// global-styles.js

import { injectGlobal } from 'styled-components';

injectGlobal`
  @font-face {
     font-family: 'My custom family';
     src: url('my-source.ttf');
  }
`

As you can see, all we do here is inject some global styling-in this case @font-face. The last thing necessary to make this work is to import this file in your main entry point:

// index.js

import './global-styles.js'

// More stuff here like ReactDOM.render(...)

This will mean your font-face is only injected once, but still all of your components have access to it with font-family: 'My custom family'!

Doing it this way will give you a flash-of-invisible-text (FOIT), but that has nothing to do with styled-components-it'd be the same if you were using vanilla CSS. To get rid of the FOIT requires a more advanced font loading strategy rather than just @font-faceing.

Since this has nothing to do with styled-components, I highly recommend watching these two Front End Center episodes to learn more about how to best do this: "Crafting Web Font Fallbacks" and "The Fastest Webfont Loader in the West"!

5
  • The new doc link should be styled-components.com/docs/api#injectglobal
    – wegry
    Aug 23, 2018 at 9:01
  • 7
    The injectGlobal API was removed and replaced by createGlobalStyle in styled-components v4. Oct 23, 2018 at 9:25
  • Is there a way to inject a global font face in styled v4 without using a react component to load it in? We're producing a web component and would like the font to be loaded before the component is placed on the page. Aug 10, 2022 at 22:47
  • @coppereyecat For me, this is the solution! "1. Group repainting": filamentgroup.com/lab/js-web-fonts.html#1.-group-repaints
    – Alexandre
    Mar 8 at 9:47
  • @Alexandre we ended up making a fontLoader function that preloaded fonts with document.createElement('link') for each font, followed by a document.createElement('style') that includes a CSS snippet defining them, which runs when our script is included on the page rather than when our component is loaded. It doesn't directly use styled at all, but the fonts are available for styled later. Mar 9 at 15:20
16

And on the other side of the same coin in Chrome:

do not use @font-face inside injectGlobal if using e.g. react-router. You will get re-paint of all of you app on each newly loaded route. And this is why: enter image description here

Same font-files downloaded on each new route. As soon as you include font-face in a separate .css file - problem solves as stated in the last comment in this github issue.

4
  • 1
    What about placing injectGlobal outside react-router? I use injectGlobal even outside ReactDOM.render(), i.e. on the very top level of the application and I don't get fonts loaded for each route navigation.
    – Oleh
    Jun 19, 2018 at 7:25
  • @Oleh how did you do that? Moved injectGlobal at the top - still getting fonts reloaded Jun 19, 2018 at 8:22
  • Probably this is something browser specific, since Edge and Firefox don't load they font each time I navigate with route, even more the don't load the font on refresh page as well. Chrome does load the font each time... So seems to be a chrome engine issue
    – Oleh
    Jun 19, 2018 at 12:52
  • @Oleh, yes i forgot to mention that this is a chrome specific issue) Jun 19, 2018 at 13:02
11

injectGlobal is deprecated. Use createGlobalStyle

import { createGlobalStyle } from 'styled-components'

export const GlobalStyle = createGlobalStyle`  
  body {
    font-family: 'Muli', sans-serif;

    h1 {
      font-weight: 800;
      font-size: 36px;

    p {
      font-size: 18px;
    }
  }
`;

Then you can use it in your App.js:

import { GlobalStyle } from './styles/global'

class App extends Component {
  render() {

    return (
      <ThemeProvider theme={theme}>
        <>
          <GlobalStyle/>
          <Container/>
        </>
      </ThemeProvider>

    )
  }
}
1
  • 11
    I still get all the fonts reloaded even with createGlobalStyle
    – George
    Mar 28, 2019 at 22:45
1

I agree

I get reloaded with

import { createGlobalStyle } from 'styled-components';
import { silver } from 'shared-components/themes/colors';

export default createGlobalStyle`
    @font-face {
        font-family: "Proxima Nova";
        font-style: normal;
        font-weight: 300;
        font-display: swap;
        src: url("/static/media/fonts/proxima_nova/ProximaNova_300.otf");
    }

and react create app

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.