UNPKG

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