2016-07-13 12:44:24 -08:00
|
|
|
import React from 'react';
|
|
|
|
|
import Component from '../component';
|
|
|
|
|
|
|
|
|
|
export default class Notification extends Component {
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
constructor() {
|
2016-07-13 12:44:24 -08:00
|
|
|
super();
|
|
|
|
|
this.state = {
|
|
|
|
|
dismissing: false
|
|
|
|
|
};
|
2016-09-21 06:27:11 -08:00
|
|
|
this.handleDismiss = this.handleDismiss.bind(this);
|
2016-07-13 12:44:24 -08:00
|
|
|
this.onElement = this.onElement.bind(this);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
componentDidMount() {
|
2016-07-13 12:44:24 -08:00
|
|
|
if (this.props.dismissAfter) {
|
|
|
|
|
this.setDismissTimer();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
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) {
|
2016-09-21 06:27:11 -08:00
|
|
|
this.setState({dismissing: false});
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
handleDismiss() {
|
|
|
|
|
this.setState({dismissing: true});
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
onElement(el) {
|
2016-07-13 12:44:24 -08:00
|
|
|
if (el) {
|
|
|
|
|
el.addEventListener('webkitTransitionEnd', () => {
|
|
|
|
|
if (this.state.dismissing) {
|
|
|
|
|
this.props.onDismiss();
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-09-21 06:27:11 -08:00
|
|
|
const {backgroundColor} = this.props;
|
2016-07-13 12:44:24 -08:00
|
|
|
if (backgroundColor) {
|
|
|
|
|
el.style.setProperty(
|
|
|
|
|
'background-color',
|
|
|
|
|
backgroundColor,
|
|
|
|
|
'important'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
setDismissTimer() {
|
2016-07-13 12:44:24 -08:00
|
|
|
this.dismissTimer = setTimeout(() => {
|
2016-09-21 06:27:11 -08:00
|
|
|
this.handleDismiss();
|
2016-07-13 12:44:24 -08:00
|
|
|
}, this.props.dismissAfter);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
resetDismissTimer() {
|
2016-07-13 12:44:24 -08:00
|
|
|
clearTimeout(this.dismissTimer);
|
|
|
|
|
this.setDismissTimer();
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
componentWillUnmount() {
|
2016-07-13 12:44:24 -08:00
|
|
|
clearTimeout(this.dismissTimer);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
template(css) {
|
|
|
|
|
const {backgroundColor} = this.props;
|
2016-07-13 12:44:24 -08:00
|
|
|
const opacity = this.state.dismissing ? 0 : 1;
|
2016-09-21 06:27:11 -08:00
|
|
|
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 }
|
|
|
|
|
{
|
2016-09-21 06:27:11 -08:00
|
|
|
this.props.userDismissable ?
|
|
|
|
|
<a
|
|
|
|
|
className={css('dismissLink')}
|
|
|
|
|
onClick={this.handleDismiss}
|
2016-10-04 11:44:10 -08:00
|
|
|
style={{color: this.props.userDismissColor}}
|
2016-09-21 06:27:11 -08:00
|
|
|
>[x]</a> :
|
|
|
|
|
null
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
{ this.props.customChildren }
|
2016-09-21 06:27:11 -08:00
|
|
|
</div>);
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
|
2016-09-21 06:27:11 -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)',
|
2016-12-12 12:38:19 -09:00
|
|
|
borderRadius: '2px',
|
2016-07-17 12:02:34 -08:00
|
|
|
padding: '8px 14px 9px',
|
2016-07-13 12:44:24 -08:00
|
|
|
marginLeft: '10px',
|
|
|
|
|
transition: '150ms opacity ease',
|
|
|
|
|
color: '#fff',
|
2016-07-17 12:02:34 -08:00
|
|
|
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: {
|
2016-07-17 12:02:34 -08:00
|
|
|
position: 'relative',
|
|
|
|
|
left: '4px',
|
2016-07-13 12:44:24 -08:00
|
|
|
cursor: 'pointer',
|
|
|
|
|
color: '#528D11',
|
|
|
|
|
':hover': {
|
|
|
|
|
color: '#2A5100'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|