2023-02-28 10:21:15 +00:00
|
|
|
import React, { KeyboardEvent, ReactNode, useCallback } from 'react';
|
|
|
|
import ReactFocusLock from 'react-focus-lock';
|
2022-09-13 07:36:00 +00:00
|
|
|
import { makeStyles } from 'tss-react/mui';
|
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
|
|
|
|
2022-11-03 08:35:51 +00:00
|
|
|
interface IProps {
|
2021-01-04 13:30:23 +00:00
|
|
|
|
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
|
|
|
*/
|
2023-02-02 11:12:31 +00:00
|
|
|
children: ReactNode;
|
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-09-08 09:52:36 +00:00
|
|
|
className?: string;
|
2021-01-04 13:30:23 +00:00
|
|
|
|
2023-02-28 10:21:15 +00:00
|
|
|
/**
|
|
|
|
* The id of the dom element acting as the Drawer label.
|
|
|
|
*/
|
|
|
|
headingId?: 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
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
isOpen: boolean;
|
2021-01-04 13:30:23 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-13 06:50:08 +00:00
|
|
|
* Function that hides the drawer.
|
2021-06-10 12:48:44 +00:00
|
|
|
*/
|
2023-02-02 11:12:31 +00:00
|
|
|
onClose?: Function;
|
2022-11-03 08:35:51 +00:00
|
|
|
}
|
2021-01-04 13:30:23 +00:00
|
|
|
|
2022-11-15 07:50:22 +00:00
|
|
|
const useStyles = makeStyles()(theme => {
|
2021-10-22 13:23:52 +00:00
|
|
|
return {
|
|
|
|
drawer: {
|
2022-10-06 10:09:40 +00:00
|
|
|
backgroundColor: theme.palette.ui01,
|
|
|
|
maxHeight: `calc(${DRAWER_MAX_HEIGHT})`,
|
|
|
|
borderRadius: '24px 24px 0 0'
|
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 = '',
|
2023-02-28 10:21:15 +00:00
|
|
|
headingId,
|
2021-01-04 13:30:23 +00:00
|
|
|
isOpen,
|
2021-10-22 13:23:52 +00:00
|
|
|
onClose
|
2022-11-03 08:35:51 +00:00
|
|
|
}: IProps) {
|
2022-09-13 07:36:00 +00:00
|
|
|
const { classes: 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();
|
2023-02-02 11:12:31 +00:00
|
|
|
onClose?.();
|
2021-07-20 08:30:12 +00:00
|
|
|
}, [ onClose ]);
|
2021-01-04 13:30:23 +00:00
|
|
|
|
2023-02-28 10:21:15 +00:00
|
|
|
/**
|
|
|
|
* Handles pressing the escape key, closing the drawer.
|
|
|
|
*
|
|
|
|
* @param {KeyboardEvent<HTMLDivElement>} event - The keydown event.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
const handleEscKey = useCallback((event: KeyboardEvent<HTMLDivElement>) => {
|
|
|
|
if (event.key === 'Escape') {
|
|
|
|
event.preventDefault();
|
|
|
|
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'
|
2023-02-28 10:21:15 +00:00
|
|
|
onClick = { handleOutsideClick }
|
|
|
|
onKeyDown = { handleEscKey }>
|
2021-07-20 08:30:12 +00:00
|
|
|
<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 }>
|
2023-02-28 10:21:15 +00:00
|
|
|
<ReactFocusLock
|
|
|
|
lockProps = {{
|
|
|
|
role: 'dialog',
|
|
|
|
'aria-modal': true,
|
|
|
|
'aria-labelledby': `#${headingId}`
|
|
|
|
}}
|
|
|
|
returnFocus = { true }>
|
|
|
|
{children}
|
|
|
|
</ReactFocusLock>
|
2021-07-20 08:30:12 +00:00
|
|
|
</div>
|
2021-01-04 13:30:23 +00:00
|
|
|
</div>
|
|
|
|
) : null
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-13 06:50:08 +00:00
|
|
|
export default Drawer;
|