UNPKG

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