index.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 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. constructor(props: PortalProps) {
  35. super(props);
  36. try {
  37. this.el = document.createElement('div');
  38. } catch (e) {
  39. }
  40. this.state = {
  41. container: undefined
  42. };
  43. }
  44. componentDidMount() {
  45. if (!this.el) {
  46. this.el = document.createElement('div');
  47. }
  48. const { state, props, context } = this;
  49. const getContainer = props.getPopupContainer || context.getPopupContainer || defaultGetContainer;
  50. const container = getContainer();
  51. if (container !== state.container) {
  52. // const computedStyle = window.getComputedStyle(container);
  53. // if (computedStyle.position !== 'relative') {
  54. // container.style.position = 'relative';
  55. // }
  56. container.appendChild(this.el);
  57. this.addStyle(props.style);
  58. this.addClass(props.prefixCls, props.className);
  59. this.setState({ container });
  60. }
  61. }
  62. componentDidUpdate(prevProps: PortalProps) {
  63. // visible callback
  64. const { didUpdate } = this.props;
  65. if (didUpdate) {
  66. didUpdate(prevProps);
  67. }
  68. }
  69. componentWillUnmount() {
  70. const { container } = this.state;
  71. if (container) {
  72. container.removeChild(this.el);
  73. }
  74. }
  75. addStyle = (style = {}) => {
  76. if (this.el) {
  77. for (const key of Object.keys(style)) {
  78. this.el.style[key] = style[key];
  79. }
  80. }
  81. };
  82. addClass = (prefixCls: string, ...classNames: string[]) => {
  83. const { direction } = this.context;
  84. const cls = classnames(prefixCls, ...classNames, {
  85. [`${prefixCls}-rtl`]: direction === 'rtl'
  86. });
  87. if (this.el) {
  88. this.el.className = cls;
  89. }
  90. };
  91. render() {
  92. const { state, props } = this;
  93. if (state.container) {
  94. return createPortal(props.children, this.el);
  95. }
  96. return null;
  97. }
  98. }
  99. export default Portal;