UNPKG

2.77 kBJavaScriptView Raw
1function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
3import * as React from 'react';
4import { Image, I18nManager, Platform } from 'react-native';
5import { Consumer as SettingsConsumer } from '../core/settings';
6import { accessibilityProps } from './MaterialCommunityIcon';
7import { withTheme } from '../core/theming';
8
9const isImageSource = source => // source is an object with uri
10typeof source === 'object' && source !== null && Object.prototype.hasOwnProperty.call(source, 'uri') && typeof source.uri === 'string' || // source is a module, e.g. - require('image')
11typeof source === 'number' || // image url on web
12Platform.OS === 'web' && typeof source === 'string' && (source.startsWith('data:image') || /\.(bmp|jpg|jpeg|png|gif|svg)$/.test(source));
13
14const getIconId = source => {
15 if (typeof source === 'object' && source !== null && Object.prototype.hasOwnProperty.call(source, 'uri') && typeof source.uri === 'string') {
16 return source.uri;
17 }
18
19 return source;
20};
21
22export const isValidIcon = source => typeof source === 'string' || typeof source === 'function' || isImageSource(source);
23export const isEqualIcon = (a, b) => a === b || getIconId(a) === getIconId(b);
24
25const Icon = _ref => {
26 let {
27 source,
28 color,
29 size,
30 theme,
31 ...rest
32 } = _ref;
33 const direction = typeof source === 'object' && source.direction && source.source ? source.direction === 'auto' ? I18nManager.isRTL ? 'rtl' : 'ltr' : source.direction : null;
34 const s = typeof source === 'object' && source.direction && source.source ? source.source : source;
35 const iconColor = color || theme.colors.text;
36
37 if (isImageSource(s)) {
38 return /*#__PURE__*/React.createElement(Image, _extends({}, rest, {
39 source: s,
40 style: [{
41 transform: [{
42 scaleX: direction === 'rtl' ? -1 : 1
43 }]
44 }, // eslint-disable-next-line react-native/no-inline-styles
45 {
46 width: size,
47 height: size,
48 tintColor: color,
49 resizeMode: 'contain'
50 }]
51 }, accessibilityProps));
52 } else if (typeof s === 'string') {
53 return /*#__PURE__*/React.createElement(SettingsConsumer, null, _ref2 => {
54 let {
55 icon
56 } = _ref2;
57 return icon({
58 name: s,
59 color: iconColor,
60 size,
61 direction
62 });
63 });
64 } else if (typeof s === 'function') {
65 return s({
66 color: iconColor,
67 size,
68 direction
69 });
70 }
71
72 return null;
73};
74
75export default withTheme(Icon);
76//# sourceMappingURL=Icon.js.map
\No newline at end of file