Skip to content

Commit 41861ce

Browse files
authored
docs: initial props demo docs & demos (#298)
* docs: clear documentation on initial properties from native to RN * feat(demo): demonstrate initial props from native to root RN component * docs: changes after self-CR
1 parent 5ac357b commit 41861ce

File tree

11 files changed

+113
-12
lines changed

11 files changed

+113
-12
lines changed

apps/AndroidApp/app/src/main/java/com/callstack/brownfield/android/example/MainActivity.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.callstack.brownfield.android.example
22

33
import android.content.Intent
44
import android.content.res.Configuration
5+
import android.os.Build
56
import android.os.Bundle
67
import android.widget.Toast
78
import androidx.activity.compose.setContent
@@ -129,6 +130,15 @@ fun ReactNativeView(
129130
ReactNativeFragmentArgNames.ARG_MODULE_NAME,
130131
ReactNativeConstants.MAIN_MODULE_NAME
131132
)
133+
putBundle(
134+
ReactNativeFragmentArgNames.ARG_LAUNCH_OPTIONS,
135+
Bundle().apply {
136+
putString(
137+
"nativeOsVersionLabel",
138+
"Android ${Build.VERSION.RELEASE}"
139+
)
140+
}
141+
)
132142
}
133143
)
134144
}

apps/AppleApp/Brownfield Apple App/components/ContentView.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ struct ContentView: View {
2323

2424
MessagesView()
2525

26-
ReactNativeView(moduleName: "main")
26+
ReactNativeView(
27+
moduleName: "main",
28+
initialProperties: [
29+
"nativeOsVersionLabel":
30+
"\(UIDevice.current.systemName) \(UIDevice.current.systemVersion)"
31+
]
32+
)
2733
.navigationBarHidden(true)
2834
.clipShape(RoundedRectangle(cornerRadius: 16))
2935
.background(Color(UIColor.systemBackground))

apps/ExpoApp54/RNApp.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,24 @@ import BrownfieldNavigation from '@callstack/brownfield-navigation';
44

55
import Counter from './components/counter';
66

7-
export default function RNApp() {
7+
type RNAppProps = {
8+
nativeOsVersionLabel?: string;
9+
};
10+
11+
export default function RNApp({ nativeOsVersionLabel }: RNAppProps) {
812
return (
913
<SafeAreaView style={styles.container}>
1014
<Text style={styles.title}>Expo React Native Brownfield</Text>
1115

16+
{nativeOsVersionLabel ? (
17+
<Text
18+
style={styles.nativeOsVersionLabel}
19+
accessibilityLabel="Native OS version"
20+
>
21+
{nativeOsVersionLabel}
22+
</Text>
23+
) : null}
24+
1225
<View style={styles.content}>
1326
<Counter />
1427

@@ -36,6 +49,12 @@ const styles = StyleSheet.create({
3649
fontWeight: 'bold',
3750
textAlign: 'center',
3851
},
52+
nativeOsVersionLabel: {
53+
fontSize: 11,
54+
opacity: 0.75,
55+
textAlign: 'center',
56+
marginTop: 4,
57+
},
3958
content: {
4059
flex: 1,
4160
justifyContent: 'center',

apps/ExpoApp55/RNApp.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,24 @@ import BrownfieldNavigation from '@callstack/brownfield-navigation';
44

55
import Counter from './src/components/counter';
66

7-
export default function RNApp() {
7+
type RNAppProps = {
8+
nativeOsVersionLabel?: string;
9+
};
10+
11+
export default function RNApp({ nativeOsVersionLabel }: RNAppProps) {
812
return (
913
<SafeAreaView style={styles.container}>
1014
<Text style={styles.title}>Expo React Native Brownfield</Text>
1115

16+
{nativeOsVersionLabel ? (
17+
<Text
18+
style={styles.nativeOsVersionLabel}
19+
accessibilityLabel="Native OS version"
20+
>
21+
{nativeOsVersionLabel}
22+
</Text>
23+
) : null}
24+
1225
<View style={styles.content}>
1326
<Counter />
1427

@@ -36,6 +49,12 @@ const styles = StyleSheet.create({
3649
fontWeight: 'bold',
3750
textAlign: 'center',
3851
},
52+
nativeOsVersionLabel: {
53+
fontSize: 11,
54+
opacity: 0.75,
55+
textAlign: 'center',
56+
marginTop: 4,
57+
},
3958
content: {
4059
flex: 1,
4160
justifyContent: 'center',

apps/RNApp/src/App.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@ import '../BrownfieldStore.brownie';
22

33
import { NavigationContainer } from '@react-navigation/native';
44

5-
import { Stack } from './navigation/RootStack';
65
import { HomeScreen } from './HomeScreen';
6+
import { NativeOsVersionLabelContext } from './nativeHostContext';
7+
import { Stack } from './navigation/RootStack';
8+
9+
type AppProps = {
10+
nativeOsVersionLabel?: string;
11+
};
712

8-
export default function App() {
13+
export default function App({ nativeOsVersionLabel }: AppProps) {
914
return (
10-
<NavigationContainer>
11-
<Stack.Navigator>
12-
<Stack.Screen name="Home" component={HomeScreen} />
13-
</Stack.Navigator>
14-
</NavigationContainer>
15+
<NativeOsVersionLabelContext.Provider value={nativeOsVersionLabel}>
16+
<NavigationContainer>
17+
<Stack.Navigator>
18+
<Stack.Screen name="Home" component={HomeScreen} />
19+
</Stack.Navigator>
20+
</NavigationContainer>
21+
</NativeOsVersionLabelContext.Provider>
1522
);
1623
}

apps/RNApp/src/HomeScreen.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import BrownfieldNavigation from '@callstack/brownfield-navigation';
1515
import { getRandomTheme } from './utils';
1616
import type { RootStackParamList } from './navigation/RootStack';
1717
import Counter from './components/counter';
18+
import { useNativeOsVersionLabel } from './nativeHostContext';
1819

1920
interface Message {
2021
id: string;
@@ -71,6 +72,7 @@ export function HomeScreen({
7172
navigation,
7273
route,
7374
}: NativeStackScreenProps<RootStackParamList, 'Home'>) {
75+
const nativeOsVersionLabel = useNativeOsVersionLabel();
7476
const colors = route.params?.theme ? route.params.theme : getRandomTheme();
7577
const [messages, setMessages] = useState<Message[]>([]);
7678
const flatListRef = useRef<FlatList<Message>>(null);
@@ -122,6 +124,15 @@ export function HomeScreen({
122124
React Native Screen
123125
</Text>
124126

127+
{nativeOsVersionLabel ? (
128+
<Text
129+
style={[styles.nativeOsVersionLabel, { color: colors.secondary }]}
130+
accessibilityLabel="Native OS version"
131+
>
132+
{nativeOsVersionLabel}
133+
</Text>
134+
) : null}
135+
125136
<Counter colors={colors} />
126137

127138
<View style={styles.messageSection}>
@@ -199,6 +210,12 @@ const styles = StyleSheet.create({
199210
fontWeight: 'bold',
200211
marginBottom: 8,
201212
},
213+
nativeOsVersionLabel: {
214+
fontSize: 11,
215+
opacity: 0.85,
216+
marginBottom: 4,
217+
textAlign: 'center',
218+
},
202219
messageSection: {
203220
flex: 1,
204221
width: '100%',
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { createContext, useContext } from 'react';
2+
3+
export const NativeOsVersionLabelContext = createContext<string | undefined>(
4+
undefined
5+
);
6+
7+
export function useNativeOsVersionLabel(): string | undefined {
8+
return useContext(NativeOsVersionLabelContext);
9+
}

docs/docs/docs/api-reference/react-native-brownfield/java.mdx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ A function used to initialize a React Native Brownfield singleton. Keep in mind
6464

6565
> `*` - From the marked fields, exactly one must be specified, excluding the others. See examples below.
6666
67-
**Available options:**
67+
> [!Note]
68+
> The `options` map for `initialize` is unrelated to the initial props passed to the root component. For passing in initial props, use the [`launchOptions` argument of `createView`](#createview).
69+
70+
**Available `options` entries:**
6871

6972
- `useDeveloperSupport`: `Boolean` - Flag to use dev support.
7073
- `packages`: `List<ReactPackage>` - List of your React Native Native modules.
@@ -167,6 +170,8 @@ Creates a React Native view with a given module name. It automatically uses an i
167170

168171
Returns: `FrameLayout` - A view containing the React Native component.
169172

173+
Root initial props can be passed through the `launchOptions` argument. That `Bundle` is forwarded as the root view's initial properties - the same concept as [initial props on `ReactRootView`](https://reactnative.dev/docs/communication-android#passing-properties-from-native-to-react-native).
174+
170175
**Examples:**
171176

172177
```java

docs/docs/docs/api-reference/react-native-brownfield/kotlin.mdx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ A function used to initialize a React Native Brownfield singleton. Keep in mind
4747

4848
> `*` - From the marked fields, exactly one must be specified, excluding the others. See examples below.
4949
50-
**Available options:**
50+
> [!Note]
51+
> The `options` map for `initialize` is unrelated to the initial props passed to the root component. For passing in initial props, use the [`launchOptions` argument of `createView`](#createview).
52+
53+
**Available `options` entries:**
5154

5255
- `useDeveloperSupport`: `Boolean` - Flag to use dev support.
5356
- `packages`: `List<ReactPackage>` - List of your React Native Native modules.
@@ -146,6 +149,8 @@ Creates a React Native view with a given module name. It automatically uses an i
146149

147150
Returns: `FrameLayout` - A view containing the React Native component.
148151

152+
Root initial props can be passed through the `launchOptions` argument. That `Bundle` is forwarded as the root view's initial properties - the same concept as [initial props on `ReactRootView`](https://reactnative.dev/docs/communication-android#passing-properties-from-native-to-react-native).
153+
149154
**Examples:**
150155

151156
```kotlin

docs/docs/docs/api-reference/react-native-brownfield/objective-c.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ Creates a React Native view for the specified module name.
6969
| `initialProps` | No | `NSDictionary` | Initial properties to be passed to React Native component. |
7070
| `launchOptions` | No | `NSDictionary` | Launch options, typically passed from AppDelegate. |
7171
72+
Root initial props can be passed through the `initialProps` argument. That `NSDictionary` is forwarded as the root view's initial properties - the same concept as [initial props on `RCTRootView`](https://reactnative.dev/docs/communication-ios#passing-properties-from-native-to-react-native).
73+
7274
**Examples:**
7375
7476
```objc

0 commit comments

Comments
 (0)