|
| 1 | +import PropTypes from './vue-types'; |
| 2 | +import switchScrollingEffect from './switchScrollingEffect'; |
| 3 | +import setStyle from './setStyle'; |
| 4 | +import Portal from './Portal'; |
| 5 | + |
| 6 | +let openCount = 0; |
| 7 | +const windowIsUndefined = !( |
| 8 | + typeof window !== 'undefined' && |
| 9 | + window.document && |
| 10 | + window.document.createElement |
| 11 | +); |
| 12 | +// https://github.com/ant-design/ant-design/issues/19340 |
| 13 | +// https://github.com/ant-design/ant-design/issues/19332 |
| 14 | +let cacheOverflow = {}; |
| 15 | + |
| 16 | +export default { |
| 17 | + name: 'PortalWrapper', |
| 18 | + props: { |
| 19 | + wrapperClassName: PropTypes.string, |
| 20 | + forceRender: PropTypes.bool, |
| 21 | + getContainer: PropTypes.any, |
| 22 | + children: PropTypes.func, |
| 23 | + visible: PropTypes.bool, |
| 24 | + }, |
| 25 | + data() { |
| 26 | + const { visible } = this.$props; |
| 27 | + openCount = visible ? openCount + 1 : openCount; |
| 28 | + return {}; |
| 29 | + }, |
| 30 | + updated() { |
| 31 | + this.setWrapperClassName(); |
| 32 | + }, |
| 33 | + watch: { |
| 34 | + visible(val) { |
| 35 | + openCount = val ? openCount + 1 : openCount - 1; |
| 36 | + }, |
| 37 | + getContainer(getContainer, prevGetContainer) { |
| 38 | + const getContainerIsFunc = |
| 39 | + typeof getContainer === 'function' && typeof prevGetContainer === 'function'; |
| 40 | + if ( |
| 41 | + getContainerIsFunc |
| 42 | + ? getContainer.toString() !== prevGetContainer.toString() |
| 43 | + : getContainer !== prevGetContainer |
| 44 | + ) { |
| 45 | + this.removeCurrentContainer(false); |
| 46 | + } |
| 47 | + }, |
| 48 | + }, |
| 49 | + beforeDestroy() { |
| 50 | + const { visible } = this.$props; |
| 51 | + // 离开时不会 render, 导到离开时数值不变,改用 func 。。 |
| 52 | + openCount = visible && openCount ? openCount - 1 : openCount; |
| 53 | + this.removeCurrentContainer(visible); |
| 54 | + }, |
| 55 | + methods: { |
| 56 | + getParent() { |
| 57 | + const { getContainer } = this.$props; |
| 58 | + if (getContainer) { |
| 59 | + if (typeof getContainer === 'string') { |
| 60 | + return document.querySelectorAll(getContainer)[0]; |
| 61 | + } |
| 62 | + if (typeof getContainer === 'function') { |
| 63 | + return getContainer(); |
| 64 | + } |
| 65 | + if (typeof getContainer === 'object' && getContainer instanceof window.HTMLElement) { |
| 66 | + return getContainer; |
| 67 | + } |
| 68 | + } |
| 69 | + return document.body; |
| 70 | + }, |
| 71 | + |
| 72 | + getDomContainer() { |
| 73 | + if (windowIsUndefined) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + if (!this.container) { |
| 77 | + this.container = document.createElement('div'); |
| 78 | + const parent = this.getParent(); |
| 79 | + if (parent) { |
| 80 | + parent.appendChild(this.container); |
| 81 | + } |
| 82 | + } |
| 83 | + this.setWrapperClassName(); |
| 84 | + return this.container; |
| 85 | + }, |
| 86 | + |
| 87 | + setWrapperClassName() { |
| 88 | + const { wrapperClassName } = this.$props; |
| 89 | + if (this.container && wrapperClassName && wrapperClassName !== this.container.className) { |
| 90 | + this.container.className = wrapperClassName; |
| 91 | + } |
| 92 | + }, |
| 93 | + |
| 94 | + savePortal(c) { |
| 95 | + // Warning: don't rename _component |
| 96 | + // https://github.com/react-component/util/pull/65#discussion_r352407916 |
| 97 | + this._component = c; |
| 98 | + }, |
| 99 | + |
| 100 | + removeCurrentContainer() { |
| 101 | + this.container = null; |
| 102 | + this._component = null; |
| 103 | + }, |
| 104 | + |
| 105 | + /** |
| 106 | + * Enhance ./switchScrollingEffect |
| 107 | + * 1. Simulate document body scroll bar with |
| 108 | + * 2. Record body has overflow style and recover when all of PortalWrapper invisible |
| 109 | + * 3. Disable body scroll when PortalWrapper has open |
| 110 | + * |
| 111 | + * @memberof PortalWrapper |
| 112 | + */ |
| 113 | + switchScrollingEffect() { |
| 114 | + if (openCount === 1 && !Object.keys(cacheOverflow).length) { |
| 115 | + switchScrollingEffect(); |
| 116 | + // Must be set after switchScrollingEffect |
| 117 | + cacheOverflow = setStyle({ |
| 118 | + overflow: 'hidden', |
| 119 | + overflowX: 'hidden', |
| 120 | + overflowY: 'hidden', |
| 121 | + }); |
| 122 | + } else if (!openCount) { |
| 123 | + setStyle(cacheOverflow); |
| 124 | + cacheOverflow = {}; |
| 125 | + switchScrollingEffect(true); |
| 126 | + } |
| 127 | + }, |
| 128 | + }, |
| 129 | + |
| 130 | + render() { |
| 131 | + const { children, forceRender, visible } = this.$props; |
| 132 | + let portal = null; |
| 133 | + const childProps = { |
| 134 | + getOpenCount: () => openCount, |
| 135 | + getContainer: this.getDomContainer, |
| 136 | + switchScrollingEffect: this.switchScrollingEffect, |
| 137 | + }; |
| 138 | + if (forceRender || visible || this._component) { |
| 139 | + portal = ( |
| 140 | + <Portal |
| 141 | + getContainer={this.getDomContainer} |
| 142 | + children={children(childProps)} |
| 143 | + {...{ |
| 144 | + directives: [ |
| 145 | + { |
| 146 | + name: 'ant-ref', |
| 147 | + value: this.savePortal, |
| 148 | + }, |
| 149 | + ], |
| 150 | + }} |
| 151 | + ></Portal> |
| 152 | + ); |
| 153 | + } |
| 154 | + return portal; |
| 155 | + }, |
| 156 | +}; |
0 commit comments