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.

215 lines
5.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 '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. class DropdownMenu extends React.PureComponent {
  11. static contextTypes = {
  12. router: PropTypes.object,
  13. };
  14. static propTypes = {
  15. items: PropTypes.array.isRequired,
  16. onClose: PropTypes.func.isRequired,
  17. style: PropTypes.object,
  18. placement: PropTypes.string,
  19. arrowOffsetLeft: PropTypes.string,
  20. arrowOffsetTop: PropTypes.string,
  21. };
  22. static defaultProps = {
  23. style: {},
  24. placement: 'bottom',
  25. };
  26. handleDocumentClick = e => {
  27. if (this.node && !this.node.contains(e.target)) {
  28. this.props.onClose();
  29. }
  30. }
  31. componentDidMount () {
  32. document.addEventListener('click', this.handleDocumentClick, false);
  33. document.addEventListener('touchend', this.handleDocumentClick, listenerOptions);
  34. }
  35. componentWillUnmount () {
  36. document.removeEventListener('click', this.handleDocumentClick, false);
  37. document.removeEventListener('touchend', this.handleDocumentClick, listenerOptions);
  38. }
  39. setRef = c => {
  40. this.node = c;
  41. }
  42. handleClick = e => {
  43. const i = Number(e.currentTarget.getAttribute('data-index'));
  44. const { action, to } = this.props.items[i];
  45. this.props.onClose();
  46. if (typeof action === 'function') {
  47. e.preventDefault();
  48. action();
  49. } else if (to) {
  50. e.preventDefault();
  51. this.context.router.history.push(to);
  52. }
  53. }
  54. renderItem (option, i) {
  55. if (option === null) {
  56. return <li key={`sep-${i}`} className='dropdown-menu__separator' />;
  57. }
  58. const { text, href = '#' } = option;
  59. return (
  60. <li className='dropdown-menu__item' key={`${text}-${i}`}>
  61. <a href={href} target='_blank' rel='noopener' role='button' tabIndex='0' autoFocus={i === 0} onClick={this.handleClick} data-index={i}>
  62. {text}
  63. </a>
  64. </li>
  65. );
  66. }
  67. render () {
  68. const { items, style, placement, arrowOffsetLeft, arrowOffsetTop } = this.props;
  69. return (
  70. <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 }) }}>
  71. {({ opacity, scaleX, scaleY }) => (
  72. <div className='dropdown-menu' style={{ ...style, opacity: opacity, transform: `scale(${scaleX}, ${scaleY})` }} ref={this.setRef}>
  73. <div className={`dropdown-menu__arrow ${placement}`} style={{ left: arrowOffsetLeft, top: arrowOffsetTop }} />
  74. <ul>
  75. {items.map((option, i) => this.renderItem(option, i))}
  76. </ul>
  77. </div>
  78. )}
  79. </Motion>
  80. );
  81. }
  82. }
  83. export default class Dropdown extends React.PureComponent {
  84. static contextTypes = {
  85. router: PropTypes.object,
  86. };
  87. static propTypes = {
  88. icon: PropTypes.string.isRequired,
  89. items: PropTypes.array.isRequired,
  90. size: PropTypes.number.isRequired,
  91. ariaLabel: PropTypes.string,
  92. disabled: PropTypes.bool,
  93. status: ImmutablePropTypes.map,
  94. isUserTouching: PropTypes.func,
  95. isModalOpen: PropTypes.bool.isRequired,
  96. onModalOpen: PropTypes.func,
  97. onModalClose: PropTypes.func,
  98. };
  99. static defaultProps = {
  100. ariaLabel: 'Menu',
  101. };
  102. state = {
  103. expanded: false,
  104. };
  105. handleClick = () => {
  106. if (!this.state.expanded && this.props.isUserTouching() && this.props.onModalOpen) {
  107. const { status, items } = this.props;
  108. this.props.onModalOpen({
  109. status,
  110. actions: items.map(
  111. (item, i) => item ? {
  112. ...item,
  113. name: `${item.text}-${i}`,
  114. onClick: this.handleItemClick.bind(this, i),
  115. } : null
  116. ),
  117. });
  118. return;
  119. }
  120. this.setState({ expanded: !this.state.expanded });
  121. }
  122. handleClose = () => {
  123. if (this.props.onModalClose) {
  124. this.props.onModalClose();
  125. }
  126. this.setState({ expanded: false });
  127. }
  128. handleKeyDown = e => {
  129. switch(e.key) {
  130. case 'Enter':
  131. this.handleClick();
  132. break;
  133. case 'Escape':
  134. this.handleClose();
  135. break;
  136. }
  137. }
  138. handleItemClick = (i, e) => {
  139. const { action, to } = this.props.items[i];
  140. this.handleClose();
  141. if (typeof action === 'function') {
  142. e.preventDefault();
  143. action();
  144. } else if (to) {
  145. e.preventDefault();
  146. this.context.router.history.push(to);
  147. }
  148. }
  149. setTargetRef = c => {
  150. this.target = c;
  151. }
  152. findTarget = () => {
  153. return this.target;
  154. }
  155. render () {
  156. const { icon, items, size, ariaLabel, disabled } = this.props;
  157. const { expanded } = this.state;
  158. return (
  159. <div onKeyDown={this.handleKeyDown}>
  160. <IconButton
  161. icon={icon}
  162. title={ariaLabel}
  163. active={expanded}
  164. disabled={disabled}
  165. size={size}
  166. ref={this.setTargetRef}
  167. onClick={this.handleClick}
  168. />
  169. <Overlay show={expanded} placement='bottom' target={this.findTarget}>
  170. <DropdownMenu items={items} onClose={this.handleClose} />
  171. </Overlay>
  172. </div>
  173. );
  174. }
  175. }