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.

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