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';
|
2022-01-06 12:53:30 +00:00
|
|
|
import { getSetting } from '@woocommerce/settings';
|
2020-10-13 01:40:53 +00:00
|
|
|
import { useSelect } from '@wordpress/data';
|
2021-02-02 00:43:37 +00:00
|
|
|
import { useEffect, useState } from '@wordpress/element';
|
2020-11-03 00:57:49 +00:00
|
|
|
import classnames from 'classnames';
|
2020-11-11 19:44:58 +00:00
|
|
|
import { debounce } from 'lodash';
|
2021-04-02 21:35:31 +00:00
|
|
|
import { addHistoryListener } from '@woocommerce/navigation';
|
2020-11-03 00:57:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import useIsScrolled from '../../../hooks/useIsScrolled';
|
2020-10-13 01:40:53 +00:00
|
|
|
|
|
|
|
const Header = () => {
|
2020-10-21 17:02:45 +00:00
|
|
|
const siteTitle = getSetting( 'siteTitle', '' );
|
2021-02-10 22:03:47 +00:00
|
|
|
const homeUrl = getSetting( 'homeUrl', '' );
|
2020-11-03 00:57:49 +00:00
|
|
|
const isScrolled = useIsScrolled();
|
2021-02-02 00:43:37 +00:00
|
|
|
const [ isFolded, setIsFolded ] = useState(
|
|
|
|
document.body.classList.contains( false )
|
|
|
|
);
|
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 );
|
2021-02-02 00:43:37 +00:00
|
|
|
setIsFolded( true );
|
2020-11-16 18:11:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const expandNav = () => {
|
|
|
|
document.body.classList.remove( navClasses.folded );
|
|
|
|
document.body.classList.add( navClasses.expanded );
|
2021-02-02 00:43:37 +00:00
|
|
|
setIsFolded( false );
|
2020-11-16 18:11:13 +00:00
|
|
|
};
|
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"
|
2021-02-02 00:43:37 +00:00
|
|
|
aria-label="Fold navigation"
|
|
|
|
role="switch"
|
|
|
|
aria-checked={ isFolded ? 'true' : 'false' }
|
2020-10-13 01:40:53 +00:00
|
|
|
>
|
|
|
|
{ buttonIcon }
|
|
|
|
</Button>
|
|
|
|
<Button
|
2022-01-05 05:39:33 +00:00
|
|
|
title={ siteTitle }
|
2021-02-10 22:03:47 +00:00
|
|
|
href={ homeUrl }
|
2020-10-13 01:40:53 +00:00
|
|
|
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;
|