From f3b162cf64be050607360d12770652f74b827c38 Mon Sep 17 00:00:00 2001 From: eder Date: Mon, 11 Jul 2022 14:23:59 -0300 Subject: [PATCH] ssr fixes --- src/lib/hooks/useBreakpoints.ts | 2 +- src/lib/hooks/useWindowDimensions.ts | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/lib/hooks/useBreakpoints.ts b/src/lib/hooks/useBreakpoints.ts index 24ecb99..bc90bd7 100644 --- a/src/lib/hooks/useBreakpoints.ts +++ b/src/lib/hooks/useBreakpoints.ts @@ -16,7 +16,7 @@ const parseBreakpoints = (breakpoints: Screens): NumberedBreakpoints => { return parsed as NumberedBreakpoints } -export type BreakpointsProps = { +export interface BreakpointsProps { breakpoints: NumberedBreakpoints current: keyof Screens isMobile: boolean diff --git a/src/lib/hooks/useWindowDimensions.ts b/src/lib/hooks/useWindowDimensions.ts index 8b55880..a029099 100644 --- a/src/lib/hooks/useWindowDimensions.ts +++ b/src/lib/hooks/useWindowDimensions.ts @@ -1,6 +1,6 @@ import { useState, useEffect } from 'react' -export type WindowDimensions = { +export interface WindowDimensions { width: { px: number rem: number @@ -11,9 +11,14 @@ export type WindowDimensions = { } } -function getWindowDimensions(): WindowDimensions { - const width = window?.innerWidth || 0 - const height = window?.innerHeight || 0 +const getWindowDimensions = (): WindowDimensions => { + let width = 0 + let height = 0 + + if (typeof window !== 'undefined') { + width = window?.innerWidth + height = window?.innerHeight + } const rootFontSize = parseInt(getComputedStyle(document?.documentElement)?.fontSize) ?? 16