react-native-navigation-appearance


Dependecies
Make sure that your are using supported versions of react-native-navigation and react-native:
1.x |
---|
react-native-navigation >= 6.4.0; react-native: >=0.62 |
Installation
Android
1 Install dependencies
Make sure that you have installed @react-native-community/async-storage
dependency or install it using:
$ yarn add @react-native-community/async-storage
Install this module using:
$ yarn add @busfor/react-native-navigation-appearance
MainActivity.java
2 Update This file is located in android/app/src/main/java/com/<yourproject>/MainActivity.java
.
+import android.os.Bundle;+import androidx.annotation.Nullable;+import com.busfor.rnnappearance.RNNAppearanceModuleKt; public class MainActivity extends NavigationActivity {+ @Override+ protected void onCreate(@Nullable Bundle savedInstanceState) {+ super.onCreate(savedInstanceState);+ RNNAppearanceModuleKt.setThemeResId(R.style.AppTheme);+ }}
3 Configure android styles.xml
This file is located in android/app/src/main/res/values/styles.xml
.
<resources>+ + <style name="Theme.AppCompat.DayNight" parent="Theme.AppCompat.Light" /> <!-- Base application theme. -->- <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">+ <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar"> <!-- Customize your theme here. --> <item name="android:textColor">#000000</item> </style> </resources>
4 Create android night styles.xml
This file should be located in android/app/src/main/res/values-night/styles.xml
.
<!-- Base application theme. --> <!-- Customize your theme here. --> #FFFFFF
IOS
Make sure you don't have UIUserInterfaceStyle
in Info.plist
Usage
- Create a theme with dark and light appearances using
createTheme
function:
const theme =
- Create default options using
createDefaultOptions
function:
const defaultOptions =
- Add
ThemeProvider
to register and init module withdefaultOptions
that we have created:
NOTE: Make sure that you are running initAppearanceModule
inside Navigation.events().registerAppLaunchedListener
callback and before Navigation.setRoot
function!
Navigation Navigation
- Create screen options using
createOptions
anddefaultOptions
functions:
This file should be located in /AppScreen/options.js
- Create styles using
createStyles
function:
This file should be located in /AppScreen/styles.js
container: flex: 1 alignItems: 'center' justifyContent: 'center' backgroundColor: themeappearancebackgroundColor text: color: themeappearancetextColor paddingVertical: 8 textAlign: 'center'
- Use
useThemedOptions
,initialOptions
to define screen options and useuseStyles
hook to provide styles:
This file should be located in /AppScreen/index.js
const AppScreen = { const styles = return <SafeAreaView style=stylescontainer> <Text>Hello world!</Text> </SafeAreaView> } AppScreenoptions =
Also you can open the example project to see how it works in the real case.
Manual appearance
You can set appearance manually using useThemeControls
hook:
const App = { const currentApearanceMode setAppearanceMode = return <> <Text>Current mode: currentApearanceMode</Text> <Button title='Dark' onPress= /> <Button title='Light' onPress= /> <Button title='System' onPress= /> </> }
Other hooks
useAppearance
You can get current appearance using useAppearance
hook:
const App = { const appearance = return <Text>Current appearance: appearance</Text>}
useThemedValue
You can get any value for current appearance using useThemedValue
hook:
const lightLogoSource = const darkLogoSource = const App = { const logoSource = return <Image source=logoSource />}
Utils
appearanceSelect
With this function, you can select specific unique styles and options per appearance (like Platform.select
)
Jest integration
- Update your Jest setup file:
jestjest
- Use
setDarkModeMock
in your test cases: