You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.4 KiB

  1. import PureRenderMixin from 'react-addons-pure-render-mixin';
  2. import IconButton from './icon_button';
  3. import { Motion, spring } from 'react-motion';
  4. import { injectIntl } from 'react-intl';
  5. const overlayStyle = {
  6. position: 'fixed',
  7. top: '0',
  8. left: '0',
  9. width: '100%',
  10. height: '100%',
  11. background: 'rgba(0, 0, 0, 0.5)',
  12. display: 'flex',
  13. justifyContent: 'center',
  14. alignContent: 'center',
  15. flexDirection: 'row',
  16. zIndex: '9999'
  17. };
  18. const dialogStyle = {
  19. color: '#282c37',
  20. boxShadow: '0 0 30px rgba(0, 0, 0, 0.8)',
  21. margin: 'auto',
  22. position: 'relative'
  23. };
  24. const closeStyle = {
  25. position: 'absolute',
  26. top: '4px',
  27. right: '4px'
  28. };
  29. const Lightbox = React.createClass({
  30. propTypes: {
  31. isVisible: React.PropTypes.bool,
  32. onOverlayClicked: React.PropTypes.func,
  33. onCloseClicked: React.PropTypes.func,
  34. intl: React.PropTypes.object.isRequired,
  35. children: React.PropTypes.node
  36. },
  37. mixins: [PureRenderMixin],
  38. componentDidMount () {
  39. this._listener = e => {
  40. if (this.props.isVisible && e.key === 'Escape') {
  41. this.props.onCloseClicked();
  42. }
  43. };
  44. window.addEventListener('keyup', this._listener);
  45. },
  46. componentWillUnmount () {
  47. window.removeEventListener('keyup', this._listener);
  48. },
  49. stopPropagation (e) {
  50. e.stopPropagation();
  51. },
  52. render () {
  53. const { intl, isVisible, onOverlayClicked, onCloseClicked, children } = this.props;
  54. return (
  55. <Motion defaultStyle={{ backgroundOpacity: 0, opacity: 0, y: -400 }} style={{ backgroundOpacity: spring(isVisible ? 50 : 0), opacity: isVisible ? spring(200) : 0, y: spring(isVisible ? 0 : -400, { stiffness: 150, damping: 12 }) }}>
  56. {({ backgroundOpacity, opacity, y }) =>
  57. <div className='lightbox' style={{...overlayStyle, background: `rgba(0, 0, 0, ${backgroundOpacity / 100})`, display: Math.floor(backgroundOpacity) === 0 ? 'none' : 'flex', pointerEvents: !isVisible ? 'none' : 'auto'}} onClick={onOverlayClicked}>
  58. <div style={{...dialogStyle, transform: `translateY(${y}px)`, opacity: opacity / 100 }} onClick={this.stopPropagation}>
  59. <IconButton title={intl.formatMessage({ id: 'lightbox.close', defaultMessage: 'Close' })} icon='times' onClick={onCloseClicked} size={16} style={closeStyle} />
  60. {children}
  61. </div>
  62. </div>
  63. }
  64. </Motion>
  65. );
  66. }
  67. });
  68. export default injectIntl(Lightbox);