From 42799389b5e38b5366dfc8628b814895daf375c4 Mon Sep 17 00:00:00 2001 From: Stefan Forstenlechner Date: Fri, 27 May 2022 23:36:19 +0200 Subject: [PATCH] Responsive layout with temporary and permanent Drawer "Clipped under the app bar" drawer did not work well on small screens --- .../src/ImageGallery/ImageGalleryAppBar.tsx | 25 +++++++++++- .../src/ImageGallery/ImageGalleryDrawer.tsx | 40 +++++++++++++++++-- .../src/ImageGalleryLayout.tsx | 19 ++++++++- 3 files changed, 77 insertions(+), 7 deletions(-) diff --git a/picture-gallery-client/src/ImageGallery/ImageGalleryAppBar.tsx b/picture-gallery-client/src/ImageGallery/ImageGalleryAppBar.tsx index 52dca24..db96f42 100644 --- a/picture-gallery-client/src/ImageGallery/ImageGalleryAppBar.tsx +++ b/picture-gallery-client/src/ImageGallery/ImageGalleryAppBar.tsx @@ -3,8 +3,20 @@ import Typography from "@mui/material/Typography"; import React from "react"; import env from "../env"; import AppBar from "@mui/material/AppBar"; +import useMediaQuery from "@mui/material/useMediaQuery"; +import { IconButton } from "@mui/material"; +import MenuIcon from "@mui/icons-material/Menu"; +import CloseIcon from "@mui/icons-material/Close"; +import { smallScreenMediaQuery } from "../ImageGalleryLayout"; -export const ImageGalleryAppBar = () => { +export const ImageGalleryAppBar = ({ + open, + onDrawerOpenClick, +}: { + open: boolean; + onDrawerOpenClick: () => void; +}) => { + const smallScreen = !useMediaQuery(smallScreenMediaQuery); return ( { style={{ backgroundColor: env.REACT_APP_APPBAR_COLOR ?? "#1976D2" }} > + {smallScreen && ( + + {open && smallScreen ? : } + + )} {env.REACT_APP_TITLE ?? "Simple Picture Gallery"} diff --git a/picture-gallery-client/src/ImageGallery/ImageGalleryDrawer.tsx b/picture-gallery-client/src/ImageGallery/ImageGalleryDrawer.tsx index ebf37f4..9935a63 100644 --- a/picture-gallery-client/src/ImageGallery/ImageGalleryDrawer.tsx +++ b/picture-gallery-client/src/ImageGallery/ImageGalleryDrawer.tsx @@ -6,6 +6,9 @@ import { useLocation, useNavigate } from "react-router-dom"; import React, { useState } from "react"; import { Folders } from "./models"; import Toolbar from "@mui/material/Toolbar"; +import { useTheme } from "@mui/material"; +import useMediaQuery from "@mui/material/useMediaQuery"; +import { smallScreenMediaQuery } from "../ImageGalleryLayout"; function getDefaultExpanded(pathname: string): string[] { const pathParts = []; @@ -105,23 +108,52 @@ function GenerateTreeView({ root }: { root: Folders }) { } export const ImageGalleryDrawer = ({ + open, drawerWidth, folder, + handleDrawerToggle, }: { + open: boolean; drawerWidth: number; folder: Folders; + handleDrawerToggle: () => void; }) => { - return ( + const theme = useTheme(); + const smallScreen = !useMediaQuery(smallScreenMediaQuery); + + const drawerContent = ( + <> + + + + ); + + return smallScreen ? ( + + {drawerContent} + + ) : ( - - + {drawerContent} ); }; diff --git a/picture-gallery-client/src/ImageGalleryLayout.tsx b/picture-gallery-client/src/ImageGalleryLayout.tsx index 8143674..3f10221 100644 --- a/picture-gallery-client/src/ImageGalleryLayout.tsx +++ b/picture-gallery-client/src/ImageGalleryLayout.tsx @@ -10,11 +10,14 @@ import { Spinner } from "./ImageGallery/Spinner"; import Toolbar from "@mui/material/Toolbar"; const drawerWidth = 240; +export const smallScreenMediaQuery = `(min-width:${drawerWidth * 3}px)`; function ImageGalleryLayout() { + const [drawerOpen, setDrawerOpen] = useState(false); const [error, setError] = useState(false); const [imagesLoaded, setImagesLoaded] = useState(false); const [images, setImages] = useState([]); + const [folders, setFolders] = useState({ name: "Home", fullPath: "/", @@ -25,6 +28,10 @@ function ImageGalleryLayout() { const location = useLocation(); const navigate = useNavigate(); + function handleDrawerToggle() { + setDrawerOpen(!drawerOpen); + } + useEffect(() => { setImages([]); setError(false); @@ -63,8 +70,16 @@ function ImageGalleryLayout() { return ( - - + + {imagesLoaded ? : }