-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathindex.tsx
258 lines (242 loc) · 7.71 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import type { ComponentPublicInstance, CSSProperties, ExtractPropTypes, PropType } from 'vue';
import {
defineComponent,
ref,
reactive,
watch,
onMounted,
getCurrentInstance,
computed,
onUnmounted,
onUpdated,
} from 'vue';
import classNames from '../_util/classNames';
import ResizeObserver from '../vc-resize-observer';
import throttleByAnimationFrame from '../_util/throttleByAnimationFrame';
import { withInstall } from '../_util/type';
import {
addObserveTarget,
removeObserveTarget,
getTargetRect,
getFixedTop,
getFixedBottom,
} from './utils';
import useConfigInject from '../_util/hooks/useConfigInject';
import omit from '../_util/omit';
function getDefaultTarget() {
return typeof window !== 'undefined' ? window : null;
}
enum AffixStatus {
None,
Prepare,
}
export interface AffixState {
affixStyle?: CSSProperties;
placeholderStyle?: CSSProperties;
status: AffixStatus;
lastAffix: boolean;
prevTarget: Window | HTMLElement | null;
}
// Affix
export const affixProps = () => ({
/**
* 距离窗口顶部达到指定偏移量后触发
*/
offsetTop: Number,
/** 距离窗口底部达到指定偏移量后触发 */
offsetBottom: Number,
/** 设置 Affix 需要监听其滚动事件的元素,值为一个返回对应 DOM 元素的函数 */
target: {
type: Function as PropType<() => Window | HTMLElement | null>,
default: getDefaultTarget,
},
prefixCls: String,
/** 固定状态改变时触发的回调函数 */
onChange: Function as PropType<AffixEmits['change']>,
onTestUpdatePosition: Function as PropType<AffixEmits['testUpdatePosition']>,
});
export type AffixProps = Partial<ExtractPropTypes<ReturnType<typeof affixProps>>>;
export type AffixEmits = {
change: (lastAffix: boolean) => void;
testUpdatePosition: () => void;
};
export type AffixExpose = {
updatePosition: (...args: any[]) => void;
lazyUpdatePosition: (...args: any[]) => void;
};
export type AffixInstance = ComponentPublicInstance<AffixProps, AffixExpose>;
const Affix = defineComponent({
compatConfig: { MODE: 3 },
name: 'AAffix',
props: affixProps(),
setup(props, { slots, emit, expose }) {
const placeholderNode = ref();
const fixedNode = ref();
const state = reactive({
affixStyle: undefined,
placeholderStyle: undefined,
status: AffixStatus.None,
lastAffix: false,
prevTarget: null,
timeout: null,
});
const currentInstance = getCurrentInstance();
const offsetTop = computed(() => {
return props.offsetBottom === undefined && props.offsetTop === undefined
? 0
: props.offsetTop;
});
const offsetBottom = computed(() => props.offsetBottom);
const measure = () => {
const { status, lastAffix } = state;
const { target } = props;
if (status !== AffixStatus.Prepare || !fixedNode.value || !placeholderNode.value || !target) {
return;
}
const targetNode = target();
if (!targetNode) {
return;
}
const newState = {
status: AffixStatus.None,
} as AffixState;
const targetRect = getTargetRect(targetNode);
const placeholderRect = getTargetRect(placeholderNode.value as HTMLElement);
const fixedTop = getFixedTop(placeholderRect, targetRect, offsetTop.value);
const fixedBottom = getFixedBottom(placeholderRect, targetRect, offsetBottom.value);
if (fixedTop !== undefined) {
newState.affixStyle = {
position: 'fixed',
top: fixedTop,
width: placeholderRect.width + 'px',
height: placeholderRect.height + 'px',
};
newState.placeholderStyle = {
width: placeholderRect.width + 'px',
height: placeholderRect.height + 'px',
};
} else if (fixedBottom !== undefined) {
newState.affixStyle = {
position: 'fixed',
bottom: fixedBottom,
width: placeholderRect.width + 'px',
height: placeholderRect.height + 'px',
};
newState.placeholderStyle = {
width: placeholderRect.width + 'px',
height: placeholderRect.height + 'px',
};
}
newState.lastAffix = !!newState.affixStyle;
if (lastAffix !== newState.lastAffix) {
emit('change', newState.lastAffix);
}
// update state
Object.assign(state, newState);
};
const prepareMeasure = () => {
Object.assign(state, {
status: AffixStatus.Prepare,
affixStyle: undefined,
placeholderStyle: undefined,
});
currentInstance.update();
// Test if `updatePosition` called
if (process.env.NODE_ENV === 'test') {
emit('testUpdatePosition');
}
};
const updatePosition = throttleByAnimationFrame(() => {
prepareMeasure();
});
const lazyUpdatePosition = throttleByAnimationFrame(() => {
const { target } = props;
const { affixStyle } = state;
// Check position change before measure to make Safari smooth
if (target && affixStyle) {
const targetNode = target();
if (targetNode && placeholderNode.value) {
const targetRect = getTargetRect(targetNode);
const placeholderRect = getTargetRect(placeholderNode.value as HTMLElement);
const fixedTop = getFixedTop(placeholderRect, targetRect, offsetTop.value);
const fixedBottom = getFixedBottom(placeholderRect, targetRect, offsetBottom.value);
if (
(fixedTop !== undefined && affixStyle.top === fixedTop) ||
(fixedBottom !== undefined && affixStyle.bottom === fixedBottom)
) {
return;
}
}
}
// Directly call prepare measure since it's already throttled.
prepareMeasure();
});
expose({
updatePosition,
lazyUpdatePosition,
});
watch(
() => props.target,
val => {
const 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);
onMounted(() => {
const { target } = props;
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();
});
}
});
onUpdated(() => {
measure();
});
onUnmounted(() => {
clearTimeout(state.timeout);
removeObserveTarget(currentInstance);
(updatePosition as any).cancel();
// https://github.com/ant-design/ant-design/issues/22683
(lazyUpdatePosition as any).cancel();
});
const { prefixCls } = useConfigInject('affix', props);
return () => {
const { affixStyle, placeholderStyle } = state;
const className = classNames({
[prefixCls.value]: affixStyle,
});
const restProps = omit(props, [
'prefixCls',
'offsetTop',
'offsetBottom',
'target',
'onChange',
'onTestUpdatePosition',
]);
return (
<ResizeObserver onResize={updatePosition}>
<div {...restProps} style={placeholderStyle} ref={placeholderNode}>
<div class={className} ref={fixedNode} style={affixStyle}>
{slots.default?.()}
</div>
</div>
</ResizeObserver>
);
};
},
});
export default withInstall(Affix);