ant-design/components/affix/index.tsx

192 lines
5.1 KiB
TypeScript
Raw Normal View History

2016-06-22 13:18:43 +08:00
import * as React from 'react';
import * as 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-05 10:46:00 +08:00
import assign from 'object-assign';
2016-07-09 16:00:05 +08:00
import shallowequal from 'shallowequal';
2015-08-03 16:49:42 +08:00
2016-07-12 16:07:12 +08:00
function getScroll(w, top?: boolean) {
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
};
}
2016-06-22 13:18:43 +08:00
// Affix
export interface AffixProps {
/**
*
*/
2016-07-13 11:14:24 +08:00
offsetTop?: number;
offset?: number;
offsetBottom?: number;
style?: React.CSSProperties;
onChange?: (affixed?: boolean) => any;
2016-06-22 13:18:43 +08:00
}
export default class Affix extends React.Component<AffixProps, any> {
static propTypes = {
2016-02-29 16:44:38 +08:00
offsetTop: React.PropTypes.number,
offsetBottom: React.PropTypes.number,
2016-07-13 11:14:24 +08:00
};
2015-09-01 16:18:46 +08:00
2016-05-26 16:06:15 +08:00
static defaultProps = {
onChange() {},
2016-07-13 11:14:24 +08:00
};
2016-05-26 16:06:15 +08:00
2016-07-13 17:22:23 +08:00
scrollEvent: any;
resizeEvent: any;
refs: {
fixedNode: any;
};
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);
2016-07-12 16:07:12 +08:00
const affixNode = ReactDOM.findDOMNode(this) as HTMLElement;
const fixedNode = ReactDOM.findDOMNode(this.refs.fixedNode) as HTMLElement;
const elemOffset = getOffset(affixNode);
2016-02-29 16:44:38 +08:00
const elemSize = {
2016-07-12 16:07:12 +08:00
width: fixedNode.offsetWidth,
height: 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: null as boolean,
bottom: null as boolean,
};
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-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,
2016-07-12 16:07:12 +08:00
width: affixNode.offsetWidth,
2016-07-09 16:00:05 +08:00
});
2016-07-11 14:52:21 +08:00
this.setPlaceholderStyle(e, {
2016-07-12 16:07:12 +08:00
width: affixNode.offsetWidth,
height: affixNode.offsetHeight,
2016-07-11 14:52:21 +08:00
});
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,
2016-07-12 16:07:12 +08:00
width: affixNode.offsetWidth,
2016-07-09 16:00:05 +08:00
});
2016-07-11 14:52:21 +08:00
this.setPlaceholderStyle(e, {
2016-07-12 16:07:12 +08:00
width: affixNode.offsetWidth,
height: affixNode.offsetHeight,
2016-07-11 14:52:21 +08:00
});
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
2016-07-05 10:46:00 +08:00
const props = assign({}, 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>
);
}
}