ant-design/components/affix/index.jsx

168 lines
4.7 KiB
React
Raw Normal View History

2015-08-03 16:49:42 +08:00
import React from 'react';
2015-10-21 17:59:57 +08:00
import ReactDOM from 'react-dom';
2016-06-16 22:34:54 +08:00
import addEventListener from 'rc-util/lib/Dom/addEventListener';
import classNames from 'classnames';
import warning from 'warning';
2016-07-09 16:00:05 +08:00
import shallowequal from 'shallowequal';
2015-08-03 16:49:42 +08:00
2015-08-04 14:57:18 +08:00
function getScroll(w, top) {
let ret = w[`page${top ? 'Y' : 'X'}Offset`];
2016-02-29 12:10:36 +08:00
const method = `scroll${top ? 'Top' : 'Left'}`;
2015-08-04 14:57:18 +08:00
if (typeof ret !== 'number') {
2016-02-29 12:10:36 +08:00
const d = w.document;
// ie6,7,8 standard mode
2015-08-04 14:57:18 +08:00
ret = d.documentElement[method];
if (typeof ret !== 'number') {
// quirks mode
2015-08-04 14:57:18 +08:00
ret = d.body[method];
}
}
return ret;
}
function getOffset(element) {
2016-02-29 12:10:36 +08:00
const rect = element.getBoundingClientRect();
const body = document.body;
const clientTop = element.clientTop || body.clientTop || 0;
const clientLeft = element.clientLeft || body.clientLeft || 0;
const scrollTop = getScroll(window, true);
const scrollLeft = getScroll(window);
2015-08-04 14:57:18 +08:00
return {
top: rect.top + scrollTop - clientTop,
2016-02-29 12:10:36 +08:00
left: rect.left + scrollLeft - clientLeft,
2015-08-04 14:57:18 +08:00
};
}
export default class Affix extends React.Component {
static propTypes = {
2016-02-29 16:44:38 +08:00
offsetTop: React.PropTypes.number,
offsetBottom: React.PropTypes.number,
}
2015-09-01 16:18:46 +08:00
2016-05-26 16:06:15 +08:00
static defaultProps = {
onChange() {},
}
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) {
2016-07-09 16:00:05 +08:00
const originalAffixStyle = this.state.affixStyle;
2016-07-11 14:52:21 +08:00
if (e.type === 'scroll' && originalAffixStyle && affixStyle) {
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)) {
this.props.onChange(affixed);
}
});
}
2016-07-11 14:52:21 +08:00
setPlaceholderStyle(e, placeholderStyle) {
const originalPlaceholderStyle = this.state.placeholderStyle;
if (e.type === 'resize') {
return;
}
if (shallowequal(placeholderStyle, originalPlaceholderStyle)) {
return;
}
this.setState({ placeholderStyle });
}
handleScroll = (e) => {
let { offsetTop, offsetBottom, offset } = this.props;
// Backwards support
offsetTop = offsetTop || offset;
2016-02-29 12:10:36 +08:00
const scrollTop = getScroll(window, true);
const elemOffset = getOffset(ReactDOM.findDOMNode(this));
2016-02-29 16:44:38 +08:00
const elemSize = {
width: ReactDOM.findDOMNode(this.refs.fixedNode).offsetWidth,
height: ReactDOM.findDOMNode(this.refs.fixedNode).offsetHeight,
};
2015-08-03 16:49:42 +08:00
2016-02-29 16:44:38 +08:00
const offsetMode = {};
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-02-29 16:44:38 +08:00
if (scrollTop > elemOffset.top - offsetTop && offsetMode.top) {
// Fixed Top
2016-07-11 14:52:21 +08:00
this.setAffixStyle(e, {
2016-07-09 16:00:05 +08:00
position: 'fixed',
top: offsetTop,
left: elemOffset.left,
width: ReactDOM.findDOMNode(this).offsetWidth,
});
2016-07-11 14:52:21 +08:00
this.setPlaceholderStyle(e, {
width: ReactDOM.findDOMNode(this).offsetWidth,
height: ReactDOM.findDOMNode(this).offsetHeight,
});
2016-02-29 16:44:38 +08:00
} else if (scrollTop < elemOffset.top + elemSize.height + offsetBottom - window.innerHeight &&
offsetMode.bottom) {
// Fixed Bottom
2016-07-11 14:52:21 +08:00
this.setAffixStyle(e, {
2016-07-09 16:00:05 +08:00
position: 'fixed',
bottom: offsetBottom,
left: elemOffset.left,
width: ReactDOM.findDOMNode(this).offsetWidth,
});
2016-07-11 14:52:21 +08:00
this.setPlaceholderStyle(e, {
width: ReactDOM.findDOMNode(this).offsetWidth,
height: ReactDOM.findDOMNode(this).offsetHeight,
});
2016-07-09 16:00:05 +08:00
} else {
2016-07-11 14:52:21 +08:00
this.setAffixStyle(e, null);
this.setPlaceholderStyle(e, null);
2015-08-03 16:49:42 +08:00
}
}
2015-08-03 16:49:42 +08:00
componentDidMount() {
warning(!('offset' in this.props), '`offset` prop of Affix is deprecated, use `offsetTop` instead.');
2016-06-16 22:34:54 +08:00
this.scrollEvent = addEventListener(window, 'scroll', this.handleScroll);
this.resizeEvent = addEventListener(window, 'resize', this.handleScroll);
}
2015-08-03 16:49:42 +08:00
componentWillUnmount() {
if (this.scrollEvent) {
this.scrollEvent.remove();
}
2015-08-04 15:03:00 +08:00
if (this.resizeEvent) {
this.resizeEvent.remove();
}
}
2015-08-03 16:49:42 +08:00
render() {
const className = classNames({
2016-02-29 16:44:38 +08:00
'ant-affix': this.state.affixStyle,
});
2015-08-03 16:49:42 +08:00
const props = { ...this.props };
delete props.offsetTop;
delete props.offsetBottom;
2015-08-03 16:49:42 +08:00
return (
2016-07-11 14:52:21 +08:00
<div {...props} style={this.state.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>
);
}
}