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.

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