Skip to content

Commit

Permalink
feat(theme-common): Get desktopThresholdWidth from docusaurus config,…
Browse files Browse the repository at this point in the history
… fall back to 996
  • Loading branch information
jgarrow committed Aug 25, 2023
1 parent b3c8f5c commit 9f90240
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions packages/docusaurus-theme-common/src/hooks/useWindowSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import {useEffect, useState} from 'react';

import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';

const windowSizes = {
desktop: 'desktop',
Expand All @@ -17,13 +18,12 @@ const windowSizes = {

type WindowSize = keyof typeof windowSizes;

const DesktopThresholdWidth = 996;

function getWindowSize() {
function getWindowSize(desktopThresholdWidth = 996): WindowSize {
if (!ExecutionEnvironment.canUseDOM) {
return windowSizes.ssr;
}
return window.innerWidth > DesktopThresholdWidth

return window.innerWidth > desktopThresholdWidth
? windowSizes.desktop
: windowSizes.mobile;
}
Expand All @@ -44,16 +44,18 @@ const DevSimulateSSR = process.env.NODE_ENV === 'development' && true;
* catch potential layout shifts, similar to strict mode calling effects twice.
*/
export function useWindowSize(): WindowSize {
const {siteConfig} = useDocusaurusContext();
const desktopThresholdWidth = siteConfig?.themeConfig?.breakpoints?.desktop;
const [windowSize, setWindowSize] = useState<WindowSize>(() => {
if (DevSimulateSSR) {
return 'ssr';
}
return getWindowSize();
return getWindowSize(desktopThresholdWidth);
});

useEffect(() => {
function updateWindowSize() {
setWindowSize(getWindowSize());
setWindowSize(getWindowSize(desktopThresholdWidth));
}

const timeout = DevSimulateSSR
Expand Down

0 comments on commit 9f90240

Please sign in to comment.