Skip to content

Commit

Permalink
chore: make example app more informational (#1050)
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 authored Aug 18, 2022
1 parent de978fc commit bc1107d
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 75 deletions.
1 change: 1 addition & 0 deletions .github/renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
},
{
"matchPackageNames": [
"@types/react-native",
"react-native",
"react-native-macos",
"react-native-windows"
Expand Down
191 changes: 116 additions & 75 deletions example/App.js
Original file line number Diff line number Diff line change
@@ -1,108 +1,149 @@
import React from "react";
// @ts-check
import React, { useCallback, useMemo, useState } from "react";
import {
ScrollView,
StatusBar,
StyleSheet,
Switch,
Text,
useColorScheme,
View,
} from "react-native";
import { SafeAreaProvider, SafeAreaView } from "react-native-safe-area-context";
import {
Colors,
DebugInstructions,
Header,
LearnMoreLinks,
ReloadInstructions,
} from "react-native/Libraries/NewAppScreen";
import { Colors, Header } from "react-native/Libraries/NewAppScreen";
// @ts-expect-error
import { version as coreVersion } from "react-native/Libraries/Core/ReactNativeVersion";

const Section = ({ children, title }) => {
const isDarkMode = useColorScheme() === "dark";
function getHermesVersion() {
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>
// @ts-expect-error
global.HermesInternal?.getRuntimeProperties?.()["OSS Release Version"] ??
false
);
}

function getReactNativeVersion() {
const version = `${coreVersion.major}.${coreVersion.minor}.${coreVersion.patch}`;
return coreVersion.prerelease
? version + `-${coreVersion.prerelease}`
: version;
}

/**
* @param {unknown} value
* @returns {"Off" | "On"}
*/
function isOnOrOff(value) {
return value ? "On" : "Off";
}

function useStyles() {
const colorScheme = useColorScheme();
return useMemo(() => {
const isDarkMode = colorScheme === "dark";

const fontSize = 18;
const groupBorderRadius = 8;
const margin = 16;

return StyleSheet.create({
body: {
backgroundColor: isDarkMode ? Colors.black : Colors.lighter,
flex: 1,
},
group: {
backgroundColor: isDarkMode ? Colors.darker : Colors.white,
borderRadius: groupBorderRadius,
margin,
},
groupItemContainer: {
alignItems: "center",
flexDirection: "row",
marginHorizontal: margin,
},
groupItemLabel: {
color: isDarkMode ? Colors.white : Colors.black,
flex: 1,
fontSize,
marginVertical: 12,
},
groupItemValue: {
color: isDarkMode ? Colors.light : Colors.dark,
fontSize: fontSize,
},
separator: {
backgroundColor: isDarkMode ? Colors.dark : Colors.light,
height: StyleSheet.hairlineWidth,
marginStart: margin,
},
});
}, [colorScheme]);
}

/** @type {React.FunctionComponent<{ children: string; value: string | boolean; }>} */
const Feature = ({ children, value }) => {
const styles = useStyles();
return (
<View style={styles.groupItemContainer}>
<Text style={styles.groupItemLabel}>{children}</Text>
{typeof value === "boolean" ? (
<Switch value={value} />
) : (
<Text style={styles.groupItemValue}>{value}</Text>
)}
</View>
);
};

const App = () => {
const Separator = () => {
const styles = useStyles();
return <View style={styles.separator} />;
};

/** @type {React.FunctionComponent<{ concurrentRoot?: boolean; }>} */
const App = ({ concurrentRoot }) => {
const isDarkMode = useColorScheme() === "dark";
const styles = useStyles();

const backgroundStyle = {
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
};
const [isFabric, setFabric] = useState(false);
const onLayout = useCallback(
(ev) => {
setFabric(
Boolean(
ev.currentTarget["_internalInstanceHandle"]?.stateNode?.canonical
)
);
},
[setFabric]
);

const hermesVersion = getHermesVersion();

return (
<SafeAreaProvider>
<SafeAreaView style={backgroundStyle}>
<SafeAreaView style={styles.body}>
<StatusBar barStyle={isDarkMode ? "light-content" : "dark-content"} />
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={backgroundStyle}
onLayout={onLayout}
style={styles.body}
>
<Header />
<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 style={styles.group}>
<Feature value={getReactNativeVersion()}>React Native</Feature>
<Separator />
<Feature value={isOnOrOff(hermesVersion)}>Hermes</Feature>
<Separator />
<Feature value={isOnOrOff(isFabric)}>Fabric</Feature>
<Separator />
<Feature value={isOnOrOff(isFabric && concurrentRoot)}>
Concurrent React
</Feature>
</View>
</ScrollView>
</SafeAreaView>
</SafeAreaProvider>
);
};

const styles = StyleSheet.create({
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
},
sectionTitle: {
fontSize: 24,
fontWeight: "600",
},
sectionDescription: {
marginTop: 8,
fontSize: 18,
fontWeight: "400",
},
highlight: {
fontWeight: "700",
},
});

export default App;
1 change: 1 addition & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
import { AppRegistry } from "react-native";
import App from "./App";
import { name as appName } from "./app.json";
Expand Down
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"devDependencies": {
"@babel/core": "^7.0.0",
"@types/jest": "^27.0.0",
"@types/react-native": "^0.68.0",
"jest": "^27.0.0",
"metro-react-native-babel-preset": "^0.67.0",
"mkdirp": "^1.0.0",
Expand Down
11 changes: 11 additions & 0 deletions example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"jsx": "react-native"
},
"include": [
"App.js",
"index.js"
]
}
42 changes: 42 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2793,6 +2793,33 @@ __metadata:
languageName: node
linkType: hard

"@types/prop-types@npm:*":
version: 15.7.5
resolution: "@types/prop-types@npm:15.7.5"
checksum: 5b43b8b15415e1f298243165f1d44390403bb2bd42e662bca3b5b5633fdd39c938e91b7fce3a9483699db0f7a715d08cef220c121f723a634972fdf596aec980
languageName: node
linkType: hard

"@types/react-native@npm:^0.68.0":
version: 0.68.3
resolution: "@types/react-native@npm:0.68.3"
dependencies:
"@types/react": ^17
checksum: e9ca9e5038499044f33a6f1f1f65f3a2a8877e793552e24a8ab0f86f6ac04e193992ea8c5a6457ee3e76fc41d7c52f81a104ca90dd61d0924d8295c8d046a622
languageName: node
linkType: hard

"@types/react@npm:^17":
version: 17.0.48
resolution: "@types/react@npm:17.0.48"
dependencies:
"@types/prop-types": "*"
"@types/scheduler": "*"
csstype: ^3.0.2
checksum: b683fa33f751ced0b8c8715df9f40de15513c1d7ce66064a75cdfb8805cc913b0b214a2e7022ef0b724bd62a1e8651d040cd265dd0452bde03cca9b8e495742d
languageName: node
linkType: hard

"@types/retry@npm:^0.12.0":
version: 0.12.1
resolution: "@types/retry@npm:0.12.1"
Expand All @@ -2810,6 +2837,13 @@ __metadata:
languageName: node
linkType: hard

"@types/scheduler@npm:*":
version: 0.16.2
resolution: "@types/scheduler@npm:0.16.2"
checksum: b6b4dcfeae6deba2e06a70941860fb1435730576d3689225a421280b7742318d1548b3d22c1f66ab68e414f346a9542f29240bc955b6332c5b11e561077583bc
languageName: node
linkType: hard

"@types/semver@npm:^7.3.6":
version: 7.3.10
resolution: "@types/semver@npm:7.3.10"
Expand Down Expand Up @@ -4650,6 +4684,13 @@ __metadata:
languageName: node
linkType: hard

"csstype@npm:^3.0.2":
version: 3.1.0
resolution: "csstype@npm:3.1.0"
checksum: 644e986cefab86525f0b674a06889cfdbb1f117e5b7d1ce0fc55b0423ecc58807a1ea42ecc75c4f18999d14fc42d1d255f84662a45003a52bb5840e977eb2ffd
languageName: node
linkType: hard

"dargs@npm:^7.0.0":
version: 7.0.0
resolution: "dargs@npm:7.0.0"
Expand Down Expand Up @@ -5526,6 +5567,7 @@ __metadata:
dependencies:
"@babel/core": ^7.0.0
"@types/jest": ^27.0.0
"@types/react-native": ^0.68.0
jest: ^27.0.0
metro-react-native-babel-preset: ^0.67.0
mkdirp: ^1.0.0
Expand Down

0 comments on commit bc1107d

Please sign in to comment.