Skip to content

Commit 39444df

Browse files
committed
feat: make vector-icons optional. fixes #303
1 parent db26d6c commit 39444df

File tree

4 files changed

+39
-16
lines changed

4 files changed

+39
-16
lines changed

docs/pages/2.getting-started.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ yarn add react-native-vector-icons
1717
react-native link react-native-vector-icons
1818
```
1919

20-
If you use CRNA or Expo, you don't need this step, but you will need to make sure that your `.babelrc` includes `babel-preset-expo`:
20+
If you don't want to install vector icons, you can use [babel-plugin-optional-require](https://github.com/satya164/babel-plugin-optional-require) to opt-out.
21+
22+
If you use CRNA or Expo, you don't need to install vector icons. But you will need to make sure that your `.babelrc` includes `babel-preset-expo`:
2123

2224
```json
2325
{

docs/pages/4.icons.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ title: Icons
66

77
Many of the components require the [react-native-vector-icons](https://github.com/oblador/react-native-vector-icons) library to render correctly. If you're using CRNA or Expo, you don't need to do anything extra, but if it's vanilla React Native project, you need link the library as described in the getting started guide.
88

9+
If you opted out of vector icons support using [babel-plugin-optional-require](https://github.com/satya164/babel-plugin-optional-require), you won't be able to use icon names for the icon prop anymore. Some components may not look correct without vector icons and might need extra configuration.
10+
911
## Using the `icon` prop
1012

1113
Many components such as `Button` accept an `icon` prop which is used to display an icon. The `icon` prop supports the following type of values:

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@
5454
},
5555
"peerDependencies": {
5656
"react": "*",
57-
"react-native": "*",
58-
"react-native-vector-icons": "*"
57+
"react-native": "*"
5958
},
6059
"jest": {
6160
"preset": "react-native",

src/components/Icon.js

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
/* @flow */
22

33
import * as React from 'react';
4-
import { Image, View, StyleSheet } from 'react-native';
5-
import MaterialIcons from 'react-native-vector-icons/MaterialIcons';
4+
import { Image, Text, View, StyleSheet } from 'react-native';
5+
6+
let MaterialIcons;
7+
8+
try {
9+
// Optionally require vector-icons
10+
MaterialIcons = require('react-native-vector-icons/MaterialIcons').default;
11+
} catch (e) {
12+
MaterialIcons = ({ name, color, size, style, ...rest }) => (
13+
<Text {...rest} style={[{ color, fontSize: size }, style]}>
14+
15+
</Text>
16+
);
17+
}
618

719
export type IconSource = string | { uri: string } | number | React.Node;
820

@@ -13,9 +25,17 @@ export type Props = {
1325
style?: any,
1426
};
1527

16-
const Icon = ({ name, ...props }: Props) => {
28+
const Icon = ({ name, color, size, style, ...rest }: Props) => {
1729
if (typeof name === 'string') {
18-
return <MaterialIcons {...props} name={name} />;
30+
return (
31+
<MaterialIcons
32+
{...rest}
33+
name={name}
34+
color={color}
35+
size={size}
36+
style={style}
37+
/>
38+
);
1939
} else if (
2040
(typeof name === 'object' &&
2141
name !== null &&
@@ -25,29 +45,29 @@ const Icon = ({ name, ...props }: Props) => {
2545
) {
2646
return (
2747
<Image
28-
{...props}
48+
{...rest}
2949
source={name}
3050
style={[
3151
{
32-
width: props.size,
33-
height: props.size,
34-
tintColor: props.color,
52+
width: size,
53+
height: size,
54+
tintColor: color,
3555
},
36-
props.style,
56+
style,
3757
]}
3858
/>
3959
);
4060
}
4161
return (
4262
<View
43-
{...props}
63+
{...rest}
4464
style={[
4565
{
46-
width: props.size,
47-
height: props.size,
66+
width: size,
67+
height: size,
4868
},
4969
styles.container,
50-
props.style,
70+
style,
5171
]}
5272
>
5373
{(name: any)}

0 commit comments

Comments
 (0)