The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

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