闭社主体 forked from https://github.com/tootsuite/mastodon
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.

54 lines
1.6 KiB

  1. import React from 'react';
  2. import { FormattedMessage } from 'react-intl';
  3. import PropTypes from 'prop-types';
  4. import Icon from 'mastodon/components/icon';
  5. import { createPortal } from 'react-dom';
  6. export default class ColumnBackButton extends React.PureComponent {
  7. static contextTypes = {
  8. router: PropTypes.object,
  9. };
  10. static propTypes = {
  11. multiColumn: PropTypes.bool,
  12. };
  13. handleClick = () => {
  14. if (window.history && window.history.length === 1) {
  15. this.context.router.history.push('/');
  16. } else {
  17. this.context.router.history.goBack();
  18. }
  19. }
  20. render () {
  21. const { multiColumn } = this.props;
  22. const component = (
  23. <button onClick={this.handleClick} className='column-back-button'>
  24. <Icon id='chevron-left' className='column-back-button__icon' fixedWidth />
  25. <FormattedMessage id='column_back_button.label' defaultMessage='Back' />
  26. </button>
  27. );
  28. if (multiColumn) {
  29. return component;
  30. } else {
  31. // The portal container and the component may be rendered to the DOM in
  32. // the same React render pass, so the container might not be available at
  33. // the time `render()` is called.
  34. const container = document.getElementById('tabs-bar__portal');
  35. if (container === null) {
  36. // The container wasn't available, force a re-render so that the
  37. // component can eventually be inserted in the container and not scroll
  38. // with the rest of the area.
  39. this.forceUpdate();
  40. return component;
  41. } else {
  42. return createPortal(component, container);
  43. }
  44. }
  45. }
  46. }