SlideWrap.tsx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import React from 'react';
  2. import { createPortal } from 'react-dom';
  3. import classNames from 'classnames';
  4. import './SlideWrap.less';
  5. const SlideWrap: React.FC<{ className?: string }> = ({
  6. className,
  7. children,
  8. }) => {
  9. const dialogNode = React.useMemo(
  10. () => {
  11. // 容器
  12. const dialogNode = document.createElement('section');
  13. document.body.appendChild(dialogNode);
  14. return dialogNode;
  15. },
  16. [] as readonly [],
  17. );
  18. const originalOverflowCallback = React.useRef('');
  19. React.useEffect(
  20. () => {
  21. const { style } = document.body;
  22. originalOverflowCallback.current = style.overflow;
  23. style.overflow = 'hidden';
  24. return () => {
  25. const { style } = document.body;
  26. style.overflow = originalOverflowCallback.current;
  27. // 清除容器
  28. document.body.removeChild(dialogNode);
  29. };
  30. },
  31. [] as readonly [],
  32. );
  33. return createPortal(
  34. <div className={classNames('PhotoView-SlideWrap', className)}>
  35. {children}
  36. </div>,
  37. dialogNode,
  38. );
  39. };
  40. SlideWrap.displayName = 'SlideWrap';
  41. export default SlideWrap;