2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import * as ReactDOM from 'react-dom';
|
2017-04-12 04:49:08 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2016-06-16 22:34:54 +08:00
|
|
|
import addEventListener from 'rc-util/lib/Dom/addEventListener';
|
2015-11-24 20:03:57 +08:00
|
|
|
import classNames from 'classnames';
|
2016-07-09 16:00:05 +08:00
|
|
|
import shallowequal from 'shallowequal';
|
2016-09-14 16:59:45 +08:00
|
|
|
import omit from 'omit.js';
|
2016-09-29 18:00:23 +08:00
|
|
|
import getScroll from '../_util/getScroll';
|
2017-01-26 11:23:54 +08:00
|
|
|
import { throttleByAnimationFrameDecorator } from '../_util/throttleByAnimationFrame';
|
2015-08-04 14:57:18 +08:00
|
|
|
|
2017-11-22 11:49:35 +08:00
|
|
|
function getTargetRect(target: HTMLElement | Window | null): ClientRect {
|
2016-08-18 11:27:37 +08:00
|
|
|
return target !== window ?
|
2017-11-22 11:49:35 +08:00
|
|
|
(target as HTMLElement).getBoundingClientRect() :
|
|
|
|
{ top: 0, left: 0, bottom: 0 } as ClientRect;
|
2016-08-18 11:27:37 +08:00
|
|
|
}
|
|
|
|
|
2017-11-22 11:49:35 +08:00
|
|
|
function getOffset(element: HTMLElement, target: HTMLElement | Window | null) {
|
2016-08-18 11:27:37 +08:00
|
|
|
const elemRect = element.getBoundingClientRect();
|
|
|
|
const targetRect = getTargetRect(target);
|
|
|
|
|
|
|
|
const scrollTop = getScroll(target, true);
|
|
|
|
const scrollLeft = getScroll(target, false);
|
|
|
|
|
|
|
|
const docElem = window.document.body;
|
|
|
|
const clientTop = docElem.clientTop || 0;
|
|
|
|
const clientLeft = docElem.clientLeft || 0;
|
2015-08-04 14:57:18 +08:00
|
|
|
|
|
|
|
return {
|
2016-08-18 11:27:37 +08:00
|
|
|
top: elemRect.top - targetRect.top +
|
|
|
|
scrollTop - clientTop,
|
|
|
|
left: elemRect.left - targetRect.left +
|
|
|
|
scrollLeft - clientLeft,
|
2017-02-21 22:17:48 +08:00
|
|
|
width: elemRect.width,
|
|
|
|
height: elemRect.height,
|
2015-08-04 14:57:18 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-10-21 17:44:34 +08:00
|
|
|
function noop() {}
|
|
|
|
|
|
|
|
function getDefaultTarget() {
|
|
|
|
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 {
|
|
|
|
/**
|
|
|
|
* 距离窗口顶部达到指定偏移量后触发
|
|
|
|
*/
|
2016-07-13 11:14:24 +08:00
|
|
|
offsetTop?: number;
|
|
|
|
offset?: 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;
|
2016-06-22 13:18:43 +08:00
|
|
|
}
|
|
|
|
|
2017-11-22 11:49:35 +08:00
|
|
|
export interface AffixState {
|
|
|
|
affixStyle: React.CSSProperties | undefined;
|
|
|
|
placeholderStyle: React.CSSProperties | undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class Affix extends React.Component<AffixProps, AffixState> {
|
2016-03-29 17:33:37 +08:00
|
|
|
static propTypes = {
|
2017-04-12 04:49:08 +08:00
|
|
|
offsetTop: PropTypes.number,
|
|
|
|
offsetBottom: PropTypes.number,
|
|
|
|
target: PropTypes.func,
|
2016-08-23 21:00:35 +08:00
|
|
|
};
|
2015-09-01 16:18:46 +08:00
|
|
|
|
2016-07-13 17:22:23 +08:00
|
|
|
scrollEvent: any;
|
|
|
|
resizeEvent: any;
|
2016-12-03 16:19:22 +08:00
|
|
|
timeout: any;
|
2016-07-13 17:22:23 +08:00
|
|
|
|
2017-08-17 11:45:46 +08:00
|
|
|
events = [
|
|
|
|
'resize',
|
|
|
|
'scroll',
|
|
|
|
'touchstart',
|
|
|
|
'touchmove',
|
|
|
|
'touchend',
|
|
|
|
'pageshow',
|
|
|
|
'load',
|
|
|
|
];
|
|
|
|
|
2017-11-22 11:49:35 +08:00
|
|
|
eventHandlers: {
|
|
|
|
[key: string]: any;
|
|
|
|
} = {};
|
2017-08-17 11:45:46 +08:00
|
|
|
|
2017-11-22 11:49:35 +08:00
|
|
|
state: AffixState = {
|
|
|
|
affixStyle: undefined,
|
|
|
|
placeholderStyle: undefined,
|
|
|
|
};
|
2015-08-03 16:49:42 +08:00
|
|
|
|
2017-11-22 12:16:19 +08:00
|
|
|
private fixedNode: HTMLElement;
|
2018-03-14 16:22:09 +08:00
|
|
|
private placeholderNode: HTMLElement;
|
2017-11-22 12:16:19 +08:00
|
|
|
|
2017-11-22 11:49:35 +08:00
|
|
|
setAffixStyle(e: any, affixStyle: React.CSSProperties | null) {
|
2016-10-21 17:44:34 +08:00
|
|
|
const { onChange = noop, target = getDefaultTarget } = this.props;
|
2016-07-09 16:00:05 +08:00
|
|
|
const originalAffixStyle = this.state.affixStyle;
|
2016-08-18 11:27:37 +08:00
|
|
|
const isWindow = target() === window;
|
|
|
|
if (e.type === 'scroll' && originalAffixStyle && affixStyle && isWindow) {
|
2016-07-11 14:52:21 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-07-09 16:00:05 +08:00
|
|
|
if (shallowequal(affixStyle, originalAffixStyle)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-22 11:49:35 +08:00
|
|
|
this.setState({ affixStyle: affixStyle as React.CSSProperties }, () => {
|
2016-07-09 16:00:05 +08:00
|
|
|
const affixed = !!this.state.affixStyle;
|
|
|
|
if ((affixStyle && !originalAffixStyle) ||
|
|
|
|
(!affixStyle && originalAffixStyle)) {
|
2016-08-18 11:27:37 +08:00
|
|
|
onChange(affixed);
|
2016-07-09 16:00:05 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-22 11:49:35 +08:00
|
|
|
setPlaceholderStyle(placeholderStyle: React.CSSProperties | null) {
|
2016-07-11 14:52:21 +08:00
|
|
|
const originalPlaceholderStyle = this.state.placeholderStyle;
|
|
|
|
if (shallowequal(placeholderStyle, originalPlaceholderStyle)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-22 11:49:35 +08:00
|
|
|
this.setState({ placeholderStyle: placeholderStyle as React.CSSProperties });
|
2016-07-11 14:52:21 +08:00
|
|
|
}
|
|
|
|
|
2018-03-14 16:22:09 +08:00
|
|
|
syncPlaceholderStyle(e: any) {
|
|
|
|
const { affixStyle } = this.state;
|
|
|
|
if (!affixStyle) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.placeholderNode.style.cssText = '';
|
|
|
|
this.setAffixStyle(e, {
|
|
|
|
...affixStyle,
|
|
|
|
width: this.placeholderNode.offsetWidth,
|
|
|
|
});
|
|
|
|
this.setPlaceholderStyle({
|
|
|
|
width: this.placeholderNode.offsetWidth,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-08 14:44:43 +08:00
|
|
|
@throttleByAnimationFrameDecorator()
|
2017-11-22 11:49:35 +08:00
|
|
|
updatePosition(e: any) {
|
2016-10-21 17:44:34 +08:00
|
|
|
let { offsetTop, offsetBottom, offset, target = getDefaultTarget } = this.props;
|
2016-08-18 11:27:37 +08:00
|
|
|
const targetNode = target();
|
2016-05-09 21:49:31 +08:00
|
|
|
|
|
|
|
// Backwards support
|
|
|
|
offsetTop = offsetTop || offset;
|
2016-08-18 11:27:37 +08:00
|
|
|
const scrollTop = getScroll(targetNode, true);
|
2016-08-22 17:18:26 +08:00
|
|
|
const affixNode = ReactDOM.findDOMNode(this) as HTMLElement;
|
|
|
|
const elemOffset = getOffset(affixNode, targetNode);
|
2016-02-29 16:44:38 +08:00
|
|
|
const elemSize = {
|
2017-09-08 09:31:07 +08:00
|
|
|
width: this.fixedNode.offsetWidth,
|
|
|
|
height: this.fixedNode.offsetHeight,
|
2016-02-29 16:44:38 +08:00
|
|
|
};
|
2015-08-03 16:49:42 +08:00
|
|
|
|
2016-07-12 16:07:12 +08:00
|
|
|
const offsetMode = {
|
2016-10-21 17:44:34 +08:00
|
|
|
top: false,
|
|
|
|
bottom: false,
|
2016-07-12 16:07:12 +08:00
|
|
|
};
|
2016-08-18 11:27:37 +08:00
|
|
|
// Default to `offsetTop=0`.
|
2016-02-29 16:44:38 +08:00
|
|
|
if (typeof offsetTop !== 'number' && typeof offsetBottom !== 'number') {
|
|
|
|
offsetMode.top = true;
|
|
|
|
offsetTop = 0;
|
|
|
|
} else {
|
|
|
|
offsetMode.top = typeof offsetTop === 'number';
|
|
|
|
offsetMode.bottom = typeof offsetBottom === 'number';
|
2015-08-03 16:49:42 +08:00
|
|
|
}
|
|
|
|
|
2016-08-18 11:27:37 +08:00
|
|
|
const targetRect = getTargetRect(targetNode);
|
2016-08-24 16:09:55 +08:00
|
|
|
const targetInnerHeight =
|
|
|
|
(targetNode as Window).innerHeight || (targetNode as HTMLElement).clientHeight;
|
2017-03-06 15:48:03 +08:00
|
|
|
if (scrollTop > elemOffset.top - (offsetTop as number) && offsetMode.top) {
|
2016-02-29 16:44:38 +08:00
|
|
|
// Fixed Top
|
2017-02-21 22:17:48 +08:00
|
|
|
const width = elemOffset.width;
|
2017-12-10 16:50:39 +08:00
|
|
|
const top = targetRect.top + (offsetTop as number);
|
2016-07-11 14:52:21 +08:00
|
|
|
this.setAffixStyle(e, {
|
2016-07-09 16:00:05 +08:00
|
|
|
position: 'fixed',
|
2017-12-10 16:50:39 +08:00
|
|
|
top,
|
2016-08-18 11:27:37 +08:00
|
|
|
left: targetRect.left + elemOffset.left,
|
2017-02-21 22:17:48 +08:00
|
|
|
width,
|
2016-07-09 16:00:05 +08:00
|
|
|
});
|
2017-01-26 11:23:54 +08:00
|
|
|
this.setPlaceholderStyle({
|
2017-02-21 22:17:48 +08:00
|
|
|
width,
|
2017-09-19 16:31:37 +08:00
|
|
|
height: elemSize.height,
|
2016-07-11 14:52:21 +08:00
|
|
|
});
|
2016-08-18 11:27:37 +08:00
|
|
|
} else if (
|
2017-03-06 15:48:03 +08:00
|
|
|
scrollTop < elemOffset.top + elemSize.height + (offsetBottom as number) - targetInnerHeight &&
|
2016-08-18 11:27:37 +08:00
|
|
|
offsetMode.bottom
|
|
|
|
) {
|
2016-02-29 16:44:38 +08:00
|
|
|
// Fixed Bottom
|
2016-08-18 11:27:37 +08:00
|
|
|
const targetBottomOffet = targetNode === window ? 0 : (window.innerHeight - targetRect.bottom);
|
2017-02-21 22:17:48 +08:00
|
|
|
const width = elemOffset.width;
|
2016-07-11 14:52:21 +08:00
|
|
|
this.setAffixStyle(e, {
|
2016-07-09 16:00:05 +08:00
|
|
|
position: 'fixed',
|
2017-03-06 15:48:03 +08:00
|
|
|
bottom: targetBottomOffet + (offsetBottom as number),
|
2016-08-18 11:27:37 +08:00
|
|
|
left: targetRect.left + elemOffset.left,
|
2017-02-21 22:17:48 +08:00
|
|
|
width,
|
2016-07-09 16:00:05 +08:00
|
|
|
});
|
2017-01-26 11:23:54 +08:00
|
|
|
this.setPlaceholderStyle({
|
2017-02-21 22:17:48 +08:00
|
|
|
width,
|
2017-09-19 16:31:37 +08:00
|
|
|
height: elemOffset.height,
|
2016-07-11 14:52:21 +08:00
|
|
|
});
|
2016-07-09 16:00:05 +08:00
|
|
|
} else {
|
2017-01-26 11:23:54 +08:00
|
|
|
const { affixStyle } = this.state;
|
|
|
|
if (e.type === 'resize' && affixStyle && affixStyle.position === 'fixed' && affixNode.offsetWidth) {
|
2017-03-23 21:15:49 +08:00
|
|
|
this.setAffixStyle(e, { ...affixStyle, width: affixNode.offsetWidth });
|
2017-01-26 11:23:54 +08:00
|
|
|
} else {
|
|
|
|
this.setAffixStyle(e, null);
|
|
|
|
}
|
|
|
|
this.setPlaceholderStyle(null);
|
2015-08-03 16:49:42 +08:00
|
|
|
}
|
2018-03-14 16:22:09 +08:00
|
|
|
|
|
|
|
if (e.type === 'resize') {
|
|
|
|
this.syncPlaceholderStyle(e);
|
|
|
|
}
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
2015-08-03 16:49:42 +08:00
|
|
|
|
|
|
|
componentDidMount() {
|
2016-10-21 17:44:34 +08:00
|
|
|
const target = this.props.target || getDefaultTarget;
|
2016-12-03 16:19:22 +08:00
|
|
|
// Wait for parent component ref has its value
|
|
|
|
this.timeout = setTimeout(() => {
|
|
|
|
this.setTargetEventListeners(target);
|
|
|
|
});
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
2015-08-03 16:49:42 +08:00
|
|
|
|
2017-11-22 11:49:35 +08:00
|
|
|
componentWillReceiveProps(nextProps: AffixProps) {
|
2016-08-18 11:27:37 +08:00
|
|
|
if (this.props.target !== nextProps.target) {
|
2017-08-17 11:45:46 +08:00
|
|
|
this.clearEventListeners();
|
2017-11-22 11:49:35 +08:00
|
|
|
this.setTargetEventListeners(nextProps.target!);
|
2016-08-18 11:27:37 +08:00
|
|
|
|
|
|
|
// Mock Event object.
|
|
|
|
this.updatePosition({});
|
2015-08-04 15:03:00 +08:00
|
|
|
}
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
2015-08-03 16:49:42 +08:00
|
|
|
|
2016-08-18 11:27:37 +08:00
|
|
|
componentWillUnmount() {
|
2017-08-17 11:45:46 +08:00
|
|
|
this.clearEventListeners();
|
2016-12-03 16:19:22 +08:00
|
|
|
clearTimeout(this.timeout);
|
2017-02-08 14:44:43 +08:00
|
|
|
(this.updatePosition as any).cancel();
|
2016-08-18 11:27:37 +08:00
|
|
|
}
|
|
|
|
|
2017-11-22 11:49:35 +08:00
|
|
|
setTargetEventListeners(getTarget: () => HTMLElement | Window | null) {
|
2016-08-18 11:27:37 +08:00
|
|
|
const target = getTarget();
|
2016-12-03 16:19:22 +08:00
|
|
|
if (!target) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-17 11:45:46 +08:00
|
|
|
this.clearEventListeners();
|
|
|
|
|
|
|
|
this.events.forEach(eventName => {
|
|
|
|
this.eventHandlers[eventName] = addEventListener(target, eventName, this.updatePosition);
|
|
|
|
});
|
2016-08-18 11:27:37 +08:00
|
|
|
}
|
|
|
|
|
2017-08-17 11:45:46 +08:00
|
|
|
clearEventListeners() {
|
|
|
|
this.events.forEach(eventName => {
|
|
|
|
const handler = this.eventHandlers[eventName];
|
|
|
|
if (handler && handler.remove) {
|
|
|
|
handler.remove();
|
2016-08-18 11:27:37 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-22 11:49:35 +08:00
|
|
|
saveFixedNode = (node: HTMLDivElement) => {
|
2017-09-08 09:31:07 +08:00
|
|
|
this.fixedNode = node;
|
|
|
|
}
|
|
|
|
|
2018-03-14 16:22:09 +08:00
|
|
|
savePlaceholderNode = (node: HTMLDivElement) => {
|
|
|
|
this.placeholderNode = node;
|
|
|
|
}
|
|
|
|
|
2015-08-03 16:49:42 +08:00
|
|
|
render() {
|
2015-11-24 20:03:57 +08:00
|
|
|
const className = classNames({
|
2016-10-21 17:44:34 +08:00
|
|
|
[this.props.prefixCls || 'ant-affix']: this.state.affixStyle,
|
2015-10-09 10:55:52 +08:00
|
|
|
});
|
2015-08-03 16:49:42 +08:00
|
|
|
|
2017-02-21 18:07:58 +08:00
|
|
|
const props = omit(this.props, ['prefixCls', 'offsetTop', 'offsetBottom', 'target', 'onChange']);
|
2017-01-26 12:08:40 +08:00
|
|
|
const placeholderStyle = { ...this.state.placeholderStyle, ...this.props.style };
|
2015-08-03 16:49:42 +08:00
|
|
|
return (
|
2018-03-14 16:22:09 +08:00
|
|
|
<div {...props} style={placeholderStyle} ref={this.savePlaceholderNode}>
|
2017-09-08 09:31:07 +08:00
|
|
|
<div className={className} ref={this.saveFixedNode} style={this.state.affixStyle}>
|
2015-08-04 14:57:18 +08:00
|
|
|
{this.props.children}
|
|
|
|
</div>
|
2015-08-03 16:49:42 +08:00
|
|
|
</div>
|
|
|
|
);
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
|
|
|
}
|