hyper/lib/components/notification.js

125 lines
2.8 KiB
JavaScript
Raw Normal View History

2016-07-13 12:44:24 -08:00
import React from 'react';
import Component from '../component';
export default class Notification extends Component {
constructor() {
2016-07-13 12:44:24 -08:00
super();
this.state = {
dismissing: false
};
this.handleDismiss = this.handleDismiss.bind(this);
2016-07-13 12:44:24 -08:00
this.onElement = this.onElement.bind(this);
}
componentDidMount() {
2016-07-13 12:44:24 -08:00
if (this.props.dismissAfter) {
this.setDismissTimer();
}
}
componentWillReceiveProps(next) {
2016-07-13 12:44:24 -08:00
// if we have a timer going and the notification text
2016-07-17 13:05:37 -08:00
// changed we reset the timer
2016-07-13 12:44:24 -08:00
if (next.text !== this.props.text) {
if (this.props.dismissAfter) {
this.resetDismissTimer();
}
if (this.state.dismissing) {
this.setState({dismissing: false});
2016-07-13 12:44:24 -08:00
}
}
}
handleDismiss() {
this.setState({dismissing: true});
2016-07-13 12:44:24 -08:00
}
onElement(el) {
2016-07-13 12:44:24 -08:00
if (el) {
el.addEventListener('webkitTransitionEnd', () => {
if (this.state.dismissing) {
this.props.onDismiss();
}
});
const {backgroundColor} = this.props;
2016-07-13 12:44:24 -08:00
if (backgroundColor) {
el.style.setProperty(
'background-color',
backgroundColor,
'important'
);
}
}
}
setDismissTimer() {
2016-07-13 12:44:24 -08:00
this.dismissTimer = setTimeout(() => {
this.handleDismiss();
2016-07-13 12:44:24 -08:00
}, this.props.dismissAfter);
}
resetDismissTimer() {
2016-07-13 12:44:24 -08:00
clearTimeout(this.dismissTimer);
this.setDismissTimer();
}
componentWillUnmount() {
2016-07-13 12:44:24 -08:00
clearTimeout(this.dismissTimer);
}
template(css) {
const {backgroundColor} = this.props;
2016-07-13 12:44:24 -08:00
const opacity = this.state.dismissing ? 0 : 1;
return (<div
style={{opacity, backgroundColor}}
ref={this.onElement}
className={css('indicator')}
>
2016-07-13 12:44:24 -08:00
{ this.props.customChildrenBefore }
{ this.props.children || this.props.text }
{
this.props.userDismissable ?
<a
className={css('dismissLink')}
onClick={this.handleDismiss}
2016-10-04 11:44:10 -08:00
style={{color: this.props.userDismissColor}}
>[x]</a> :
null
2016-07-13 12:44:24 -08:00
}
{ this.props.customChildren }
</div>);
2016-07-13 12:44:24 -08:00
}
styles() {
2016-07-13 12:44:24 -08:00
return {
indicator: {
display: 'inline-block',
cursor: 'default',
WebkitUserSelect: 'none',
background: 'rgba(255, 255, 255, .2)',
padding: '8px 14px 9px',
2016-07-13 12:44:24 -08:00
marginLeft: '10px',
transition: '150ms opacity ease',
color: '#fff',
fontSize: '11px',
fontFamily: `-apple-system, BlinkMacSystemFont,
"Segoe UI", "Roboto", "Oxygen",
"Ubuntu", "Cantarell", "Fira Sans",
"Droid Sans", "Helvetica Neue", sans-serif`
2016-07-13 12:44:24 -08:00
},
dismissLink: {
position: 'relative',
left: '4px',
2016-07-13 12:44:24 -08:00
cursor: 'pointer',
color: '#528D11',
':hover': {
color: '#2A5100'
}
}
};
}
}