2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2015-11-24 20:03:57 +08:00
|
|
|
import classNames from 'classnames';
|
2021-01-13 21:00:30 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2019-09-29 17:56:56 +08:00
|
|
|
import ResizeObserver from 'rc-resize-observer';
|
2020-04-29 20:22:16 +08:00
|
|
|
import { ConfigContext, ConfigConsumerProps } from '../config-provider';
|
2017-01-26 11:23:54 +08:00
|
|
|
import { throttleByAnimationFrameDecorator } from '../_util/throttleByAnimationFrame';
|
2015-08-04 14:57:18 +08:00
|
|
|
|
2019-04-24 19:53:53 +08:00
|
|
|
import {
|
|
|
|
addObserveTarget,
|
|
|
|
removeObserveTarget,
|
|
|
|
getTargetRect,
|
|
|
|
getFixedTop,
|
|
|
|
getFixedBottom,
|
|
|
|
} from './utils';
|
2016-10-21 17:44:34 +08:00
|
|
|
|
|
|
|
function getDefaultTarget() {
|
2018-05-23 21:03:32 +08:00
|
|
|
return typeof window !== 'undefined' ? window : null;
|
2017-05-25 16:54:15 +08:00
|
|
|
}
|
2016-10-21 17:44:34 +08:00
|
|
|
|
2016-06-22 13:18:43 +08:00
|
|
|
// Affix
|
|
|
|
export interface AffixProps {
|
2020-12-28 15:30:18 +08:00
|
|
|
/** 距离窗口顶部达到指定偏移量后触发 */
|
2016-07-13 11:14:24 +08:00
|
|
|
offsetTop?: number;
|
2016-09-19 10:17:07 +08:00
|
|
|
/** 距离窗口底部达到指定偏移量后触发 */
|
2016-07-13 11:14:24 +08:00
|
|
|
offsetBottom?: number;
|
|
|
|
style?: React.CSSProperties;
|
2016-09-19 10:17:07 +08:00
|
|
|
/** 固定状态改变时触发的回调函数 */
|
2016-10-21 17:44:34 +08:00
|
|
|
onChange?: (affixed?: boolean) => void;
|
2016-09-19 10:17:07 +08:00
|
|
|
/** 设置 Affix 需要监听其滚动事件的元素,值为一个返回对应 DOM 元素的函数 */
|
2017-11-22 11:49:35 +08:00
|
|
|
target?: () => Window | HTMLElement | null;
|
2016-09-14 16:18:33 +08:00
|
|
|
prefixCls?: string;
|
2019-01-31 11:20:58 +08:00
|
|
|
className?: string;
|
2020-04-18 00:31:31 +08:00
|
|
|
children: React.ReactNode;
|
2019-03-03 10:04:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
enum AffixStatus {
|
|
|
|
None,
|
|
|
|
Prepare,
|
2016-06-22 13:18:43 +08:00
|
|
|
}
|
|
|
|
|
2017-11-22 11:49:35 +08:00
|
|
|
export interface AffixState {
|
2019-03-03 10:04:21 +08:00
|
|
|
affixStyle?: React.CSSProperties;
|
|
|
|
placeholderStyle?: React.CSSProperties;
|
|
|
|
status: AffixStatus;
|
|
|
|
lastAffix: boolean;
|
2019-04-18 01:33:05 +08:00
|
|
|
|
|
|
|
prevTarget: Window | HTMLElement | null;
|
2017-11-22 11:49:35 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 10:04:21 +08:00
|
|
|
class Affix extends React.Component<AffixProps, AffixState> {
|
2020-04-29 20:22:16 +08:00
|
|
|
static contextType = ConfigContext;
|
2015-09-01 16:18:46 +08:00
|
|
|
|
2018-11-01 02:46:42 +08:00
|
|
|
state: AffixState = {
|
2019-03-03 11:40:10 +08:00
|
|
|
status: AffixStatus.None,
|
2019-03-03 10:04:21 +08:00
|
|
|
lastAffix: false,
|
2019-04-18 01:33:05 +08:00
|
|
|
prevTarget: null,
|
2018-11-01 02:46:42 +08:00
|
|
|
};
|
2016-07-13 17:22:23 +08:00
|
|
|
|
2019-03-03 10:04:21 +08:00
|
|
|
placeholderNode: HTMLDivElement;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2019-03-03 10:04:21 +08:00
|
|
|
fixedNode: HTMLDivElement;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2021-02-20 14:08:49 +08:00
|
|
|
private timeout: any;
|
2017-08-17 11:45:46 +08:00
|
|
|
|
2020-04-29 20:22:16 +08:00
|
|
|
context: ConfigConsumerProps;
|
|
|
|
|
|
|
|
private getTargetFunc() {
|
|
|
|
const { getTargetContainer } = this.context;
|
|
|
|
const { target } = this.props;
|
|
|
|
|
|
|
|
if (target !== undefined) {
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
return getTargetContainer || getDefaultTarget;
|
|
|
|
}
|
|
|
|
|
2019-03-03 10:04:21 +08:00
|
|
|
// Event handler
|
|
|
|
componentDidMount() {
|
2020-04-29 20:22:16 +08:00
|
|
|
const targetFunc = this.getTargetFunc();
|
|
|
|
if (targetFunc) {
|
2019-03-03 23:19:07 +08:00
|
|
|
// [Legacy] Wait for parent component ref has its value.
|
|
|
|
// We should use target as directly element instead of function which makes element check hard.
|
2019-03-03 10:04:21 +08:00
|
|
|
this.timeout = setTimeout(() => {
|
2020-04-29 20:22:16 +08:00
|
|
|
addObserveTarget(targetFunc(), this);
|
2019-03-03 11:40:10 +08:00
|
|
|
// Mock Event object.
|
2019-08-05 18:38:10 +08:00
|
|
|
this.updatePosition();
|
2019-03-03 10:04:21 +08:00
|
|
|
});
|
2016-07-09 16:00:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 10:04:21 +08:00
|
|
|
componentDidUpdate(prevProps: AffixProps) {
|
2019-04-18 01:33:05 +08:00
|
|
|
const { prevTarget } = this.state;
|
2020-04-29 20:22:16 +08:00
|
|
|
const targetFunc = this.getTargetFunc();
|
2019-04-18 01:33:05 +08:00
|
|
|
let newTarget = null;
|
2020-04-29 20:22:16 +08:00
|
|
|
if (targetFunc) {
|
|
|
|
newTarget = targetFunc() || null;
|
2019-04-18 01:33:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (prevTarget !== newTarget) {
|
2019-03-03 10:04:21 +08:00
|
|
|
removeObserveTarget(this);
|
2019-04-18 01:33:05 +08:00
|
|
|
if (newTarget) {
|
|
|
|
addObserveTarget(newTarget, this);
|
2019-03-03 11:40:10 +08:00
|
|
|
// Mock Event object.
|
2019-08-05 18:38:10 +08:00
|
|
|
this.updatePosition();
|
2019-03-03 10:04:21 +08:00
|
|
|
}
|
2019-04-18 01:33:05 +08:00
|
|
|
|
2020-06-08 18:01:50 +08:00
|
|
|
// eslint-disable-next-line react/no-did-update-set-state
|
2019-04-18 01:33:05 +08:00
|
|
|
this.setState({ prevTarget: newTarget });
|
2016-07-11 14:52:21 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 23:19:07 +08:00
|
|
|
if (
|
|
|
|
prevProps.offsetTop !== this.props.offsetTop ||
|
|
|
|
prevProps.offsetBottom !== this.props.offsetBottom
|
|
|
|
) {
|
2019-08-05 18:38:10 +08:00
|
|
|
this.updatePosition();
|
2019-03-03 23:19:07 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 10:04:21 +08:00
|
|
|
this.measure();
|
2018-03-14 16:22:09 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 10:04:21 +08:00
|
|
|
componentWillUnmount() {
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
removeObserveTarget(this);
|
2019-03-03 11:40:10 +08:00
|
|
|
(this.updatePosition as any).cancel();
|
2020-03-31 21:25:40 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/22683
|
|
|
|
(this.lazyUpdatePosition as any).cancel();
|
2019-03-03 10:04:21 +08:00
|
|
|
}
|
2015-08-03 16:49:42 +08:00
|
|
|
|
2019-04-24 19:53:53 +08:00
|
|
|
getOffsetTop = () => {
|
2019-12-31 11:54:39 +08:00
|
|
|
const { offsetBottom } = this.props;
|
2019-04-24 19:53:53 +08:00
|
|
|
let { offsetTop } = this.props;
|
|
|
|
if (offsetBottom === undefined && offsetTop === undefined) {
|
|
|
|
offsetTop = 0;
|
|
|
|
}
|
|
|
|
return offsetTop;
|
|
|
|
};
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-12-09 17:12:32 +08:00
|
|
|
getOffsetBottom = () => this.props.offsetBottom;
|
2019-04-24 19:53:53 +08:00
|
|
|
|
2019-03-03 10:04:21 +08:00
|
|
|
savePlaceholderNode = (node: HTMLDivElement) => {
|
|
|
|
this.placeholderNode = node;
|
|
|
|
};
|
2018-03-14 16:22:09 +08:00
|
|
|
|
2019-03-03 10:04:21 +08:00
|
|
|
saveFixedNode = (node: HTMLDivElement) => {
|
|
|
|
this.fixedNode = node;
|
|
|
|
};
|
2015-08-03 16:49:42 +08:00
|
|
|
|
2019-03-03 10:04:21 +08:00
|
|
|
// =================== Measure ===================
|
|
|
|
measure = () => {
|
2019-03-03 11:40:10 +08:00
|
|
|
const { status, lastAffix } = this.state;
|
2020-04-29 20:22:16 +08:00
|
|
|
const { onChange } = this.props;
|
|
|
|
const targetFunc = this.getTargetFunc();
|
|
|
|
if (status !== AffixStatus.Prepare || !this.fixedNode || !this.placeholderNode || !targetFunc) {
|
2019-03-03 10:04:21 +08:00
|
|
|
return;
|
2015-08-04 15:03:00 +08:00
|
|
|
}
|
2019-03-03 11:40:10 +08:00
|
|
|
|
2019-04-24 19:53:53 +08:00
|
|
|
const offsetTop = this.getOffsetTop();
|
|
|
|
const offsetBottom = this.getOffsetBottom();
|
2019-03-03 11:40:10 +08:00
|
|
|
|
2020-04-29 20:22:16 +08:00
|
|
|
const targetNode = targetFunc();
|
2019-03-03 10:04:21 +08:00
|
|
|
if (!targetNode) {
|
2016-12-03 16:19:22 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-08-18 11:27:37 +08:00
|
|
|
|
2019-03-03 10:04:21 +08:00
|
|
|
const newState: Partial<AffixState> = {
|
|
|
|
status: AffixStatus.None,
|
|
|
|
};
|
|
|
|
const targetRect = getTargetRect(targetNode);
|
|
|
|
const placeholderReact = getTargetRect(this.placeholderNode);
|
2019-04-24 19:53:53 +08:00
|
|
|
const fixedTop = getFixedTop(placeholderReact, targetRect, offsetTop);
|
|
|
|
const fixedBottom = getFixedBottom(placeholderReact, targetRect, offsetBottom);
|
2016-08-18 11:27:37 +08:00
|
|
|
|
2019-04-24 19:53:53 +08:00
|
|
|
if (fixedTop !== undefined) {
|
2019-03-03 10:04:21 +08:00
|
|
|
newState.affixStyle = {
|
|
|
|
position: 'fixed',
|
2019-04-24 19:53:53 +08:00
|
|
|
top: fixedTop,
|
2019-03-03 23:25:52 +08:00
|
|
|
width: placeholderReact.width,
|
|
|
|
height: placeholderReact.height,
|
2019-03-03 10:04:21 +08:00
|
|
|
};
|
|
|
|
newState.placeholderStyle = {
|
|
|
|
width: placeholderReact.width,
|
|
|
|
height: placeholderReact.height,
|
|
|
|
};
|
2019-04-24 19:53:53 +08:00
|
|
|
} else if (fixedBottom !== undefined) {
|
2019-03-03 10:04:21 +08:00
|
|
|
newState.affixStyle = {
|
|
|
|
position: 'fixed',
|
2019-04-24 19:53:53 +08:00
|
|
|
bottom: fixedBottom,
|
2019-03-03 23:25:52 +08:00
|
|
|
width: placeholderReact.width,
|
|
|
|
height: placeholderReact.height,
|
2019-03-03 10:04:21 +08:00
|
|
|
};
|
|
|
|
newState.placeholderStyle = {
|
|
|
|
width: placeholderReact.width,
|
|
|
|
height: placeholderReact.height,
|
|
|
|
};
|
|
|
|
}
|
2017-09-08 09:31:07 +08:00
|
|
|
|
2019-03-03 11:40:10 +08:00
|
|
|
newState.lastAffix = !!newState.affixStyle;
|
|
|
|
if (onChange && lastAffix !== newState.lastAffix) {
|
|
|
|
onChange(newState.lastAffix);
|
|
|
|
}
|
|
|
|
|
2019-03-03 10:04:21 +08:00
|
|
|
this.setState(newState as AffixState);
|
2019-03-03 11:40:10 +08:00
|
|
|
};
|
2018-03-14 16:22:09 +08:00
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
// @ts-ignore TS6133
|
|
|
|
prepareMeasure = () => {
|
|
|
|
// event param is used before. Keep compatible ts define here.
|
|
|
|
this.setState({
|
|
|
|
status: AffixStatus.Prepare,
|
|
|
|
affixStyle: undefined,
|
|
|
|
placeholderStyle: undefined,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test if `updatePosition` called
|
|
|
|
if (process.env.NODE_ENV === 'test') {
|
|
|
|
const { onTestUpdatePosition } = this.props as any;
|
2021-02-19 18:26:53 +08:00
|
|
|
onTestUpdatePosition?.();
|
2019-08-05 18:38:10 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Handle realign logic
|
|
|
|
@throttleByAnimationFrameDecorator()
|
|
|
|
updatePosition() {
|
|
|
|
this.prepareMeasure();
|
|
|
|
}
|
|
|
|
|
|
|
|
@throttleByAnimationFrameDecorator()
|
|
|
|
lazyUpdatePosition() {
|
2020-04-29 20:22:16 +08:00
|
|
|
const targetFunc = this.getTargetFunc();
|
2019-08-05 18:38:10 +08:00
|
|
|
const { affixStyle } = this.state;
|
|
|
|
|
|
|
|
// Check position change before measure to make Safari smooth
|
2020-04-29 20:22:16 +08:00
|
|
|
if (targetFunc && affixStyle) {
|
2019-08-05 18:38:10 +08:00
|
|
|
const offsetTop = this.getOffsetTop();
|
|
|
|
const offsetBottom = this.getOffsetBottom();
|
|
|
|
|
2020-04-29 20:22:16 +08:00
|
|
|
const targetNode = targetFunc();
|
2020-02-12 13:03:32 +08:00
|
|
|
if (targetNode && this.placeholderNode) {
|
2019-08-05 18:38:10 +08:00
|
|
|
const targetRect = getTargetRect(targetNode);
|
|
|
|
const placeholderReact = getTargetRect(this.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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Directly call prepare measure since it's already throttled.
|
|
|
|
this.prepareMeasure();
|
|
|
|
}
|
|
|
|
|
2019-03-03 10:04:21 +08:00
|
|
|
// =================== Render ===================
|
2020-04-29 20:22:16 +08:00
|
|
|
render = () => {
|
|
|
|
const { getPrefixCls } = this.context;
|
2019-07-16 20:48:03 +08:00
|
|
|
const { affixStyle, placeholderStyle } = this.state;
|
|
|
|
const { prefixCls, children } = this.props;
|
2015-11-24 20:03:57 +08:00
|
|
|
const className = classNames({
|
2019-03-03 10:04:21 +08:00
|
|
|
[getPrefixCls('affix', prefixCls)]: affixStyle,
|
2015-10-09 10:55:52 +08:00
|
|
|
});
|
2015-08-03 16:49:42 +08:00
|
|
|
|
2019-04-07 10:06:49 +08:00
|
|
|
let props = omit(this.props, ['prefixCls', 'offsetTop', 'offsetBottom', 'target', 'onChange']);
|
|
|
|
// Omit this since `onTestUpdatePosition` only works on test.
|
|
|
|
if (process.env.NODE_ENV === 'test') {
|
2021-01-13 21:00:30 +08:00
|
|
|
props = omit(props as typeof props & { onTestUpdatePosition: any }, ['onTestUpdatePosition']);
|
2019-04-07 10:06:49 +08:00
|
|
|
}
|
2019-07-16 20:48:03 +08:00
|
|
|
|
2015-08-03 16:49:42 +08:00
|
|
|
return (
|
2019-07-16 20:48:03 +08:00
|
|
|
<ResizeObserver
|
|
|
|
onResize={() => {
|
|
|
|
this.updatePosition();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div {...props} ref={this.savePlaceholderNode}>
|
|
|
|
{affixStyle && <div style={placeholderStyle} aria-hidden="true" />}
|
|
|
|
<div className={className} ref={this.saveFixedNode} style={affixStyle}>
|
|
|
|
<ResizeObserver
|
|
|
|
onResize={() => {
|
|
|
|
this.updatePosition();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</ResizeObserver>
|
|
|
|
</div>
|
2015-08-04 14:57:18 +08:00
|
|
|
</div>
|
2019-07-16 20:48:03 +08:00
|
|
|
</ResizeObserver>
|
2015-08-03 16:49:42 +08:00
|
|
|
);
|
2018-12-05 19:12:18 +08:00
|
|
|
};
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
2019-03-03 10:04:21 +08:00
|
|
|
|
|
|
|
export default Affix;
|