Skip to content

Commit 61f087a

Browse files
committed
perf: affix
1 parent ea2d8e2 commit 61f087a

File tree

1 file changed

+16
-41
lines changed

1 file changed

+16
-41
lines changed

components/affix/index.tsx

+16-41
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import {
77
watch,
88
onMounted,
99
getCurrentInstance,
10+
computed,
1011
onUnmounted,
12+
onUpdated,
1113
} from 'vue';
1214
import PropTypes from '../_util/vue-types';
1315
import classNames from '../_util/classNames';
1416
import omit from 'omit.js';
1517
import ResizeObserver from '../vc-resize-observer';
16-
// import BaseMixin from '../_util/BaseMixin';
1718
import throttleByAnimationFrame from '../_util/throttleByAnimationFrame';
1819
import { defaultConfigProvider } from '../config-provider';
19-
import warning from '../_util/warning';
2020
import { withInstall } from '../_util/type';
2121
import {
2222
addObserveTarget,
@@ -76,36 +76,19 @@ const Affix = defineComponent({
7676
});
7777
const currentInstance = getCurrentInstance();
7878

79-
const getOffsetTop = () => {
80-
const { offset, offsetBottom } = props;
81-
let { offsetTop } = props;
82-
if (offsetTop === undefined) {
83-
offsetTop = offset;
84-
warning(
85-
offset === undefined,
86-
'Affix',
87-
'`offset` is deprecated. Please use `offsetTop` instead.',
88-
);
89-
}
90-
91-
if (offsetBottom === undefined && offsetTop === undefined) {
92-
offsetTop = 0;
93-
}
94-
return offsetTop;
95-
};
96-
const getOffsetBottom = () => {
97-
return props.offsetBottom;
98-
};
79+
const offsetTop = computed(() => {
80+
return props.offsetBottom === undefined && props.offsetTop === undefined
81+
? 0
82+
: props.offsetTop;
83+
});
84+
const offsetBottom = computed(() => props.offsetBottom);
9985
const measure = () => {
10086
const { status, lastAffix } = state;
10187
const { target } = props;
10288
if (status !== AffixStatus.Prepare || !fixedNode.value || !placeholderNode.value || !target) {
10389
return;
10490
}
10591

106-
const offsetTop = getOffsetTop();
107-
const offsetBottom = getOffsetBottom();
108-
10992
const targetNode = target();
11093
if (!targetNode) {
11194
return;
@@ -116,8 +99,8 @@ const Affix = defineComponent({
11699
} as AffixState;
117100
const targetRect = getTargetRect(targetNode);
118101
const placeholderReact = getTargetRect(placeholderNode.value as HTMLElement);
119-
const fixedTop = getFixedTop(placeholderReact, targetRect, offsetTop);
120-
const fixedBottom = getFixedBottom(placeholderReact, targetRect, offsetBottom);
102+
const fixedTop = getFixedTop(placeholderReact, targetRect, offsetTop.value);
103+
const fixedBottom = getFixedBottom(placeholderReact, targetRect, offsetBottom.value);
121104
if (fixedTop !== undefined) {
122105
newState.affixStyle = {
123106
position: 'fixed',
@@ -155,6 +138,7 @@ const Affix = defineComponent({
155138
affixStyle: undefined,
156139
placeholderStyle: undefined,
157140
});
141+
currentInstance.update();
158142
// Test if `updatePosition` called
159143
if (process.env.NODE_ENV === 'test') {
160144
emit('testUpdatePosition');
@@ -170,16 +154,12 @@ const Affix = defineComponent({
170154

171155
// Check position change before measure to make Safari smooth
172156
if (target && affixStyle) {
173-
const offsetTop = getOffsetTop();
174-
const offsetBottom = getOffsetBottom();
175-
176157
const targetNode = target();
177158
if (targetNode && placeholderNode.value) {
178159
const targetRect = getTargetRect(targetNode);
179160
const placeholderReact = getTargetRect(placeholderNode.value as HTMLElement);
180-
const fixedTop = getFixedTop(placeholderReact, targetRect, offsetTop);
181-
const fixedBottom = getFixedBottom(placeholderReact, targetRect, offsetBottom);
182-
161+
const fixedTop = getFixedTop(placeholderReact, targetRect, offsetTop.value);
162+
const fixedBottom = getFixedBottom(placeholderReact, targetRect, offsetBottom.value);
183163
if (
184164
(fixedTop !== undefined && affixStyle.top === fixedTop) ||
185165
(fixedBottom !== undefined && affixStyle.bottom === fixedBottom)
@@ -215,13 +195,6 @@ const Affix = defineComponent({
215195
},
216196
);
217197
watch(() => [props.offsetTop, props.offsetBottom], updatePosition);
218-
watch(
219-
() => state.status,
220-
() => {
221-
measure();
222-
},
223-
);
224-
225198
onMounted(() => {
226199
const { target } = props;
227200
if (target) {
@@ -234,7 +207,9 @@ const Affix = defineComponent({
234207
});
235208
}
236209
});
237-
210+
onUpdated(() => {
211+
measure();
212+
});
238213
onUnmounted(() => {
239214
clearTimeout(state.timeout);
240215
removeObserveTarget(currentInstance);

0 commit comments

Comments
 (0)