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.8 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. identity: PropTypes.object,
  18. };
  19. static propTypes = {
  20. intl: PropTypes.object.isRequired,
  21. title: PropTypes.node,
  22. icon: PropTypes.string,
  23. active: PropTypes.bool,
  24. multiColumn: PropTypes.bool,
  25. extraButton: PropTypes.node,
  26. showBackButton: PropTypes.bool,
  27. children: PropTypes.node,
  28. pinned: PropTypes.bool,
  29. placeholder: PropTypes.bool,
  30. onPin: PropTypes.func,
  31. onMove: PropTypes.func,
  32. onClick: PropTypes.func,
  33. appendContent: PropTypes.node,
  34. collapseIssues: PropTypes.bool,
  35. };
  36. state = {
  37. collapsed: true,
  38. animating: false,
  39. };
  40. historyBack = () => {
  41. if (window.history && window.history.length === 1) {
  42. this.context.router.history.push('/');
  43. } else {
  44. this.context.router.history.goBack();
  45. }
  46. };
  47. handleToggleClick = (e) => {
  48. e.stopPropagation();
  49. this.setState({ collapsed: !this.state.collapsed, animating: true });
  50. };
  51. handleTitleClick = () => {
  52. this.props.onClick?.();
  53. };
  54. handleMoveLeft = () => {
  55. this.props.onMove(-1);
  56. };
  57. handleMoveRight = () => {
  58. this.props.onMove(1);
  59. };
  60. handleBackClick = () => {
  61. this.historyBack();
  62. };
  63. handleTransitionEnd = () => {
  64. this.setState({ animating: false });
  65. };
  66. handlePin = () => {
  67. if (!this.props.pinned) {
  68. this.context.router.history.replace('/');
  69. }
  70. this.props.onPin();
  71. };
  72. render () {
  73. const { title, icon, active, children, pinned, multiColumn, extraButton, showBackButton, intl: { formatMessage }, placeholder, appendContent, collapseIssues } = this.props;
  74. const { collapsed, animating } = this.state;
  75. const wrapperClassName = classNames('column-header__wrapper', {
  76. 'active': active,
  77. });
  78. const buttonClassName = classNames('column-header', {
  79. 'active': active,
  80. });
  81. const collapsibleClassName = classNames('column-header__collapsible', {
  82. 'collapsed': collapsed,
  83. 'animating': animating,
  84. });
  85. const collapsibleButtonClassName = classNames('column-header__button', {
  86. 'active': !collapsed,
  87. });
  88. let extraContent, pinButton, moveButtons, backButton, collapseButton;
  89. if (children) {
  90. extraContent = (
  91. <div key='extra-content' className='column-header__collapsible__extra'>
  92. {children}
  93. </div>
  94. );
  95. }
  96. if (multiColumn && pinned) {
  97. 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>;
  98. moveButtons = (
  99. <div key='move-buttons' className='column-header__setting-arrows'>
  100. <button title={formatMessage(messages.moveLeft)} aria-label={formatMessage(messages.moveLeft)} className='icon-button column-header__setting-btn' onClick={this.handleMoveLeft}><Icon id='chevron-left' /></button>
  101. <button title={formatMessage(messages.moveRight)} aria-label={formatMessage(messages.moveRight)} className='icon-button column-header__setting-btn' onClick={this.handleMoveRight}><Icon id='chevron-right' /></button>
  102. </div>
  103. );
  104. } else if (multiColumn && this.props.onPin) {
  105. 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>;
  106. }
  107. if (!pinned && (multiColumn || showBackButton)) {
  108. backButton = (
  109. <button onClick={this.handleBackClick} className='column-header__back-button'>
  110. <Icon id='chevron-left' className='column-back-button__icon' fixedWidth />
  111. <FormattedMessage id='column_back_button.label' defaultMessage='Back' />
  112. </button>
  113. );
  114. }
  115. const collapsedContent = [
  116. extraContent,
  117. ];
  118. if (multiColumn) {
  119. collapsedContent.push(pinButton);
  120. collapsedContent.push(moveButtons);
  121. }
  122. if (this.context.identity.signedIn && (children || (multiColumn && this.props.onPin))) {
  123. collapseButton = (
  124. <button
  125. className={collapsibleButtonClassName}
  126. title={formatMessage(collapsed ? messages.show : messages.hide)}
  127. aria-label={formatMessage(collapsed ? messages.show : messages.hide)}
  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. }