index.tsx 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import React, { useEffect, useState } from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { canUseDOM } from '../../helpers/dom';
  4. const createContainer = (zIndex: number): HTMLDivElement => {
  5. const container = document.createElement('div');
  6. container.setAttribute('class', 'portal');
  7. container.setAttribute('style', `z-index: ${zIndex};`);
  8. return container;
  9. };
  10. type Props = {
  11. zIndex?: number;
  12. children: React.ReactNode;
  13. };
  14. const Portal: React.FunctionComponent<Props> = ({
  15. zIndex = 0,
  16. children = '',
  17. }) => {
  18. const [container, setContainer] = useState(canUseDOM() ? createContainer(zIndex) : undefined);
  19. useEffect(() => {
  20. if (container) {
  21. document.body.appendChild(container);
  22. } else {
  23. const newContainer = createContainer(zIndex);
  24. setContainer(newContainer);
  25. }
  26. return (): void => {
  27. if (container) {
  28. document.body.removeChild(container);
  29. }
  30. };
  31. }, []);
  32. return container ? (
  33. ReactDOM.createPortal(children, container)
  34. ) : null;
  35. };
  36. export default Portal;