about summary refs log tree commit diff
path: root/app/javascript/mastodon/features/compose/containers/sensitive_button_container.js
blob: 8624849f3d6b74582f026eb2d2386b4348f3e5a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import React from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import IconButton from '../../../components/icon_button';
import { changeComposeSensitivity } from '../../../actions/compose';
import Motion from 'react-motion/lib/Motion';
import spring from 'react-motion/lib/spring';
import { injectIntl, defineMessages } from 'react-intl';

const messages = defineMessages({
  title: { id: 'compose_form.sensitive', defaultMessage: 'Mark media as sensitive' },
});

const mapStateToProps = state => ({
  visible: state.getIn(['compose', 'media_attachments']).size > 0,
  active: state.getIn(['compose', 'sensitive']),
  disabled: state.getIn(['compose', 'spoiler']),
});

const mapDispatchToProps = dispatch => ({

  onClick () {
    dispatch(changeComposeSensitivity());
  },

});

class SensitiveButton extends React.PureComponent {

  static propTypes = {
    visible: PropTypes.bool,
    active: PropTypes.bool,
    disabled: PropTypes.bool,
    onClick: PropTypes.func.isRequired,
    intl: PropTypes.object.isRequired,
  };

  render () {
    const { visible, active, disabled, onClick, intl } = this.props;

    return (
      <Motion defaultStyle={{ scale: 0.87 }} style={{ scale: spring(visible ? 1 : 0.87, { stiffness: 200, damping: 3 }) }}>
        {({ scale }) => {
          const icon = active ? 'eye-slash' : 'eye';
          const className = classNames('compose-form__sensitive-button', {
            'compose-form__sensitive-button--visible': visible,
          });
          return (
            <div className={className} style={{ transform: `translateZ(0) scale(${scale})` }}>
              <IconButton
                className='compose-form__sensitive-button__icon'
                title={intl.formatMessage(messages.title)}
                icon={icon}
                onClick={onClick}
                size={18}
                active={active}
                disabled={disabled}
                style={{ lineHeight: null, height: null }}
                inverted
              />
            </div>
          );
        }}
      </Motion>
    );
  }

}

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(SensitiveButton));