您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DrawerPortal.js 989B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // @flow
  2. import { useEffect, useState } from 'react';
  3. import ReactDOM from 'react-dom';
  4. type Props = {
  5. /**
  6. * The component(s) to be displayed within the drawer portal.
  7. */
  8. children: React$Node
  9. };
  10. /**
  11. * Component meant to render a drawer at the bottom of the screen,
  12. * by creating a portal containing the component's children.
  13. *
  14. * @returns {ReactElement}
  15. */
  16. function DrawerPortal({ children }: Props) {
  17. const [ portalTarget ] = useState(() => {
  18. const portalDiv = document.createElement('div');
  19. portalDiv.className = 'drawer-portal';
  20. return portalDiv;
  21. });
  22. useEffect(() => {
  23. if (document.body) {
  24. document.body.appendChild(portalTarget);
  25. }
  26. return () => {
  27. if (document.body) {
  28. document.body.removeChild(portalTarget);
  29. }
  30. };
  31. }, []);
  32. return ReactDOM.createPortal(
  33. children,
  34. portalTarget
  35. );
  36. }
  37. export default DrawerPortal;