react-google-font-loader icon indicating copy to clipboard operation
react-google-font-loader copied to clipboard

Add a prop for a custom document (good for iframes).

Open Reklino opened this issue 5 years ago • 5 comments

Hey there! This is great little tool 🙏 . Thanks for putting it together.

I'm using this in a project that uses Google Fonts in an iframe, so I need the <style> tags to be included in that document rather than the base document. This simply adds a new prop for this use case.

Reklino avatar Jun 12 '20 19:06 Reklino

Hi @Reklino, how's it going - did you manage to figure out what the types should be? 😆😋

jakewtaylor avatar Jun 24 '20 09:06 jakewtaylor

@jakewtaylor thanks for the reminder. #document is just a good old object. Just updated.

Reklino avatar Jun 24 '20 14:06 Reklino

Apologies for the delay @Reklino, the code looks good to me now. Sorry to be a pain, but could you provide a small example of how this new prop works, just so I can test it? 😄

jakewtaylor avatar Sep 25 '20 10:09 jakewtaylor

@jakewtaylor sure!

If you have a rendered iframe, just get the document of that iframe and pass it to your component via the contentDocument prop.

Example using react-frame-component (link)

const MyComponent = (props, context) => (
<Fragment>
  <Frame>
    <FrameContextConsumer>
      {
        // Callback is invoked with iframe's window and document instances
        ({document, window}) => {
          <Fragment>
           <GoogleFontLoader
              fonts={[
                {
                  font: 'Roboto',
                  weights: [400, '400i'],
                },
              ]}
              contentDocument={document}
            />
          <h1 style={{ fontFamily: 'Roboto'; }}>Will render with Roboto</h1>
          </Fragment>
        }
      }
    </FrameContextConsumer>
  </Frame>
  <h1 style={{ fontFamily: 'Roboto'; }}>Will not render with Roboto</h1>
</Fragment>
);

Of course, you don't have to use react-frame-component. That's just my preferred method.

Reklino avatar Sep 26 '20 18:09 Reklino

Hey @jakewtaylor 👋 . Any chance we could merge this one in?

Reklino avatar Dec 11 '20 01:12 Reklino