ant-design/components/affix/index.tsx

225 lines
6.0 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2019-03-03 10:04:21 +08:00
import { polyfill } from 'react-lifecycles-compat';
import classNames from 'classnames';
2016-09-14 16:59:45 +08:00
import omit from 'omit.js';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import { throttleByAnimationFrameDecorator } from '../_util/throttleByAnimationFrame';
2015-08-04 14:57:18 +08:00
2019-03-03 10:04:21 +08:00
import warning from '../_util/warning';
2019-03-03 11:40:10 +08:00
import { addObserveTarget, removeObserveTarget, getTargetRect } from './utils';
function getDefaultTarget() {
2018-05-23 21:03:32 +08:00
return typeof window !== 'undefined' ? window : null;
}
2016-06-22 13:18:43 +08:00
// Affix
export interface AffixProps {
/**
*
*/
2016-07-13 11:14:24 +08:00
offsetTop?: number;
offset?: number;
/** 距离窗口底部达到指定偏移量后触发 */
2016-07-13 11:14:24 +08:00
offsetBottom?: number;
style?: React.CSSProperties;
/** 固定状态改变时触发的回调函数 */
onChange?: (affixed?: boolean) => void;
/** 设置 Affix 需要监听其滚动事件的元素,值为一个返回对应 DOM 元素的函数 */
2017-11-22 11:49:35 +08:00
target?: () => Window | HTMLElement | null;
prefixCls?: string;
2019-01-31 11:20:58 +08:00
className?: string;
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;
2017-11-22 11:49:35 +08:00
}
2019-03-03 10:04:21 +08:00
class Affix extends React.Component<AffixProps, AffixState> {
static defaultProps = {
target: getDefaultTarget,
2016-08-23 21:00:35 +08:00
};
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,
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;
fixedNode: HTMLDivElement;
2019-03-03 11:40:10 +08:00
private timeout: number;
2017-08-17 11:45:46 +08:00
2019-03-03 10:04:21 +08:00
// Event handler
componentDidMount() {
const { target } = this.props;
if (target) {
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(() => {
addObserveTarget(target(), this);
2019-03-03 11:40:10 +08:00
// Mock Event object.
this.updatePosition({} as Event);
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) {
const { target } = this.props;
if (prevProps.target !== target) {
removeObserveTarget(this);
if (target) {
addObserveTarget(target(), this);
2019-03-03 11:40:10 +08:00
// Mock Event object.
2019-03-03 23:19:07 +08:00
this.updatePosition({} as Event);
2019-03-03 10:04:21 +08:00
}
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
) {
this.updatePosition({} as Event);
}
2019-03-03 10:04:21 +08:00
this.measure();
}
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();
2019-03-03 10:04:21 +08:00
}
2015-08-03 16:49:42 +08:00
2019-03-03 10:04:21 +08:00
savePlaceholderNode = (node: HTMLDivElement) => {
this.placeholderNode = node;
};
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 ===================
// Handle realign logic
@throttleByAnimationFrameDecorator()
2019-03-03 11:40:10 +08:00
// @ts-ignore TS6133
2019-03-03 10:04:21 +08:00
updatePosition(e: Event) {
2019-03-03 11:40:10 +08:00
// event param is used before. Keep compatible ts define here.
2019-03-03 10:04:21 +08:00
this.setState({
status: AffixStatus.Prepare,
affixStyle: undefined,
placeholderStyle: undefined,
});
}
2015-08-03 16:49:42 +08:00
2019-03-03 10:04:21 +08:00
measure = () => {
2019-03-03 11:40:10 +08:00
const { status, lastAffix } = this.state;
const { target, offset, offsetBottom, onChange } = this.props;
2019-03-03 10:04:21 +08:00
if (status !== AffixStatus.Prepare || !this.fixedNode || !this.placeholderNode || !target) {
return;
2015-08-04 15:03:00 +08:00
}
2019-03-03 11:40:10 +08:00
2019-03-03 10:04:21 +08:00
let { offsetTop } = this.props;
if (typeof offsetTop === 'undefined') {
offsetTop = offset;
warning(
typeof offset === 'undefined',
'Affix',
'`offset` is deprecated. Please use `offsetTop` instead.',
);
}
2019-03-03 11:40:10 +08:00
if (offsetBottom === undefined && offsetTop === undefined) {
offsetTop = 0;
}
2019-03-03 10:04:21 +08:00
const targetNode = target();
if (!targetNode) {
return;
}
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-03-03 10:04:21 +08:00
if (offsetTop !== undefined && targetRect.top > placeholderReact.top - offsetTop) {
newState.affixStyle = {
position: 'fixed',
2019-03-03 11:40:10 +08:00
top: offsetTop + targetRect.top,
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-03-03 11:40:10 +08:00
} else if (
offsetBottom !== undefined &&
targetRect.bottom < placeholderReact.bottom + offsetBottom
) {
const targetBottomOffset = targetNode === window ? 0 : window.innerHeight - targetRect.bottom;
2019-03-03 10:04:21 +08:00
newState.affixStyle = {
position: 'fixed',
2019-03-03 11:40:10 +08:00
bottom: offsetBottom + targetBottomOffset,
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-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
};
2019-03-03 10:04:21 +08:00
// =================== Render ===================
renderAffix = ({ getPrefixCls }: ConfigConsumerProps) => {
2019-03-03 10:04:21 +08:00
const { affixStyle, placeholderStyle, status } = this.state;
const { prefixCls, style, children } = this.props;
const className = classNames({
2019-03-03 10:04:21 +08:00
[getPrefixCls('affix', prefixCls)]: affixStyle,
});
2015-08-03 16:49:42 +08:00
2018-12-07 20:02:01 +08:00
const props = omit(this.props, [
'prefixCls',
'offsetTop',
'offsetBottom',
'target',
'onChange',
]);
2019-03-03 10:04:21 +08:00
const mergedPlaceholderStyle = {
...(status === AffixStatus.None ? placeholderStyle : null),
2019-03-03 11:40:10 +08:00
...style,
2019-03-03 10:04:21 +08:00
};
2015-08-03 16:49:42 +08:00
return (
2019-03-03 10:04:21 +08:00
<div {...props} style={mergedPlaceholderStyle} ref={this.savePlaceholderNode}>
<div className={className} ref={this.saveFixedNode} style={this.state.affixStyle}>
2019-03-03 10:04:21 +08:00
{children}
2015-08-04 14:57:18 +08:00
</div>
2015-08-03 16:49:42 +08:00
</div>
);
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderAffix}</ConfigConsumer>;
}
}
2019-03-03 10:04:21 +08:00
polyfill(Affix);
export default Affix;