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.js 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // @flow
  2. import { makeStyles } from '@material-ui/core';
  3. import React, { useCallback } from 'react';
  4. import { DRAWER_MAX_HEIGHT } from '../../constants';
  5. type Props = {
  6. /**
  7. * Class name for custom styles.
  8. */
  9. className: string,
  10. /**
  11. * The component(s) to be displayed within the drawer menu.
  12. */
  13. children: React$Node,
  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 => {
  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 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;