UNPKG

10.9 kBJavaScriptView Raw
1import { parse, icon } from '@fortawesome/fontawesome-svg-core';
2import PropTypes from 'prop-types';
3import React from 'react';
4
5function _typeof(obj) {
6 if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
7 _typeof = function (obj) {
8 return typeof obj;
9 };
10 } else {
11 _typeof = function (obj) {
12 return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
13 };
14 }
15
16 return _typeof(obj);
17}
18
19function _defineProperty(obj, key, value) {
20 if (key in obj) {
21 Object.defineProperty(obj, key, {
22 value: value,
23 enumerable: true,
24 configurable: true,
25 writable: true
26 });
27 } else {
28 obj[key] = value;
29 }
30
31 return obj;
32}
33
34function ownKeys(object, enumerableOnly) {
35 var keys = Object.keys(object);
36
37 if (Object.getOwnPropertySymbols) {
38 var symbols = Object.getOwnPropertySymbols(object);
39 if (enumerableOnly) symbols = symbols.filter(function (sym) {
40 return Object.getOwnPropertyDescriptor(object, sym).enumerable;
41 });
42 keys.push.apply(keys, symbols);
43 }
44
45 return keys;
46}
47
48function _objectSpread2(target) {
49 for (var i = 1; i < arguments.length; i++) {
50 var source = arguments[i] != null ? arguments[i] : {};
51
52 if (i % 2) {
53 ownKeys(Object(source), true).forEach(function (key) {
54 _defineProperty(target, key, source[key]);
55 });
56 } else if (Object.getOwnPropertyDescriptors) {
57 Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
58 } else {
59 ownKeys(Object(source)).forEach(function (key) {
60 Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
61 });
62 }
63 }
64
65 return target;
66}
67
68function _objectWithoutPropertiesLoose(source, excluded) {
69 if (source == null) return {};
70 var target = {};
71 var sourceKeys = Object.keys(source);
72 var key, i;
73
74 for (i = 0; i < sourceKeys.length; i++) {
75 key = sourceKeys[i];
76 if (excluded.indexOf(key) >= 0) continue;
77 target[key] = source[key];
78 }
79
80 return target;
81}
82
83function _objectWithoutProperties(source, excluded) {
84 if (source == null) return {};
85
86 var target = _objectWithoutPropertiesLoose(source, excluded);
87
88 var key, i;
89
90 if (Object.getOwnPropertySymbols) {
91 var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
92
93 for (i = 0; i < sourceSymbolKeys.length; i++) {
94 key = sourceSymbolKeys[i];
95 if (excluded.indexOf(key) >= 0) continue;
96 if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
97 target[key] = source[key];
98 }
99 }
100
101 return target;
102}
103
104function _toConsumableArray(arr) {
105 return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
106}
107
108function _arrayWithoutHoles(arr) {
109 if (Array.isArray(arr)) {
110 for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
111
112 return arr2;
113 }
114}
115
116function _iterableToArray(iter) {
117 if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
118}
119
120function _nonIterableSpread() {
121 throw new TypeError("Invalid attempt to spread non-iterable instance");
122}
123
124// Get CSS class list from a props object
125function classList(props) {
126 var _classes;
127
128 var spin = props.spin,
129 pulse = props.pulse,
130 fixedWidth = props.fixedWidth,
131 inverse = props.inverse,
132 border = props.border,
133 listItem = props.listItem,
134 flip = props.flip,
135 size = props.size,
136 rotation = props.rotation,
137 pull = props.pull; // map of CSS class names to properties
138
139 var classes = (_classes = {
140 'fa-spin': spin,
141 'fa-pulse': pulse,
142 'fa-fw': fixedWidth,
143 'fa-inverse': inverse,
144 'fa-border': border,
145 'fa-li': listItem,
146 'fa-flip-horizontal': flip === 'horizontal' || flip === 'both',
147 'fa-flip-vertical': flip === 'vertical' || flip === 'both'
148 }, _defineProperty(_classes, "fa-".concat(size), typeof size !== 'undefined' && size !== null), _defineProperty(_classes, "fa-rotate-".concat(rotation), typeof rotation !== 'undefined' && rotation !== null), _defineProperty(_classes, "fa-pull-".concat(pull), typeof pull !== 'undefined' && pull !== null), _defineProperty(_classes, 'fa-swap-opacity', props.swapOpacity), _classes); // map over all the keys in the classes object
149 // return an array of the keys where the value for the key is not null
150
151 return Object.keys(classes).map(function (key) {
152 return classes[key] ? key : null;
153 }).filter(function (key) {
154 return key;
155 });
156}
157
158// Camelize taken from humps
159// humps is copyright © 2012+ Dom Christie
160// Released under the MIT license.
161// Performant way to determine if object coerces to a number
162function _isNumerical(obj) {
163 obj = obj - 0; // eslint-disable-next-line no-self-compare
164
165 return obj === obj;
166}
167
168function camelize(string) {
169 if (_isNumerical(string)) {
170 return string;
171 } // eslint-disable-next-line no-useless-escape
172
173
174 string = string.replace(/[\-_\s]+(.)?/g, function (match, chr) {
175 return chr ? chr.toUpperCase() : '';
176 }); // Ensure 1st char is always lowercase
177
178 return string.substr(0, 1).toLowerCase() + string.substr(1);
179}
180
181function capitalize(val) {
182 return val.charAt(0).toUpperCase() + val.slice(1);
183}
184
185function styleToObject(style) {
186 return style.split(';').map(function (s) {
187 return s.trim();
188 }).filter(function (s) {
189 return s;
190 }).reduce(function (acc, pair) {
191 var i = pair.indexOf(':');
192 var prop = camelize(pair.slice(0, i));
193 var value = pair.slice(i + 1).trim();
194 prop.startsWith('webkit') ? acc[capitalize(prop)] = value : acc[prop] = value;
195 return acc;
196 }, {});
197}
198
199function convert(createElement, element) {
200 var extraProps = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
201
202 if (typeof element === 'string') {
203 return element;
204 }
205
206 var children = (element.children || []).map(function (child) {
207 return convert(createElement, child);
208 });
209 /* eslint-disable dot-notation */
210
211 var mixins = Object.keys(element.attributes || {}).reduce(function (acc, key) {
212 var val = element.attributes[key];
213
214 switch (key) {
215 case 'class':
216 acc.attrs['className'] = val;
217 delete element.attributes['class'];
218 break;
219
220 case 'style':
221 acc.attrs['style'] = styleToObject(val);
222 break;
223
224 default:
225 if (key.indexOf('aria-') === 0 || key.indexOf('data-') === 0) {
226 acc.attrs[key.toLowerCase()] = val;
227 } else {
228 acc.attrs[camelize(key)] = val;
229 }
230
231 }
232
233 return acc;
234 }, {
235 attrs: {}
236 });
237
238 var _extraProps$style = extraProps.style,
239 existingStyle = _extraProps$style === void 0 ? {} : _extraProps$style,
240 remaining = _objectWithoutProperties(extraProps, ["style"]);
241
242 mixins.attrs['style'] = _objectSpread2({}, mixins.attrs['style'], {}, existingStyle);
243 /* eslint-enable */
244
245 return createElement.apply(void 0, [element.tag, _objectSpread2({}, mixins.attrs, {}, remaining)].concat(_toConsumableArray(children)));
246}
247
248var PRODUCTION = false;
249
250try {
251 PRODUCTION = process.env.NODE_ENV === 'production';
252} catch (e) {}
253
254function log () {
255 if (!PRODUCTION && console && typeof console.error === 'function') {
256 var _console;
257
258 (_console = console).error.apply(_console, arguments);
259 }
260}
261
262// Normalize icon arguments
263function normalizeIconArgs(icon) {
264 // if the icon is null, there's nothing to do
265 if (icon === null) {
266 return null;
267 } // if the icon is an object and has a prefix and an icon name, return it
268
269
270 if (_typeof(icon) === 'object' && icon.prefix && icon.iconName) {
271 return icon;
272 } // if it's an array with length of two
273
274
275 if (Array.isArray(icon) && icon.length === 2) {
276 // use the first item as prefix, second as icon name
277 return {
278 prefix: icon[0],
279 iconName: icon[1]
280 };
281 } // if it's a string, use it as the icon name
282
283
284 if (typeof icon === 'string') {
285 return {
286 prefix: 'fas',
287 iconName: icon
288 };
289 }
290}
291
292// creates an object with a key of key
293// and a value of value
294// if certain conditions are met
295function objectWithKey(key, value) {
296 // if the value is a non-empty array
297 // or it's not an array but it is truthy
298 // then create the object with the key and the value
299 // if not, return an empty array
300 return Array.isArray(value) && value.length > 0 || !Array.isArray(value) && value ? _defineProperty({}, key, value) : {};
301}
302
303function FontAwesomeIcon(props) {
304 var iconArgs = props.icon,
305 maskArgs = props.mask,
306 symbol = props.symbol,
307 className = props.className,
308 title = props.title;
309 var iconLookup = normalizeIconArgs(iconArgs);
310 var classes = objectWithKey('classes', [].concat(_toConsumableArray(classList(props)), _toConsumableArray(className.split(' '))));
311 var transform = objectWithKey('transform', typeof props.transform === 'string' ? parse.transform(props.transform) : props.transform);
312 var mask = objectWithKey('mask', normalizeIconArgs(maskArgs));
313 var renderedIcon = icon(iconLookup, _objectSpread2({}, classes, {}, transform, {}, mask, {
314 symbol: symbol,
315 title: title
316 }));
317
318 if (!renderedIcon) {
319 log('Could not find icon', iconLookup);
320 return null;
321 }
322
323 var abstract = renderedIcon.abstract;
324 var extraProps = {};
325 Object.keys(props).forEach(function (key) {
326 // eslint-disable-next-line no-prototype-builtins
327 if (!FontAwesomeIcon.defaultProps.hasOwnProperty(key)) {
328 extraProps[key] = props[key];
329 }
330 });
331 return convertCurry(abstract[0], extraProps);
332}
333FontAwesomeIcon.displayName = 'FontAwesomeIcon';
334FontAwesomeIcon.propTypes = {
335 border: PropTypes.bool,
336 className: PropTypes.string,
337 mask: PropTypes.oneOfType([PropTypes.object, PropTypes.array, PropTypes.string]),
338 fixedWidth: PropTypes.bool,
339 inverse: PropTypes.bool,
340 flip: PropTypes.oneOf(['horizontal', 'vertical', 'both']),
341 icon: PropTypes.oneOfType([PropTypes.object, PropTypes.array, PropTypes.string]),
342 listItem: PropTypes.bool,
343 pull: PropTypes.oneOf(['right', 'left']),
344 pulse: PropTypes.bool,
345 rotation: PropTypes.oneOf([90, 180, 270]),
346 size: PropTypes.oneOf(['lg', 'xs', 'sm', '1x', '2x', '3x', '4x', '5x', '6x', '7x', '8x', '9x', '10x']),
347 spin: PropTypes.bool,
348 symbol: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
349 title: PropTypes.string,
350 transform: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
351 swapOpacity: PropTypes.bool
352};
353FontAwesomeIcon.defaultProps = {
354 border: false,
355 className: '',
356 mask: null,
357 fixedWidth: false,
358 inverse: false,
359 flip: null,
360 icon: null,
361 listItem: false,
362 pull: null,
363 pulse: false,
364 rotation: null,
365 size: null,
366 spin: false,
367 symbol: false,
368 title: '',
369 transform: null,
370 swapOpacity: false
371};
372var convertCurry = convert.bind(null, React.createElement);
373
374export { FontAwesomeIcon };