bigbuffet-rw/app/soapbox/components/sub_navigation.js

92 lines
2.1 KiB
JavaScript
Raw Normal View History

2021-09-12 16:16:53 -07:00
import React from 'react';
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
import { throttle } from 'lodash';
2021-09-12 16:16:53 -07:00
import Icon from 'soapbox/components/icon';
import classNames from 'classnames';
2021-09-12 16:16:53 -07:00
export default class SubNavigation extends React.PureComponent {
2021-09-12 16:16:53 -07:00
static propTypes = {
message: PropTypes.string,
2021-09-12 16:16:53 -07:00
}
static contextTypes = {
router: PropTypes.object.isRequired,
}
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();
}
}
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() {
const { message } = this.props;
const { scrolled } = this.state;
2021-09-12 16:16:53 -07:00
return (
<div className={classNames('sub-navigation', { 'sub-navigation--scrolled': scrolled })} ref={this.setRef}>
<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')} />
<FormattedMessage id='sub_navigation.back' defaultMessage='Back' />
</button>
<div className='sub-navigation__message'>
{message}
</div>
2021-09-12 16:16:53 -07:00
</div>
</div>
);
}
}