2018-05-10 18:35:55 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-05-24 16:03:03 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2018-07-09 15:46:31 +00:00
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import classnames from 'classnames';
|
2018-07-23 20:14:40 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2018-05-18 17:31:08 +00:00
|
|
|
import { Fill } from 'react-slot-fill';
|
2018-05-10 18:35:55 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-07-09 15:46:31 +00:00
|
|
|
import ReactDom from 'react-dom';
|
2018-05-10 18:35:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
2018-06-28 13:52:45 +00:00
|
|
|
import ActivityPanel from '../activity-panel';
|
2018-08-20 21:18:13 +00:00
|
|
|
import { Link } from '@woocommerce/components';
|
2018-05-10 18:35:55 +00:00
|
|
|
|
2018-07-09 15:46:31 +00:00
|
|
|
class Header extends Component {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.state = {
|
|
|
|
isScrolled: false,
|
|
|
|
};
|
2018-05-10 18:35:55 +00:00
|
|
|
|
2018-07-09 15:46:31 +00:00
|
|
|
this.onWindowScroll = this.onWindowScroll.bind( this );
|
|
|
|
this.updateIsScrolled = this.updateIsScrolled.bind( this );
|
|
|
|
}
|
2018-05-24 16:03:03 +00:00
|
|
|
|
2018-07-09 15:46:31 +00:00
|
|
|
componentDidMount() {
|
|
|
|
this.threshold = ReactDom.findDOMNode( this ).offsetTop;
|
|
|
|
window.addEventListener( 'scroll', this.onWindowScroll );
|
|
|
|
this.updateIsScrolled();
|
|
|
|
}
|
2018-05-24 16:03:03 +00:00
|
|
|
|
2018-07-09 15:46:31 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener( 'scroll', this.onWindowScroll );
|
|
|
|
window.cancelAnimationFrame( this.handle );
|
|
|
|
}
|
|
|
|
|
|
|
|
onWindowScroll() {
|
|
|
|
this.handle = window.requestAnimationFrame( this.updateIsScrolled );
|
|
|
|
}
|
|
|
|
|
|
|
|
updateIsScrolled() {
|
|
|
|
const isScrolled = window.pageYOffset > this.threshold - 20;
|
|
|
|
if ( isScrolled !== this.state.isScrolled ) {
|
|
|
|
this.setState( {
|
|
|
|
isScrolled: isScrolled,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { sections, isEmbedded } = this.props;
|
|
|
|
const { isScrolled } = this.state;
|
2018-09-03 15:09:09 +00:00
|
|
|
const _sections = Array.isArray( sections ) ? sections : [ sections ];
|
2018-07-09 15:46:31 +00:00
|
|
|
|
|
|
|
const documentTitle = _sections
|
|
|
|
.map( section => {
|
2018-09-03 15:09:09 +00:00
|
|
|
return Array.isArray( section ) ? section[ 1 ] : section;
|
2018-07-09 15:46:31 +00:00
|
|
|
} )
|
|
|
|
.reverse()
|
|
|
|
.join( ' ‹ ' );
|
|
|
|
|
|
|
|
document.title = decodeEntities(
|
2018-08-01 19:19:25 +00:00
|
|
|
sprintf(
|
|
|
|
__( '%1$s ‹ %2$s — WooCommerce', 'wc-admin' ),
|
|
|
|
documentTitle,
|
|
|
|
wcSettings.siteTitle
|
|
|
|
)
|
2018-07-09 15:46:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const className = classnames( 'woocommerce-layout__header', {
|
|
|
|
'is-scrolled': isScrolled,
|
|
|
|
} );
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={ className }>
|
|
|
|
<h1 className="woocommerce-layout__header-breadcrumbs">
|
|
|
|
<span>
|
2018-07-17 18:51:56 +00:00
|
|
|
<Link href="/">WooCommerce</Link>
|
2018-07-09 15:46:31 +00:00
|
|
|
</span>
|
|
|
|
{ _sections.map( ( section, i ) => {
|
2018-09-03 15:09:09 +00:00
|
|
|
const sectionPiece = Array.isArray( section ) ? (
|
2018-07-17 18:51:56 +00:00
|
|
|
<Link href={ section[ 0 ] } type={ isEmbedded ? 'wp-admin' : 'wc-admin' }>
|
2018-07-09 15:46:31 +00:00
|
|
|
{ section[ 1 ] }
|
|
|
|
</Link>
|
|
|
|
) : (
|
|
|
|
section
|
|
|
|
);
|
|
|
|
return <span key={ i }>{ sectionPiece }</span>;
|
|
|
|
} ) }
|
|
|
|
</h1>
|
|
|
|
<ActivityPanel />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-05-10 18:35:55 +00:00
|
|
|
|
|
|
|
Header.propTypes = {
|
|
|
|
sections: PropTypes.node.isRequired,
|
2018-06-26 14:49:42 +00:00
|
|
|
isEmbedded: PropTypes.bool,
|
2018-05-10 18:35:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Header.defaultProps = {
|
2018-06-26 14:49:42 +00:00
|
|
|
isEmbedded: false,
|
2018-05-22 15:19:56 +00:00
|
|
|
};
|
2018-05-10 18:35:55 +00:00
|
|
|
|
2018-05-18 17:31:08 +00:00
|
|
|
export default function( props ) {
|
|
|
|
return (
|
|
|
|
<Fill name="header">
|
|
|
|
<Header { ...props } />
|
|
|
|
</Fill>
|
|
|
|
);
|
|
|
|
}
|