File size: 2,526 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
import clsx from 'clsx';
import {
	iconsThatNeedOffset,
	iconsThatNeedOffsetX,
	iconsThatNeedOffsetY,
} from 'gridicons/dist/util/icons-offset';
import spritePath from 'gridicons/svg-sprite/gridicons.svg';
import * as React from 'react';
import type { ReactNode } from 'react';
import type { Assign } from 'utility-types';

interface Props {
	icon: string;
	size?: number;
	title?: ReactNode;
}

type AllProps = Assign< React.SVGProps< SVGSVGElement >, Props >;

function isCrossOrigin( url: string ) {
	if ( typeof window === 'undefined' ) {
		return false;
	}
	return new URL( url, window.location.href ).origin !== window.location.origin;
}

/**
 * Fetches the SVG file and creates a proxy URL for it.
 * @param url the original cross-origin URL
 * @returns the proxied URL.
 */
function useProxiedURL( url: string ) {
	const [ urlProxy, setUrlProxy ] = React.useState( () =>
		isCrossOrigin( url ) ? undefined : url
	);
	React.useEffect( () => {
		if ( isCrossOrigin( url ) ) {
			setUrlProxy( undefined );
			fetch( url )
				.then( ( res ) => res.blob() )
				.then( ( blob ) => {
					const urlProxy = URL.createObjectURL( blob );
					setUrlProxy( urlProxy );
				} )
				.catch( () => {} );
		} else {
			setUrlProxy( url );
		}
	}, [ url ] );

	return urlProxy;
}

const Gridicon = React.memo(
	React.forwardRef< SVGSVGElement, AllProps >( ( props: AllProps, ref ) => {
		const { size = 24, icon, className, title, ...otherProps } = props;
		const isModulo18 = size % 18 === 0;

		// Proxy URL to support cross-origin SVGs.
		const proxiedSpritePath = useProxiedURL( spritePath );

		// Using a missing icon doesn't produce any errors, just a blank icon, which is the exact intended behaviour.
		// This means we don't need to perform any checks on the icon name.
		const iconName = `gridicons-${ icon }`;

		const iconClass = clsx( 'gridicon', iconName, className, {
			'needs-offset': isModulo18 && iconsThatNeedOffset.includes( iconName ),
			'needs-offset-x': isModulo18 && iconsThatNeedOffsetX.includes( iconName ),
			'needs-offset-y': isModulo18 && iconsThatNeedOffsetY.includes( iconName ),
		} );

		return (
			<svg
				xmlns="http://www.w3.org/2000/svg"
				viewBox="0 0 24 24"
				className={ iconClass }
				height={ size }
				width={ size }
				ref={ ref }
				{ ...otherProps }
			>
				{ title && <title>{ title }</title> }
				{ proxiedSpritePath && <use xlinkHref={ `${ proxiedSpritePath }#${ iconName }` } /> }
			</svg>
		);
	} )
);

Gridicon.displayName = 'Gridicon';

export default Gridicon;