闭社主体 forked from https://github.com/tootsuite/mastodon
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.

306 lines
7.9 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import IconButton from './icon_button';
  5. import Overlay from 'react-overlays/lib/Overlay';
  6. import Motion from '../features/ui/util/optional_motion';
  7. import spring from 'react-motion/lib/spring';
  8. import detectPassiveEvents from 'detect-passive-events';
  9. const listenerOptions = detectPassiveEvents.hasSupport ? { passive: true } : false;
  10. let id = 0;
  11. class DropdownMenu extends React.PureComponent {
  12. static contextTypes = {
  13. router: PropTypes.object,
  14. };
  15. static propTypes = {
  16. items: PropTypes.array.isRequired,
  17. onClose: PropTypes.func.isRequired,
  18. style: PropTypes.object,
  19. placement: PropTypes.string,
  20. arrowOffsetLeft: PropTypes.string,
  21. arrowOffsetTop: PropTypes.string,
  22. openedViaKeyboard: PropTypes.bool,
  23. };
  24. static defaultProps = {
  25. style: {},
  26. placement: 'bottom',
  27. };
  28. state = {
  29. mounted: false,
  30. };
  31. handleDocumentClick = e => {
  32. if (this.node && !this.node.contains(e.target)) {
  33. this.props.onClose();
  34. }
  35. }
  36. componentDidMount () {
  37. document.addEventListener('click', this.handleDocumentClick, false);
  38. document.addEventListener('keydown', this.handleKeyDown, false);
  39. document.addEventListener('touchend', this.handleDocumentClick, listenerOptions);
  40. if (this.focusedItem && this.props.openedViaKeyboard) {
  41. this.focusedItem.focus();
  42. }
  43. this.setState({ mounted: true });
  44. }
  45. componentWillUnmount () {
  46. document.removeEventListener('click', this.handleDocumentClick, false);
  47. document.removeEventListener('keydown', this.handleKeyDown, false);
  48. document.removeEventListener('touchend', this.handleDocumentClick, listenerOptions);
  49. }
  50. setRef = c => {
  51. this.node = c;
  52. }
  53. setFocusRef = c => {
  54. this.focusedItem = c;
  55. }
  56. handleKeyDown = e => {
  57. const items = Array.from(this.node.getElementsByTagName('a'));
  58. const index = items.indexOf(document.activeElement);
  59. let element;
  60. switch(e.key) {
  61. case 'ArrowDown':
  62. element = items[index+1];
  63. if (element) {
  64. element.focus();
  65. }
  66. break;
  67. case 'ArrowUp':
  68. element = items[index-1];
  69. if (element) {
  70. element.focus();
  71. }
  72. break;
  73. case 'Tab':
  74. if (e.shiftKey) {
  75. element = items[index-1] || items[items.length-1];
  76. } else {
  77. element = items[index+1] || items[0];
  78. }
  79. if (element) {
  80. element.focus();
  81. e.preventDefault();
  82. e.stopPropagation();
  83. }
  84. break;
  85. case 'Home':
  86. element = items[0];
  87. if (element) {
  88. element.focus();
  89. }
  90. break;
  91. case 'End':
  92. element = items[items.length-1];
  93. if (element) {
  94. element.focus();
  95. }
  96. break;
  97. case 'Escape':
  98. this.props.onClose();
  99. break;
  100. }
  101. }
  102. handleItemKeyPress = e => {
  103. if (e.key === 'Enter' || e.key === ' ') {
  104. this.handleClick(e);
  105. }
  106. }
  107. handleClick = e => {
  108. const i = Number(e.currentTarget.getAttribute('data-index'));
  109. const { action, to } = this.props.items[i];
  110. this.props.onClose();
  111. if (typeof action === 'function') {
  112. e.preventDefault();
  113. action(e);
  114. } else if (to) {
  115. e.preventDefault();
  116. this.context.router.history.push(to);
  117. }
  118. }
  119. renderItem (option, i) {
  120. if (option === null) {
  121. return <li key={`sep-${i}`} className='dropdown-menu__separator' />;
  122. }
  123. const { text, href = '#', target = '_blank', method } = option;
  124. return (
  125. <li className='dropdown-menu__item' key={`${text}-${i}`}>
  126. <a href={href} target={target} data-method={method} rel='noopener' role='button' tabIndex='0' ref={i === 0 ? this.setFocusRef : null} onClick={this.handleClick} onKeyPress={this.handleItemKeyPress} data-index={i}>
  127. {text}
  128. </a>
  129. </li>
  130. );
  131. }
  132. render () {
  133. const { items, style, placement, arrowOffsetLeft, arrowOffsetTop } = this.props;
  134. const { mounted } = this.state;
  135. return (
  136. <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 }) }}>
  137. {({ opacity, scaleX, scaleY }) => (
  138. // It should not be transformed when mounting because the resulting
  139. // size will be used to determine the coordinate of the menu by
  140. // react-overlays
  141. <div className={`dropdown-menu ${placement}`} style={{ ...style, opacity: opacity, transform: mounted ? `scale(${scaleX}, ${scaleY})` : null }} ref={this.setRef}>
  142. <div className={`dropdown-menu__arrow ${placement}`} style={{ left: arrowOffsetLeft, top: arrowOffsetTop }} />
  143. <ul>
  144. {items.map((option, i) => this.renderItem(option, i))}
  145. </ul>
  146. </div>
  147. )}
  148. </Motion>
  149. );
  150. }
  151. }
  152. export default class Dropdown extends React.PureComponent {
  153. static contextTypes = {
  154. router: PropTypes.object,
  155. };
  156. static propTypes = {
  157. icon: PropTypes.string.isRequired,
  158. items: PropTypes.array.isRequired,
  159. size: PropTypes.number.isRequired,
  160. title: PropTypes.string,
  161. disabled: PropTypes.bool,
  162. status: ImmutablePropTypes.map,
  163. isUserTouching: PropTypes.func,
  164. isModalOpen: PropTypes.bool.isRequired,
  165. onOpen: PropTypes.func.isRequired,
  166. onClose: PropTypes.func.isRequired,
  167. dropdownPlacement: PropTypes.string,
  168. openDropdownId: PropTypes.number,
  169. openedViaKeyboard: PropTypes.bool,
  170. };
  171. static defaultProps = {
  172. title: 'Menu',
  173. };
  174. state = {
  175. id: id++,
  176. };
  177. handleClick = ({ target, type }) => {
  178. if (this.state.id === this.props.openDropdownId) {
  179. this.handleClose();
  180. } else {
  181. const { top } = target.getBoundingClientRect();
  182. const placement = top * 2 < innerHeight ? 'bottom' : 'top';
  183. this.props.onOpen(this.state.id, this.handleItemClick, placement, type !== 'click');
  184. }
  185. }
  186. handleClose = () => {
  187. if (this.activeElement) {
  188. this.activeElement.focus();
  189. this.activeElement = null;
  190. }
  191. this.props.onClose(this.state.id);
  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. handleKeyPress = (e) => {
  207. switch(e.key) {
  208. case ' ':
  209. case 'Enter':
  210. this.handleClick(e);
  211. e.stopPropagation();
  212. e.preventDefault();
  213. break;
  214. }
  215. }
  216. handleItemClick = e => {
  217. const i = Number(e.currentTarget.getAttribute('data-index'));
  218. const { action, to } = this.props.items[i];
  219. this.handleClose();
  220. if (typeof action === 'function') {
  221. e.preventDefault();
  222. action();
  223. } else if (to) {
  224. e.preventDefault();
  225. this.context.router.history.push(to);
  226. }
  227. }
  228. setTargetRef = c => {
  229. this.target = c;
  230. }
  231. findTarget = () => {
  232. return this.target;
  233. }
  234. componentWillUnmount = () => {
  235. if (this.state.id === this.props.openDropdownId) {
  236. this.handleClose();
  237. }
  238. }
  239. render () {
  240. const { icon, items, size, title, disabled, dropdownPlacement, openDropdownId, openedViaKeyboard } = this.props;
  241. const open = this.state.id === openDropdownId;
  242. return (
  243. <div>
  244. <IconButton
  245. icon={icon}
  246. title={title}
  247. active={open}
  248. disabled={disabled}
  249. size={size}
  250. ref={this.setTargetRef}
  251. onClick={this.handleClick}
  252. onMouseDown={this.handleMouseDown}
  253. onKeyDown={this.handleButtonKeyDown}
  254. onKeyPress={this.handleKeyPress}
  255. />
  256. <Overlay show={open} placement={dropdownPlacement} target={this.findTarget}>
  257. <DropdownMenu items={items} onClose={this.handleClose} openedViaKeyboard={openedViaKeyboard} />
  258. </Overlay>
  259. </div>
  260. );
  261. }
  262. }