pleroma/app/soapbox/components/permalink.js

40 lines
1,012 B
JavaScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
import PropTypes from 'prop-types';
import React from 'react';
2022-03-17 18:17:28 -07:00
import { withRouter } from 'react-router-dom';
2020-03-27 13:59:38 -07:00
2022-03-17 18:17:28 -07:00
export default @withRouter
class Permalink extends React.PureComponent {
2020-03-27 13:59:38 -07:00
static propTypes = {
className: PropTypes.string,
href: PropTypes.string.isRequired,
to: PropTypes.string.isRequired,
children: PropTypes.node,
onInterceptClick: PropTypes.func,
2022-03-17 18:17:28 -07:00
history: PropTypes.object,
2020-03-27 13:59:38 -07:00
};
handleClick = e => {
if (this.props.onInterceptClick && this.props.onInterceptClick()) {
e.preventDefault();
return;
}
2022-03-17 18:17:28 -07:00
if (this.props.history && e.button === 0 && !(e.ctrlKey || e.metaKey)) {
2020-03-27 13:59:38 -07:00
e.preventDefault();
2022-03-17 18:17:28 -07:00
this.props.history.push(this.props.to);
2020-03-27 13:59:38 -07:00
}
}
render() {
2020-03-27 13:59:38 -07:00
const { href, children, className, onInterceptClick, ...other } = this.props;
return (
<a target='_blank' href={href} onClick={this.handleClick} {...other} className={`permalink${className ? ' ' + className : ''}`}>
{children}
</a>
);
}
}