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
6.7 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { createPortal } from 'react-dom';
  4. import classNames from 'classnames';
  5. import { FormattedMessage, injectIntl, defineMessages } from 'react-intl';
  6. import Icon from 'mastodon/components/icon';
  7. const messages = defineMessages({
  8. show: { id: 'column_header.show_settings', defaultMessage: 'Show settings' },
  9. hide: { id: 'column_header.hide_settings', defaultMessage: 'Hide settings' },
  10. moveLeft: { id: 'column_header.moveLeft_settings', defaultMessage: 'Move column to the left' },
  11. moveRight: { id: 'column_header.moveRight_settings', defaultMessage: 'Move column to the right' },
  12. });
  13. export default @injectIntl
  14. class ColumnHeader extends React.PureComponent {
  15. static contextTypes = {
  16. router: PropTypes.object,
  17. };
  18. static propTypes = {
  19. intl: PropTypes.object.isRequired,
  20. title: PropTypes.node,
  21. icon: PropTypes.string,
  22. active: PropTypes.bool,
  23. multiColumn: PropTypes.bool,
  24. extraButton: PropTypes.node,
  25. showBackButton: PropTypes.bool,
  26. children: PropTypes.node,
  27. pinned: PropTypes.bool,
  28. placeholder: PropTypes.bool,
  29. onPin: PropTypes.func,
  30. onMove: PropTypes.func,
  31. onClick: PropTypes.func,
  32. appendContent: PropTypes.node,
  33. collapseIssues: PropTypes.bool,
  34. };
  35. state = {
  36. collapsed: true,
  37. animating: false,
  38. };
  39. historyBack = () => {
  40. if (window.history && window.history.length === 1) {
  41. this.context.router.history.push('/');
  42. } else {
  43. this.context.router.history.goBack();
  44. }
  45. }
  46. handleToggleClick = (e) => {
  47. e.stopPropagation();
  48. this.setState({ collapsed: !this.state.collapsed, animating: true });
  49. }
  50. handleTitleClick = () => {
  51. this.props.onClick();
  52. }
  53. handleMoveLeft = () => {
  54. this.props.onMove(-1);
  55. }
  56. handleMoveRight = () => {
  57. this.props.onMove(1);
  58. }
  59. handleBackClick = () => {
  60. this.historyBack();
  61. }
  62. handleTransitionEnd = () => {
  63. this.setState({ animating: false });
  64. }
  65. handlePin = () => {
  66. if (!this.props.pinned) {
  67. this.context.router.history.replace('/');
  68. }
  69. this.props.onPin();
  70. }
  71. render () {
  72. const { title, icon, active, children, pinned, multiColumn, extraButton, showBackButton, intl: { formatMessage }, placeholder, appendContent, collapseIssues } = this.props;
  73. const { collapsed, animating } = this.state;
  74. const wrapperClassName = classNames('column-header__wrapper', {
  75. 'active': active,
  76. });
  77. const buttonClassName = classNames('column-header', {
  78. 'active': active,
  79. });
  80. const collapsibleClassName = classNames('column-header__collapsible', {
  81. 'collapsed': collapsed,
  82. 'animating': animating,
  83. });
  84. const collapsibleButtonClassName = classNames('column-header__button', {
  85. 'active': !collapsed,
  86. });
  87. let extraContent, pinButton, moveButtons, backButton, collapseButton;
  88. if (children) {
  89. extraContent = (
  90. <div key='extra-content' className='column-header__collapsible__extra'>
  91. {children}
  92. </div>
  93. );
  94. }
  95. if (multiColumn && pinned) {
  96. pinButton = <button key='pin-button' className='text-btn column-header__setting-btn' onClick={this.handlePin}><Icon id='times' /> <FormattedMessage id='column_header.unpin' defaultMessage='Unpin' /></button>;
  97. moveButtons = (
  98. <div key='move-buttons' className='column-header__setting-arrows'>
  99. <button title={formatMessage(messages.moveLeft)} aria-label={formatMessage(messages.moveLeft)} className='text-btn column-header__setting-btn' onClick={this.handleMoveLeft}><Icon id='chevron-left' /></button>
  100. <button title={formatMessage(messages.moveRight)} aria-label={formatMessage(messages.moveRight)} className='text-btn column-header__setting-btn' onClick={this.handleMoveRight}><Icon id='chevron-right' /></button>
  101. </div>
  102. );
  103. } else if (multiColumn && this.props.onPin) {
  104. pinButton = <button key='pin-button' className='text-btn column-header__setting-btn' onClick={this.handlePin}><Icon id='plus' /> <FormattedMessage id='column_header.pin' defaultMessage='Pin' /></button>;
  105. }
  106. if (!pinned && (multiColumn || showBackButton)) {
  107. backButton = (
  108. <button onClick={this.handleBackClick} className='column-header__back-button'>
  109. <Icon id='chevron-left' className='column-back-button__icon' fixedWidth />
  110. <FormattedMessage id='column_back_button.label' defaultMessage='Back' />
  111. </button>
  112. );
  113. }
  114. const collapsedContent = [
  115. extraContent,
  116. ];
  117. if (multiColumn) {
  118. collapsedContent.push(moveButtons);
  119. collapsedContent.push(pinButton);
  120. }
  121. if (children || (multiColumn && this.props.onPin)) {
  122. collapseButton = (
  123. <button
  124. className={collapsibleButtonClassName}
  125. title={formatMessage(collapsed ? messages.show : messages.hide)}
  126. aria-label={formatMessage(collapsed ? messages.show : messages.hide)}
  127. aria-pressed={collapsed ? 'false' : 'true'}
  128. onClick={this.handleToggleClick}
  129. >
  130. <i className='icon-with-badge'>
  131. <Icon id='sliders' />
  132. {collapseIssues && <i className='icon-with-badge__issue-badge' />}
  133. </i>
  134. </button>
  135. );
  136. }
  137. const hasTitle = icon && title;
  138. const component = (
  139. <div className={wrapperClassName}>
  140. <h1 className={buttonClassName}>
  141. {hasTitle && (
  142. <button onClick={this.handleTitleClick}>
  143. <Icon id={icon} fixedWidth className='column-header__icon' />
  144. {title}
  145. </button>
  146. )}
  147. {!hasTitle && backButton}
  148. <div className='column-header__buttons'>
  149. {hasTitle && backButton}
  150. {extraButton}
  151. {collapseButton}
  152. </div>
  153. </h1>
  154. <div className={collapsibleClassName} tabIndex={collapsed ? -1 : null} onTransitionEnd={this.handleTransitionEnd}>
  155. <div className='column-header__collapsible-inner'>
  156. {(!collapsed || animating) && collapsedContent}
  157. </div>
  158. </div>
  159. {appendContent}
  160. </div>
  161. );
  162. if (multiColumn || placeholder) {
  163. return component;
  164. } else {
  165. // The portal container and the component may be rendered to the DOM in
  166. // the same React render pass, so the container might not be available at
  167. // the time `render()` is called.
  168. const container = document.getElementById('tabs-bar__portal');
  169. if (container === null) {
  170. // The container wasn't available, force a re-render so that the
  171. // component can eventually be inserted in the container and not scroll
  172. // with the rest of the area.
  173. this.forceUpdate();
  174. return component;
  175. } else {
  176. return createPortal(component, container);
  177. }
  178. }
  179. }
  180. }