UNPKG

12.3 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 _objectSpread(target) {
35 for (var i = 1; i < arguments.length; i++) {
36 var source = arguments[i] != null ? arguments[i] : {};
37 var ownKeys = Object.keys(source);
38
39 if (typeof Object.getOwnPropertySymbols === 'function') {
40 ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
41 return Object.getOwnPropertyDescriptor(source, sym).enumerable;
42 }));
43 }
44
45 ownKeys.forEach(function (key) {
46 _defineProperty(target, key, source[key]);
47 });
48 }
49
50 return target;
51}
52
53function _objectWithoutPropertiesLoose(source, excluded) {
54 if (source == null) return {};
55 var target = {};
56 var sourceKeys = Object.keys(source);
57 var key, i;
58
59 for (i = 0; i < sourceKeys.length; i++) {
60 key = sourceKeys[i];
61 if (excluded.indexOf(key) >= 0) continue;
62 target[key] = source[key];
63 }
64
65 return target;
66}
67
68function _objectWithoutProperties(source, excluded) {
69 if (source == null) return {};
70
71 var target = _objectWithoutPropertiesLoose(source, excluded);
72
73 var key, i;
74
75 if (Object.getOwnPropertySymbols) {
76 var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
77
78 for (i = 0; i < sourceSymbolKeys.length; i++) {
79 key = sourceSymbolKeys[i];
80 if (excluded.indexOf(key) >= 0) continue;
81 if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
82 target[key] = source[key];
83 }
84 }
85
86 return target;
87}
88
89function _toConsumableArray(arr) {
90 return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
91}
92
93function _arrayWithoutHoles(arr) {
94 if (Array.isArray(arr)) {
95 for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
96
97 return arr2;
98 }
99}
100
101function _iterableToArray(iter) {
102 if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
103}
104
105function _nonIterableSpread() {
106 throw new TypeError("Invalid attempt to spread non-iterable instance");
107}
108
109var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
110
111function createCommonjsModule(fn, module) {
112 return module = { exports: {} }, fn(module, module.exports), module.exports;
113}
114
115var humps = createCommonjsModule(function (module) {
116(function(global) {
117
118 var _processKeys = function(convert, obj, options) {
119 if(!_isObject(obj) || _isDate(obj) || _isRegExp(obj) || _isBoolean(obj) || _isFunction(obj)) {
120 return obj;
121 }
122
123 var output,
124 i = 0,
125 l = 0;
126
127 if(_isArray(obj)) {
128 output = [];
129 for(l=obj.length; i<l; i++) {
130 output.push(_processKeys(convert, obj[i], options));
131 }
132 }
133 else {
134 output = {};
135 for(var key in obj) {
136 if(Object.prototype.hasOwnProperty.call(obj, key)) {
137 output[convert(key, options)] = _processKeys(convert, obj[key], options);
138 }
139 }
140 }
141 return output;
142 };
143
144 // String conversion methods
145
146 var separateWords = function(string, options) {
147 options = options || {};
148 var separator = options.separator || '_';
149 var split = options.split || /(?=[A-Z])/;
150
151 return string.split(split).join(separator);
152 };
153
154 var camelize = function(string) {
155 if (_isNumerical(string)) {
156 return string;
157 }
158 string = string.replace(/[\-_\s]+(.)?/g, function(match, chr) {
159 return chr ? chr.toUpperCase() : '';
160 });
161 // Ensure 1st char is always lowercase
162 return string.substr(0, 1).toLowerCase() + string.substr(1);
163 };
164
165 var pascalize = function(string) {
166 var camelized = camelize(string);
167 // Ensure 1st char is always uppercase
168 return camelized.substr(0, 1).toUpperCase() + camelized.substr(1);
169 };
170
171 var decamelize = function(string, options) {
172 return separateWords(string, options).toLowerCase();
173 };
174
175 // Utilities
176 // Taken from Underscore.js
177
178 var toString = Object.prototype.toString;
179
180 var _isFunction = function(obj) {
181 return typeof(obj) === 'function';
182 };
183 var _isObject = function(obj) {
184 return obj === Object(obj);
185 };
186 var _isArray = function(obj) {
187 return toString.call(obj) == '[object Array]';
188 };
189 var _isDate = function(obj) {
190 return toString.call(obj) == '[object Date]';
191 };
192 var _isRegExp = function(obj) {
193 return toString.call(obj) == '[object RegExp]';
194 };
195 var _isBoolean = function(obj) {
196 return toString.call(obj) == '[object Boolean]';
197 };
198
199 // Performant way to determine if obj coerces to a number
200 var _isNumerical = function(obj) {
201 obj = obj - 0;
202 return obj === obj;
203 };
204
205 // Sets up function which handles processing keys
206 // allowing the convert function to be modified by a callback
207 var _processor = function(convert, options) {
208 var callback = options && 'process' in options ? options.process : options;
209
210 if(typeof(callback) !== 'function') {
211 return convert;
212 }
213
214 return function(string, options) {
215 return callback(string, convert, options);
216 }
217 };
218
219 var humps = {
220 camelize: camelize,
221 decamelize: decamelize,
222 pascalize: pascalize,
223 depascalize: decamelize,
224 camelizeKeys: function(object, options) {
225 return _processKeys(_processor(camelize, options), object);
226 },
227 decamelizeKeys: function(object, options) {
228 return _processKeys(_processor(decamelize, options), object, options);
229 },
230 pascalizeKeys: function(object, options) {
231 return _processKeys(_processor(pascalize, options), object);
232 },
233 depascalizeKeys: function () {
234 return this.decamelizeKeys.apply(this, arguments);
235 }
236 };
237
238 if (module.exports) {
239 module.exports = humps;
240 } else {
241 global.humps = humps;
242 }
243
244})(commonjsGlobal);
245});
246
247function capitalize(val) {
248 return val.charAt(0).toUpperCase() + val.slice(1);
249}
250
251function styleToObject(style) {
252 return style.split(';').map(function (s) {
253 return s.trim();
254 }).filter(function (s) {
255 return s;
256 }).reduce(function (acc, pair) {
257 var i = pair.indexOf(':');
258 var prop = humps.camelize(pair.slice(0, i));
259 var value = pair.slice(i + 1).trim();
260 prop.startsWith('webkit') ? acc[capitalize(prop)] = value : acc[prop] = value;
261 return acc;
262 }, {});
263}
264
265function convert(createElement, element) {
266 var extraProps = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
267
268 if (typeof element === 'string') {
269 return element;
270 }
271
272 var children = (element.children || []).map(function (child) {
273 return convert(createElement, child);
274 });
275 var mixins = Object.keys(element.attributes || {}).reduce(function (acc, key) {
276 var val = element.attributes[key];
277
278 switch (key) {
279 case 'class':
280 acc.attrs['className'] = val;
281 delete element.attributes['class'];
282 break;
283
284 case 'style':
285 acc.attrs['style'] = styleToObject(val);
286 break;
287
288 default:
289 if (key.indexOf('aria-') === 0 || key.indexOf('data-') === 0) {
290 acc.attrs[key.toLowerCase()] = val;
291 } else {
292 acc.attrs[humps.camelize(key)] = val;
293 }
294
295 }
296
297 return acc;
298 }, {
299 attrs: {}
300 });
301
302 var _extraProps$style = extraProps.style,
303 existingStyle = _extraProps$style === void 0 ? {} : _extraProps$style,
304 remaining = _objectWithoutProperties(extraProps, ["style"]);
305
306 mixins.attrs['style'] = _objectSpread({}, mixins.attrs['style'], existingStyle);
307 return createElement.apply(void 0, [element.tag, _objectSpread({}, mixins.attrs, remaining)].concat(_toConsumableArray(children)));
308}
309
310var PRODUCTION = false;
311
312try {
313 PRODUCTION = process.env.NODE_ENV === 'production';
314} catch (e) {}
315
316function log () {
317 if (!PRODUCTION && console && typeof console.error === 'function') {
318 var _console;
319
320 (_console = console).error.apply(_console, arguments);
321 }
322}
323
324function objectWithKey(key, value) {
325 return Array.isArray(value) && value.length > 0 || !Array.isArray(value) && value ? _defineProperty({}, key, value) : {};
326}
327
328function classList(props) {
329 var _classes;
330
331 var classes = (_classes = {
332 'fa-spin': props.spin,
333 'fa-pulse': props.pulse,
334 'fa-fw': props.fixedWidth,
335 'fa-inverse': props.inverse,
336 'fa-border': props.border,
337 'fa-li': props.listItem,
338 'fa-flip-horizontal': props.flip === 'horizontal' || props.flip === 'both',
339 'fa-flip-vertical': props.flip === 'vertical' || props.flip === 'both'
340 }, _defineProperty(_classes, "fa-".concat(props.size), props.size !== null), _defineProperty(_classes, "fa-rotate-".concat(props.rotation), props.rotation !== null), _defineProperty(_classes, "fa-pull-".concat(props.pull), props.pull !== null), _classes);
341 return Object.keys(classes).map(function (key) {
342 return classes[key] ? key : null;
343 }).filter(function (key) {
344 return key;
345 });
346}
347
348function normalizeIconArgs(icon$$1) {
349 if (icon$$1 === null) {
350 return null;
351 }
352
353 if (_typeof(icon$$1) === 'object' && icon$$1.prefix && icon$$1.iconName) {
354 return icon$$1;
355 }
356
357 if (Array.isArray(icon$$1) && icon$$1.length === 2) {
358 return {
359 prefix: icon$$1[0],
360 iconName: icon$$1[1]
361 };
362 }
363
364 if (typeof icon$$1 === 'string') {
365 return {
366 prefix: 'fas',
367 iconName: icon$$1
368 };
369 }
370}
371
372function FontAwesomeIcon(props) {
373 var iconArgs = props.icon,
374 maskArgs = props.mask,
375 symbol = props.symbol,
376 className = props.className,
377 title = props.title;
378 var iconLookup = normalizeIconArgs(iconArgs);
379 var classes = objectWithKey('classes', [].concat(_toConsumableArray(classList(props)), _toConsumableArray(className.split(' '))));
380 var transform = objectWithKey('transform', typeof props.transform === 'string' ? parse.transform(props.transform) : props.transform);
381 var mask = objectWithKey('mask', normalizeIconArgs(maskArgs));
382 var renderedIcon = icon(iconLookup, _objectSpread({}, classes, transform, mask, {
383 symbol: symbol,
384 title: title
385 }));
386
387 if (!renderedIcon) {
388 log('Could not find icon', iconLookup);
389 return null;
390 }
391
392 var abstract = renderedIcon.abstract;
393 var extraProps = {};
394 Object.keys(props).forEach(function (key) {
395 if (!FontAwesomeIcon.defaultProps.hasOwnProperty(key)) {
396 extraProps[key] = props[key];
397 }
398 });
399 return convertCurry(abstract[0], extraProps);
400}
401FontAwesomeIcon.displayName = 'FontAwesomeIcon';
402FontAwesomeIcon.propTypes = {
403 border: PropTypes.bool,
404 className: PropTypes.string,
405 mask: PropTypes.oneOfType([PropTypes.object, PropTypes.array, PropTypes.string]),
406 fixedWidth: PropTypes.bool,
407 inverse: PropTypes.bool,
408 flip: PropTypes.oneOf(['horizontal', 'vertical', 'both']),
409 icon: PropTypes.oneOfType([PropTypes.object, PropTypes.array, PropTypes.string]),
410 listItem: PropTypes.bool,
411 pull: PropTypes.oneOf(['right', 'left']),
412 pulse: PropTypes.bool,
413 rotation: PropTypes.oneOf([90, 180, 270]),
414 size: PropTypes.oneOf(['lg', 'xs', 'sm', '1x', '2x', '3x', '4x', '5x', '6x', '7x', '8x', '9x', '10x']),
415 spin: PropTypes.bool,
416 symbol: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
417 title: PropTypes.string,
418 transform: PropTypes.oneOfType([PropTypes.string, PropTypes.object])
419};
420FontAwesomeIcon.defaultProps = {
421 border: false,
422 className: '',
423 mask: null,
424 fixedWidth: false,
425 inverse: false,
426 flip: null,
427 icon: null,
428 listItem: false,
429 pull: null,
430 pulse: false,
431 rotation: null,
432 size: null,
433 spin: false,
434 symbol: false,
435 title: '',
436 transform: null
437};
438var convertCurry = convert.bind(null, React.createElement);
439
440export { FontAwesomeIcon };