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

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