index.tsx 3.2 KB

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