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.

280 lines
8.9 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: 'Visible for all, shown in public timelines' },
  14. unlisted_short: { id: 'privacy.unlisted.short', defaultMessage: 'Unlisted' },
  15. unlisted_long: { id: 'privacy.unlisted.long', defaultMessage: 'Visible for all, but not in public timelines' },
  16. private_short: { id: 'privacy.private.short', defaultMessage: 'Followers-only' },
  17. private_long: { id: 'privacy.private.long', defaultMessage: 'Visible for followers only' },
  18. direct_short: { id: 'privacy.direct.short', defaultMessage: 'Direct' },
  19. direct_long: { id: 'privacy.direct.long', defaultMessage: 'Visible for 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 = null;
  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] || this.node.firstChild;
  56. break;
  57. case 'ArrowUp':
  58. element = this.node.childNodes[index - 1] || this.node.lastChild;
  59. break;
  60. case 'Tab':
  61. if (e.shiftKey) {
  62. element = this.node.childNodes[index - 1] || this.node.lastChild;
  63. } else {
  64. element = this.node.childNodes[index + 1] || this.node.firstChild;
  65. }
  66. break;
  67. case 'Home':
  68. element = this.node.firstChild;
  69. break;
  70. case 'End':
  71. element = this.node.lastChild;
  72. break;
  73. }
  74. if (element) {
  75. element.focus();
  76. this.props.onChange(element.getAttribute('data-index'));
  77. e.preventDefault();
  78. e.stopPropagation();
  79. }
  80. }
  81. handleClick = e => {
  82. const value = e.currentTarget.getAttribute('data-index');
  83. e.preventDefault();
  84. this.props.onClose();
  85. this.props.onChange(value);
  86. }
  87. componentDidMount () {
  88. document.addEventListener('click', this.handleDocumentClick, false);
  89. document.addEventListener('touchend', this.handleDocumentClick, listenerOptions);
  90. if (this.focusedItem) this.focusedItem.focus();
  91. this.setState({ mounted: true });
  92. }
  93. componentWillUnmount () {
  94. document.removeEventListener('click', this.handleDocumentClick, false);
  95. document.removeEventListener('touchend', this.handleDocumentClick, listenerOptions);
  96. }
  97. setRef = c => {
  98. this.node = c;
  99. }
  100. setFocusRef = c => {
  101. this.focusedItem = c;
  102. }
  103. render () {
  104. const { mounted } = this.state;
  105. const { style, items, placement, value } = this.props;
  106. return (
  107. <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 }) }}>
  108. {({ opacity, scaleX, scaleY }) => (
  109. // It should not be transformed when mounting because the resulting
  110. // size will be used to determine the coordinate of the menu by
  111. // react-overlays
  112. <div className={`privacy-dropdown__dropdown ${placement}`} style={{ ...style, opacity: opacity, transform: mounted ? `scale(${scaleX}, ${scaleY})` : null, zIndex: 2 }} role='listbox' ref={this.setRef}>
  113. {items.map(item => (
  114. <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}>
  115. <div className='privacy-dropdown__option__icon'>
  116. <Icon id={item.icon} fixedWidth />
  117. </div>
  118. <div className='privacy-dropdown__option__content'>
  119. <strong>{item.text}</strong>
  120. {item.meta}
  121. </div>
  122. </div>
  123. ))}
  124. </div>
  125. )}
  126. </Motion>
  127. );
  128. }
  129. }
  130. export default @injectIntl
  131. class PrivacyDropdown extends React.PureComponent {
  132. static propTypes = {
  133. isUserTouching: PropTypes.func,
  134. isModalOpen: PropTypes.bool.isRequired,
  135. onModalOpen: PropTypes.func,
  136. onModalClose: PropTypes.func,
  137. value: PropTypes.string.isRequired,
  138. onChange: PropTypes.func.isRequired,
  139. intl: PropTypes.object.isRequired,
  140. };
  141. state = {
  142. open: false,
  143. placement: 'bottom',
  144. };
  145. handleToggle = ({ target }) => {
  146. if (this.props.isUserTouching()) {
  147. if (this.state.open) {
  148. this.props.onModalClose();
  149. } else {
  150. this.props.onModalOpen({
  151. actions: this.options.map(option => ({ ...option, active: option.value === this.props.value })),
  152. onClick: this.handleModalActionClick,
  153. });
  154. }
  155. } else {
  156. const { top } = target.getBoundingClientRect();
  157. if (this.state.open && this.activeElement) {
  158. this.activeElement.focus();
  159. }
  160. this.setState({ placement: top * 2 < innerHeight ? 'bottom' : 'top' });
  161. this.setState({ open: !this.state.open });
  162. }
  163. }
  164. handleModalActionClick = (e) => {
  165. e.preventDefault();
  166. const { value } = this.options[e.currentTarget.getAttribute('data-index')];
  167. this.props.onModalClose();
  168. this.props.onChange(value);
  169. }
  170. handleKeyDown = e => {
  171. switch(e.key) {
  172. case 'Escape':
  173. this.handleClose();
  174. break;
  175. }
  176. }
  177. handleMouseDown = () => {
  178. if (!this.state.open) {
  179. this.activeElement = document.activeElement;
  180. }
  181. }
  182. handleButtonKeyDown = (e) => {
  183. switch(e.key) {
  184. case ' ':
  185. case 'Enter':
  186. this.handleMouseDown();
  187. break;
  188. }
  189. }
  190. handleClose = () => {
  191. if (this.state.open && this.activeElement) {
  192. this.activeElement.focus();
  193. }
  194. this.setState({ open: false });
  195. }
  196. handleChange = value => {
  197. this.props.onChange(value);
  198. }
  199. componentWillMount () {
  200. const { intl: { formatMessage } } = this.props;
  201. this.options = [
  202. { icon: 'globe', value: 'public', text: formatMessage(messages.public_short), meta: formatMessage(messages.public_long) },
  203. { icon: 'unlock', value: 'unlisted', text: formatMessage(messages.unlisted_short), meta: formatMessage(messages.unlisted_long) },
  204. { icon: 'lock', value: 'private', text: formatMessage(messages.private_short), meta: formatMessage(messages.private_long) },
  205. { icon: 'envelope', value: 'direct', text: formatMessage(messages.direct_short), meta: formatMessage(messages.direct_long) },
  206. ];
  207. }
  208. render () {
  209. const { value, intl } = this.props;
  210. const { open, placement } = this.state;
  211. const valueOption = this.options.find(item => item.value === value);
  212. return (
  213. <div className={classNames('privacy-dropdown', placement, { active: open })} onKeyDown={this.handleKeyDown}>
  214. <div className={classNames('privacy-dropdown__value', { active: this.options.indexOf(valueOption) === (placement === 'bottom' ? 0 : (this.options.length - 1)) })}>
  215. <IconButton
  216. className='privacy-dropdown__value-icon'
  217. icon={valueOption.icon}
  218. title={intl.formatMessage(messages.change_privacy)}
  219. size={18}
  220. expanded={open}
  221. active={open}
  222. inverted
  223. onClick={this.handleToggle}
  224. onMouseDown={this.handleMouseDown}
  225. onKeyDown={this.handleButtonKeyDown}
  226. style={{ height: null, lineHeight: '27px' }}
  227. />
  228. </div>
  229. <Overlay show={open} placement={placement} target={this}>
  230. <PrivacyDropdownMenu
  231. items={this.options}
  232. value={value}
  233. onClose={this.handleClose}
  234. onChange={this.handleChange}
  235. placement={placement}
  236. />
  237. </Overlay>
  238. </div>
  239. );
  240. }
  241. }