UNPKG

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