2020-10-13 01:40:53 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Button } from '@wordpress/components';
|
|
|
|
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';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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', '' );
|
|
|
|
const siteUrl = getSetting( 'siteUrl', '' );
|
2020-11-03 00:57:49 +00:00
|
|
|
const isScrolled = useIsScrolled();
|
2020-10-21 17:02:45 +00:00
|
|
|
|
2020-10-13 01:40:53 +00:00
|
|
|
const toggleFolded = () => {
|
|
|
|
document.body.classList.toggle( 'is-folded' );
|
|
|
|
};
|
|
|
|
|
2020-11-03 00:57:49 +00:00
|
|
|
const foldOnMobile = ( screenWidth ) => {
|
|
|
|
if ( screenWidth <= 960 ) {
|
|
|
|
document.body.classList.add( 'is-folded' );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
foldOnMobile( document.body.clientWidth );
|
|
|
|
|
|
|
|
window.addEventListener(
|
|
|
|
'orientationchange',
|
|
|
|
( e ) => foldOnMobile( e.target.screen.availWidth ),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}, [] );
|
|
|
|
|
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"
|
|
|
|
>
|
|
|
|
{ siteTitle }
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Header;
|