]> cat aescling's git repositories - mastodon.git/blob - app/javascript/mastodon/features/compose/components/privacy_dropdown.js
Performance improvements (#3168)
[mastodon.git] / app / javascript / mastodon / features / compose / components / privacy_dropdown.js
1 import React from 'react';
2 import PropTypes from 'prop-types';
3 import { injectIntl, defineMessages } from 'react-intl';
4 import IconButton from '../../../components/icon_button';
5
6 const messages = defineMessages({
7 public_short: { id: 'privacy.public.short', defaultMessage: 'Public' },
8 public_long: { id: 'privacy.public.long', defaultMessage: 'Post to public timelines' },
9 unlisted_short: { id: 'privacy.unlisted.short', defaultMessage: 'Unlisted' },
10 unlisted_long: { id: 'privacy.unlisted.long', defaultMessage: 'Do not show in public timelines' },
11 private_short: { id: 'privacy.private.short', defaultMessage: 'Followers-only' },
12 private_long: { id: 'privacy.private.long', defaultMessage: 'Post to followers only' },
13 direct_short: { id: 'privacy.direct.short', defaultMessage: 'Direct' },
14 direct_long: { id: 'privacy.direct.long', defaultMessage: 'Post to mentioned users only' },
15 change_privacy: { id: 'privacy.change', defaultMessage: 'Adjust status privacy' }
16 });
17
18 const iconStyle = {
19 height: null,
20 lineHeight: '27px'
21 }
22
23 class PrivacyDropdown extends React.PureComponent {
24
25 static propTypes = {
26 value: PropTypes.string.isRequired,
27 onChange: PropTypes.func.isRequired,
28 intl: PropTypes.object.isRequired
29 };
30
31 state = {
32 open: false
33 };
34
35 handleToggle = () => {
36 this.setState({ open: !this.state.open });
37 }
38
39 handleClick = (e) => {
40 const value = e.currentTarget.getAttribute('data-index');
41 e.preventDefault();
42 this.setState({ open: false });
43 this.props.onChange(value);
44 }
45
46 onGlobalClick = (e) => {
47 if (e.target !== this.node && !this.node.contains(e.target) && this.state.open) {
48 this.setState({ open: false });
49 }
50 }
51
52 componentDidMount () {
53 window.addEventListener('click', this.onGlobalClick);
54 window.addEventListener('touchstart', this.onGlobalClick);
55 }
56
57 componentWillUnmount () {
58 window.removeEventListener('click', this.onGlobalClick);
59 window.removeEventListener('touchstart', this.onGlobalClick);
60 }
61
62 setRef = (c) => {
63 this.node = c;
64 }
65
66 render () {
67 const { value, onChange, intl } = this.props;
68 const { open } = this.state;
69
70 const options = [
71 { icon: 'globe', value: 'public', shortText: intl.formatMessage(messages.public_short), longText: intl.formatMessage(messages.public_long) },
72 { icon: 'unlock-alt', value: 'unlisted', shortText: intl.formatMessage(messages.unlisted_short), longText: intl.formatMessage(messages.unlisted_long) },
73 { icon: 'lock', value: 'private', shortText: intl.formatMessage(messages.private_short), longText: intl.formatMessage(messages.private_long) },
74 { icon: 'envelope', value: 'direct', shortText: intl.formatMessage(messages.direct_short), longText: intl.formatMessage(messages.direct_long) }
75 ];
76
77 const valueOption = options.find(item => item.value === value);
78
79 return (
80 <div ref={this.setRef} className={`privacy-dropdown ${open ? 'active' : ''}`}>
81 <div className='privacy-dropdown__value'><IconButton className='privacy-dropdown__value-icon' icon={valueOption.icon} title={intl.formatMessage(messages.change_privacy)} size={18} active={open} inverted onClick={this.handleToggle} style={iconStyle}/></div>
82 <div className='privacy-dropdown__dropdown'>
83 {open && options.map(item =>
84 <div role='button' tabIndex='0' key={item.value} data-index={item.value} onClick={this.handleClick} className={`privacy-dropdown__option ${item.value === value ? 'active' : ''}`}>
85 <div className='privacy-dropdown__option__icon'><i className={`fa fa-fw fa-${item.icon}`} /></div>
86 <div className='privacy-dropdown__option__content'>
87 <strong>{item.shortText}</strong>
88 {item.longText}
89 </div>
90 </div>
91 )}
92 </div>
93 </div>
94 );
95 }
96
97 }
98
99 export default injectIntl(PrivacyDropdown);
This page took 0.118924 seconds and 4 git commands to generate.