ant-design/components/affix/index.tsx

256 lines
7.1 KiB
TypeScript
Raw Normal View History

2016-09-21 11:54:53 +08:00
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
2016-06-16 22:34:54 +08:00
import addEventListener from 'rc-util/lib/Dom/addEventListener';
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';
import getScroll from '../_util/getScroll';
import { throttleByAnimationFrameDecorator } from '../_util/throttleByAnimationFrame';
2015-08-04 14:57:18 +08:00
function getTargetRect(target): ClientRect {
return target !== window ?
target.getBoundingClientRect() :
{ top: 0, left: 0, bottom: 0 };
}
function getOffset(element: HTMLElement, target) {
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 {
top: elemRect.top - targetRect.top +
scrollTop - clientTop,
left: elemRect.left - targetRect.left +
scrollLeft - clientLeft,
width: elemRect.width,
height: elemRect.height,
2015-08-04 14:57:18 +08:00
};
}
function noop() {}
function getDefaultTarget() {
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 元素的函数 */
2016-08-22 17:18:26 +08:00
target?: () => Window | HTMLElement;
prefixCls?: string;
2016-06-22 13:18:43 +08:00
}
export default class Affix extends React.Component<AffixProps, any> {
static propTypes = {
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;
timeout: any;
2016-07-13 17:22:23 +08:00
refs: {
2016-08-01 13:30:13 +08:00
fixedNode: HTMLElement;
2016-07-13 17:22:23 +08:00
};
2017-08-17 11:45:46 +08:00
events = [
'resize',
'scroll',
'touchstart',
'touchmove',
'touchend',
'pageshow',
'load',
];
eventHandlers = {};
constructor(props) {
super(props);
this.state = {
2016-02-29 12:10:36 +08:00
affixStyle: null,
2016-07-11 14:52:21 +08:00
placeholderStyle: null,
2015-08-03 16:49:42 +08:00
};
}
2015-08-03 16:49:42 +08:00
2016-07-11 14:52:21 +08:00
setAffixStyle(e, affixStyle) {
const { onChange = noop, target = getDefaultTarget } = this.props;
2016-07-09 16:00:05 +08:00
const originalAffixStyle = this.state.affixStyle;
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;
}
this.setState({ affixStyle }, () => {
const affixed = !!this.state.affixStyle;
if ((affixStyle && !originalAffixStyle) ||
(!affixStyle && originalAffixStyle)) {
onChange(affixed);
2016-07-09 16:00:05 +08:00
}
});
}
setPlaceholderStyle(placeholderStyle) {
2016-07-11 14:52:21 +08:00
const originalPlaceholderStyle = this.state.placeholderStyle;
if (shallowequal(placeholderStyle, originalPlaceholderStyle)) {
return;
}
this.setState({ placeholderStyle });
}
@throttleByAnimationFrameDecorator()
updatePosition(e) {
let { offsetTop, offsetBottom, offset, target = getDefaultTarget } = this.props;
const targetNode = target();
// Backwards support
offsetTop = offsetTop || offset;
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 = {
width: this.refs.fixedNode.offsetWidth,
height: this.refs.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 = {
top: false,
bottom: false,
2016-07-12 16:07:12 +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
}
const targetRect = getTargetRect(targetNode);
2016-08-24 16:09:55 +08:00
const targetInnerHeight =
(targetNode as Window).innerHeight || (targetNode as HTMLElement).clientHeight;
if (scrollTop > elemOffset.top - (offsetTop as number) && offsetMode.top) {
2016-02-29 16:44:38 +08:00
// Fixed Top
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',
top: targetRect.top + (offsetTop as number),
left: targetRect.left + elemOffset.left,
width,
2016-07-09 16:00:05 +08:00
});
this.setPlaceholderStyle({
width,
2016-07-12 16:07:12 +08:00
height: affixNode.offsetHeight,
2016-07-11 14:52:21 +08:00
});
} else if (
scrollTop < elemOffset.top + elemSize.height + (offsetBottom as number) - targetInnerHeight &&
offsetMode.bottom
) {
2016-02-29 16:44:38 +08:00
// Fixed Bottom
const targetBottomOffet = targetNode === window ? 0 : (window.innerHeight - targetRect.bottom);
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',
bottom: targetBottomOffet + (offsetBottom as number),
left: targetRect.left + elemOffset.left,
width,
2016-07-09 16:00:05 +08:00
});
this.setPlaceholderStyle({
width,
2016-07-12 16:07:12 +08:00
height: affixNode.offsetHeight,
2016-07-11 14:52:21 +08:00
});
2016-07-09 16:00:05 +08:00
} else {
const { affixStyle } = this.state;
if (e.type === 'resize' && affixStyle && affixStyle.position === 'fixed' && affixNode.offsetWidth) {
this.setAffixStyle(e, { ...affixStyle, width: affixNode.offsetWidth });
} else {
this.setAffixStyle(e, null);
}
this.setPlaceholderStyle(null);
2015-08-03 16:49:42 +08:00
}
}
2015-08-03 16:49:42 +08:00
componentDidMount() {
const target = this.props.target || getDefaultTarget;
// Wait for parent component ref has its value
this.timeout = setTimeout(() => {
this.setTargetEventListeners(target);
});
}
2015-08-03 16:49:42 +08:00
componentWillReceiveProps(nextProps) {
if (this.props.target !== nextProps.target) {
2017-08-17 11:45:46 +08:00
this.clearEventListeners();
this.setTargetEventListeners(nextProps.target);
// Mock Event object.
this.updatePosition({});
2015-08-04 15:03:00 +08:00
}
}
2015-08-03 16:49:42 +08:00
componentWillUnmount() {
2017-08-17 11:45:46 +08:00
this.clearEventListeners();
clearTimeout(this.timeout);
(this.updatePosition as any).cancel();
}
setTargetEventListeners(getTarget) {
const target = getTarget();
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);
});
}
2017-08-17 11:45:46 +08:00
clearEventListeners() {
this.events.forEach(eventName => {
const handler = this.eventHandlers[eventName];
if (handler && handler.remove) {
handler.remove();
}
});
}
2015-08-03 16:49:42 +08:00
render() {
const className = classNames({
[this.props.prefixCls || 'ant-affix']: this.state.affixStyle,
});
2015-08-03 16:49:42 +08:00
const props = omit(this.props, ['prefixCls', 'offsetTop', 'offsetBottom', 'target', 'onChange']);
const placeholderStyle = { ...this.state.placeholderStyle, ...this.props.style };
2015-08-03 16:49:42 +08:00
return (
<div {...props} style={placeholderStyle}>
2016-02-29 16:44:38 +08:00
<div className={className} ref="fixedNode" 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>
);
}
}