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.

211 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. 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. handleToggleClick = (e) => {
  41. e.stopPropagation();
  42. this.setState({ collapsed: !this.state.collapsed, animating: true });
  43. };
  44. handleTitleClick = () => {
  45. this.props.onClick?.();
  46. };
  47. handleMoveLeft = () => {
  48. this.props.onMove(-1);
  49. };
  50. handleMoveRight = () => {
  51. this.props.onMove(1);
  52. };
  53. handleBackClick = () => {
  54. if (window.history && window.history.state) {
  55. this.context.router.history.goBack();
  56. } else {
  57. this.context.router.history.push('/');
  58. }
  59. };
  60. handleTransitionEnd = () => {
  61. this.setState({ animating: false });
  62. };
  63. handlePin = () => {
  64. if (!this.props.pinned) {
  65. this.context.router.history.replace('/');
  66. }
  67. this.props.onPin();
  68. };
  69. render () {
  70. const { title, icon, active, children, pinned, multiColumn, extraButton, showBackButton, intl: { formatMessage }, placeholder, appendContent, collapseIssues } = this.props;
  71. const { collapsed, animating } = this.state;
  72. const wrapperClassName = classNames('column-header__wrapper', {
  73. 'active': active,
  74. });
  75. const buttonClassName = classNames('column-header', {
  76. 'active': active,
  77. });
  78. const collapsibleClassName = classNames('column-header__collapsible', {
  79. 'collapsed': collapsed,
  80. 'animating': animating,
  81. });
  82. const collapsibleButtonClassName = classNames('column-header__button', {
  83. 'active': !collapsed,
  84. });
  85. let extraContent, pinButton, moveButtons, backButton, collapseButton;
  86. if (children) {
  87. extraContent = (
  88. <div key='extra-content' className='column-header__collapsible__extra'>
  89. {children}
  90. </div>
  91. );
  92. }
  93. if (multiColumn && pinned) {
  94. 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>;
  95. moveButtons = (
  96. <div key='move-buttons' className='column-header__setting-arrows'>
  97. <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>
  98. <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>
  99. </div>
  100. );
  101. } else if (multiColumn && this.props.onPin) {
  102. 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>;
  103. }
  104. if (!pinned && (multiColumn || showBackButton)) {
  105. backButton = (
  106. <button onClick={this.handleBackClick} className='column-header__back-button'>
  107. <Icon id='chevron-left' className='column-back-button__icon' fixedWidth />
  108. <FormattedMessage id='column_back_button.label' defaultMessage='Back' />
  109. </button>
  110. );
  111. }
  112. const collapsedContent = [
  113. extraContent,
  114. ];
  115. if (multiColumn) {
  116. collapsedContent.push(pinButton);
  117. collapsedContent.push(moveButtons);
  118. }
  119. if (this.context.identity.signedIn && (children || (multiColumn && this.props.onPin))) {
  120. collapseButton = (
  121. <button
  122. className={collapsibleButtonClassName}
  123. title={formatMessage(collapsed ? messages.show : messages.hide)}
  124. aria-label={formatMessage(collapsed ? messages.show : messages.hide)}
  125. onClick={this.handleToggleClick}
  126. >
  127. <i className='icon-with-badge'>
  128. <Icon id='sliders' />
  129. {collapseIssues && <i className='icon-with-badge__issue-badge' />}
  130. </i>
  131. </button>
  132. );
  133. }
  134. const hasTitle = icon && title;
  135. const component = (
  136. <div className={wrapperClassName}>
  137. <h1 className={buttonClassName}>
  138. {hasTitle && (
  139. <button onClick={this.handleTitleClick}>
  140. <Icon id={icon} fixedWidth className='column-header__icon' />
  141. {title}
  142. </button>
  143. )}
  144. {!hasTitle && backButton}
  145. <div className='column-header__buttons'>
  146. {hasTitle && backButton}
  147. {extraButton}
  148. {collapseButton}
  149. </div>
  150. </h1>
  151. <div className={collapsibleClassName} tabIndex={collapsed ? -1 : null} onTransitionEnd={this.handleTransitionEnd}>
  152. <div className='column-header__collapsible-inner'>
  153. {(!collapsed || animating) && collapsedContent}
  154. </div>
  155. </div>
  156. {appendContent}
  157. </div>
  158. );
  159. if (multiColumn || placeholder) {
  160. return component;
  161. } else {
  162. // The portal container and the component may be rendered to the DOM in
  163. // the same React render pass, so the container might not be available at
  164. // the time `render()` is called.
  165. const container = document.getElementById('tabs-bar__portal');
  166. if (container === null) {
  167. // The container wasn't available, force a re-render so that the
  168. // component can eventually be inserted in the container and not scroll
  169. // with the rest of the area.
  170. this.forceUpdate();
  171. return component;
  172. } else {
  173. return createPortal(component, container);
  174. }
  175. }
  176. }
  177. }