Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sidebar: Hide when minimizing screen by default #970

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions static/js/redux/hooks/useWindowSize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React, { useLayoutEffect, useState } from "react";

function useWindowSize() {
const [size, setSize] = useState([0, 0]);
useLayoutEffect(() => {
function updateSize() {
setSize([window.innerWidth, window.innerHeight]);
}
window.addEventListener("resize", updateSize);
updateSize();
return () => window.removeEventListener("resize", updateSize);
}, []);
return size;
}

export default useWindowSize;
21 changes: 14 additions & 7 deletions static/js/redux/ui/TopBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import SocialProfileContainer from "./containers/social_profile_container";
import { getCurrentSemester } from "../state";
import ThemeToggle from "./ThemeToggle";
import { selectTheme } from "../state/slices/themeSlice";
import useWindowSize from "../hooks/useWindowSize";

/**
* This component is the top row of the app which contains the logo, search bar,
Expand All @@ -33,6 +34,7 @@ const TopBar = () => {
const theme = useAppSelector(selectTheme);

const [sideBarCollapsed, setSideBarCollapsed] = useState("neutral");
const [width] = useWindowSize();

const mainBarSelector = isComparing ? ".main-bar-compare-timetable" : ".main-bar";
const sideBarSelector = isComparing ? ".side-bar-compare-timetable" : ".side-bar";
Expand All @@ -51,28 +53,33 @@ const TopBar = () => {

const toggleSideBar = () => {
if (sideBarCollapsed === "neutral") {
const bodyWidth = $(window).width();
if (bodyWidth > 999) {
if (width > 1024) {
setSideBarCollapsed("closed");
} else {
setSideBarCollapsed("open");
}
return;
}
if (sideBarCollapsed === "open") {
} else if (sideBarCollapsed === "open") {
setSideBarCollapsed("closed");
} else {
setSideBarCollapsed("open");
}
};

useEffect(() => {
if (sideBarCollapsed === "closed") {
console.log(sideBarCollapsed);
if (sideBarCollapsed === "neutral") {
console.log(width);
if (width > 1024) {
expandSideBar();
} else {
collapseSideBar();
}
} else if (sideBarCollapsed === "closed") {
collapseSideBar();
} else {
expandSideBar();
}
}, [sideBarCollapsed]);
}, [sideBarCollapsed, width]);

const renderUserForPrint = () => (
<div className="print">
Expand Down