Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DialogPortal.ts 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { ReactNode, useEffect, useRef, useState } from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { useSelector } from 'react-redux';
  4. import { IReduxState } from '../../../app/types';
  5. import { debounce } from '../../../base/config/functions.any';
  6. import { ZINDEX_DIALOG_PORTAL } from '../../constants';
  7. interface IProps {
  8. /**
  9. * The component(s) to be displayed within the drawer portal.
  10. */
  11. children: ReactNode;
  12. /**
  13. * Custom class name to apply on the container div.
  14. */
  15. className?: string;
  16. /**
  17. * Function used to get the reference to the container div.
  18. */
  19. getRef?: Function;
  20. /**
  21. * Function called when the portal target becomes actually visible.
  22. */
  23. onVisible?: Function;
  24. /**
  25. * Function used to get the updated size info of the container on it's resize.
  26. */
  27. setSize?: Function;
  28. /**
  29. * Custom style to apply to the container div.
  30. */
  31. style?: any;
  32. /**
  33. * The selector for the element we consider the content container.
  34. * This is used to determine the correct size of the portal content.
  35. */
  36. targetSelector?: string;
  37. }
  38. /**
  39. * Component meant to render a drawer at the bottom of the screen,
  40. * by creating a portal containing the component's children.
  41. *
  42. * @returns {ReactElement}
  43. */
  44. function DialogPortal({ children, className, style, getRef, setSize, targetSelector, onVisible }: IProps) {
  45. const clientWidth = useSelector((state: IReduxState) => state['features/base/responsive-ui'].clientWidth);
  46. const [ portalTarget ] = useState(() => {
  47. const portalDiv = document.createElement('div');
  48. portalDiv.style.visibility = 'hidden';
  49. return portalDiv;
  50. });
  51. const timerRef = useRef<number>();
  52. useEffect(() => {
  53. if (style) {
  54. for (const styleProp of Object.keys(style)) {
  55. const objStyle: any = portalTarget.style;
  56. objStyle[styleProp] = style[styleProp];
  57. }
  58. }
  59. if (className) {
  60. portalTarget.className = className;
  61. }
  62. }, [ style, className ]);
  63. useEffect(() => {
  64. if (portalTarget && getRef) {
  65. getRef(portalTarget);
  66. portalTarget.style.zIndex = `${ZINDEX_DIALOG_PORTAL}`;
  67. }
  68. }, [ portalTarget, getRef ]);
  69. useEffect(() => {
  70. const size = {
  71. width: 1,
  72. height: 1
  73. };
  74. const debouncedResizeCallback = debounce((entries: ResizeObserverEntry[]) => {
  75. const { contentRect } = entries[0];
  76. if (contentRect.width !== size.width || contentRect.height !== size.height) {
  77. setSize?.(contentRect);
  78. clearTimeout(timerRef.current);
  79. timerRef.current = window.setTimeout(() => {
  80. portalTarget.style.visibility = 'visible';
  81. onVisible?.();
  82. }, 100);
  83. }
  84. }, 20); // 20ms delay
  85. // Create and observe ResizeObserver
  86. const observer = new ResizeObserver(debouncedResizeCallback);
  87. const target = targetSelector ? portalTarget.querySelector(targetSelector) : portalTarget;
  88. if (document.body) {
  89. document.body.appendChild(portalTarget);
  90. observer.observe(target ?? portalTarget);
  91. }
  92. return () => {
  93. observer.unobserve(target ?? portalTarget);
  94. if (document.body) {
  95. document.body.removeChild(portalTarget);
  96. }
  97. };
  98. }, [ clientWidth ]);
  99. return ReactDOM.createPortal(
  100. children,
  101. portalTarget
  102. );
  103. }
  104. export default DialogPortal;