-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathIcon.js
228 lines (199 loc) · 4.75 KB
/
Icon.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* Copyright IBM Corp. 2016, 2018
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import invariant from 'invariant';
import PropTypes from 'prop-types';
import React from 'react';
import icons from 'carbon-icons';
/**
* The icons list object from `carbon-icons`.
* @type {Object}
*/
let iconsList = icons;
/**
* Returns a single icon Object
* @param {string} name - "name" property of icon
* @param {Object} [iconsObj=icons] - JSON Array of Objects
* @example
* // Returns a single icon Object
* this.findIcon('copy-code', icons.json);
*/
export function findIcon(name, iconsObj = iconsList) {
const icon = iconsObj.filter(obj => obj.name === name);
if (icon.length === 0) {
return false;
} else if (icon.length > 1) {
throw new Error('Multiple icons found...');
} else {
return icon[0];
}
}
/**
* Sets the icons list object from `carbon-icons`.
* Doing so instead of having this module directly import `carbon-icons`
* avoids the icons list being included in applications' bundles if only limited set of icons is in use.
* @param {Object} list The icons list from `carbon-icons`.
*/
export function setIconsList(list) {
iconsList = list;
}
/**
* Returns "svgData" Object
* @param {string} iconName - "name" property of icon
* @example
* // Returns svgData Object for given iconName
* this.getSvgData('copy-code');
*/
export function getSvgData(iconName) {
const icon = findIcon(iconName);
return icon ? icon.svgData : false;
}
/**
* @param {Object} svgData - JSON Object for an SVG icon
* @returns {ReactElement} Elements/Nodes for SVG
* @example
* // Returns SVG elements
* const svgData = getSvgData('copy-code');
* svgShapes(svgData);
*/
export function svgShapes(svgData) {
const svgElements = Object.keys(svgData)
.filter(key => svgData[key])
.map(svgProp => {
const data = svgData[svgProp];
if (svgProp === 'circles') {
return data.map((circle, index) => {
const circleProps = {
cx: circle.cx,
cy: circle.cy,
r: circle.r,
key: `circle${index}`,
};
return <circle {...circleProps} />;
});
} else if (svgProp === 'paths') {
return data.map((path, index) => (
<path d={path.d} key={`key${index}`} />
));
} else if (svgProp === 'polygons') {
return data.map((polygon, index) => (
<polygon points={polygon.points} key={`key${index}`} />
));
}
return '';
});
return svgElements;
}
export function isPrefixed(name) {
if (__DEV__) {
invariant(
typeof name === 'string',
'[Icon] icon name is missing. You likely forgot to specify the icon, ' +
'or are using older (pre-`7.x`) version of `carbon-icons` library. ' +
'To specify the icon, use either `icon` (data) or `name` (icon name) properties.'
);
}
return name && name.split('--')[0] === 'icon';
}
const Icon = ({
className,
iconTitle,
description,
fill,
fillRule,
height,
name,
icon,
role,
style,
width,
iconRef,
...other
}) => {
const props = {
className,
fill,
fillRule,
height: height || icon.height,
name: isPrefixed ? name : `icon--${name}`,
role,
style,
viewBox: icon.viewBox,
width: width || icon.width,
ref: iconRef,
...other,
};
const svgContent = icon ? svgShapes(icon.svgData) : '';
return (
<svg {...props} aria-label={description}>
<title>
{typeof iconTitle === 'undefined' ? description : iconTitle}
</title>
{svgContent}
</svg>
);
};
Icon.propTypes = {
/**
* The CSS class name.
*/
className: PropTypes.string,
/**
* The icon title.
*/
iconTitle: PropTypes.string,
/**
* The icon description.
*/
description: PropTypes.string.isRequired,
/**
* The `<svg>` `fill` attribute.
*/
fill: PropTypes.string,
/**
* The `<svg>` `fillRule` attribute.
*/
fillRule: PropTypes.string,
/**
* The `<svg>` `height` attribute.
*/
height: PropTypes.string,
/**
* The icon data.
*/
icon: PropTypes.shape({
width: PropTypes.string,
height: PropTypes.string,
viewBox: PropTypes.string.isRequired,
svgData: PropTypes.object.isRequired,
}),
/**
* The `role` attribute.
*/
role: PropTypes.string,
/**
* The CSS styles.
*/
style: PropTypes.object,
/**
* The `<svg>` `viewbox` attribute.
*/
viewBox: PropTypes.string,
/**
* The `<svg>` `width` attribute.
*/
width: PropTypes.string,
/**
* The `ref` callback for the icon.
*/
iconRef: PropTypes.func,
};
Icon.defaultProps = {
fillRule: 'evenodd',
role: 'img',
};
export { icons };
export default Icon;