Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

DialogPortal.ts 3.4KB

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