about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/features/compose/components/dropdown.js
blob: 1b0000fb7f9430f634ab13cfad09574b0b956674 (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
72
73
74
75
76
77
//  Package imports.
import React from 'react';
import PropTypes from 'prop-types';

//  Our imports.
import IconButton from 'flavours/glitch/components/icon_button';

const iconStyle = {
  height     : null,
  lineHeight : '27px',
};

export default class ComposeDropdown extends React.PureComponent {

  static propTypes = {
    title: PropTypes.string.isRequired,
    icon: PropTypes.string,
    highlight: PropTypes.bool,
    disabled: PropTypes.bool,
    children: PropTypes.arrayOf(PropTypes.node).isRequired,
  };

  state = {
    open: false,
  };

  onGlobalClick = (e) => {
    if (e.target !== this.node && !this.node.contains(e.target) && this.state.open) {
      this.setState({ open: false });
    }
  };

  componentDidMount () {
    window.addEventListener('click', this.onGlobalClick);
    window.addEventListener('touchstart', this.onGlobalClick);
  }
  componentWillUnmount () {
    window.removeEventListener('click', this.onGlobalClick);
    window.removeEventListener('touchstart', this.onGlobalClick);
  }

  onToggleDropdown = () => {
    if (this.props.disabled) return;
    this.setState({ open: !this.state.open });
  };

  setRef = (c) => {
    this.node = c;
  };

  render () {
    const { open } = this.state;
    let { highlight, title, icon, disabled } = this.props;

    if (!icon) icon = 'ellipsis-h';

    return (
      <div ref={this.setRef} className={`advanced-options-dropdown ${open ?  'open' : ''} ${highlight ? 'active' : ''} `}>
        <div className='advanced-options-dropdown__value'>
          <IconButton
            className={'inverted'}
            title={title}
            icon={icon} active={open || highlight}
            size={18}
            style={iconStyle}
            disabled={disabled}
            onClick={this.onToggleDropdown}
          />
        </div>
        <div className='advanced-options-dropdown__dropdown'>
          {this.props.children}
        </div>
      </div>
    );
  }

}