2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import omit from 'omit.js';
|
2020-03-07 11:45:13 +00:00
|
|
|
import ResizeObserver from '../vc-resize-observer';
|
2019-01-12 03:33:27 +00:00
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
|
|
|
import throttleByAnimationFrame from '../_util/throttleByAnimationFrame';
|
2019-03-11 03:54:14 +00:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2019-08-28 02:50:19 +00:00
|
|
|
import Base from '../base';
|
2020-03-07 11:45:13 +00:00
|
|
|
import warning from '../_util/warning';
|
|
|
|
import {
|
|
|
|
addObserveTarget,
|
|
|
|
removeObserveTarget,
|
|
|
|
getTargetRect,
|
|
|
|
getFixedTop,
|
|
|
|
getFixedBottom,
|
|
|
|
} from './utils';
|
2018-03-01 14:47:08 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function getDefaultTarget() {
|
|
|
|
return typeof window !== 'undefined' ? window : null;
|
2018-03-01 14:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Affix
|
|
|
|
const AffixProps = {
|
|
|
|
/**
|
|
|
|
* 距离窗口顶部达到指定偏移量后触发
|
|
|
|
*/
|
|
|
|
offsetTop: PropTypes.number,
|
|
|
|
offset: PropTypes.number,
|
|
|
|
/** 距离窗口底部达到指定偏移量后触发 */
|
|
|
|
offsetBottom: PropTypes.number,
|
|
|
|
/** 固定状态改变时触发的回调函数 */
|
|
|
|
// onChange?: (affixed?: boolean) => void;
|
|
|
|
/** 设置 Affix 需要监听其滚动事件的元素,值为一个返回对应 DOM 元素的函数 */
|
2020-03-07 11:45:13 +00:00
|
|
|
target: PropTypes.func.def(getDefaultTarget),
|
2018-03-01 14:47:08 +00:00
|
|
|
prefixCls: PropTypes.string,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2020-03-07 11:45:13 +00:00
|
|
|
const AffixStatus = {
|
|
|
|
None: 'none',
|
|
|
|
Prepare: 'Prepare',
|
|
|
|
};
|
2018-09-19 05:21:57 +00:00
|
|
|
const Affix = {
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'AAffix',
|
2018-03-01 14:47:08 +00:00
|
|
|
props: AffixProps,
|
2018-03-02 08:54:22 +00:00
|
|
|
mixins: [BaseMixin],
|
2019-03-11 03:54:14 +00:00
|
|
|
inject: {
|
2019-09-11 14:35:25 +00:00
|
|
|
configProvider: { default: () => ConfigConsumerProps },
|
2019-03-11 03:54:14 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
2018-03-01 14:47:08 +00:00
|
|
|
return {
|
|
|
|
affixStyle: undefined,
|
|
|
|
placeholderStyle: undefined,
|
2020-03-07 11:45:13 +00:00
|
|
|
status: AffixStatus.None,
|
|
|
|
lastAffix: false,
|
|
|
|
prevTarget: null,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-01 14:47:08 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
beforeMount() {
|
|
|
|
this.updatePosition = throttleByAnimationFrame(this.updatePosition);
|
2020-03-07 11:45:13 +00:00
|
|
|
this.lazyUpdatePosition = throttleByAnimationFrame(this.lazyUpdatePosition);
|
2018-03-02 08:54:22 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
mounted() {
|
2020-03-07 11:45:13 +00:00
|
|
|
const { target } = this;
|
|
|
|
if (target) {
|
|
|
|
// [Legacy] Wait for parent component ref has its value.
|
|
|
|
// We should use target as directly element instead of function which makes element check hard.
|
|
|
|
this.timeout = setTimeout(() => {
|
|
|
|
addObserveTarget(target(), this);
|
|
|
|
// Mock Event object.
|
|
|
|
this.updatePosition();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
updated() {
|
|
|
|
this.measure();
|
2018-03-01 14:47:08 +00:00
|
|
|
},
|
|
|
|
watch: {
|
2019-01-12 03:33:27 +00:00
|
|
|
target(val) {
|
2020-03-07 11:45:13 +00:00
|
|
|
let newTarget = null;
|
|
|
|
if (val) {
|
|
|
|
newTarget = val() || null;
|
|
|
|
}
|
|
|
|
if (this.prevTarget !== newTarget) {
|
|
|
|
removeObserveTarget(this);
|
|
|
|
if (newTarget) {
|
|
|
|
addObserveTarget(newTarget, this);
|
|
|
|
// Mock Event object.
|
|
|
|
this.updatePosition();
|
|
|
|
}
|
|
|
|
this.prevTarget = newTarget;
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-02-01 09:23:00 +00:00
|
|
|
offsetTop() {
|
2020-03-07 11:45:13 +00:00
|
|
|
this.updatePosition();
|
2018-09-05 13:28:54 +00:00
|
|
|
},
|
2019-02-01 09:23:00 +00:00
|
|
|
offsetBottom() {
|
2020-03-07 11:45:13 +00:00
|
|
|
this.updatePosition();
|
2018-03-01 14:47:08 +00:00
|
|
|
},
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
beforeDestroy() {
|
|
|
|
clearTimeout(this.timeout);
|
2020-03-07 11:45:13 +00:00
|
|
|
removeObserveTarget(this);
|
2019-01-12 03:33:27 +00:00
|
|
|
this.updatePosition.cancel();
|
2020-05-15 06:09:19 +00:00
|
|
|
// https://github.com/ant-design/ant-design/issues/22683
|
|
|
|
this.lazyUpdatePosition.cancel();
|
2018-03-01 14:47:08 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2020-03-07 11:45:13 +00:00
|
|
|
getOffsetTop() {
|
|
|
|
const { offset, offsetBottom } = this;
|
|
|
|
let { offsetTop } = this;
|
|
|
|
if (typeof offsetTop === 'undefined') {
|
|
|
|
offsetTop = offset;
|
|
|
|
warning(
|
|
|
|
typeof offset === 'undefined',
|
|
|
|
'Affix',
|
|
|
|
'`offset` is deprecated. Please use `offsetTop` instead.',
|
|
|
|
);
|
2018-03-01 14:47:08 +00:00
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
|
|
|
|
if (offsetBottom === undefined && offsetTop === undefined) {
|
|
|
|
offsetTop = 0;
|
2018-03-01 14:47:08 +00:00
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
return offsetTop;
|
2018-03-01 14:47:08 +00:00
|
|
|
},
|
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
getOffsetBottom() {
|
|
|
|
return this.offsetBottom;
|
2018-03-01 14:47:08 +00:00
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
// =================== Measure ===================
|
|
|
|
measure() {
|
|
|
|
const { status, lastAffix } = this;
|
|
|
|
const { target } = this;
|
|
|
|
if (
|
|
|
|
status !== AffixStatus.Prepare ||
|
|
|
|
!this.$refs.fixedNode ||
|
|
|
|
!this.$refs.placeholderNode ||
|
|
|
|
!target
|
|
|
|
) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-04-06 12:56:19 +00:00
|
|
|
}
|
2018-03-01 14:47:08 +00:00
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
const offsetTop = this.getOffsetTop();
|
|
|
|
const offsetBottom = this.getOffsetBottom();
|
2018-03-01 14:47:08 +00:00
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
const targetNode = target();
|
|
|
|
if (!targetNode) {
|
|
|
|
return;
|
2018-03-01 14:47:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
const newState = {
|
|
|
|
status: AffixStatus.None,
|
|
|
|
};
|
2019-01-12 03:33:27 +00:00
|
|
|
const targetRect = getTargetRect(targetNode);
|
2020-03-07 11:45:13 +00:00
|
|
|
const placeholderReact = getTargetRect(this.$refs.placeholderNode);
|
|
|
|
const fixedTop = getFixedTop(placeholderReact, targetRect, offsetTop);
|
|
|
|
const fixedBottom = getFixedBottom(placeholderReact, targetRect, offsetBottom);
|
|
|
|
if (fixedTop !== undefined) {
|
|
|
|
newState.affixStyle = {
|
2018-03-01 14:47:08 +00:00
|
|
|
position: 'fixed',
|
2020-03-07 11:45:13 +00:00
|
|
|
top: fixedTop,
|
|
|
|
width: placeholderReact.width + 'px',
|
|
|
|
height: placeholderReact.height + 'px',
|
|
|
|
};
|
|
|
|
newState.placeholderStyle = {
|
|
|
|
width: placeholderReact.width + 'px',
|
|
|
|
height: placeholderReact.height + 'px',
|
|
|
|
};
|
|
|
|
} else if (fixedBottom !== undefined) {
|
|
|
|
newState.affixStyle = {
|
2018-03-01 14:47:08 +00:00
|
|
|
position: 'fixed',
|
2020-03-07 11:45:13 +00:00
|
|
|
bottom: fixedBottom,
|
|
|
|
width: placeholderReact.width + 'px',
|
|
|
|
height: placeholderReact.height + 'px',
|
|
|
|
};
|
|
|
|
newState.placeholderStyle = {
|
|
|
|
width: placeholderReact.width + 'px',
|
|
|
|
height: placeholderReact.height + 'px',
|
|
|
|
};
|
2018-03-01 14:47:08 +00:00
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
|
|
|
|
newState.lastAffix = !!newState.affixStyle;
|
|
|
|
if (lastAffix !== newState.lastAffix) {
|
|
|
|
this.$emit('change', newState.lastAffix);
|
2018-04-06 12:56:19 +00:00
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
|
|
|
|
this.setState(newState);
|
2018-03-01 14:47:08 +00:00
|
|
|
},
|
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
// @ts-ignore TS6133
|
|
|
|
prepareMeasure() {
|
|
|
|
this.setState({
|
|
|
|
status: AffixStatus.Prepare,
|
|
|
|
affixStyle: undefined,
|
|
|
|
placeholderStyle: undefined,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2020-03-07 11:45:13 +00:00
|
|
|
this.$forceUpdate();
|
|
|
|
|
|
|
|
// Test if `updatePosition` called
|
|
|
|
if (process.env.NODE_ENV === 'test') {
|
|
|
|
this.$emit('testUpdatePosition');
|
|
|
|
}
|
2018-03-01 14:47:08 +00:00
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
updatePosition() {
|
|
|
|
this.prepareMeasure();
|
|
|
|
},
|
|
|
|
lazyUpdatePosition() {
|
|
|
|
const { target } = this;
|
|
|
|
const { affixStyle } = this;
|
|
|
|
|
|
|
|
// Check position change before measure to make Safari smooth
|
|
|
|
if (target && affixStyle) {
|
|
|
|
const offsetTop = this.getOffsetTop();
|
|
|
|
const offsetBottom = this.getOffsetBottom();
|
2018-03-01 14:47:08 +00:00
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
const targetNode = target();
|
|
|
|
if (targetNode && this.$refs.placeholderNode) {
|
|
|
|
const targetRect = getTargetRect(targetNode);
|
|
|
|
const placeholderReact = getTargetRect(this.$refs.placeholderNode);
|
|
|
|
const fixedTop = getFixedTop(placeholderReact, targetRect, offsetTop);
|
|
|
|
const fixedBottom = getFixedBottom(placeholderReact, targetRect, offsetBottom);
|
|
|
|
|
|
|
|
if (
|
|
|
|
(fixedTop !== undefined && affixStyle.top === fixedTop) ||
|
|
|
|
(fixedBottom !== undefined && affixStyle.bottom === fixedBottom)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-01 14:47:08 +00:00
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
}
|
|
|
|
// Directly call prepare measure since it's already throttled.
|
|
|
|
this.prepareMeasure();
|
2018-03-01 14:47:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2020-03-18 06:52:02 +00:00
|
|
|
const { prefixCls, affixStyle, placeholderStyle, $slots, $props } = this;
|
2019-09-11 14:35:25 +00:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2018-03-01 14:47:08 +00:00
|
|
|
const className = classNames({
|
2019-03-11 03:54:14 +00:00
|
|
|
[getPrefixCls('affix', prefixCls)]: affixStyle,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-03-01 14:47:08 +00:00
|
|
|
|
|
|
|
const props = {
|
|
|
|
attrs: omit($props, ['prefixCls', 'offsetTop', 'offsetBottom', 'target']),
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-01 14:47:08 +00:00
|
|
|
return (
|
2020-03-07 11:45:13 +00:00
|
|
|
<ResizeObserver
|
|
|
|
onResize={() => {
|
|
|
|
this.updatePosition();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div {...props} style={placeholderStyle} ref="placeholderNode">
|
|
|
|
<div class={className} ref="fixedNode" style={affixStyle}>
|
|
|
|
{$slots.default}
|
|
|
|
</div>
|
2018-03-01 14:47:08 +00:00
|
|
|
</div>
|
2020-03-07 11:45:13 +00:00
|
|
|
</ResizeObserver>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-03-01 14:47:08 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-01 14:47:08 +00:00
|
|
|
|
2018-09-19 05:21:57 +00:00
|
|
|
/* istanbul ignore next */
|
2019-01-12 03:33:27 +00:00
|
|
|
Affix.install = function(Vue) {
|
2019-08-28 02:50:19 +00:00
|
|
|
Vue.use(Base);
|
2019-01-12 03:33:27 +00:00
|
|
|
Vue.component(Affix.name, Affix);
|
|
|
|
};
|
2018-09-19 05:21:57 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
export default Affix;
|