2018-05-10 18:35:55 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-05-24 16:03:03 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2021-02-12 16:32:26 +00:00
|
|
|
import { useEffect, useLayoutEffect, useRef } from '@wordpress/element';
|
2018-07-09 15:46:31 +00:00
|
|
|
import classnames from 'classnames';
|
2018-07-23 20:14:40 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2023-03-10 14:58:02 +00:00
|
|
|
import {
|
|
|
|
WC_HEADER_SLOT_NAME,
|
|
|
|
WC_HEADER_PAGE_TITLE_SLOT_NAME,
|
|
|
|
WooHeaderNavigationItem,
|
|
|
|
WooHeaderItem,
|
|
|
|
WooHeaderPageTitle,
|
|
|
|
} from '@woocommerce/admin-layout';
|
2022-01-06 12:53:30 +00:00
|
|
|
import { getSetting } from '@woocommerce/settings';
|
2021-12-14 16:56:42 +00:00
|
|
|
import { Text, useSlot } from '@woocommerce/experimental';
|
2018-11-05 21:02:04 +00:00
|
|
|
|
2018-05-10 18:35:55 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
2020-11-03 00:57:49 +00:00
|
|
|
import useIsScrolled from '../hooks/useIsScrolled';
|
2023-05-17 01:54:31 +00:00
|
|
|
import { TasksReminderBar, useActiveSetupTasklist } from '../task-lists';
|
2018-05-10 18:35:55 +00:00
|
|
|
|
2021-12-14 16:56:42 +00:00
|
|
|
export const PAGE_TITLE_FILTER = 'woocommerce_admin_header_page_title';
|
2021-03-03 00:05:08 +00:00
|
|
|
|
2020-08-23 22:46:18 +00:00
|
|
|
export const Header = ( { sections, isEmbedded = false, query } ) => {
|
|
|
|
const headerElement = useRef( null );
|
2022-06-01 17:10:20 +00:00
|
|
|
const activeSetupList = useActiveSetupTasklist();
|
2020-08-23 22:46:18 +00:00
|
|
|
const siteTitle = getSetting( 'siteTitle', '' );
|
2020-10-06 20:42:46 +00:00
|
|
|
const pageTitle = sections.slice( -1 )[ 0 ];
|
2023-10-12 14:56:48 +00:00
|
|
|
const { isScrolled } = useIsScrolled();
|
2021-02-12 16:32:26 +00:00
|
|
|
let debounceTimer = null;
|
2020-01-20 17:34:24 +00:00
|
|
|
|
2020-08-23 22:46:18 +00:00
|
|
|
const className = classnames( 'woocommerce-layout__header', {
|
|
|
|
'is-scrolled': isScrolled,
|
|
|
|
} );
|
2020-01-20 17:34:24 +00:00
|
|
|
|
2023-03-10 14:58:02 +00:00
|
|
|
const pageTitleSlot = useSlot( WC_HEADER_PAGE_TITLE_SLOT_NAME );
|
2021-12-14 16:56:42 +00:00
|
|
|
const hasPageTitleFills = Boolean( pageTitleSlot?.fills?.length );
|
2023-03-10 14:58:02 +00:00
|
|
|
const headerItemSlot = useSlot( WC_HEADER_SLOT_NAME );
|
2021-12-14 16:56:42 +00:00
|
|
|
const headerItemSlotFills = headerItemSlot?.fills;
|
|
|
|
|
2023-03-10 14:58:02 +00:00
|
|
|
const updateBodyMargin = () => {
|
|
|
|
clearTimeout( debounceTimer );
|
|
|
|
debounceTimer = setTimeout( function () {
|
|
|
|
const wpBody = document.querySelector( '#wpbody' );
|
|
|
|
|
|
|
|
if ( ! wpBody || ! headerElement.current ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-13 20:22:11 +00:00
|
|
|
wpBody.style.marginTop = `${ headerElement.current.height }px`;
|
2023-03-10 14:58:02 +00:00
|
|
|
}, 200 );
|
|
|
|
};
|
|
|
|
|
2021-02-12 16:32:26 +00:00
|
|
|
useLayoutEffect( () => {
|
|
|
|
updateBodyMargin();
|
|
|
|
window.addEventListener( 'resize', updateBodyMargin );
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener( 'resize', updateBodyMargin );
|
|
|
|
const wpBody = document.querySelector( '#wpbody' );
|
|
|
|
|
|
|
|
if ( ! wpBody ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wpBody.style.marginTop = null;
|
|
|
|
};
|
2021-12-14 16:56:42 +00:00
|
|
|
}, [ headerItemSlotFills ] );
|
2021-02-12 16:32:26 +00:00
|
|
|
|
2020-08-23 22:46:18 +00:00
|
|
|
useEffect( () => {
|
|
|
|
if ( ! isEmbedded ) {
|
2020-08-27 01:46:53 +00:00
|
|
|
const documentTitle = sections
|
2020-08-23 22:46:18 +00:00
|
|
|
.map( ( section ) => {
|
|
|
|
return Array.isArray( section ) ? section[ 1 ] : section;
|
|
|
|
} )
|
|
|
|
.reverse()
|
|
|
|
.join( ' ‹ ' );
|
|
|
|
|
|
|
|
const decodedTitle = decodeEntities(
|
|
|
|
sprintf(
|
|
|
|
/* translators: 1: document title. 2: page title */
|
|
|
|
__(
|
|
|
|
'%1$s ‹ %2$s — WooCommerce',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2020-08-23 22:46:18 +00:00
|
|
|
),
|
|
|
|
documentTitle,
|
|
|
|
siteTitle
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( document.title !== decodedTitle ) {
|
|
|
|
document.title = decodedTitle;
|
|
|
|
}
|
|
|
|
}
|
2020-08-27 01:46:53 +00:00
|
|
|
}, [ isEmbedded, sections, siteTitle ] );
|
|
|
|
|
2020-08-23 22:46:18 +00:00
|
|
|
return (
|
|
|
|
<div className={ className } ref={ headerElement }>
|
2022-06-01 17:10:20 +00:00
|
|
|
{ activeSetupList && (
|
2022-03-22 15:28:58 +00:00
|
|
|
<TasksReminderBar
|
|
|
|
updateBodyMargin={ updateBodyMargin }
|
2022-06-01 17:10:20 +00:00
|
|
|
taskListId={ activeSetupList }
|
2022-03-22 15:28:58 +00:00
|
|
|
/>
|
|
|
|
) }
|
2021-12-14 16:56:42 +00:00
|
|
|
<div className="woocommerce-layout__header-wrapper">
|
|
|
|
<WooHeaderNavigationItem.Slot
|
|
|
|
fillProps={ { isEmbedded, query } }
|
2020-08-27 01:46:53 +00:00
|
|
|
/>
|
2020-10-06 20:42:46 +00:00
|
|
|
|
2021-02-12 16:32:26 +00:00
|
|
|
<Text
|
2021-12-14 16:56:42 +00:00
|
|
|
className={ `woocommerce-layout__header-heading` }
|
2021-02-12 16:32:26 +00:00
|
|
|
as="h1"
|
|
|
|
>
|
2021-12-14 16:56:42 +00:00
|
|
|
{ decodeEntities(
|
|
|
|
hasPageTitleFills ? (
|
|
|
|
<WooHeaderPageTitle.Slot
|
|
|
|
fillProps={ { isEmbedded, query } }
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
pageTitle
|
|
|
|
)
|
|
|
|
) }
|
2021-02-12 16:32:26 +00:00
|
|
|
</Text>
|
|
|
|
|
2021-12-14 16:56:42 +00:00
|
|
|
<WooHeaderItem.Slot fillProps={ { isEmbedded, query } } />
|
2021-02-12 16:32:26 +00:00
|
|
|
</div>
|
2020-08-23 22:46:18 +00:00
|
|
|
</div>
|
|
|
|
);
|
2018-05-22 15:19:56 +00:00
|
|
|
};
|