UNPKG

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