Skip to content

Commit 0541da0

Browse files
authored
feat: introduce expo config plugin (#94)
* feat: introduce expo config plugin * docs: update getting started
1 parent faff34b commit 0541da0

File tree

6 files changed

+354
-41
lines changed

6 files changed

+354
-41
lines changed

app.plugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib/commonjs/expo');

docs/docs/docs/getting-started/quick-start.mdx

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { PackageManagerTabs } from '@theme';
66

77
<PackageManagerTabs command="install react-native-bottom-tabs" />
88

9-
If you use React Native version 0.75 or lower:
9+
<details>
10+
<summary>If you use React Native <b>version 0.75 or lower</b></summary>
1011

1112
- For `@react-native-community/cli` users, open Podfile in ios folder and change minimum iOS version to `14.0` before `pod install`
1213

@@ -34,6 +35,44 @@ If you use React Native version 0.75 or lower:
3435
}
3536
```
3637

38+
</details>
39+
40+
41+
42+
### Expo
43+
44+
Add the library plugin in your `app.json` config file and [create a new build](https://docs.expo.dev/develop/development-builds/create-a-build/):
45+
46+
```diff
47+
"expo": {
48+
+ "plugins": ["react-native-bottom-tabs"]
49+
}
50+
}
51+
```
52+
53+
:::warning
54+
55+
This library is not supported in [Expo Go](https://expo.dev/go).
56+
57+
:::
58+
59+
### React Native Community CLI users
60+
61+
Edit `android/app/src/main/res/values/styles.xml` to inherit from provided theme in order to customize the appearance of the native bottom tabs.
62+
63+
64+
```diff
65+
<resources>
66+
- <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
67+
+ <style name="AppTheme" parent="Theme.Material3.DayNight.NoActionBar">
68+
<!-- … -->
69+
</style>
70+
</resources>
71+
```
72+
73+
Here you can read more about [Android Native Styling](/docs/guides/android-native-styling).
74+
75+
3776
## Example usage
3877

3978
:::warning

docs/docs/docs/guides/android-native-styling.md

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,47 @@
22

33
## Expo users
44

5-
TODO: Add instructions for Expo users
5+
Use Expo Config Plugin for Material 3 styling:
66

7-
## React Native Community CLI users
7+
```diff
8+
"expo": {
9+
+ "plugins": ["react-native-bottom-tabs"]
10+
}
11+
}
12+
```
813

9-
Inside of your `android/app/src/main/res/values/styles.xml` file you can customize the appearance of the native bottom tabs.
14+
If you want to use Material2 styling, you can pass `theme` option to the plugin:
1015

11-
Here is how the file looks by default:
1216

13-
```xml
14-
<resources>
15-
<!-- Base application theme. -->
16-
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
17-
<!-- Customize your theme here. -->
18-
<item name="android:editTextBackground">@drawable/rn_edit_text_material</item>
19-
</style>
20-
</resources>
17+
```diff
18+
"expo": {
19+
+ "plugins": [["react-native-bottom-tabs", { "theme": "material2" }]]
20+
}
21+
}
2122
```
2223

23-
In order to use the native bottom tabs, you need to change `AppTheme` to extend from `Theme.MaterialComponents.DayNight.NoActionBar`:
24+
## React Native Community CLI users
25+
26+
Inside of your `android/app/src/main/res/values/styles.xml` file you can customize the appearance of the native bottom tabs.
27+
2428

25-
```xml
29+
```diff
2630
<resources>
27-
<!-- Base application theme. -->
28-
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
29-
<!-- Customize your theme here. -->
30-
<item name="android:editTextBackground">@drawable/rn_edit_text_material</item>
31-
</style>
31+
- <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
32+
+ <style name="AppTheme" parent="Theme.Material3.DayNight.NoActionBar">
33+
<!-- … -->
34+
</style>
3235
</resources>
3336
```
3437

35-
:::warning
36-
This is required for the native bottom tabs to work correctly.
37-
38-
You might see this error if you don't change the theme:
39-
40-
`The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).`
38+
If you want to use Material Design 2, you can extend from `Theme.MaterialComponents.DayNight.NoActionBar`:
4139

42-
:::
43-
44-
If you want to use Material Design 3, you can extend from `Theme.Material3.DayNight.NoActionBar`:
45-
46-
```xml
40+
```diff
4741
<resources>
48-
<!-- Base application theme. -->
49-
<style name="AppTheme" parent="Theme.Material3.DayNight.NoActionBar">
50-
<!-- Customize your theme here. -->
51-
<item name="android:editTextBackground">@drawable/rn_edit_text_material</item>
52-
</style>
42+
- <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
43+
+ <style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
44+
<!-- … -->
45+
</style>
5346
</resources>
5447
```
48+

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"default": "./lib/commonjs/react-navigation/index.js"
2828
}
2929
},
30-
"./package.json": "./package.json"
30+
"./package.json": "./package.json",
31+
"./app.plugin.js": "./app.plugin.js"
3132
},
3233
"files": [
3334
"src",
@@ -38,6 +39,7 @@
3839
"ios",
3940
"cpp",
4041
"react-native.config.js",
42+
"app.plugin.js",
4143
"*.podspec",
4244
"!ios/build",
4345
"!android/build",
@@ -81,6 +83,7 @@
8183
"devDependencies": {
8284
"@commitlint/config-conventional": "^17.0.2",
8385
"@evilmartians/lefthook": "^1.5.0",
86+
"@expo/config-plugins": "^7.0.0 || ^8.0.0",
8487
"@react-native/babel-preset": "0.75.3",
8588
"@react-native/eslint-config": "^0.73.1",
8689
"@react-navigation/native": "^6.1.18",

src/expo.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {
2+
ConfigPlugin,
3+
createRunOncePlugin,
4+
withAndroidStyles,
5+
} from '@expo/config-plugins';
6+
7+
const MATERIAL3_THEME = 'Theme.Material3.DayNight.NoActionBar';
8+
const MATERIAL2_THEME = 'Theme.MaterialComponents.DayNight.NoActionBar';
9+
10+
type ConfigProps = {
11+
/*
12+
* Define theme that should be used.
13+
* @default 'material3'
14+
*/
15+
theme: 'material2' | 'material3';
16+
};
17+
18+
const withMaterial3Theme: ConfigPlugin<ConfigProps> = (config, options) => {
19+
const theme = options?.theme;
20+
21+
return withAndroidStyles(config, (stylesConfig) => {
22+
stylesConfig.modResults.resources.style =
23+
stylesConfig.modResults.resources.style?.map((style) => {
24+
if (style.$.name === 'AppTheme') {
25+
if (theme === 'material2') {
26+
style.$.parent = MATERIAL2_THEME;
27+
} else {
28+
style.$.parent = MATERIAL3_THEME;
29+
}
30+
}
31+
32+
return style;
33+
});
34+
35+
return stylesConfig;
36+
});
37+
};
38+
39+
export default createRunOncePlugin(
40+
withMaterial3Theme,
41+
'react-native-bottom-tabs'
42+
);

0 commit comments

Comments
 (0)