Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Commit

Permalink
fix: 🐛 fix issue when accessing document object in SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
filipowm committed Aug 3, 2020
1 parent 5ff87f0 commit 5603035
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 34 deletions.
15 changes: 0 additions & 15 deletions src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,6 @@ export default class HTML extends React.Component {
{this.props.preBodyComponents}
<div key={`body`} id="___gatsby" dangerouslySetInnerHTML={{ __html: this.props.body }} />
{this.props.postBodyComponents}
<script
defer
dangerouslySetInnerHTML={{
__html: `
function navBarClose() {
document.getElementById("navbar").classList.toggle("responsive");
}
document.addEventListener('click',function(e){
if(e.target && e.target.tagName.toLowerCase() === 'a'){
navBarClose();
}
});
`,
}}
/>
</body>
</html>
);
Expand Down
38 changes: 19 additions & 19 deletions src/styles/responsive.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
const breakpointsInt = {
small: 768,
large: 1170,
small: 768,
large: 1170,
};

const breakpoints = {
};
const breakpoints = {};

Object.keys(breakpointsInt).map(function(key, index) {
breakpoints[key] = breakpointsInt[key] + 'px';
});
Object.keys(breakpointsInt).map(function (key, index) {
breakpoints[key] = breakpointsInt[key] + 'px';
});

const mq = Object.values(breakpoints).map(
bp => `@media (max-width: ${bp})`
)
const mq = Object.values(breakpoints).map((bp) => `@media (max-width: ${bp})`);

const checkViewport = (maxValue) => {
const vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0)
if (typeof document !== `undefined`) {
const vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0);
return vw <= maxValue;
}
}
return false;
};

export const isMobile = () => {
return checkViewport(breakpointsInt.small);
}
return checkViewport(breakpointsInt.small);
};

export const isTablet = () => {
return checkViewport(breakpointsInt.large);
}
return checkViewport(breakpointsInt.large);
};

export const isNormal = () => {
return !(isMobile() || isTablet());
}
return !(isMobile() || isTablet());
};

export const onMobile = mq[0];

export const onTablet = mq[1];
export const onTablet = mq[1];

0 comments on commit 5603035

Please sign in to comment.