-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdocumentation-link-props.js
78 lines (75 loc) · 2.32 KB
/
documentation-link-props.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
78
/**
* External dependencies
*/
import { recordEvent } from '@woocommerce/tracks';
/**
* Clicking on an external documentation link.
*
* @event wcadmin_pfw_documentation_link_click
*
* @property {string} link_id Identifier of the link.
* @property {string} context `'settings' | 'welcome-section' | 'wizard'` In which context the link was placed?
* @property {string} href Href to which the user was navigated to.
*/
/**
* Clicking on the link inside the notice.
*
* @event wcadmin_pfw_get_started_notice_link_click
*
* @property {string} link_id Identifier of the link.
* @property {string} context What action was initiated.
* @property {string} href Href to which the user was navigated to.
*
*
*/
/**
* Creates properties for an external documentation link.
* May take any other props to be extended and forwarded to a link element (`<a>`, `<Button isLink>`).
*
* Sets `target="_blank" rel="noopener"` and `onClick` handler that fires track event.
*
* Please be careful not to overwrite the `onClick` handler coincidently.
*
*
* @fires wcadmin_pfw_documentation_link_click on click, with given `linkId` and `context`.
* @param {Object} props React props.
* @param {string} props.href Href to used by link and in track event.
* @param {string} props.linkId Forwarded to {@link wcadmin_pfw_documentation_link_click}
* @param {string} props.context Forwarded to {@link wcadmin_pfw_documentation_link_click}
* @param {string} [props.target='_blank']
* @param {string} [props.rel='noopener']
* @param {Function} [props.onClick] onClick event handler to be decorated with firing Track event.
* @param {string} [props.eventName='pfw_documentation_link_click'] The name of the event to be recorded
* @param {...import('react').AnchorHTMLAttributes} props.props
* @return {{href: string, target: string, rel: string, onClick: Function, props}} Documentation link props.
*/
function documentationLinkProps( {
href,
linkId,
context,
target = '_blank',
rel = 'noopener',
onClick,
eventName = 'pfw_documentation_link_click',
...props
} ) {
return {
href,
target,
rel,
...props,
onClick: ( event ) => {
if ( onClick ) {
onClick( event );
}
if ( ! event.defaultPrevented ) {
recordEvent( eventName, {
link_id: linkId,
context,
href,
} );
}
},
};
}
export default documentationLinkProps;