|
1 |
| -import { useContext, useEffect, useState } from 'react'; |
| 1 | +import { |
| 2 | + useContext, useEffect, useLayoutEffect, useState, |
| 3 | +} from 'react'; |
2 | 4 | import { useDispatch, useSelector } from 'react-redux';
|
3 | 5 | import { useParams } from 'react-router-dom';
|
4 | 6 | import { sendTrackEvent, sendTrackingLogEvent } from '@edx/frontend-platform/analytics';
|
| 7 | +import { breakpoints } from '@openedx/paragon'; |
5 | 8 |
|
6 | 9 | import { useModel } from '@src/generic/model-store';
|
7 | 10 | import { LOADED } from '@src/constants';
|
@@ -54,10 +57,14 @@ export const useCourseOutlineSidebar = () => {
|
54 | 57 | } = course.entranceExamData || {};
|
55 | 58 | const isActiveEntranceExam = entranceExamEnabled && !entranceExamPassed;
|
56 | 59 |
|
| 60 | + const collapseSidebar = () => { |
| 61 | + toggleSidebar(null); |
| 62 | + window.sessionStorage.setItem('hideCourseOutlineSidebar', 'true'); |
| 63 | + }; |
| 64 | + |
57 | 65 | const handleToggleCollapse = () => {
|
58 | 66 | if (currentSidebar === ID) {
|
59 |
| - toggleSidebar(null); |
60 |
| - window.sessionStorage.setItem('hideCourseOutlineSidebar', 'true'); |
| 67 | + collapseSidebar(); |
61 | 68 | } else {
|
62 | 69 | toggleSidebar(ID);
|
63 | 70 | window.sessionStorage.removeItem('hideCourseOutlineSidebar');
|
@@ -107,6 +114,21 @@ export const useCourseOutlineSidebar = () => {
|
107 | 114 | }
|
108 | 115 | }, [courseId, isEnabledSidebar, courseOutlineShouldUpdate]);
|
109 | 116 |
|
| 117 | + // Collapse sidebar if screen resized to a width that displays the sidebar automatically |
| 118 | + useLayoutEffect(() => { |
| 119 | + const handleResize = () => { |
| 120 | + // breakpoints.large.maxWidth is 1200px and currently the breakpoint for showing the sidebar |
| 121 | + if (currentSidebar === ID && global.innerWidth < breakpoints.large.maxWidth) { |
| 122 | + collapseSidebar(); |
| 123 | + } |
| 124 | + }; |
| 125 | + |
| 126 | + global.addEventListener('resize', handleResize); |
| 127 | + return () => { |
| 128 | + global.removeEventListener('resize', handleResize); |
| 129 | + }; |
| 130 | + }, [isOpen]); |
| 131 | + |
110 | 132 | return {
|
111 | 133 | courseId,
|
112 | 134 | unitId,
|
|
0 commit comments