Skip to content

Commit

Permalink
fix: Use inline style override to reset sdk styles
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Jun 2, 2020
1 parent c35cc0f commit 4dc68cf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
15 changes: 15 additions & 0 deletions gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
// This is needed to ensure the NR1 SDK can load properly
const React = require('react');

const sdkStyleOverrides = `
html {
line-height: 1.15;
}
body {
font-size: 16px;
line-height: 1.2;
color: var(--color-black);
}
`;

exports.onPreRenderHTML = ({ getHeadComponents, replaceHeadComponents }) => {
replaceHeadComponents([
...getHeadComponents(),
Expand Down Expand Up @@ -35,5 +47,8 @@ exports.onPreRenderHTML = ({ getHeadComponents, replaceHeadComponents }) => {
crossOrigin="anonymous"
src="//nr1.nr-assets.net/lib/d3/3.5.17/d3.js"
/>,
<style key="sdk-overrides" id="sdk-overrides">
{sdkStyleOverrides}
</style>,
]);
};
4 changes: 3 additions & 1 deletion src/hooks/useSdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { JS_BUNDLE, CSS_BUNDLE } from '../utils/sdk';

const useSdk = () => {
const scriptLoaded = useScript(JS_BUNDLE);
const stylesheetLoaded = useStylesheet(CSS_BUNDLE);
const stylesheetLoaded = useStylesheet(CSS_BUNDLE, {
insertBefore: 'sdk-overrides',
});

return scriptLoaded && stylesheetLoaded;
};
Expand Down
11 changes: 8 additions & 3 deletions src/hooks/useStylesheet.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useState } from 'react';

const useStylesheet = (src) => {
const useStylesheet = (src, { insertBefore } = {}) => {
const [loaded, setLoaded] = useState(false);

useEffect(() => {
Expand All @@ -10,10 +10,15 @@ const useStylesheet = (src) => {
link.crossOrigin = 'anonymous';
link.onload = () => setLoaded(true);
link.href = src;
document.head.prepend(link);

if (insertBefore) {
document.head.insertBefore(link, document.getElementById(insertBefore));
} else {
document.body.appendChild(link);
}

return () => document.head.removeChild(link);
}, [src]);
}, [src, insertBefore]);

return loaded;
};
Expand Down

0 comments on commit 4dc68cf

Please sign in to comment.