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.

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