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.

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