forked from woocommerce/google-listings-and-ads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (69 loc) · 1.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* External dependencies
*/
import classnames from 'classnames';
import { __ } from '@wordpress/i18n';
import { Popover } from '@wordpress/components';
import { useState } from '@wordpress/element';
import GridiconHelpOutline from 'gridicons/dist/help-outline';
/**
* Internal dependencies
*/
import { recordGlaEvent } from '.~/utils/tracks';
import './index.scss';
/**
* Viewing tooltip
*
* @event gla_tooltip_viewed
* @property {string} id Tooltip identifier.
*/
/**
* @param {Object} props React props
* @param {string} [props.className] Additional CSS class name to be appended.
* @param {string} [props.id] The Popover’s ID for event tracking.
* @param {boolean} [props.disabled=false] Whether to disable the help icon button and also hide the popover.
* @param {number} [props.iconSize=16] Size of the help icon.
* @param {Array<JSX.Element>} props.children The Popover’s content
* @fires gla_tooltip_viewed with the given `id`.
*/
const HelpPopover = ( {
className,
id,
disabled = false,
iconSize = 16,
children,
...props
} ) => {
const [ showPopover, setShowPopover ] = useState( false );
const handleButtonClick = () => {
setShowPopover( true );
if ( id ) {
recordGlaEvent( 'gla_tooltip_viewed', { id } );
}
};
const handlePopoverClose = () => {
setShowPopover( false );
};
return (
<span className={ classnames( 'help-popover', className ) }>
<button
aria-label={ __( 'Open popover', 'google-listings-and-ads' ) }
disabled={ disabled }
onClick={ handleButtonClick }
>
<GridiconHelpOutline size={ iconSize }></GridiconHelpOutline>
</button>
{ showPopover && ! disabled && (
<Popover
focusOnMount="container"
inline
onClose={ handlePopoverClose }
{ ...props }
>
{ children }
</Popover>
) }
</span>
);
};
export default HelpPopover;