mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
2b56011f9c
Summary: This PR adds support for the dark mode and dynamic theme changing to the default App template and to the related `NewAppScreen` components. Using `useColorScheme` hook forced me to refactor a bit main `App.js` file, but I think those changes are step in the right direction according to way in which React Native is used in larger apps, so new `Section` component has been extracted to reduce code redundancy/repetition inside `App`. Additional color `darker` has been added to the `Colors` statics from `NewAppScreen` because `dark` was too bright for the Dark Mode backgrounds. Also main `StoryBoard` on iOS has been updated to use theme based colors instead of static or hardcoded ones. There was also an unused, empty `Label` which I have removed. ~~I'm not so much experienced with Android. If someone could also update Android splash screen (if Android requires such change) it will be nice. I want to look at this later using simulator.~~ > I have updated the Android splash screen and tested this change on the Android emulator. If you have any comment or corrections feel free to post them out, I would like to put more work into this PR if it's needed. Dark Mode this days is a part of near every OS, so it could be considered as a standard feature. I hope those changes helps people which struggle with the basic theming implementation (+ there is now an example of hook and `children` prop usage in the template). ## Changelog [Internal] [Added] - Add dark mode support to the default app template Pull Request resolved: https://github.com/facebook/react-native/pull/28711 Test Plan: I have tested the App from the template on the iOS device and in Android emulator with RN `0.63.0-rc`. Screen recording on iOS (demonstarates both modes, both splash screens and transition):  Screenshot of iOS app in Dark Mode:  Screenshot of iOS app splash screen in Dark Mode:  Screenshot of Android app in the emulator:  Differential Revision: D21236148 Pulled By: shergin fbshipit-source-id: 0c8a9534d3a3f8f8099af939243a889ac4df6cda
139 lines
2.9 KiB
JavaScript
139 lines
2.9 KiB
JavaScript
/**
|
|
* Sample React Native App
|
|
* https://github.com/facebook/react-native
|
|
*
|
|
* @format
|
|
* @flow strict-local
|
|
*/
|
|
|
|
import React from 'react';
|
|
import type {Node} from 'react';
|
|
import {
|
|
SafeAreaView,
|
|
ScrollView,
|
|
StatusBar,
|
|
StyleSheet,
|
|
Text,
|
|
useColorScheme,
|
|
View,
|
|
} from 'react-native';
|
|
|
|
import {
|
|
Colors,
|
|
DebugInstructions,
|
|
Header,
|
|
LearnMoreLinks,
|
|
ReloadInstructions,
|
|
} from 'react-native/Libraries/NewAppScreen';
|
|
|
|
const Section = ({children, title}): Node => {
|
|
const isDarkMode = useColorScheme() === 'dark';
|
|
return (
|
|
<View style={styles.sectionContainer}>
|
|
<Text
|
|
style={[
|
|
styles.sectionTitle,
|
|
{
|
|
color: isDarkMode ? Colors.white : Colors.black,
|
|
},
|
|
]}>
|
|
{title}
|
|
</Text>
|
|
<Text
|
|
style={[
|
|
styles.sectionDescription,
|
|
{
|
|
color: isDarkMode ? Colors.light : Colors.dark,
|
|
},
|
|
]}>
|
|
{children}
|
|
</Text>
|
|
</View>
|
|
);
|
|
};
|
|
|
|
const App: () => Node = () => {
|
|
const isDarkMode = useColorScheme() === 'dark';
|
|
|
|
const backgroundStyle = {
|
|
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
|
|
};
|
|
|
|
const hermes = global.HermesInternal ? (
|
|
<View style={styles.engine}>
|
|
<Text
|
|
style={[
|
|
styles.footer,
|
|
{
|
|
color: isDarkMode ? Colors.light : Colors.dark,
|
|
},
|
|
]}>
|
|
Engine: Hermes
|
|
</Text>
|
|
</View>
|
|
) : null;
|
|
|
|
return (
|
|
<SafeAreaView style={backgroundStyle}>
|
|
<StatusBar barStyle={isDarkMode ? 'light-content' : 'dark-content'} />
|
|
<ScrollView
|
|
contentInsetAdjustmentBehavior="automatic"
|
|
style={backgroundStyle}>
|
|
<Header />
|
|
{hermes}
|
|
<View
|
|
style={{
|
|
backgroundColor: isDarkMode ? Colors.black : Colors.white,
|
|
}}>
|
|
<Section title="Step One">
|
|
Edit <Text style={styles.highlight}>App.js</Text> to change this
|
|
screen and then come back to see your edits.
|
|
</Section>
|
|
<Section title="See Your Changes">
|
|
<ReloadInstructions />
|
|
</Section>
|
|
<Section title="Debug">
|
|
<DebugInstructions />
|
|
</Section>
|
|
<Section title="Learn More">
|
|
Read the docs to discover what to do next:
|
|
</Section>
|
|
<LearnMoreLinks />
|
|
</View>
|
|
</ScrollView>
|
|
</SafeAreaView>
|
|
);
|
|
};
|
|
|
|
const styles = StyleSheet.create({
|
|
engine: {
|
|
position: 'absolute',
|
|
right: 0,
|
|
},
|
|
sectionContainer: {
|
|
marginTop: 32,
|
|
paddingHorizontal: 24,
|
|
},
|
|
sectionTitle: {
|
|
fontSize: 24,
|
|
fontWeight: '600',
|
|
},
|
|
sectionDescription: {
|
|
marginTop: 8,
|
|
fontSize: 18,
|
|
fontWeight: '400',
|
|
},
|
|
highlight: {
|
|
fontWeight: '700',
|
|
},
|
|
footer: {
|
|
fontSize: 12,
|
|
fontWeight: '600',
|
|
padding: 4,
|
|
paddingRight: 12,
|
|
textAlign: 'right',
|
|
},
|
|
});
|
|
|
|
export default App;
|