Created
July 3, 2020 07:35
-
-
Save acidsound/ba37985e4c9cadda60c456e68e95cbc7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {useCallback, useRef, useState} from 'react'; | |
import {Drawer, makeStyles} from '@material-ui/core'; | |
const useStyles = makeStyles((theme) => ({ | |
dragger: { | |
height: '4px', | |
cursor: 'ns-resize', | |
padding: '0 4px', | |
borderLeft: '1px solid #ddd', | |
position: 'absolute', | |
top: 0, | |
left: 0, | |
right: 0, | |
zIndex: 999, | |
background: '#eaeaea' | |
} | |
})); | |
export const ResizableDrawer = ({children, minHeight, maxHeight, ...props}) => { | |
const classes = useStyles(); | |
const drawerRef = useRef(); | |
const [drawerHeight, setDrawerHeight] = useState(240); | |
const onMouseMove = useCallback(e => { | |
const newHeight = window.innerHeight-e.clientY; | |
if (newHeight > minHeight && newHeight < maxHeight) { | |
setDrawerHeight(newHeight); | |
} | |
e.preventDefault(); | |
}, []); | |
const onMouseDown = e => { | |
document.addEventListener('mouseup', onMouseUp, true); | |
document.addEventListener('mousemove', onMouseMove, true); | |
e.preventDefault(); | |
}; | |
const onMouseUp = () => { | |
document.removeEventListener('mouseup', onMouseDown, true); | |
document.removeEventListener('mousemove', onMouseMove, true); | |
}; | |
return ( | |
<Drawer ref={drawerRef} {...props} PaperProps={{style: {height: drawerHeight}}}> | |
<div onMouseDown={e => onMouseDown(e)} className={classes.dragger}/> | |
{children} | |
</Drawer> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment