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.

40 lines
860 B

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. export default class Permalink extends React.PureComponent {
  4. static contextTypes = {
  5. router: PropTypes.object,
  6. };
  7. static propTypes = {
  8. className: PropTypes.string,
  9. href: PropTypes.string.isRequired,
  10. to: PropTypes.string.isRequired,
  11. children: PropTypes.node,
  12. };
  13. handleClick = (e) => {
  14. if (this.context.router && e.button === 0 && !(e.ctrlKey || e.metaKey)) {
  15. e.preventDefault();
  16. this.context.router.history.push(this.props.to);
  17. }
  18. }
  19. render () {
  20. const {
  21. children,
  22. className,
  23. href,
  24. to,
  25. ...other
  26. } = this.props;
  27. return (
  28. <a target='_blank' href={href} onClick={this.handleClick} {...other} className={`permalink${className ? ' ' + className : ''}`}>
  29. {children}
  30. </a>
  31. );
  32. }
  33. }