2021-10-22 13:23:52 +00:00
|
|
|
import { makeStyles } from '@material-ui/core';
|
2022-08-25 11:35:19 +00:00
|
|
|
import React, { ReactElement, useCallback } from 'react';
|
2021-01-04 13:30:23 +00:00
|
|
|
|
2021-12-13 13:51:05 +00:00
|
|
|
import { DRAWER_MAX_HEIGHT } from '../../constants';
|
|
|
|
|
2021-01-04 13:30:23 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
2022-03-11 13:00:49 +00:00
|
|
|
/**
|
2022-08-25 11:35:19 +00:00
|
|
|
* The component(s) to be displayed within the drawer menu.
|
2022-03-11 13:00:49 +00:00
|
|
|
*/
|
2022-08-25 11:35:19 +00:00
|
|
|
children: ReactElement,
|
2022-03-11 13:00:49 +00:00
|
|
|
|
2021-01-04 13:30:23 +00:00
|
|
|
/**
|
2022-08-25 11:35:19 +00:00
|
|
|
* Class name for custom styles.
|
2021-01-04 13:30:23 +00:00
|
|
|
*/
|
2022-08-25 11:35:19 +00:00
|
|
|
className?: string,
|
2021-01-04 13:30:23 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-13 06:50:08 +00:00
|
|
|
* Whether the drawer should be shown or not.
|
2021-01-04 13:30:23 +00:00
|
|
|
*/
|
|
|
|
isOpen: boolean,
|
|
|
|
|
|
|
|
/**
|
2021-07-13 06:50:08 +00:00
|
|
|
* Function that hides the drawer.
|
2021-06-10 12:48:44 +00:00
|
|
|
*/
|
2021-07-13 06:50:08 +00:00
|
|
|
onClose: Function
|
2021-01-04 13:30:23 +00:00
|
|
|
};
|
|
|
|
|
2022-08-25 11:35:19 +00:00
|
|
|
const useStyles = makeStyles((theme: any) => {
|
2021-10-22 13:23:52 +00:00
|
|
|
return {
|
|
|
|
drawer: {
|
2021-12-13 13:51:05 +00:00
|
|
|
backgroundColor: theme.palette.ui02,
|
|
|
|
maxHeight: `calc(${DRAWER_MAX_HEIGHT})`
|
2021-10-22 13:23:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-01-04 13:30:23 +00:00
|
|
|
/**
|
|
|
|
* Component that displays the mobile friendly drawer on web.
|
|
|
|
*
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
function Drawer({
|
|
|
|
children,
|
2022-03-11 13:00:49 +00:00
|
|
|
className = '',
|
2021-01-04 13:30:23 +00:00
|
|
|
isOpen,
|
2021-10-22 13:23:52 +00:00
|
|
|
onClose
|
|
|
|
}: Props) {
|
|
|
|
const styles = useStyles();
|
2021-01-04 13:30:23 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-20 08:30:12 +00:00
|
|
|
* Handles clicks within the menu, preventing the propagation of the click event.
|
2021-01-04 13:30:23 +00:00
|
|
|
*
|
2021-07-20 08:30:12 +00:00
|
|
|
* @param {Object} event - The click event.
|
2021-01-04 13:30:23 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2021-07-20 08:30:12 +00:00
|
|
|
const handleInsideClick = useCallback(event => {
|
|
|
|
event.stopPropagation();
|
|
|
|
}, []);
|
2021-01-04 13:30:23 +00:00
|
|
|
|
2021-07-20 08:30:12 +00:00
|
|
|
/**
|
|
|
|
* Handles clicks outside of the menu, closing it, and also stopping further propagation.
|
|
|
|
*
|
|
|
|
* @param {Object} event - The click event.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
const handleOutsideClick = useCallback(event => {
|
|
|
|
event.stopPropagation();
|
|
|
|
onClose();
|
|
|
|
}, [ onClose ]);
|
2021-01-04 13:30:23 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
isOpen ? (
|
|
|
|
<div
|
2021-07-20 08:30:12 +00:00
|
|
|
className = 'drawer-menu-container'
|
|
|
|
onClick = { handleOutsideClick }>
|
|
|
|
<div
|
2022-03-11 13:00:49 +00:00
|
|
|
className = { `drawer-menu ${styles.drawer} ${className}` }
|
2021-07-20 08:30:12 +00:00
|
|
|
onClick = { handleInsideClick }>
|
|
|
|
{children}
|
|
|
|
</div>
|
2021-01-04 13:30:23 +00:00
|
|
|
</div>
|
|
|
|
) : null
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-13 06:50:08 +00:00
|
|
|
export default Drawer;
|