index.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import React, { PureComponent } from 'react';
  2. import { createPortal } from 'react-dom';
  3. import { BASE_CLASS_PREFIX } from '@douyinfe/semi-foundation/base/constants';
  4. import PropTypes from 'prop-types';
  5. import classnames from 'classnames';
  6. import ConfigContext, { ContextValue } from '../configProvider/context';
  7. import '@douyinfe/semi-foundation/_portal/portal.scss';
  8. export interface PortalProps {
  9. children: React.ReactNode;
  10. style?: React.CSSProperties;
  11. prefixCls?: string;
  12. className?: string;
  13. getPopupContainer?: () => HTMLElement;
  14. didUpdate?: (props: PortalProps) => void
  15. }
  16. export interface PortalState {
  17. container: undefined | HTMLElement
  18. }
  19. const defaultGetContainer = () => document.body;
  20. class Portal extends PureComponent<PortalProps, PortalState> {
  21. static contextType = ConfigContext;
  22. static defaultProps = {
  23. // getPopupContainer: () => document.body,
  24. prefixCls: `${BASE_CLASS_PREFIX}-portal`,
  25. };
  26. static propTypes = {
  27. children: PropTypes.node,
  28. prefixCls: PropTypes.string,
  29. getPopupContainer: PropTypes.func,
  30. className: PropTypes.string,
  31. didUpdate: PropTypes.func,
  32. };
  33. el: HTMLElement;
  34. context: ContextValue;
  35. constructor(props: PortalProps, context: ContextValue) {
  36. super(props);
  37. this.state = {
  38. container: this.initContainer(context, true)
  39. };
  40. }
  41. componentDidMount() {
  42. const container = this.initContainer(this.context);
  43. if (container!==this.state.container) {
  44. this.setState({ container });
  45. }
  46. }
  47. initContainer = (context: ContextValue, catchError = false) => {
  48. try {
  49. let container: HTMLElement | undefined = undefined;
  50. if (!this.el || !this.state?.container || !Array.from(this.state.container.childNodes).includes(this.el)) {
  51. this.el = document.createElement('div');
  52. const getContainer = this.props.getPopupContainer || context.getPopupContainer || defaultGetContainer;
  53. const portalContainer = getContainer();
  54. portalContainer.appendChild(this.el);
  55. this.addStyle(this.props.style);
  56. this.addClass(this.props.prefixCls, context, this.props.className);
  57. container = portalContainer;
  58. return container;
  59. }
  60. } catch (e) {
  61. if (!catchError) {
  62. throw e;
  63. }
  64. }
  65. return this.state?.container;
  66. }
  67. componentDidUpdate(prevProps: PortalProps) {
  68. // visible callback
  69. const { didUpdate } = this.props;
  70. if (didUpdate) {
  71. didUpdate(prevProps);
  72. }
  73. }
  74. componentWillUnmount() {
  75. const { container } = this.state;
  76. if (container) {
  77. container.removeChild(this.el);
  78. }
  79. }
  80. addStyle = (style = {}) => {
  81. if (this.el) {
  82. for (const key of Object.keys(style)) {
  83. this.el.style[key] = style[key];
  84. }
  85. }
  86. };
  87. addClass = (prefixCls: string, context = this.context, ...classNames: string[]) => {
  88. const { direction } = context;
  89. const cls = classnames(prefixCls, ...classNames, {
  90. [`${prefixCls}-rtl`]: direction === 'rtl'
  91. });
  92. if (this.el) {
  93. this.el.className = cls;
  94. }
  95. };
  96. render() {
  97. const { state, props } = this;
  98. if (state.container) {
  99. return createPortal(props.children, this.el);
  100. }
  101. return null;
  102. }
  103. }
  104. export default Portal;