logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

tabs_bar.js (3594B)


  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { NavLink, withRouter } from 'react-router-dom';
  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='/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>,
  9. <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>,
  10. <NavLink className='tabs-bar__link primary' to='/search' data-preview-title-id='tabs_bar.search' data-preview-icon='bell' ><i className='fa fa-fw fa-search' /><FormattedMessage id='tabs_bar.search' defaultMessage='Search' /></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='bars' ><i className='fa fa-fw fa-bars' /></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. @withRouter
  23. export default class TabsBar extends React.PureComponent {
  24. static propTypes = {
  25. intl: PropTypes.object.isRequired,
  26. history: PropTypes.object.isRequired,
  27. }
  28. setRef = ref => {
  29. this.node = ref;
  30. }
  31. handleClick = (e) => {
  32. // Only apply optimization for touch devices, which we assume are slower
  33. // We thus avoid the 250ms delay for non-touch devices and the lag for touch devices
  34. if (isUserTouching()) {
  35. e.preventDefault();
  36. e.persist();
  37. requestAnimationFrame(() => {
  38. const tabs = Array(...this.node.querySelectorAll('.tabs-bar__link'));
  39. const currentTab = tabs.find(tab => tab.classList.contains('active'));
  40. const nextTab = tabs.find(tab => tab.contains(e.target));
  41. const { props: { to } } = links[Array(...this.node.childNodes).indexOf(nextTab)];
  42. if (currentTab !== nextTab) {
  43. if (currentTab) {
  44. currentTab.classList.remove('active');
  45. }
  46. const listener = debounce(() => {
  47. nextTab.removeEventListener('transitionend', listener);
  48. this.props.history.push(to);
  49. }, 50);
  50. nextTab.addEventListener('transitionend', listener);
  51. nextTab.classList.add('active');
  52. }
  53. });
  54. }
  55. }
  56. render () {
  57. const { intl: { formatMessage } } = this.props;
  58. return (
  59. <nav className='tabs-bar' ref={this.setRef}>
  60. {links.map(link => React.cloneElement(link, { key: link.props.to, onClick: this.handleClick, 'aria-label': formatMessage({ id: link.props['data-preview-title-id'] }) }))}
  61. </nav>
  62. );
  63. }
  64. }