2021-12-14 16:56:42 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Tooltip } from '@wordpress/components';
|
|
|
|
import { Icon, chevronLeft } from '@wordpress/icons';
|
|
|
|
import { getHistory, updateQueryString } from '@woocommerce/navigation';
|
|
|
|
import { ENTER, SPACE } from '@wordpress/keycodes';
|
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './back-button.scss';
|
|
|
|
|
|
|
|
export type BackButtonProps = {
|
|
|
|
title: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const BackButton: React.FC< BackButtonProps > = ( { title } ) => {
|
2022-03-30 09:00:04 +00:00
|
|
|
const homeText = __( 'WooCommerce Home', 'woocommerce' );
|
2021-12-14 16:56:42 +00:00
|
|
|
|
|
|
|
const navigateHome = () => {
|
|
|
|
recordEvent( 'topbar_back_button', {
|
|
|
|
page_name: title,
|
|
|
|
} );
|
2022-05-30 06:51:33 +00:00
|
|
|
updateQueryString( {}, getHistory().location.pathname, {} );
|
2021-12-14 16:56:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// if it's a task list page, render a back button to the homescreen
|
|
|
|
return (
|
|
|
|
<Tooltip text={ homeText }>
|
|
|
|
<div
|
|
|
|
tabIndex={ 0 }
|
|
|
|
role="button"
|
|
|
|
data-testid="header-back-button"
|
|
|
|
className="woocommerce-layout__header-back-button"
|
|
|
|
onKeyDown={ ( { keyCode } ) => {
|
|
|
|
if ( keyCode === ENTER || keyCode === SPACE ) {
|
|
|
|
navigateHome();
|
|
|
|
}
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
<Icon icon={ chevronLeft } onClick={ navigateHome } />
|
|
|
|
</div>
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
};
|