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 React, { ReactNode, useCallback } from 'react';
  2. import { makeStyles } from 'tss-react/mui';
  3. import { DRAWER_MAX_HEIGHT } from '../../constants';
  4. interface IProps {
  5. /**
  6. * The component(s) to be displayed within the drawer menu.
  7. */
  8. children: ReactNode;
  9. /**
  10. * Class name for custom styles.
  11. */
  12. className?: string;
  13. /**
  14. * Whether the drawer should be shown or not.
  15. */
  16. isOpen: boolean;
  17. /**
  18. * Function that hides the drawer.
  19. */
  20. onClose?: Function;
  21. }
  22. const useStyles = makeStyles()(theme => {
  23. return {
  24. drawer: {
  25. backgroundColor: theme.palette.ui01,
  26. maxHeight: `calc(${DRAWER_MAX_HEIGHT})`,
  27. borderRadius: '24px 24px 0 0'
  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. }: IProps) {
  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;