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.

TouchmoveHack.js 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // @flow
  2. import React, { useEffect, useRef } from 'react';
  3. import { isMobileBrowser } from '../../../base/environment/utils';
  4. type Props = {
  5. /**
  6. * The component(s) that need to be scrollable on mobile.
  7. */
  8. children: React$Node,
  9. /**
  10. * Whether the component is rendered within a modal.
  11. */
  12. isModal: boolean
  13. };
  14. /**
  15. * Component that disables {@code touchmove} propagation below it.
  16. *
  17. * @returns {ReactElement}
  18. */
  19. function TouchmoveHack({ children, isModal }: Props) {
  20. if (!isModal || !isMobileBrowser()) {
  21. return children;
  22. }
  23. const touchMoveElementRef = useRef(null);
  24. /**
  25. * Atlaskit's {@code Modal} uses a third party library to disable touchmove events
  26. * which makes scrolling inside dialogs impossible. We therefore employ this hack
  27. * to intercept and stop the propagation of touchmove events from this wrapper that
  28. * is placed around the chat conversation from the {@code ChatDialog}.
  29. *
  30. * @param {Event} event - The touchmove event fired within the component.
  31. * @returns {void}
  32. */
  33. function handleTouchMove(event: TouchEvent) {
  34. event.stopImmediatePropagation();
  35. }
  36. useEffect(() => {
  37. if (touchMoveElementRef && touchMoveElementRef.current) {
  38. touchMoveElementRef.current.addEventListener('touchmove', handleTouchMove, true);
  39. }
  40. return () => {
  41. if (touchMoveElementRef && touchMoveElementRef.current) {
  42. touchMoveElementRef.current.removeEventListener('touchmove', handleTouchMove, true);
  43. }
  44. };
  45. }, []);
  46. return (
  47. <div
  48. className = 'touchmove-hack'
  49. ref = { touchMoveElementRef }>
  50. {children}
  51. </div>
  52. );
  53. }
  54. export default TouchmoveHack;