Skip to content

Commit 7f7b7d4

Browse files
Migrate 'Elements' examples to static API in v7 (#1318)
1 parent b2ecc08 commit 7f7b7d4

File tree

1 file changed

+156
-13
lines changed

1 file changed

+156
-13
lines changed

versioned_docs/version-7.x/elements.md

Lines changed: 156 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ title: Elements Library
44
sidebar_label: Elements
55
---
66

7+
import Tabs from '@theme/Tabs';
8+
import TabItem from '@theme/TabItem';
9+
710
A component library containing the UI elements and helpers used in React Navigation. It can be useful if you're building your own navigator, or want to reuse a default functionality in your app.
811

912
## Installation
@@ -41,6 +44,29 @@ Whether header title font should scale to respect Text Size accessibility settin
4144

4245
Function which returns a React Element to display on the left side of the header. You can use it to implement your custom left button, for example:
4346

47+
<Tabs groupId="config" queryString="config">
48+
<TabItem value="static" label="Static" default>
49+
50+
```js
51+
const RootStack = createNativeStackNavigator({
52+
screens: {
53+
Home: {
54+
screen: HomeScreen,
55+
options: {
56+
headerLeft: (props) => (
57+
<MyButton {...props} onPress={() => {
58+
// Do something
59+
}}>
60+
)
61+
}
62+
}
63+
}
64+
})
65+
```
66+
67+
</TabItem>
68+
<TabItem value="dynamic" label="Dynamic" default>
69+
4470
```js
4571
<Stack.Screen
4672
name="Home"
@@ -58,6 +84,9 @@ Function which returns a React Element to display on the left side of the header
5884
/>
5985
```
6086

87+
</TabItem>
88+
</Tabs>
89+
6190
#### `headerRight`
6291

6392
Function which returns a React Element to display on the right side of the header.
@@ -132,25 +161,139 @@ Function which returns a React Element to render as the background of the header
132161

133162
For example, you can use this with `headerTransparent` to render a blur view to create a translucent header.
134163

135-
<samp id="header-blur" />
164+
<Tabs groupId="config" queryString="config">
165+
<TabItem value="static" label="Static" default>
166+
167+
```js name="Header blur" snack version=7 dependencies=expo-blur@~12.9.1
168+
import * as React from 'react';
169+
import { Button, View, StyleSheet } from 'react-native';
170+
import {
171+
createStaticNavigation,
172+
useNavigation,
173+
} from '@react-navigation/native';
174+
import { createStackNavigator } from '@react-navigation/stack';
175+
import { BlurView } from 'expo-blur';
136176

137-
```js
177+
function HomeScreen() {
178+
const navigation = useNavigation();
179+
180+
return (
181+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
182+
<Button
183+
title="Go to Profile"
184+
onPress={() => navigation.navigate('Profile')}
185+
/>
186+
</View>
187+
);
188+
}
189+
190+
function ProfileScreen() {
191+
const navigation = useNavigation();
192+
193+
return (
194+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
195+
<Button title="Go back" onPress={() => navigation.goBack()} />
196+
</View>
197+
);
198+
}
199+
200+
// codeblock-focus-start
201+
const Stack = createStackNavigator({
202+
initialRouteName: 'Home',
203+
screens: {
204+
Home: {
205+
screen: HomeScreen,
206+
options: {
207+
headerTransparent: true,
208+
headerBackground: () => (
209+
<BlurView
210+
tint="dark"
211+
intensity={100}
212+
style={StyleSheet.absoluteFill}
213+
/>
214+
),
215+
},
216+
},
217+
Profile: ProfileScreen,
218+
},
219+
});
220+
// codeblock-focus-end
221+
222+
const Navigation = createStaticNavigation(Stack);
223+
224+
function App() {
225+
return <Navigation />;
226+
}
227+
228+
export default App;
229+
```
230+
231+
</TabItem>
232+
<TabItem value="dynamic" label="Dynamic" default>
233+
234+
```js name="Header blur" snack version=7 dependencies=expo-blur@~12.9.1
235+
import * as React from 'react';
236+
import { Button, View, StyleSheet } from 'react-native';
237+
import { NavigationContainer } from '@react-navigation/native';
238+
import { createStackNavigator } from '@react-navigation/stack';
239+
// codeblock-focus-start
138240
import { BlurView } from 'expo-blur';
139241

140-
// ...
242+
// codeblock-focus-end
141243

142-
<Stack.Screen
143-
name="Home"
144-
component={HomeScreen}
145-
options={{
146-
headerTransparent: true,
147-
headerBackground: () => (
148-
<BlurView tint="light" intensity={100} style={StyleSheet.absoluteFill} />
149-
),
150-
}}
151-
/>;
244+
function HomeScreen({ navigation }) {
245+
return (
246+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
247+
<Button
248+
title="Go to Profile"
249+
onPress={() => navigation.navigate('Profile')}
250+
/>
251+
</View>
252+
);
253+
}
254+
255+
function ProfileScreen({ navigation }) {
256+
return (
257+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
258+
<Button title="Go back" onPress={() => navigation.goBack()} />
259+
</View>
260+
);
261+
}
262+
263+
const Stack = createStackNavigator();
264+
265+
function App() {
266+
return (
267+
<NavigationContainer>
268+
<Stack.Navigator initialRouteName="Home">
269+
// codeblock-focus-start
270+
<Stack.Screen
271+
name="Home"
272+
component={HomeScreen}
273+
options={{
274+
headerTransparent: true,
275+
headerBackground: () => (
276+
<BlurView
277+
tint="dark"
278+
intensity={100}
279+
style={StyleSheet.absoluteFill}
280+
/>
281+
),
282+
}}
283+
/>
284+
// codeblock-focus-end
285+
<Stack.Screen name="Profile" component={ProfileScreen} />
286+
</Stack.Navigator>
287+
</NavigationContainer>
288+
);
289+
}
290+
291+
export default App;
152292
```
153293

294+
</TabItem>
295+
</Tabs>
296+
154297
#### `headerStatusBarHeight`
155298

156299
Extra padding to add at the top of header to account for translucent status bar. By default, it uses the top value from the safe area insets of the device. Pass 0 or a custom value to disable the default behavior, and customize the height.

0 commit comments

Comments
 (0)