You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Drawer.tsx 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { Theme } from '@mui/material';
  2. import React, { ReactElement, useCallback } from 'react';
  3. import { makeStyles } from 'tss-react/mui';
  4. import { DRAWER_MAX_HEIGHT } from '../../constants';
  5. type Props = {
  6. /**
  7. * The component(s) to be displayed within the drawer menu.
  8. */
  9. children: ReactElement;
  10. /**
  11. * Class name for custom styles.
  12. */
  13. className?: string;
  14. /**
  15. * Whether the drawer should be shown or not.
  16. */
  17. isOpen: boolean;
  18. /**
  19. * Function that hides the drawer.
  20. */
  21. onClose: Function;
  22. };
  23. const useStyles = makeStyles()((theme: Theme) => {
  24. return {
  25. drawer: {
  26. backgroundColor: theme.palette.ui02,
  27. maxHeight: `calc(${DRAWER_MAX_HEIGHT})`
  28. }
  29. };
  30. });
  31. /**
  32. * Component that displays the mobile friendly drawer on web.
  33. *
  34. * @returns {ReactElement}
  35. */
  36. function Drawer({
  37. children,
  38. className = '',
  39. isOpen,
  40. onClose
  41. }: Props) {
  42. const { classes: styles } = useStyles();
  43. /**
  44. * Handles clicks within the menu, preventing the propagation of the click event.
  45. *
  46. * @param {Object} event - The click event.
  47. * @returns {void}
  48. */
  49. const handleInsideClick = useCallback(event => {
  50. event.stopPropagation();
  51. }, []);
  52. /**
  53. * Handles clicks outside of the menu, closing it, and also stopping further propagation.
  54. *
  55. * @param {Object} event - The click event.
  56. * @returns {void}
  57. */
  58. const handleOutsideClick = useCallback(event => {
  59. event.stopPropagation();
  60. onClose();
  61. }, [ onClose ]);
  62. return (
  63. isOpen ? (
  64. <div
  65. className = 'drawer-menu-container'
  66. onClick = { handleOutsideClick }>
  67. <div
  68. className = { `drawer-menu ${styles.drawer} ${className}` }
  69. onClick = { handleInsideClick }>
  70. {children}
  71. </div>
  72. </div>
  73. ) : null
  74. );
  75. }
  76. export default Drawer;