2021-02-16 20:01:11 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-02-25 12:56:00 +00:00
|
|
|
import { Suspense, useRef, useCallback } from '@wordpress/element';
|
2021-02-16 20:01:11 +00:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import { Spinner } from '@woocommerce/components';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-12-14 16:56:42 +00:00
|
|
|
import useFocusOnMount from '~/hooks/useFocusOnMount';
|
|
|
|
import useFocusOutside from '~/hooks/useFocusOutside';
|
2021-02-16 20:01:11 +00:00
|
|
|
|
|
|
|
export const Panel = ( {
|
|
|
|
content,
|
|
|
|
isPanelOpen,
|
|
|
|
isPanelSwitching,
|
2021-02-25 12:56:00 +00:00
|
|
|
currentTab,
|
2021-02-16 20:01:11 +00:00
|
|
|
tab,
|
|
|
|
closePanel,
|
|
|
|
clearPanel,
|
|
|
|
} ) => {
|
2021-03-25 23:42:01 +00:00
|
|
|
const panelClass = 'woocommerce-layout__activity-panel-wrapper';
|
|
|
|
|
2021-02-16 20:01:11 +00:00
|
|
|
const handleFocusOutside = ( event ) => {
|
|
|
|
const isClickOnModalOrSnackbar =
|
2021-03-09 13:11:49 +00:00
|
|
|
event.relatedTarget &&
|
2021-03-25 23:42:01 +00:00
|
|
|
( event.relatedTarget.closest(
|
|
|
|
'.woocommerce-inbox-dismiss-confirmation_modal'
|
|
|
|
) ||
|
|
|
|
event.relatedTarget.closest( '.components-snackbar__action' ) );
|
2021-03-09 13:11:49 +00:00
|
|
|
|
2021-03-25 23:42:01 +00:00
|
|
|
if ( isPanelOpen && ! isClickOnModalOrSnackbar ) {
|
2021-02-16 20:01:11 +00:00
|
|
|
closePanel();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-25 12:56:00 +00:00
|
|
|
const possibleFocusPanel = () => {
|
|
|
|
if ( ! containerRef.current || ! isPanelOpen || ! tab ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
focusOnMountRef( containerRef.current );
|
|
|
|
};
|
|
|
|
|
|
|
|
const finishTransition = ( e ) => {
|
|
|
|
if ( e && e.propertyName === 'transform' ) {
|
|
|
|
clearPanel();
|
|
|
|
possibleFocusPanel();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const focusOnMountRef = useFocusOnMount();
|
2021-02-16 20:01:11 +00:00
|
|
|
const useFocusOutsideProps = useFocusOutside( handleFocusOutside );
|
2021-02-25 12:56:00 +00:00
|
|
|
const containerRef = useRef( null );
|
|
|
|
|
|
|
|
const mergedContainerRef = useCallback( ( node ) => {
|
|
|
|
containerRef.current = node;
|
|
|
|
focusOnMountRef( node );
|
|
|
|
}, [] );
|
2021-02-16 20:01:11 +00:00
|
|
|
|
|
|
|
if ( ! tab ) {
|
2021-03-25 23:42:01 +00:00
|
|
|
return <div className={ panelClass } />;
|
2021-02-16 20:01:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! content ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:42:01 +00:00
|
|
|
const classNames = classnames( panelClass, {
|
|
|
|
'is-open': isPanelOpen,
|
|
|
|
'is-switching': isPanelSwitching,
|
|
|
|
} );
|
2021-02-16 20:01:11 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={ classNames }
|
|
|
|
tabIndex={ 0 }
|
|
|
|
role="tabpanel"
|
|
|
|
aria-label={ tab.title }
|
2021-02-25 12:56:00 +00:00
|
|
|
onTransitionEnd={ finishTransition }
|
2021-02-16 20:01:11 +00:00
|
|
|
{ ...useFocusOutsideProps }
|
2021-02-25 12:56:00 +00:00
|
|
|
ref={ mergedContainerRef }
|
2021-02-16 20:01:11 +00:00
|
|
|
>
|
|
|
|
<div
|
|
|
|
className="woocommerce-layout__activity-panel-content"
|
|
|
|
key={ 'activity-panel-' + currentTab }
|
|
|
|
id={ 'activity-panel-' + currentTab }
|
|
|
|
>
|
|
|
|
<Suspense fallback={ <Spinner /> }>{ content }</Suspense>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Panel;
|