From 5003899222603b7cce4d06ca8cfcb644a8945f07 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 5 Mar 2020 10:38:00 +0100 Subject: [PATCH] app/Sidebar: update to latest version with peek --- .../app/src/components/SideBar/SideBar.tsx | 65 ++++++++++++++++--- 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/frontend/packages/app/src/components/SideBar/SideBar.tsx b/frontend/packages/app/src/components/SideBar/SideBar.tsx index 028de88e16..8474043876 100644 --- a/frontend/packages/app/src/components/SideBar/SideBar.tsx +++ b/frontend/packages/app/src/components/SideBar/SideBar.tsx @@ -1,4 +1,4 @@ -import React, { FC, useState, createContext, useContext } from 'react'; +import React, { FC, useRef, useState, createContext, useContext } from 'react'; import clsx from 'clsx'; import { makeStyles, @@ -18,6 +18,9 @@ import CreateIcon from '@material-ui/icons/AddCircleOutline'; const drawerWidthClosed = 64; const drawerWidthOpen = 220; +const defaultOpenDelayMs = 400; +const defaultCloseDelayMs = 200; + const Context = createContext(false); const useSidebarItemStyles = makeStyles(theme => ({ @@ -160,13 +163,12 @@ const useStyles = makeStyles(theme => ({ position: 'relative', overflow: 'visible', width: theme.spacing(7) + 1, - height: '100%', }, drawer: { display: 'flex', flexFlow: 'column nowrap', alignItems: 'flex-start', - position: 'fixed', + position: 'absolute', left: 0, top: 0, bottom: 0, @@ -186,18 +188,58 @@ const useStyles = makeStyles(theme => ({ duration: theme.transitions.duration.shorter, }), }, + drawerPeek: { + width: drawerWidthClosed + 4, + }, })); -const SideBar: FC<{}> = () => { +enum State { + Closed, + Peek, + Open, +} + +type Props = { + openDelayMs?: number; + closeDelayMs?: number; +}; + +const SideBar: FC = ({ + openDelayMs = defaultOpenDelayMs, + closeDelayMs = defaultCloseDelayMs, +}) => { const classes = useStyles(); - const [open, setOpen] = useState(false); + const [state, setState] = useState(State.Closed); + const hoverTimerRef = useRef(); const handleOpen = () => { - setOpen(true); + if (hoverTimerRef.current) { + clearTimeout(hoverTimerRef.current); + hoverTimerRef.current = undefined; + } + if (state !== State.Open) { + hoverTimerRef.current = setTimeout(() => { + hoverTimerRef.current = undefined; + setState(State.Open); + }, openDelayMs); + + setState(State.Peek); + } }; const handleClose = () => { - setOpen(false); + if (hoverTimerRef.current) { + clearTimeout(hoverTimerRef.current); + hoverTimerRef.current = undefined; + } + if (state === State.Peek) { + setState(State.Closed); + } else if (state === State.Open) { + hoverTimerRef.current = setTimeout(() => { + hoverTimerRef.current = undefined; + setState(State.Closed); + }, closeDelayMs); + } }; return ( @@ -209,8 +251,13 @@ const SideBar: FC<{}> = () => { onBlur={handleClose} data-testid="sidebar-root" > - -
+ +