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

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