|
|
@ -1,9 +1,19 @@
|
|
|
|
import { CSSProperties, defineComponent, inject } from 'vue';
|
|
|
|
import {
|
|
|
|
|
|
|
|
CSSProperties,
|
|
|
|
|
|
|
|
defineComponent,
|
|
|
|
|
|
|
|
inject,
|
|
|
|
|
|
|
|
ref,
|
|
|
|
|
|
|
|
reactive,
|
|
|
|
|
|
|
|
watch,
|
|
|
|
|
|
|
|
onMounted,
|
|
|
|
|
|
|
|
getCurrentInstance,
|
|
|
|
|
|
|
|
onUnmounted,
|
|
|
|
|
|
|
|
} from 'vue';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import classNames from '../_util/classNames';
|
|
|
|
import classNames from '../_util/classNames';
|
|
|
|
import omit from 'omit.js';
|
|
|
|
import omit from 'omit.js';
|
|
|
|
import ResizeObserver from '../vc-resize-observer';
|
|
|
|
import ResizeObserver from '../vc-resize-observer';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
|
|
|
// import BaseMixin from '../_util/BaseMixin';
|
|
|
|
import throttleByAnimationFrame from '../_util/throttleByAnimationFrame';
|
|
|
|
import throttleByAnimationFrame from '../_util/throttleByAnimationFrame';
|
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
|
|
|
import warning from '../_util/warning';
|
|
|
|
import warning from '../_util/warning';
|
|
|
@ -50,81 +60,29 @@ const AffixProps = {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
const Affix = defineComponent({
|
|
|
|
const Affix = defineComponent({
|
|
|
|
name: 'AAffix',
|
|
|
|
name: 'AAffix',
|
|
|
|
mixins: [BaseMixin],
|
|
|
|
|
|
|
|
props: AffixProps,
|
|
|
|
props: AffixProps,
|
|
|
|
emits: ['change', 'testUpdatePosition'],
|
|
|
|
emits: ['change', 'testUpdatePosition'],
|
|
|
|
setup() {
|
|
|
|
setup(props, { slots, emit, expose }) {
|
|
|
|
return {
|
|
|
|
const configProvider = inject('configProvider', defaultConfigProvider);
|
|
|
|
configProvider: inject('configProvider', defaultConfigProvider),
|
|
|
|
const placeholderNode = ref();
|
|
|
|
};
|
|
|
|
const fixedNode = ref();
|
|
|
|
},
|
|
|
|
const state = reactive({
|
|
|
|
data() {
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
affixStyle: undefined,
|
|
|
|
affixStyle: undefined,
|
|
|
|
placeholderStyle: undefined,
|
|
|
|
placeholderStyle: undefined,
|
|
|
|
status: AffixStatus.None,
|
|
|
|
status: AffixStatus.None,
|
|
|
|
lastAffix: false,
|
|
|
|
lastAffix: false,
|
|
|
|
prevTarget: null,
|
|
|
|
prevTarget: null,
|
|
|
|
timeout: null,
|
|
|
|
timeout: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
watch: {
|
|
|
|
|
|
|
|
target(val) {
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
offsetTop() {
|
|
|
|
|
|
|
|
this.updatePosition();
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
offsetBottom() {
|
|
|
|
|
|
|
|
this.updatePosition();
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
beforeMount() {
|
|
|
|
|
|
|
|
this.updatePosition = throttleByAnimationFrame(this.updatePosition);
|
|
|
|
|
|
|
|
this.lazyUpdatePosition = throttleByAnimationFrame(this.lazyUpdatePosition);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
mounted() {
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const currentInstance = getCurrentInstance();
|
|
|
|
},
|
|
|
|
|
|
|
|
updated() {
|
|
|
|
const getOffsetTop = () => {
|
|
|
|
this.measure();
|
|
|
|
const { offset, offsetBottom } = props;
|
|
|
|
},
|
|
|
|
let { offsetTop } = props;
|
|
|
|
beforeUnmount() {
|
|
|
|
if (offsetTop === undefined) {
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
|
|
|
|
removeObserveTarget(this);
|
|
|
|
|
|
|
|
(this.updatePosition as any).cancel();
|
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/22683
|
|
|
|
|
|
|
|
(this.lazyUpdatePosition as any).cancel();
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
|
|
|
|
getOffsetTop() {
|
|
|
|
|
|
|
|
const { offset, offsetBottom } = this;
|
|
|
|
|
|
|
|
let { offsetTop } = this;
|
|
|
|
|
|
|
|
if (typeof offsetTop === 'undefined') {
|
|
|
|
|
|
|
|
offsetTop = offset;
|
|
|
|
offsetTop = offset;
|
|
|
|
warning(
|
|
|
|
warning(
|
|
|
|
typeof offset === 'undefined',
|
|
|
|
offset === undefined,
|
|
|
|
'Affix',
|
|
|
|
'Affix',
|
|
|
|
'`offset` is deprecated. Please use `offsetTop` instead.',
|
|
|
|
'`offset` is deprecated. Please use `offsetTop` instead.',
|
|
|
|
);
|
|
|
|
);
|
|
|
@ -134,26 +92,19 @@ const Affix = defineComponent({
|
|
|
|
offsetTop = 0;
|
|
|
|
offsetTop = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return offsetTop;
|
|
|
|
return offsetTop;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const getOffsetBottom = () => {
|
|
|
|
getOffsetBottom() {
|
|
|
|
return props.offsetBottom;
|
|
|
|
return this.offsetBottom;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
const measure = () => {
|
|
|
|
// =================== Measure ===================
|
|
|
|
const { status, lastAffix } = state;
|
|
|
|
measure() {
|
|
|
|
const { target } = props;
|
|
|
|
const { status, lastAffix } = this;
|
|
|
|
if (status !== AffixStatus.Prepare || !fixedNode.value || !placeholderNode.value || !target) {
|
|
|
|
const { target } = this;
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
|
|
|
status !== AffixStatus.Prepare ||
|
|
|
|
|
|
|
|
!this.$refs.fixedNode ||
|
|
|
|
|
|
|
|
!this.$refs.placeholderNode ||
|
|
|
|
|
|
|
|
!target
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const offsetTop = this.getOffsetTop();
|
|
|
|
const offsetTop = getOffsetTop();
|
|
|
|
const offsetBottom = this.getOffsetBottom();
|
|
|
|
const offsetBottom = getOffsetBottom();
|
|
|
|
|
|
|
|
|
|
|
|
const targetNode = target();
|
|
|
|
const targetNode = target();
|
|
|
|
if (!targetNode) {
|
|
|
|
if (!targetNode) {
|
|
|
@ -164,7 +115,7 @@ const Affix = defineComponent({
|
|
|
|
status: AffixStatus.None,
|
|
|
|
status: AffixStatus.None,
|
|
|
|
} as AffixState;
|
|
|
|
} as AffixState;
|
|
|
|
const targetRect = getTargetRect(targetNode);
|
|
|
|
const targetRect = getTargetRect(targetNode);
|
|
|
|
const placeholderReact = getTargetRect(this.$refs.placeholderNode as HTMLElement);
|
|
|
|
const placeholderReact = getTargetRect(placeholderNode.value as HTMLElement);
|
|
|
|
const fixedTop = getFixedTop(placeholderReact, targetRect, offsetTop);
|
|
|
|
const fixedTop = getFixedTop(placeholderReact, targetRect, offsetTop);
|
|
|
|
const fixedBottom = getFixedBottom(placeholderReact, targetRect, offsetBottom);
|
|
|
|
const fixedBottom = getFixedBottom(placeholderReact, targetRect, offsetBottom);
|
|
|
|
if (fixedTop !== undefined) {
|
|
|
|
if (fixedTop !== undefined) {
|
|
|
@ -193,41 +144,39 @@ const Affix = defineComponent({
|
|
|
|
|
|
|
|
|
|
|
|
newState.lastAffix = !!newState.affixStyle;
|
|
|
|
newState.lastAffix = !!newState.affixStyle;
|
|
|
|
if (lastAffix !== newState.lastAffix) {
|
|
|
|
if (lastAffix !== newState.lastAffix) {
|
|
|
|
this.$emit('change', newState.lastAffix);
|
|
|
|
emit('change', newState.lastAffix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update state
|
|
|
|
this.setState(newState);
|
|
|
|
Object.assign(state, newState);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const prepareMeasure = () => {
|
|
|
|
prepareMeasure() {
|
|
|
|
Object.assign(state, {
|
|
|
|
this.setState({
|
|
|
|
|
|
|
|
status: AffixStatus.Prepare,
|
|
|
|
status: AffixStatus.Prepare,
|
|
|
|
affixStyle: undefined,
|
|
|
|
affixStyle: undefined,
|
|
|
|
placeholderStyle: undefined,
|
|
|
|
placeholderStyle: undefined,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this.$forceUpdate();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Test if `updatePosition` called
|
|
|
|
// Test if `updatePosition` called
|
|
|
|
if (process.env.NODE_ENV === 'test') {
|
|
|
|
if (process.env.NODE_ENV === 'test') {
|
|
|
|
this.$emit('testUpdatePosition');
|
|
|
|
emit('testUpdatePosition');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
updatePosition() {
|
|
|
|
|
|
|
|
this.prepareMeasure();
|
|
|
|
const updatePosition = throttleByAnimationFrame(() => {
|
|
|
|
},
|
|
|
|
prepareMeasure();
|
|
|
|
lazyUpdatePosition() {
|
|
|
|
});
|
|
|
|
const { target } = this;
|
|
|
|
const lazyUpdatePosition = throttleByAnimationFrame(() => {
|
|
|
|
const { affixStyle } = this;
|
|
|
|
const { target } = props;
|
|
|
|
|
|
|
|
const { affixStyle } = state;
|
|
|
|
|
|
|
|
|
|
|
|
// Check position change before measure to make Safari smooth
|
|
|
|
// Check position change before measure to make Safari smooth
|
|
|
|
if (target && affixStyle) {
|
|
|
|
if (target && affixStyle) {
|
|
|
|
const offsetTop = this.getOffsetTop();
|
|
|
|
const offsetTop = getOffsetTop();
|
|
|
|
const offsetBottom = this.getOffsetBottom();
|
|
|
|
const offsetBottom = getOffsetBottom();
|
|
|
|
|
|
|
|
|
|
|
|
const targetNode = target();
|
|
|
|
const targetNode = target();
|
|
|
|
if (targetNode && this.$refs.placeholderNode) {
|
|
|
|
if (targetNode && placeholderNode.value) {
|
|
|
|
const targetRect = getTargetRect(targetNode);
|
|
|
|
const targetRect = getTargetRect(targetNode);
|
|
|
|
const placeholderReact = getTargetRect(this.$refs.placeholderNode as HTMLElement);
|
|
|
|
const placeholderReact = getTargetRect(placeholderNode.value as HTMLElement);
|
|
|
|
const fixedTop = getFixedTop(placeholderReact, targetRect, offsetTop);
|
|
|
|
const fixedTop = getFixedTop(placeholderReact, targetRect, offsetTop);
|
|
|
|
const fixedBottom = getFixedBottom(placeholderReact, targetRect, offsetBottom);
|
|
|
|
const fixedBottom = getFixedBottom(placeholderReact, targetRect, offsetBottom);
|
|
|
|
|
|
|
|
|
|
|
@ -240,30 +189,78 @@ const Affix = defineComponent({
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Directly call prepare measure since it's already throttled.
|
|
|
|
// Directly call prepare measure since it's already throttled.
|
|
|
|
this.prepareMeasure();
|
|
|
|
prepareMeasure();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
expose({
|
|
|
|
|
|
|
|
updatePosition,
|
|
|
|
|
|
|
|
lazyUpdatePosition,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
watch(
|
|
|
|
|
|
|
|
() => props.target,
|
|
|
|
|
|
|
|
val => {
|
|
|
|
|
|
|
|
let newTarget = null;
|
|
|
|
|
|
|
|
if (val) {
|
|
|
|
|
|
|
|
newTarget = val() || null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.prevTarget !== newTarget) {
|
|
|
|
|
|
|
|
removeObserveTarget(currentInstance);
|
|
|
|
|
|
|
|
if (newTarget) {
|
|
|
|
|
|
|
|
addObserveTarget(newTarget, currentInstance);
|
|
|
|
|
|
|
|
// Mock Event object.
|
|
|
|
|
|
|
|
updatePosition();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
state.prevTarget = newTarget;
|
|
|
|
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
watch(() => [props.offsetTop, props.offsetBottom], updatePosition);
|
|
|
|
|
|
|
|
watch(
|
|
|
|
|
|
|
|
() => state.status,
|
|
|
|
|
|
|
|
() => {
|
|
|
|
|
|
|
|
measure();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
onMounted(() => {
|
|
|
|
const { prefixCls, affixStyle, placeholderStyle, $slots, $props } = this;
|
|
|
|
const { target } = props;
|
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
|
|
|
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.
|
|
|
|
|
|
|
|
state.timeout = setTimeout(() => {
|
|
|
|
|
|
|
|
addObserveTarget(target(), currentInstance);
|
|
|
|
|
|
|
|
// Mock Event object.
|
|
|
|
|
|
|
|
updatePosition();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
|
|
|
|
clearTimeout(state.timeout);
|
|
|
|
|
|
|
|
removeObserveTarget(currentInstance);
|
|
|
|
|
|
|
|
(updatePosition as any).cancel();
|
|
|
|
|
|
|
|
// https://github.com/ant-design/ant-design/issues/22683
|
|
|
|
|
|
|
|
(lazyUpdatePosition as any).cancel();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
|
|
|
const { prefixCls } = props;
|
|
|
|
|
|
|
|
const { affixStyle, placeholderStyle } = state;
|
|
|
|
|
|
|
|
const { getPrefixCls } = configProvider;
|
|
|
|
const className = classNames({
|
|
|
|
const className = classNames({
|
|
|
|
[getPrefixCls('affix', prefixCls)]: affixStyle,
|
|
|
|
[getPrefixCls('affix', prefixCls)]: affixStyle,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
const props = omit($props, ['prefixCls', 'offsetTop', 'offsetBottom', 'target']);
|
|
|
|
const restProps = omit(props, ['prefixCls', 'offsetTop', 'offsetBottom', 'target']);
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<ResizeObserver
|
|
|
|
<ResizeObserver onResize={updatePosition}>
|
|
|
|
onResize={() => {
|
|
|
|
<div {...restProps} style={placeholderStyle} ref={placeholderNode}>
|
|
|
|
this.updatePosition();
|
|
|
|
<div class={className} ref={fixedNode} style={affixStyle}>
|
|
|
|
}}
|
|
|
|
{slots.default?.()}
|
|
|
|
>
|
|
|
|
|
|
|
|
<div {...props} style={placeholderStyle} ref="placeholderNode">
|
|
|
|
|
|
|
|
<div class={className} ref="fixedNode" style={affixStyle}>
|
|
|
|
|
|
|
|
{$slots.default?.()}
|
|
|
|
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</ResizeObserver>
|
|
|
|
</ResizeObserver>
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|