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.

252 lines
6.4 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. };
  23. static defaultProps = {
  24. style: {},
  25. placement: 'bottom',
  26. };
  27. state = {
  28. mounted: false,
  29. };
  30. handleDocumentClick = e => {
  31. if (this.node && !this.node.contains(e.target)) {
  32. this.props.onClose();
  33. }
  34. }
  35. componentDidMount () {
  36. document.addEventListener('click', this.handleDocumentClick, false);
  37. document.addEventListener('touchend', this.handleDocumentClick, listenerOptions);
  38. if (this.focusedItem) this.focusedItem.focus();
  39. this.setState({ mounted: true });
  40. }
  41. componentWillUnmount () {
  42. document.removeEventListener('click', this.handleDocumentClick, false);
  43. document.removeEventListener('touchend', this.handleDocumentClick, listenerOptions);
  44. }
  45. setRef = c => {
  46. this.node = c;
  47. }
  48. setFocusRef = c => {
  49. this.focusedItem = c;
  50. }
  51. handleKeyDown = e => {
  52. const items = Array.from(this.node.getElementsByTagName('a'));
  53. const index = items.indexOf(e.currentTarget);
  54. let element;
  55. switch(e.key) {
  56. case 'Enter':
  57. this.handleClick(e);
  58. break;
  59. case 'ArrowDown':
  60. element = items[index+1];
  61. if (element) {
  62. element.focus();
  63. }
  64. break;
  65. case 'ArrowUp':
  66. element = items[index-1];
  67. if (element) {
  68. element.focus();
  69. }
  70. break;
  71. case 'Home':
  72. element = items[0];
  73. if (element) {
  74. element.focus();
  75. }
  76. break;
  77. case 'End':
  78. element = items[items.length-1];
  79. if (element) {
  80. element.focus();
  81. }
  82. break;
  83. }
  84. }
  85. handleClick = e => {
  86. const i = Number(e.currentTarget.getAttribute('data-index'));
  87. const { action, to } = this.props.items[i];
  88. this.props.onClose();
  89. if (typeof action === 'function') {
  90. e.preventDefault();
  91. action(e);
  92. } else if (to) {
  93. e.preventDefault();
  94. this.context.router.history.push(to);
  95. }
  96. }
  97. renderItem (option, i) {
  98. if (option === null) {
  99. return <li key={`sep-${i}`} className='dropdown-menu__separator' />;
  100. }
  101. const { text, href = '#' } = option;
  102. return (
  103. <li className='dropdown-menu__item' key={`${text}-${i}`}>
  104. <a href={href} target='_blank' rel='noopener' role='button' tabIndex='0' ref={i === 0 ? this.setFocusRef : null} onClick={this.handleClick} onKeyDown={this.handleKeyDown} data-index={i}>
  105. {text}
  106. </a>
  107. </li>
  108. );
  109. }
  110. render () {
  111. const { items, style, placement, arrowOffsetLeft, arrowOffsetTop } = this.props;
  112. const { mounted } = this.state;
  113. return (
  114. <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 }) }}>
  115. {({ opacity, scaleX, scaleY }) => (
  116. // It should not be transformed when mounting because the resulting
  117. // size will be used to determine the coordinate of the menu by
  118. // react-overlays
  119. <div className='dropdown-menu' style={{ ...style, opacity: opacity, transform: mounted ? `scale(${scaleX}, ${scaleY})` : null }} ref={this.setRef}>
  120. <div className={`dropdown-menu__arrow ${placement}`} style={{ left: arrowOffsetLeft, top: arrowOffsetTop }} />
  121. <ul>
  122. {items.map((option, i) => this.renderItem(option, i))}
  123. </ul>
  124. </div>
  125. )}
  126. </Motion>
  127. );
  128. }
  129. }
  130. export default class Dropdown extends React.PureComponent {
  131. static contextTypes = {
  132. router: PropTypes.object,
  133. };
  134. static propTypes = {
  135. icon: PropTypes.string.isRequired,
  136. items: PropTypes.array.isRequired,
  137. size: PropTypes.number.isRequired,
  138. title: PropTypes.string,
  139. disabled: PropTypes.bool,
  140. status: ImmutablePropTypes.map,
  141. isUserTouching: PropTypes.func,
  142. isModalOpen: PropTypes.bool.isRequired,
  143. onOpen: PropTypes.func.isRequired,
  144. onClose: PropTypes.func.isRequired,
  145. dropdownPlacement: PropTypes.string,
  146. openDropdownId: PropTypes.number,
  147. };
  148. static defaultProps = {
  149. title: 'Menu',
  150. };
  151. state = {
  152. id: id++,
  153. };
  154. handleClick = ({ target }) => {
  155. if (this.state.id === this.props.openDropdownId) {
  156. this.handleClose();
  157. } else {
  158. const { top } = target.getBoundingClientRect();
  159. const placement = top * 2 < innerHeight ? 'bottom' : 'top';
  160. this.props.onOpen(this.state.id, this.handleItemClick, placement);
  161. }
  162. }
  163. handleClose = () => {
  164. this.props.onClose(this.state.id);
  165. }
  166. handleKeyDown = e => {
  167. switch(e.key) {
  168. case 'Escape':
  169. this.handleClose();
  170. break;
  171. }
  172. }
  173. handleItemClick = e => {
  174. const i = Number(e.currentTarget.getAttribute('data-index'));
  175. const { action, to } = this.props.items[i];
  176. this.handleClose();
  177. if (typeof action === 'function') {
  178. e.preventDefault();
  179. action();
  180. } else if (to) {
  181. e.preventDefault();
  182. this.context.router.history.push(to);
  183. }
  184. }
  185. setTargetRef = c => {
  186. this.target = c;
  187. }
  188. findTarget = () => {
  189. return this.target;
  190. }
  191. render () {
  192. const { icon, items, size, title, disabled, dropdownPlacement, openDropdownId } = this.props;
  193. const open = this.state.id === openDropdownId;
  194. return (
  195. <div onKeyDown={this.handleKeyDown}>
  196. <IconButton
  197. icon={icon}
  198. title={title}
  199. active={open}
  200. disabled={disabled}
  201. size={size}
  202. ref={this.setTargetRef}
  203. onClick={this.handleClick}
  204. />
  205. <Overlay show={open} placement={dropdownPlacement} target={this.findTarget}>
  206. <DropdownMenu items={items} onClose={this.handleClose} />
  207. </Overlay>
  208. </div>
  209. );
  210. }
  211. }