fix: mobile side bar auto collapsing

This commit is contained in:
Jing Hua 2023-04-01 23:21:44 +08:00
parent b868e713d0
commit fc95eecc11

View file

@ -1,4 +1,5 @@
import React, { useEffect } from 'react'; import React, { useEffect, useRef } from 'react';
import useStore from '@store/store'; import useStore from '@store/store';
import NewChat from './NewChat'; import NewChat from './NewChat';
@ -13,10 +14,16 @@ const Menu = () => {
const hideSideMenu = useStore((state) => state.hideSideMenu); const hideSideMenu = useStore((state) => state.hideSideMenu);
const setHideSideMenu = useStore((state) => state.setHideSideMenu); const setHideSideMenu = useStore((state) => state.setHideSideMenu);
const windowWidthRef = useRef<number>(window.innerWidth);
useEffect(() => { useEffect(() => {
if (window.innerWidth < 768) setHideSideMenu(true); if (window.innerWidth < 768) setHideSideMenu(true);
window.addEventListener('resize', () => { window.addEventListener('resize', () => {
if (window.innerWidth < 768) setHideSideMenu(true); if (
windowWidthRef.current !== window.innerWidth &&
window.innerWidth < 768
)
setHideSideMenu(true);
}); });
}, []); }, []);