2020-10-13 01:40:53 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Button } from '@wordpress/components';
|
2020-12-28 18:48:29 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2020-10-13 01:40:53 +00:00
|
|
|
import { Icon, wordpress } from '@wordpress/icons';
|
2020-10-21 17:02:45 +00:00
|
|
|
import { getSetting } from '@woocommerce/wc-admin-settings';
|
2020-10-13 01:40:53 +00:00
|
|
|
import { useSelect } from '@wordpress/data';
|
2020-11-03 00:57:49 +00:00
|
|
|
import { useEffect } from 'react';
|
|
|
|
import classnames from 'classnames';
|
2020-11-11 19:44:58 +00:00
|
|
|
import { debounce } from 'lodash';
|
2020-11-03 00:57:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import useIsScrolled from '../../../hooks/useIsScrolled';
|
2021-01-21 20:13:09 +00:00
|
|
|
import { addHistoryListener } from '../../utils';
|
2020-10-13 01:40:53 +00:00
|
|
|
|
|
|
|
const Header = () => {
|
2020-10-21 17:02:45 +00:00
|
|
|
const siteTitle = getSetting( 'siteTitle', '' );
|
|
|
|
const siteUrl = getSetting( 'siteUrl', '' );
|
2020-11-03 00:57:49 +00:00
|
|
|
const isScrolled = useIsScrolled();
|
2020-11-16 18:11:13 +00:00
|
|
|
const navClasses = {
|
2020-11-18 18:43:20 +00:00
|
|
|
folded: 'is-wc-nav-folded',
|
|
|
|
expanded: 'is-wc-nav-expanded',
|
2020-11-16 18:11:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const foldNav = () => {
|
|
|
|
document.body.classList.add( navClasses.folded );
|
|
|
|
document.body.classList.remove( navClasses.expanded );
|
|
|
|
};
|
|
|
|
|
|
|
|
const expandNav = () => {
|
|
|
|
document.body.classList.remove( navClasses.folded );
|
|
|
|
document.body.classList.add( navClasses.expanded );
|
|
|
|
};
|
2020-10-21 17:02:45 +00:00
|
|
|
|
2020-10-13 01:40:53 +00:00
|
|
|
const toggleFolded = () => {
|
2020-11-16 18:11:13 +00:00
|
|
|
if ( document.body.classList.contains( navClasses.folded ) ) {
|
|
|
|
expandNav();
|
|
|
|
} else {
|
|
|
|
foldNav();
|
|
|
|
}
|
2020-10-13 01:40:53 +00:00
|
|
|
};
|
|
|
|
|
2020-11-11 19:44:58 +00:00
|
|
|
const foldOnMobile = ( screenWidth = document.body.clientWidth ) => {
|
2020-11-16 18:11:13 +00:00
|
|
|
if ( screenWidth <= 960 ) {
|
|
|
|
foldNav();
|
|
|
|
} else {
|
|
|
|
expandNav();
|
|
|
|
}
|
2020-11-03 00:57:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
useEffect( () => {
|
2020-11-11 19:44:58 +00:00
|
|
|
foldOnMobile();
|
|
|
|
const foldEvents = [
|
|
|
|
{
|
|
|
|
eventName: 'orientationchange',
|
|
|
|
handler: ( e ) => foldOnMobile( e.target.screen.availWidth ),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
eventName: 'resize',
|
|
|
|
handler: debounce( () => foldOnMobile(), 200 ),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
for ( const { eventName, handler } of foldEvents ) {
|
|
|
|
window.addEventListener( eventName, handler, false );
|
|
|
|
}
|
2021-01-21 20:13:09 +00:00
|
|
|
|
|
|
|
addHistoryListener( () => foldOnMobile() );
|
2020-11-03 00:57:49 +00:00
|
|
|
}, [] );
|
|
|
|
|
2020-10-13 01:40:53 +00:00
|
|
|
let buttonIcon = <Icon size="36px" icon={ wordpress } />;
|
|
|
|
|
2020-10-21 17:02:45 +00:00
|
|
|
const { isRequestingSiteIcon, siteIconUrl } = useSelect( ( select ) => {
|
|
|
|
const { isResolving } = select( 'core/data' );
|
|
|
|
const { getEntityRecord } = select( 'core' );
|
|
|
|
const siteData =
|
|
|
|
getEntityRecord( 'root', '__unstableBase', undefined ) || {};
|
|
|
|
|
|
|
|
return {
|
|
|
|
isRequestingSiteIcon: isResolving( 'core', 'getEntityRecord', [
|
|
|
|
'root',
|
|
|
|
'__unstableBase',
|
|
|
|
undefined,
|
|
|
|
] ),
|
|
|
|
siteIconUrl: siteData.siteIconUrl,
|
|
|
|
};
|
|
|
|
} );
|
2020-10-13 01:40:53 +00:00
|
|
|
|
|
|
|
if ( siteIconUrl ) {
|
|
|
|
buttonIcon = <img alt={ __( 'Site Icon' ) } src={ siteIconUrl } />;
|
|
|
|
} else if ( isRequestingSiteIcon ) {
|
|
|
|
buttonIcon = null;
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:57:49 +00:00
|
|
|
const className = classnames( 'woocommerce-navigation-header', {
|
|
|
|
'is-scrolled': isScrolled,
|
|
|
|
} );
|
|
|
|
|
2020-10-13 01:40:53 +00:00
|
|
|
return (
|
2020-11-03 00:57:49 +00:00
|
|
|
<div className={ className }>
|
2020-10-13 01:40:53 +00:00
|
|
|
<Button
|
|
|
|
onClick={ () => toggleFolded() }
|
|
|
|
className="woocommerce-navigation-header__site-icon"
|
|
|
|
>
|
|
|
|
{ buttonIcon }
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
href={ siteUrl }
|
|
|
|
className="woocommerce-navigation-header__site-title"
|
|
|
|
as="span"
|
|
|
|
>
|
2020-12-28 18:48:29 +00:00
|
|
|
{ decodeEntities( siteTitle ) }
|
2020-10-13 01:40:53 +00:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Header;
|