2016-05-28 18:00:42 +03:00
|
|
|
import React, { PropTypes } from 'react';
|
2016-05-15 14:23:55 +03:00
|
|
|
import classnames from 'classnames';
|
2016-05-15 14:36:13 +03:00
|
|
|
import { themr } from 'react-css-themr';
|
2016-05-28 18:00:42 +03:00
|
|
|
import { APP_BAR } from '../identifiers.js';
|
2016-09-09 09:40:22 +03:00
|
|
|
import InjectIconButton from '../button/IconButton.js';
|
2015-10-28 03:49:22 +03:00
|
|
|
|
2016-09-09 09:40:22 +03:00
|
|
|
const factory = (IconButton) => {
|
2016-08-25 10:14:35 +03:00
|
|
|
class AppBar extends React.Component {
|
|
|
|
static propTypes = {
|
|
|
|
children: PropTypes.node,
|
|
|
|
className: PropTypes.string,
|
|
|
|
fixed: PropTypes.bool,
|
|
|
|
flat: PropTypes.bool,
|
|
|
|
leftIcon: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.element
|
|
|
|
]),
|
|
|
|
onLeftIconClick: PropTypes.func,
|
|
|
|
onRightIconClick: PropTypes.func,
|
|
|
|
rightIcon: PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.element
|
|
|
|
]),
|
|
|
|
scrollHide: PropTypes.bool,
|
|
|
|
theme: PropTypes.shape({
|
|
|
|
appBar: PropTypes.string,
|
2017-01-05 04:42:18 +03:00
|
|
|
inner: PropTypes.string,
|
2016-08-25 10:14:35 +03:00
|
|
|
fixed: PropTypes.string,
|
|
|
|
flat: PropTypes.string,
|
|
|
|
leftIcon: PropTypes.string,
|
|
|
|
rightIcon: PropTypes.string,
|
2016-12-19 22:39:07 +03:00
|
|
|
scrollHide: PropTypes.string,
|
2016-08-25 10:14:35 +03:00
|
|
|
title: PropTypes.string
|
|
|
|
}),
|
|
|
|
title: PropTypes.string
|
|
|
|
};
|
2015-10-28 03:49:22 +03:00
|
|
|
|
2016-08-25 10:14:35 +03:00
|
|
|
static defaultProps = {
|
|
|
|
className: '',
|
|
|
|
fixed: false,
|
|
|
|
flat: false,
|
|
|
|
scrollHide: false
|
|
|
|
};
|
2015-10-28 03:49:22 +03:00
|
|
|
|
2016-08-25 10:14:35 +03:00
|
|
|
state = {hidden: false, height: 0};
|
|
|
|
|
|
|
|
componentDidMount () {
|
2016-10-07 15:40:28 +03:00
|
|
|
if (this.props.scrollHide) {
|
|
|
|
this.initializeScroll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
if (!this.props.scrollHide && nextProps.scrollHide) {
|
|
|
|
this.initializeScroll();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.props.scrollHide && !nextProps.scrollHide) {
|
|
|
|
this.endScroll();
|
|
|
|
}
|
2016-08-25 10:14:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
2016-10-07 15:40:28 +03:00
|
|
|
if (this.props.scrollHide) {
|
|
|
|
this.endScroll();
|
|
|
|
}
|
2016-08-25 10:14:35 +03:00
|
|
|
}
|
|
|
|
|
2016-10-07 15:40:28 +03:00
|
|
|
initializeScroll = () => {
|
|
|
|
window.addEventListener('scroll', this.handleScroll);
|
|
|
|
const { height } = this.rootNode.getBoundingClientRect();
|
|
|
|
this.curScroll = window.scrollY;
|
|
|
|
this.setState({height});
|
|
|
|
};
|
|
|
|
|
|
|
|
endScroll = () => {
|
|
|
|
window.removeEventListener('scroll', this.handleScroll);
|
|
|
|
};
|
|
|
|
|
2016-08-25 10:14:35 +03:00
|
|
|
handleScroll = () => {
|
|
|
|
const scrollDiff = this.curScroll - window.scrollY;
|
|
|
|
const hidden = scrollDiff < 0 && window.scrollY !== undefined && window.scrollY > this.state.height;
|
|
|
|
this.setState({hidden});
|
|
|
|
this.curScroll = window.scrollY;
|
|
|
|
};
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { children, leftIcon, onLeftIconClick, onRightIconClick, rightIcon, theme, title } = this.props;
|
|
|
|
const className = classnames(theme.appBar, {
|
|
|
|
[theme.fixed]: this.props.fixed,
|
|
|
|
[theme.flat]: this.props.flat,
|
|
|
|
[theme.scrollHide]: this.state.hidden
|
|
|
|
}, this.props.className);
|
2016-09-03 13:23:22 +03:00
|
|
|
|
2016-08-25 10:14:35 +03:00
|
|
|
return (
|
2016-10-07 15:40:28 +03:00
|
|
|
<header
|
|
|
|
className={className}
|
|
|
|
data-react-toolbox='app-bar'
|
|
|
|
ref={node => {this.rootNode = node;}}
|
|
|
|
>
|
2017-01-05 04:42:18 +03:00
|
|
|
<div className={theme.inner}>
|
|
|
|
{leftIcon && <IconButton
|
|
|
|
inverse
|
|
|
|
className={classnames(theme.leftIcon)}
|
|
|
|
onClick={onLeftIconClick}
|
|
|
|
icon={leftIcon} />
|
|
|
|
}
|
|
|
|
{title && <h1 className={classnames(theme.title)}>{title}</h1>}
|
|
|
|
{children}
|
|
|
|
{rightIcon && <IconButton
|
|
|
|
inverse
|
|
|
|
className={classnames(theme.rightIcon)}
|
|
|
|
onClick={onRightIconClick}
|
|
|
|
icon={rightIcon} />
|
|
|
|
}
|
|
|
|
</div>
|
2016-08-25 10:14:35 +03:00
|
|
|
</header>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2015-10-28 03:49:22 +03:00
|
|
|
|
2016-09-03 13:23:22 +03:00
|
|
|
return AppBar;
|
2015-10-28 03:49:22 +03:00
|
|
|
};
|
|
|
|
|
2016-09-09 09:40:22 +03:00
|
|
|
const AppBar = factory(InjectIconButton);
|
|
|
|
export default themr(APP_BAR)(AppBar);
|
2016-09-03 13:23:22 +03:00
|
|
|
export { factory as appBarFactory };
|
2016-05-28 18:00:42 +03:00
|
|
|
export { AppBar };
|