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.
 
 
 
 

84 lines
3.5 KiB

  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. import Icon from 'mastodon/components/icon';
  8. export const links = [
  9. <NavLink className='tabs-bar__link primary' to='/timelines/home' data-preview-title-id='column.home' data-preview-icon='home' ><Icon id='home' fixedWidth /><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' ><Icon id='bell' fixedWidth /><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' ><Icon id='users' fixedWidth /><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' ><Icon id='globe' fixedWidth /><FormattedMessage id='tabs_bar.federated_timeline' defaultMessage='Federated' /></NavLink>,
  13. <NavLink className='tabs-bar__link primary' to='/search' data-preview-title-id='tabs_bar.search' data-preview-icon='bell' ><Icon id='search' fixedWidth /><FormattedMessage id='tabs_bar.search' defaultMessage='Search' /></NavLink>,
  14. <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' ><Icon id='bars' fixedWidth /></NavLink>,
  15. ];
  16. export function getIndex (path) {
  17. return links.findIndex(link => link.props.to === path);
  18. }
  19. export function getLink (index) {
  20. return links[index].props.to;
  21. }
  22. export default @injectIntl
  23. @withRouter
  24. class TabsBar extends React.PureComponent {
  25. static propTypes = {
  26. intl: PropTypes.object.isRequired,
  27. history: 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.props.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. }