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.

295 lines
9.3 KiB

  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. import Overlay from 'react-overlays/lib/Overlay';
  6. import Motion from '../../ui/util/optional_motion';
  7. import spring from 'react-motion/lib/spring';
  8. import detectPassiveEvents from 'detect-passive-events';
  9. import classNames from 'classnames';
  10. import Icon from 'mastodon/components/icon';
  11. const messages = defineMessages({
  12. public_short: { id: 'privacy.public.short', defaultMessage: 'Public' },
  13. public_long: { id: 'privacy.public.long', defaultMessage: 'Post to public timelines' },
  14. unlisted_short: { id: 'privacy.unlisted.short', defaultMessage: 'Unlisted' },
  15. unlisted_long: { id: 'privacy.unlisted.long', defaultMessage: 'Do not show in public timelines' },
  16. private_short: { id: 'privacy.private.short', defaultMessage: 'Followers-only' },
  17. private_long: { id: 'privacy.private.long', defaultMessage: 'Post to followers only' },
  18. direct_short: { id: 'privacy.direct.short', defaultMessage: 'Direct' },
  19. direct_long: { id: 'privacy.direct.long', defaultMessage: 'Post to mentioned users only' },
  20. change_privacy: { id: 'privacy.change', defaultMessage: 'Adjust status privacy' },
  21. });
  22. const listenerOptions = detectPassiveEvents.hasSupport ? { passive: true } : false;
  23. class PrivacyDropdownMenu extends React.PureComponent {
  24. static propTypes = {
  25. style: PropTypes.object,
  26. items: PropTypes.array.isRequired,
  27. value: PropTypes.string.isRequired,
  28. placement: PropTypes.string.isRequired,
  29. onClose: PropTypes.func.isRequired,
  30. onChange: PropTypes.func.isRequired,
  31. };
  32. state = {
  33. mounted: false,
  34. };
  35. handleDocumentClick = e => {
  36. if (this.node && !this.node.contains(e.target)) {
  37. this.props.onClose();
  38. }
  39. }
  40. handleKeyDown = e => {
  41. const { items } = this.props;
  42. const value = e.currentTarget.getAttribute('data-index');
  43. const index = items.findIndex(item => {
  44. return (item.value === value);
  45. });
  46. let element;
  47. switch(e.key) {
  48. case 'Escape':
  49. this.props.onClose();
  50. break;
  51. case 'Enter':
  52. this.handleClick(e);
  53. break;
  54. case 'ArrowDown':
  55. element = this.node.childNodes[index + 1];
  56. if (element) {
  57. element.focus();
  58. this.props.onChange(element.getAttribute('data-index'));
  59. }
  60. break;
  61. case 'ArrowUp':
  62. element = this.node.childNodes[index - 1];
  63. if (element) {
  64. element.focus();
  65. this.props.onChange(element.getAttribute('data-index'));
  66. }
  67. break;
  68. case 'Tab':
  69. if (e.shiftKey) {
  70. element = this.node.childNodes[index - 1] || this.node.lastChild;
  71. } else {
  72. element = this.node.childNodes[index + 1] || this.node.firstChild;
  73. }
  74. if (element) {
  75. element.focus();
  76. this.props.onChange(element.getAttribute('data-index'));
  77. e.preventDefault();
  78. e.stopPropagation();
  79. }
  80. break;
  81. case 'Home':
  82. element = this.node.firstChild;
  83. if (element) {
  84. element.focus();
  85. this.props.onChange(element.getAttribute('data-index'));
  86. }
  87. break;
  88. case 'End':
  89. element = this.node.lastChild;
  90. if (element) {
  91. element.focus();
  92. this.props.onChange(element.getAttribute('data-index'));
  93. }
  94. break;
  95. }
  96. }
  97. handleClick = e => {
  98. const value = e.currentTarget.getAttribute('data-index');
  99. e.preventDefault();
  100. this.props.onClose();
  101. this.props.onChange(value);
  102. }
  103. componentDidMount () {
  104. document.addEventListener('click', this.handleDocumentClick, false);
  105. document.addEventListener('touchend', this.handleDocumentClick, listenerOptions);
  106. if (this.focusedItem) this.focusedItem.focus();
  107. this.setState({ mounted: true });
  108. }
  109. componentWillUnmount () {
  110. document.removeEventListener('click', this.handleDocumentClick, false);
  111. document.removeEventListener('touchend', this.handleDocumentClick, listenerOptions);
  112. }
  113. setRef = c => {
  114. this.node = c;
  115. }
  116. setFocusRef = c => {
  117. this.focusedItem = c;
  118. }
  119. render () {
  120. const { mounted } = this.state;
  121. const { style, items, placement, value } = this.props;
  122. return (
  123. <Motion defaultStyle={{ opacity: 0, scaleX: 0.85, scaleY: 0.75 }} style={{ opacity: spring(1, { damping: 35, stiffness: 400 }), scaleX: spring(1, { damping: 35, stiffness: 400 }), scaleY: spring(1, { damping: 35, stiffness: 400 }) }}>
  124. {({ opacity, scaleX, scaleY }) => (
  125. // It should not be transformed when mounting because the resulting
  126. // size will be used to determine the coordinate of the menu by
  127. // react-overlays
  128. <div className={`privacy-dropdown__dropdown ${placement}`} style={{ ...style, opacity: opacity, transform: mounted ? `scale(${scaleX}, ${scaleY})` : null, zIndex: 2 }} role='listbox' ref={this.setRef}>
  129. {items.map(item => (
  130. <div role='option' tabIndex='0' key={item.value} data-index={item.value} onKeyDown={this.handleKeyDown} onClick={this.handleClick} className={classNames('privacy-dropdown__option', { active: item.value === value })} aria-selected={item.value === value} ref={item.value === value ? this.setFocusRef : null}>
  131. <div className='privacy-dropdown__option__icon'>
  132. <Icon id={item.icon} fixedWidth />
  133. </div>
  134. <div className='privacy-dropdown__option__content'>
  135. <strong>{item.text}</strong>
  136. {item.meta}
  137. </div>
  138. </div>
  139. ))}
  140. </div>
  141. )}
  142. </Motion>
  143. );
  144. }
  145. }
  146. export default @injectIntl
  147. class PrivacyDropdown extends React.PureComponent {
  148. static propTypes = {
  149. isUserTouching: PropTypes.func,
  150. isModalOpen: PropTypes.bool.isRequired,
  151. onModalOpen: PropTypes.func,
  152. onModalClose: PropTypes.func,
  153. value: PropTypes.string.isRequired,
  154. onChange: PropTypes.func.isRequired,
  155. intl: PropTypes.object.isRequired,
  156. };
  157. state = {
  158. open: false,
  159. placement: 'bottom',
  160. };
  161. handleToggle = ({ target }) => {
  162. if (this.props.isUserTouching()) {
  163. if (this.state.open) {
  164. this.props.onModalClose();
  165. } else {
  166. this.props.onModalOpen({
  167. actions: this.options.map(option => ({ ...option, active: option.value === this.props.value })),
  168. onClick: this.handleModalActionClick,
  169. });
  170. }
  171. } else {
  172. const { top } = target.getBoundingClientRect();
  173. if (this.state.open && this.activeElement) {
  174. this.activeElement.focus();
  175. }
  176. this.setState({ placement: top * 2 < innerHeight ? 'bottom' : 'top' });
  177. this.setState({ open: !this.state.open });
  178. }
  179. }
  180. handleModalActionClick = (e) => {
  181. e.preventDefault();
  182. const { value } = this.options[e.currentTarget.getAttribute('data-index')];
  183. this.props.onModalClose();
  184. this.props.onChange(value);
  185. }
  186. handleKeyDown = e => {
  187. switch(e.key) {
  188. case 'Escape':
  189. this.handleClose();
  190. break;
  191. }
  192. }
  193. handleMouseDown = () => {
  194. if (!this.state.open) {
  195. this.activeElement = document.activeElement;
  196. }
  197. }
  198. handleButtonKeyDown = (e) => {
  199. switch(e.key) {
  200. case ' ':
  201. case 'Enter':
  202. this.handleMouseDown();
  203. break;
  204. }
  205. }
  206. handleClose = () => {
  207. if (this.state.open && this.activeElement) {
  208. this.activeElement.focus();
  209. }
  210. this.setState({ open: false });
  211. }
  212. handleChange = value => {
  213. this.props.onChange(value);
  214. }
  215. componentWillMount () {
  216. const { intl: { formatMessage } } = this.props;
  217. this.options = [
  218. { icon: 'globe', value: 'public', text: formatMessage(messages.public_short), meta: formatMessage(messages.public_long) },
  219. { icon: 'unlock', value: 'unlisted', text: formatMessage(messages.unlisted_short), meta: formatMessage(messages.unlisted_long) },
  220. { icon: 'lock', value: 'private', text: formatMessage(messages.private_short), meta: formatMessage(messages.private_long) },
  221. { icon: 'envelope', value: 'direct', text: formatMessage(messages.direct_short), meta: formatMessage(messages.direct_long) },
  222. ];
  223. }
  224. render () {
  225. const { value, intl } = this.props;
  226. const { open, placement } = this.state;
  227. const valueOption = this.options.find(item => item.value === value);
  228. return (
  229. <div className={classNames('privacy-dropdown', placement, { active: open })} onKeyDown={this.handleKeyDown}>
  230. <div className={classNames('privacy-dropdown__value', { active: this.options.indexOf(valueOption) === (placement === 'bottom' ? 0 : (this.options.length - 1)) })}>
  231. <IconButton
  232. className='privacy-dropdown__value-icon'
  233. icon={valueOption.icon}
  234. title={intl.formatMessage(messages.change_privacy)}
  235. size={18}
  236. expanded={open}
  237. active={open}
  238. inverted
  239. onClick={this.handleToggle}
  240. onMouseDown={this.handleMouseDown}
  241. onKeyDown={this.handleButtonKeyDown}
  242. style={{ height: null, lineHeight: '27px' }}
  243. />
  244. </div>
  245. <Overlay show={open} placement={placement} target={this}>
  246. <PrivacyDropdownMenu
  247. items={this.options}
  248. value={value}
  249. onClose={this.handleClose}
  250. onChange={this.handleChange}
  251. placement={placement}
  252. />
  253. </Overlay>
  254. </div>
  255. );
  256. }
  257. }