File size: 2,673 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { localizeUrl } from '@automattic/i18n-utils';
import clsx from 'clsx';
import { useTranslate } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { ScreenReaderText, Gridicon } from '..';

import './style.scss';

/**
 * External link component that optionally includes an external link icon.
 * @type {import('react').FC}
 * @param {Object} props Component props
 * @param {string} [props.className] Additional CSS class names
 * @param {string} [props.href] URL the link points to
 * @param {Function} [props.onClick] Click handler
 * @param {boolean} [props.icon] Whether to show an external link icon
 * @param {number} [props.iconSize] Size of the icon in pixels
 * @param {string} [props.target] Link target attribute
 * @param {boolean} [props.showIconFirst] Whether to show icon before the text
 * @param {string} [props.iconClassName] Additional CSS class for the icon
 * @param {import('react').ReactNode} [props.iconComponent] Custom icon component to use instead of default
 * @param {boolean} [props.localizeUrl] Whether to localize the URL
 * @param {import('react').ReactNode} [props.children] Link content
 * @returns {import('react').ReactElement} External link component
 */
function ExternalLink( {
	className,
	icon = false,
	iconSize = 18,
	showIconFirst = false,
	iconClassName,
	iconComponent = null,
	localizeUrl: shouldLocalizeUrl = true,
	children,
	...rest
} ) {
	const translate = useTranslate();
	const classes = clsx( 'external-link', className, {
		'icon-first': showIconFirst,
		'has-icon': icon,
	} );

	const linkProps = {
		...rest,
		className: classes,
		rel: 'external',
	};

	if ( icon ) {
		linkProps.target = '_blank';
	}

	if ( linkProps.target ) {
		linkProps.rel = linkProps.rel.concat( ' noopener noreferrer' );
	}

	if ( linkProps.href && shouldLocalizeUrl ) {
		linkProps.href = localizeUrl( linkProps.href );
	}

	const iconEl = iconComponent || (
		<Gridicon className={ iconClassName } icon="external" size={ iconSize } />
	);

	return (
		<a { ...linkProps }>
			{ icon && showIconFirst && iconEl }
			{ children }
			{ icon && ! showIconFirst && iconEl }
			{ icon && (
				<ScreenReaderText>
					{ translate( '(opens in a new tab)', {
						comment: 'accessibility label for an external link',
					} ) }
				</ScreenReaderText>
			) }
		</a>
	);
}

ExternalLink.propTypes = {
	className: PropTypes.string,
	href: PropTypes.string,
	onClick: PropTypes.func,
	icon: PropTypes.bool,
	iconSize: PropTypes.number,
	target: PropTypes.string,
	showIconFirst: PropTypes.bool,
	iconClassName: PropTypes.string,
	iconComponent: PropTypes.object,
	localizeUrl: PropTypes.bool,
};
export default ExternalLink;