2021-09-12 16:16:53 -07:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2021-09-27 11:38:02 -07:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2021-10-08 17:09:34 -07:00
|
|
|
import { throttle } from 'lodash';
|
2021-09-12 16:16:53 -07:00
|
|
|
import Icon from 'soapbox/components/icon';
|
2021-10-08 17:09:34 -07:00
|
|
|
import classNames from 'classnames';
|
2021-10-14 11:26:07 -07:00
|
|
|
import Helmet from 'soapbox/components/helmet';
|
2021-09-12 16:16:53 -07:00
|
|
|
|
2021-09-27 11:38:02 -07:00
|
|
|
export default class SubNavigation extends React.PureComponent {
|
2021-09-12 16:16:53 -07:00
|
|
|
|
|
|
|
static propTypes = {
|
2021-09-27 11:38:02 -07:00
|
|
|
message: PropTypes.string,
|
2021-09-12 16:16:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static contextTypes = {
|
|
|
|
router: PropTypes.object.isRequired,
|
|
|
|
}
|
|
|
|
|
2021-10-08 17:09:34 -07:00
|
|
|
state = {
|
|
|
|
scrolled: false,
|
|
|
|
}
|
|
|
|
|
2021-09-12 16:16:53 -07:00
|
|
|
handleBackClick = () => {
|
|
|
|
if (window.history && window.history.length === 1) {
|
|
|
|
this.context.router.history.push('/');
|
|
|
|
} else {
|
|
|
|
this.context.router.history.goBack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleBackKeyUp = (e) => {
|
|
|
|
if (e.key === 'Enter') {
|
|
|
|
this.handleClick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 17:09:34 -07:00
|
|
|
componentDidMount() {
|
|
|
|
this.attachScrollListener();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.detachScrollListener();
|
|
|
|
}
|
|
|
|
|
|
|
|
attachScrollListener() {
|
|
|
|
window.addEventListener('scroll', this.handleScroll);
|
|
|
|
}
|
|
|
|
|
|
|
|
detachScrollListener() {
|
|
|
|
window.removeEventListener('scroll', this.handleScroll);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleScroll = throttle(() => {
|
|
|
|
if (this.node) {
|
|
|
|
const { top } = this.node.getBoundingClientRect();
|
|
|
|
|
|
|
|
if (top <= 50) {
|
|
|
|
this.setState({ scrolled: true });
|
|
|
|
} else {
|
|
|
|
this.setState({ scrolled: false });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 150, { trailing: true });
|
|
|
|
|
|
|
|
setRef = c => {
|
|
|
|
this.node = c;
|
|
|
|
}
|
|
|
|
|
2021-09-12 16:16:53 -07:00
|
|
|
render() {
|
2021-09-27 11:38:02 -07:00
|
|
|
const { message } = this.props;
|
2021-10-08 17:09:34 -07:00
|
|
|
const { scrolled } = this.state;
|
2021-09-12 16:16:53 -07:00
|
|
|
|
|
|
|
return (
|
2021-10-08 17:09:34 -07:00
|
|
|
<div className={classNames('sub-navigation', { 'sub-navigation--scrolled': scrolled })} ref={this.setRef}>
|
2021-09-21 11:42:25 -07:00
|
|
|
<div className='sub-navigation__content'>
|
|
|
|
<button
|
|
|
|
className='sub-navigation__back'
|
|
|
|
onClick={this.handleBackClick}
|
|
|
|
onKeyUp={this.handleBackKeyUp}
|
|
|
|
>
|
|
|
|
<Icon src={require('@tabler/icons/icons/arrow-back.svg')} />
|
2021-10-14 11:38:16 -07:00
|
|
|
<FormattedMessage id='column_back_button.label' defaultMessage='Back' />
|
2021-09-21 11:42:25 -07:00
|
|
|
</button>
|
2021-10-14 11:26:07 -07:00
|
|
|
{message && (
|
|
|
|
<div className='sub-navigation__message'>
|
|
|
|
<Helmet><title>{message}</title></Helmet>
|
|
|
|
{message}
|
|
|
|
</div>
|
|
|
|
)}
|
2021-09-12 16:16:53 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|