闭社主体 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.

84 lines
3.5 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import NavLink from 'react-router-dom/NavLink';
  4. import { FormattedMessage, injectIntl } from 'react-intl';
  5. import { debounce } from 'lodash';
  6. import { isUserTouching } from '../../../is_mobile';
  7. export const links = [
  8. <NavLink className='tabs-bar__link primary' to='/statuses/new' data-preview-title-id='tabs_bar.compose' data-preview-icon='pencil' ><i className='fa fa-fw fa-pencil' /><FormattedMessage id='tabs_bar.compose' defaultMessage='Compose' /></NavLink>,
  9. <NavLink className='tabs-bar__link primary' to='/timelines/home' data-preview-title-id='column.home' data-preview-icon='home' ><i className='fa fa-fw fa-home' /><FormattedMessage id='tabs_bar.home' defaultMessage='Home' /></NavLink>,
  10. <NavLink className='tabs-bar__link primary' to='/notifications' data-preview-title-id='column.notifications' data-preview-icon='bell' ><i className='fa fa-fw fa-bell' /><FormattedMessage id='tabs_bar.notifications' defaultMessage='Notifications' /></NavLink>,
  11. <NavLink className='tabs-bar__link secondary' to='/timelines/public/local' data-preview-title-id='column.community' data-preview-icon='users' ><i className='fa fa-fw fa-users' /><FormattedMessage id='tabs_bar.local_timeline' defaultMessage='Local' /></NavLink>,
  12. <NavLink className='tabs-bar__link secondary' exact to='/timelines/public' data-preview-title-id='column.public' data-preview-icon='globe' ><i className='fa fa-fw fa-globe' /><FormattedMessage id='tabs_bar.federated_timeline' defaultMessage='Federated' /></NavLink>,
  13. <NavLink className='tabs-bar__link primary' style={{ flexGrow: '0', flexBasis: '30px' }} to='/getting-started' data-preview-title-id='getting_started.heading' data-preview-icon='asterisk' ><i className='fa fa-fw fa-asterisk' /></NavLink>,
  14. ];
  15. export function getIndex (path) {
  16. return links.findIndex(link => link.props.to === path);
  17. }
  18. export function getLink (index) {
  19. return links[index].props.to;
  20. }
  21. @injectIntl
  22. export default class TabsBar extends React.Component {
  23. static contextTypes = {
  24. router: PropTypes.object.isRequired,
  25. }
  26. static propTypes = {
  27. intl: PropTypes.object.isRequired,
  28. }
  29. setRef = ref => {
  30. this.node = ref;
  31. }
  32. handleClick = (e) => {
  33. // Only apply optimization for touch devices, which we assume are slower
  34. // We thus avoid the 250ms delay for non-touch devices and the lag for touch devices
  35. if (isUserTouching()) {
  36. e.preventDefault();
  37. e.persist();
  38. requestAnimationFrame(() => {
  39. const tabs = Array(...this.node.querySelectorAll('.tabs-bar__link'));
  40. const currentTab = tabs.find(tab => tab.classList.contains('active'));
  41. const nextTab = tabs.find(tab => tab.contains(e.target));
  42. const { props: { to } } = links[Array(...this.node.childNodes).indexOf(nextTab)];
  43. if (currentTab !== nextTab) {
  44. if (currentTab) {
  45. currentTab.classList.remove('active');
  46. }
  47. const listener = debounce(() => {
  48. nextTab.removeEventListener('transitionend', listener);
  49. this.context.router.history.push(to);
  50. }, 50);
  51. nextTab.addEventListener('transitionend', listener);
  52. nextTab.classList.add('active');
  53. }
  54. });
  55. }
  56. }
  57. render () {
  58. const { intl: { formatMessage } } = this.props;
  59. return (
  60. <nav className='tabs-bar' ref={this.setRef}>
  61. {links.map(link => React.cloneElement(link, { key: link.props.to, onClick: this.handleClick, 'aria-label': formatMessage({ id: link.props['data-preview-title-id'] }) }))}
  62. </nav>
  63. );
  64. }
  65. }