2020-12-18 13:17:07 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import {
|
|
|
|
Popover,
|
|
|
|
Card,
|
|
|
|
CardBody,
|
|
|
|
CardHeader,
|
|
|
|
CardFooter,
|
|
|
|
Button,
|
|
|
|
IsolatedEventContainer,
|
|
|
|
} from '@wordpress/components';
|
2021-02-10 20:28:33 +00:00
|
|
|
import {
|
|
|
|
useState,
|
|
|
|
useEffect,
|
|
|
|
createPortal,
|
|
|
|
useLayoutEffect,
|
|
|
|
} from '@wordpress/element';
|
2020-12-18 13:17:07 +00:00
|
|
|
import { close } from '@wordpress/icons';
|
|
|
|
import { noop } from 'lodash';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
const SHOW_CLASS = 'highlight-tooltip__show';
|
|
|
|
function HighlightTooltip( {
|
|
|
|
title,
|
|
|
|
closeButtonText,
|
|
|
|
content,
|
|
|
|
show = true,
|
|
|
|
id,
|
|
|
|
onClose,
|
|
|
|
delay,
|
|
|
|
onShow = noop,
|
2021-02-10 20:28:33 +00:00
|
|
|
useAnchor = false,
|
2020-12-18 13:17:07 +00:00
|
|
|
} ) {
|
|
|
|
const [ showHighlight, setShowHighlight ] = useState(
|
|
|
|
delay > 0 ? null : show
|
|
|
|
);
|
|
|
|
const [ node, setNode ] = useState( null );
|
2021-02-10 20:28:33 +00:00
|
|
|
const [ anchorRect, setAnchorRect ] = useState( null );
|
2020-12-18 13:17:07 +00:00
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
const element = document.getElementById( id );
|
2021-02-10 20:28:33 +00:00
|
|
|
let container, parent;
|
2020-12-18 13:17:07 +00:00
|
|
|
if ( element && ! node ) {
|
|
|
|
// Add tooltip container
|
2021-02-10 20:28:33 +00:00
|
|
|
if ( ! useAnchor ) {
|
|
|
|
parent = element.parentElement;
|
|
|
|
} else {
|
|
|
|
parent = document.createElement( 'div' );
|
|
|
|
document.body.appendChild( parent );
|
|
|
|
}
|
2020-12-18 13:17:07 +00:00
|
|
|
container = document.createElement( 'div' );
|
|
|
|
container.classList.add( 'highlight-tooltip__container' );
|
|
|
|
parent.appendChild( container );
|
|
|
|
setNode( container );
|
|
|
|
}
|
2021-02-10 20:28:33 +00:00
|
|
|
const timeoutId = triggerShowTooltip( container );
|
2020-12-18 13:17:07 +00:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
if ( container ) {
|
2021-02-10 20:28:33 +00:00
|
|
|
const parentElement = container.parentElement;
|
|
|
|
parentElement.removeChild( container );
|
|
|
|
if ( useAnchor ) {
|
|
|
|
parentElement.remove();
|
|
|
|
}
|
2020-12-18 13:17:07 +00:00
|
|
|
}
|
|
|
|
if ( timeoutId ) {
|
|
|
|
clearTimeout( timeoutId );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [] );
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
if ( ! showHighlight && node ) {
|
|
|
|
node.classList.remove( SHOW_CLASS );
|
|
|
|
}
|
|
|
|
}, [ showHighlight ] );
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
if ( show !== showHighlight && showHighlight !== null && node ) {
|
|
|
|
setShowHighlight( show );
|
|
|
|
if ( ! show ) {
|
|
|
|
node.classList.remove( SHOW_CLASS );
|
|
|
|
} else if ( node ) {
|
2021-02-10 20:28:33 +00:00
|
|
|
triggerShowTooltip( node );
|
2020-12-18 13:17:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [ show ] );
|
|
|
|
|
2021-02-10 20:28:33 +00:00
|
|
|
useLayoutEffect( () => {
|
|
|
|
window.addEventListener( 'resize', updateSize );
|
|
|
|
return () => window.removeEventListener( 'resize', updateSize );
|
|
|
|
}, [] );
|
|
|
|
|
|
|
|
function updateSize() {
|
|
|
|
if ( useAnchor ) {
|
|
|
|
const element = document.getElementById( id );
|
|
|
|
setAnchorRect( element.getBoundingClientRect() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const triggerShowTooltip = ( container ) => {
|
2020-12-18 13:17:07 +00:00
|
|
|
let timeoutId = null;
|
|
|
|
if ( delay > 0 ) {
|
|
|
|
timeoutId = setTimeout( () => {
|
|
|
|
timeoutId = null;
|
2021-02-10 20:28:33 +00:00
|
|
|
showTooltip( container );
|
2020-12-18 13:17:07 +00:00
|
|
|
}, delay );
|
|
|
|
} else if ( ! showHighlight ) {
|
2021-02-10 20:28:33 +00:00
|
|
|
showTooltip( container );
|
2020-12-18 13:17:07 +00:00
|
|
|
}
|
|
|
|
return timeoutId;
|
|
|
|
};
|
|
|
|
|
2021-02-10 20:28:33 +00:00
|
|
|
const showTooltip = ( container ) => {
|
|
|
|
const element = document.getElementById( id );
|
|
|
|
if ( element && useAnchor ) {
|
|
|
|
setAnchorRect( element.getBoundingClientRect() );
|
|
|
|
}
|
|
|
|
if ( container ) {
|
|
|
|
container.classList.add( SHOW_CLASS );
|
|
|
|
}
|
|
|
|
setShowHighlight( true );
|
|
|
|
onShow();
|
|
|
|
};
|
|
|
|
|
2020-12-18 13:17:07 +00:00
|
|
|
const triggerClose = () => {
|
|
|
|
setShowHighlight( false );
|
|
|
|
if ( onClose ) {
|
|
|
|
onClose();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( ! node ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return createPortal(
|
|
|
|
<div className="highlight-tooltip__portal">
|
|
|
|
{ showHighlight ? (
|
|
|
|
<>
|
|
|
|
<IsolatedEventContainer className="highlight-tooltip__overlay" />
|
|
|
|
<Popover
|
|
|
|
className="highlight-tooltip__popover"
|
|
|
|
noArrow={ false }
|
2021-02-10 20:28:33 +00:00
|
|
|
anchorRect={ anchorRect }
|
2020-12-18 13:17:07 +00:00
|
|
|
focusOnMount="container"
|
|
|
|
>
|
|
|
|
<Card size="medium">
|
|
|
|
<CardHeader>
|
|
|
|
{ title }
|
|
|
|
<Button
|
|
|
|
isSmall
|
|
|
|
onClick={ triggerClose }
|
|
|
|
icon={ close }
|
|
|
|
/>
|
|
|
|
</CardHeader>
|
|
|
|
<CardBody>{ content || null }</CardBody>
|
|
|
|
<CardFooter isBorderless={ true }>
|
|
|
|
<Button
|
|
|
|
size="small"
|
|
|
|
isPrimary
|
|
|
|
onClick={ triggerClose }
|
|
|
|
>
|
|
|
|
{ closeButtonText ||
|
2022-03-30 09:00:04 +00:00
|
|
|
__( 'Close', 'woocommerce' ) }
|
2020-12-18 13:17:07 +00:00
|
|
|
</Button>
|
|
|
|
</CardFooter>
|
|
|
|
</Card>
|
|
|
|
</Popover>
|
|
|
|
</>
|
|
|
|
) : null }
|
|
|
|
</div>,
|
|
|
|
node
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
HighlightTooltip.propTypes = {
|
|
|
|
/**
|
|
|
|
* The id of the element it should highlight, should be unique per HighlightTooltip.
|
|
|
|
*/
|
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
/**
|
|
|
|
* Title of the popup
|
|
|
|
*/
|
|
|
|
title: PropTypes.string.isRequired,
|
|
|
|
/**
|
|
|
|
* Text of the close button.
|
|
|
|
*/
|
|
|
|
closeButtonText: PropTypes.string.isRequired,
|
|
|
|
/**
|
|
|
|
* Content of the popup, can be either text or react element.
|
|
|
|
*/
|
|
|
|
content: PropTypes.oneOfType( [ PropTypes.string, PropTypes.node ] ),
|
|
|
|
/**
|
|
|
|
* If to show the popup, defaults to true.
|
|
|
|
*/
|
|
|
|
show: PropTypes.bool,
|
|
|
|
/**
|
|
|
|
* Callback for when the user closes the popup.
|
|
|
|
*/
|
|
|
|
onClose: PropTypes.func,
|
|
|
|
/**
|
|
|
|
* This will delay the popup from appearing by the number of ms.
|
|
|
|
*/
|
|
|
|
delay: PropTypes.number,
|
|
|
|
/**
|
|
|
|
* A callback for when the tooltip is shown.
|
|
|
|
*/
|
|
|
|
onShow: PropTypes.func,
|
2021-02-10 20:28:33 +00:00
|
|
|
/**
|
|
|
|
* useAnchor, will append the tooltip to the body tag, and make use of the anchorRect to display the tooltip.
|
|
|
|
* Defaults to false.
|
|
|
|
*/
|
|
|
|
useAnchor: PropTypes.bool,
|
2020-12-18 13:17:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export { HighlightTooltip };
|