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': flip === true,
171 'fa-flip-horizontal': flip === 'horizontal' || flip === 'both',
172 'fa-flip-vertical': flip === 'vertical' || flip === 'both'
173 }, _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
174 // return an array of the keys where the value for the key is not null
175
176 return Object.keys(classes).map(function (key) {
177 return classes[key] ? key : null;
178 }).filter(function (key) {
179 return key;
180 });
181 }
182
183 // Camelize taken from humps
184 // humps is copyright © 2012+ Dom Christie
185 // Released under the MIT license.
186 // Performant way to determine if object coerces to a number
187 function _isNumerical(obj) {
188 obj = obj - 0; // eslint-disable-next-line no-self-compare
189
190 return obj === obj;
191 }
192
193 function camelize(string) {
194 if (_isNumerical(string)) {
195 return string;
196 } // eslint-disable-next-line no-useless-escape
197
198
199 string = string.replace(/[\-_\s]+(.)?/g, function (match, chr) {
200 return chr ? chr.toUpperCase() : '';
201 }); // Ensure 1st char is always lowercase
202
203 return string.substr(0, 1).toLowerCase() + string.substr(1);
204 }
205
206 var _excluded = ["style"];
207
208 function capitalize(val) {
209 return val.charAt(0).toUpperCase() + val.slice(1);
210 }
211
212 function styleToObject(style) {
213 return style.split(';').map(function (s) {
214 return s.trim();
215 }).filter(function (s) {
216 return s;
217 }).reduce(function (acc, pair) {
218 var i = pair.indexOf(':');
219 var prop = camelize(pair.slice(0, i));
220 var value = pair.slice(i + 1).trim();
221 prop.startsWith('webkit') ? acc[capitalize(prop)] = value : acc[prop] = value;
222 return acc;
223 }, {});
224 }
225
226 function convert(createElement, element) {
227 var extraProps = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
228
229 if (typeof element === 'string') {
230 return element;
231 }
232
233 var children = (element.children || []).map(function (child) {
234 return convert(createElement, child);
235 });
236 /* eslint-disable dot-notation */
237
238 var mixins = Object.keys(element.attributes || {}).reduce(function (acc, key) {
239 var val = element.attributes[key];
240
241 switch (key) {
242 case 'class':
243 acc.attrs['className'] = val;
244 delete element.attributes['class'];
245 break;
246
247 case 'style':
248 acc.attrs['style'] = styleToObject(val);
249 break;
250
251 default:
252 if (key.indexOf('aria-') === 0 || key.indexOf('data-') === 0) {
253 acc.attrs[key.toLowerCase()] = val;
254 } else {
255 acc.attrs[camelize(key)] = val;
256 }
257
258 }
259
260 return acc;
261 }, {
262 attrs: {}
263 });
264
265 var _extraProps$style = extraProps.style,
266 existingStyle = _extraProps$style === void 0 ? {} : _extraProps$style,
267 remaining = _objectWithoutProperties(extraProps, _excluded);
268
269 mixins.attrs['style'] = _objectSpread2(_objectSpread2({}, mixins.attrs['style']), existingStyle);
270 /* eslint-enable */
271
272 return createElement.apply(void 0, [element.tag, _objectSpread2(_objectSpread2({}, mixins.attrs), remaining)].concat(_toConsumableArray(children)));
273 }
274
275 var PRODUCTION = false;
276
277 try {
278 PRODUCTION = process.env.NODE_ENV === 'production';
279 } catch (e) {}
280
281 function log () {
282 if (!PRODUCTION && console && typeof console.error === 'function') {
283 var _console;
284
285 (_console = console).error.apply(_console, arguments);
286 }
287 }
288
289 function normalizeIconArgs(icon) {
290 // this has everything that it needs to be rendered which means it was probably imported
291 // directly from an icon svg package
292 if (icon && _typeof(icon) === 'object' && icon.prefix && icon.iconName && icon.icon) {
293 return icon;
294 }
295
296 if (fontawesomeSvgCore.parse.icon) {
297 return fontawesomeSvgCore.parse.icon(icon);
298 } // if the icon is null, there's nothing to do
299
300
301 if (icon === null) {
302 return null;
303 } // if the icon is an object and has a prefix and an icon name, return it
304
305
306 if (icon && _typeof(icon) === 'object' && icon.prefix && icon.iconName) {
307 return icon;
308 } // if it's an array with length of two
309
310
311 if (Array.isArray(icon) && icon.length === 2) {
312 // use the first item as prefix, second as icon name
313 return {
314 prefix: icon[0],
315 iconName: icon[1]
316 };
317 } // if it's a string, use it as the icon name
318
319
320 if (typeof icon === 'string') {
321 return {
322 prefix: 'fas',
323 iconName: icon
324 };
325 }
326 }
327
328 // creates an object with a key of key
329 // and a value of value
330 // if certain conditions are met
331 function objectWithKey(key, value) {
332 // if the value is a non-empty array
333 // or it's not an array but it is truthy
334 // then create the object with the key and the value
335 // if not, return an empty array
336 return Array.isArray(value) && value.length > 0 || !Array.isArray(value) && value ? _defineProperty({}, key, value) : {};
337 }
338
339 var FontAwesomeIcon = /*#__PURE__*/React__default["default"].forwardRef(function (props, ref) {
340 var iconArgs = props.icon,
341 maskArgs = props.mask,
342 symbol = props.symbol,
343 className = props.className,
344 title = props.title,
345 titleId = props.titleId,
346 maskId = props.maskId;
347 var iconLookup = normalizeIconArgs(iconArgs);
348 var classes = objectWithKey('classes', [].concat(_toConsumableArray(classList(props)), _toConsumableArray(className.split(' '))));
349 var transform = objectWithKey('transform', typeof props.transform === 'string' ? fontawesomeSvgCore.parse.transform(props.transform) : props.transform);
350 var mask = objectWithKey('mask', normalizeIconArgs(maskArgs));
351 var renderedIcon = fontawesomeSvgCore.icon(iconLookup, _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({}, classes), transform), mask), {}, {
352 symbol: symbol,
353 title: title,
354 titleId: titleId,
355 maskId: maskId
356 }));
357
358 if (!renderedIcon) {
359 log('Could not find icon', iconLookup);
360 return null;
361 }
362
363 var abstract = renderedIcon.abstract;
364 var extraProps = {
365 ref: ref
366 };
367 Object.keys(props).forEach(function (key) {
368 // eslint-disable-next-line no-prototype-builtins
369 if (!FontAwesomeIcon.defaultProps.hasOwnProperty(key)) {
370 extraProps[key] = props[key];
371 }
372 });
373 return convertCurry(abstract[0], extraProps);
374 });
375 FontAwesomeIcon.displayName = 'FontAwesomeIcon';
376 FontAwesomeIcon.propTypes = {
377 beat: PropTypes__default["default"].bool,
378 border: PropTypes__default["default"].bool,
379 beatFade: PropTypes__default["default"].bool,
380 bounce: PropTypes__default["default"].bool,
381 className: PropTypes__default["default"].string,
382 fade: PropTypes__default["default"].bool,
383 flash: PropTypes__default["default"].bool,
384 mask: PropTypes__default["default"].oneOfType([PropTypes__default["default"].object, PropTypes__default["default"].array, PropTypes__default["default"].string]),
385 maskId: PropTypes__default["default"].string,
386 fixedWidth: PropTypes__default["default"].bool,
387 inverse: PropTypes__default["default"].bool,
388 flip: PropTypes__default["default"].oneOf([true, false, 'horizontal', 'vertical', 'both']),
389 icon: PropTypes__default["default"].oneOfType([PropTypes__default["default"].object, PropTypes__default["default"].array, PropTypes__default["default"].string]),
390 listItem: PropTypes__default["default"].bool,
391 pull: PropTypes__default["default"].oneOf(['right', 'left']),
392 pulse: PropTypes__default["default"].bool,
393 rotation: PropTypes__default["default"].oneOf([0, 90, 180, 270]),
394 shake: PropTypes__default["default"].bool,
395 size: PropTypes__default["default"].oneOf(['2xs', 'xs', 'sm', 'lg', 'xl', '2xl', '1x', '2x', '3x', '4x', '5x', '6x', '7x', '8x', '9x', '10x']),
396 spin: PropTypes__default["default"].bool,
397 spinPulse: PropTypes__default["default"].bool,
398 spinReverse: PropTypes__default["default"].bool,
399 symbol: PropTypes__default["default"].oneOfType([PropTypes__default["default"].bool, PropTypes__default["default"].string]),
400 title: PropTypes__default["default"].string,
401 titleId: PropTypes__default["default"].string,
402 transform: PropTypes__default["default"].oneOfType([PropTypes__default["default"].string, PropTypes__default["default"].object]),
403 swapOpacity: PropTypes__default["default"].bool
404 };
405 FontAwesomeIcon.defaultProps = {
406 border: false,
407 className: '',
408 mask: null,
409 maskId: null,
410 fixedWidth: false,
411 inverse: false,
412 flip: false,
413 icon: null,
414 listItem: false,
415 pull: null,
416 pulse: false,
417 rotation: null,
418 size: null,
419 spin: false,
420 spinPulse: false,
421 spinReverse: 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}));