woocommerce-paypal-payments/modules/ppcp-settings/resources/js/Components/ReusableComponents/TopNavigation.js

98 lines
2.4 KiB
JavaScript
Raw Normal View History

import { useCallback, useLayoutEffect } from '@wordpress/element';
import { Button, Icon } from '@wordpress/components';
import { chevronLeft } from '@wordpress/icons';
import classNames from 'classnames';
import useIsScrolled from '../../hooks/useIsScrolled';
import { useNavigation } from '../../hooks/useNavigation';
import BusyStateWrapper from './BusyStateWrapper';
2025-01-28 08:59:06 +01:00
import TabNavigation from './TabNavigation';
const TopNavigation = ( {
title,
children,
isMainTitle = true,
exitOnTitleClick = false,
onTitleClick = null,
showProgressBar = false,
progressBarPercent = 0,
2025-01-28 08:59:06 +01:00
tabs,
activePanel,
setActivePanel,
} ) => {
const { goToWooCommercePaymentsTab } = useNavigation();
const { isScrolled } = useIsScrolled();
const className = classNames( 'ppcp-r-navigation-container', {
2025-01-22 15:08:14 +01:00
'ppcp--is-scrolled': isScrolled,
} );
2025-01-23 19:54:17 +01:00
const titleClassName = classNames( 'ppcp--nav-title', {
2025-01-22 15:08:14 +01:00
'ppcp--big': isMainTitle,
} );
const handleTitleClick = useCallback( () => {
if ( exitOnTitleClick ) {
goToWooCommercePaymentsTab();
} else if ( 'function' === typeof onTitleClick ) {
onTitleClick();
}
}, [ exitOnTitleClick, goToWooCommercePaymentsTab, onTitleClick ] );
// Removes the excess padding at the top of the navigation bar.
useLayoutEffect( () => {
window.dispatchEvent( new Event( 'resize' ) );
}, [] );
return (
<div className={ className }>
<div className="ppcp-r-navigation">
2025-01-28 12:15:49 +01:00
<div className="ppcp-r-navigation--row">
<BusyStateWrapper
className="ppcp-r-navigation--left"
busySpinner={ false }
enabled={ ! exitOnTitleClick }
>
2025-01-28 12:15:49 +01:00
<Button
variant="link"
onClick={ handleTitleClick }
className="is-title"
>
<Icon icon={ chevronLeft } />
<span className={ titleClassName }>{ title }</span>
</Button>
</BusyStateWrapper>
2025-01-28 12:15:49 +01:00
<BusyStateWrapper
className="ppcp-r-navigation--right"
busySpinner={ false }
>
{ children }
</BusyStateWrapper>
</div>
2025-01-28 08:59:06 +01:00
<TabNavigation
tabs={ tabs }
activePanel={ activePanel }
setActivePanel={ setActivePanel }
></TabNavigation>
{ showProgressBar && (
<ProgressBar percent={ progressBarPercent } />
) }
</div>
</div>
);
};
const ProgressBar = ( { percent } ) => {
percent = Math.min( Math.max( percent, 0 ), 100 );
return (
<div
className="ppcp-r-navigation--progress-bar"
style={ { width: `${ percent }%` } }
/>
);
};
export default TopNavigation;