logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

dropdown_menu.js (5748B)


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