Skip to content

Commit 730e8b8

Browse files
committed
close mobilemenu on resize about laptop breakpoint
1 parent 3018566 commit 730e8b8

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/components/MobileSideBarMenu/index.jsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import styles from './styles.module.scss';
66
const MobileSideBarMenu = ({sidebar, menu}) => {
77
const [currentMenuState, setMenuState] = useState(false);
88

9+
// Define the breakpoint where mobile menu should be hidden (laptop breakpoint)
10+
const LAPTOP_BREAKPOINT = 1330;
11+
912
// Prevent body scroll when menu is open
1013
useEffect(() => {
1114
if (currentMenuState) {
@@ -29,6 +32,26 @@ const MobileSideBarMenu = ({sidebar, menu}) => {
2932
}
3033
}, [currentMenuState]);
3134

35+
// Close mobile menu when viewport exceeds mobile breakpoint
36+
useEffect(() => {
37+
const handleResize = () => {
38+
if (window.innerWidth >= LAPTOP_BREAKPOINT && currentMenuState) {
39+
setMenuState(false);
40+
}
41+
};
42+
43+
// Add event listener
44+
window.addEventListener('resize', handleResize);
45+
46+
// Also check on mount in case the component mounts with a large viewport
47+
handleResize();
48+
49+
// Cleanup
50+
return () => {
51+
window.removeEventListener('resize', handleResize);
52+
};
53+
}, [currentMenuState, LAPTOP_BREAKPOINT]);
54+
3255
// Function to handle menu close
3356
const handleMenuClose = () => {
3457
setMenuState(false);

src/css/breakpoints.scss

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
$mobile-breakpoint: 996px;
22
$tablet-breakpoint: 1024px;
33
$laptop-breakpoint: 1330px;
4-
$large-desktop-breakpoint: 1440px;
4+
$large-desktop-breakpoint: 1440px;
5+
6+
// If you change these breakpoints, make sure to update
7+
// MobileSideBarMenu/index.js accordingly

0 commit comments

Comments
 (0)