Skip to content

Commit c063b18

Browse files
committed
minor refactors
1 parent 4f3ab26 commit c063b18

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

packages/docusaurus-theme-common/src/hooks/useWindowSize.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@ const windowSizes = {
1717

1818
type WindowSize = keyof typeof windowSizes;
1919

20-
function getWindowSize(desktopThresholdWidth: number): WindowSize {
20+
// Note: this value is also hardcoded in Infima
21+
// Both JS and CSS must have the same value
22+
// Updating this JS value alone is not enough
23+
// See https://github.com/facebook/docusaurus/issues/9603
24+
const DesktopBreakpoint = 996;
25+
26+
function getWindowSize(desktopBreakpoint: number): WindowSize {
2127
if (!ExecutionEnvironment.canUseDOM) {
2228
throw new Error(
2329
'getWindowSize() should only be called after React hydration',
2430
);
2531
}
2632

27-
return window.innerWidth > desktopThresholdWidth
33+
return window.innerWidth > desktopBreakpoint
2834
? windowSizes.desktop
2935
: windowSizes.mobile;
3036
}
@@ -40,9 +46,9 @@ function getWindowSize(desktopThresholdWidth: number): WindowSize {
4046
* explicit.
4147
*/
4248
export function useWindowSize({
43-
desktopThresholdWidth = 996,
49+
desktopBreakpoint = DesktopBreakpoint,
4450
}: {
45-
desktopThresholdWidth?: number;
51+
desktopBreakpoint?: number;
4652
} = {}): WindowSize {
4753
const [windowSize, setWindowSize] = useState<WindowSize>(
4854
() =>
@@ -53,7 +59,7 @@ export function useWindowSize({
5359

5460
useEffect(() => {
5561
function updateWindowSize() {
56-
setWindowSize(getWindowSize({desktopThresholdWidth}));
62+
setWindowSize(getWindowSize(desktopBreakpoint));
5763
}
5864

5965
updateWindowSize();
@@ -63,7 +69,7 @@ export function useWindowSize({
6369
return () => {
6470
window.removeEventListener('resize', updateWindowSize);
6571
};
66-
}, [desktopThresholdWidth]);
72+
}, [desktopBreakpoint]);
6773

6874
return windowSize;
6975
}

0 commit comments

Comments
 (0)