@rn-tools/navigation 2.2.5 → 2.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -29
- package/package.json +1 -1
- package/src/stack.tsx +20 -19
package/README.md
CHANGED
|
@@ -301,16 +301,12 @@ function switchMainTabsToTab(tabIndex: number) {
|
|
|
301
301
|
|
|
302
302
|
### Rendering a header
|
|
303
303
|
|
|
304
|
-
Use the `Stack.Header` component to render a native header in a screen.
|
|
304
|
+
Use the `Stack.Header` component to render a native header in a screen by passing it as a prop to `Stack.Screen`.
|
|
305
305
|
|
|
306
306
|
Under the hood this is using `react-native-screens` header - [here is a reference for the available props](https://github.com/software-mansion/react-native-screens/blob/main/guides/GUIDE_FOR_LIBRARY_AUTHORS.md#screenstackheaderconfig)
|
|
307
307
|
|
|
308
308
|
You can provide custom left, center, and right views in the header by using the `Stack.HeaderLeft`, `Stack.HeaderCenter`, and `Stack.HeaderRight` view container components as children of `Stack.Header`.
|
|
309
309
|
|
|
310
|
-
**Note:** Wrap your App in a `SafeAreaProvider` to ensure your screen components are rendered correctly with the header
|
|
311
|
-
|
|
312
|
-
**Note:**: The header component **has to be the first child** of a `Stack.Screen` component.
|
|
313
|
-
|
|
314
310
|
```tsx
|
|
315
311
|
import { navigation, Stack } from "@rn-tools/navigation";
|
|
316
312
|
import * as React from "react";
|
|
@@ -331,20 +327,20 @@ function MyScreenWithHeader() {
|
|
|
331
327
|
let [title, setTitle] = React.useState("");
|
|
332
328
|
|
|
333
329
|
return (
|
|
334
|
-
<Stack.Screen
|
|
335
|
-
{
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
</Stack.
|
|
346
|
-
|
|
347
|
-
|
|
330
|
+
<Stack.Screen
|
|
331
|
+
header={
|
|
332
|
+
<Stack.Header
|
|
333
|
+
title={title}
|
|
334
|
+
backTitle="Custom back title"
|
|
335
|
+
backTitleFontSize={16}
|
|
336
|
+
hideBackButton={false}
|
|
337
|
+
>
|
|
338
|
+
<Stack.HeaderRight>
|
|
339
|
+
<Text>Custom right text!</Text>
|
|
340
|
+
</Stack.HeaderRight>
|
|
341
|
+
</Stack.Header>
|
|
342
|
+
}
|
|
343
|
+
>
|
|
348
344
|
<View
|
|
349
345
|
style={{
|
|
350
346
|
flex: 1,
|
|
@@ -751,17 +747,19 @@ function MyScreen() {
|
|
|
751
747
|
preventNativeDismiss={!canGoBack}
|
|
752
748
|
nativeBackButtonDismissalEnabled={!canGoBack}
|
|
753
749
|
gestureEnabled={canGoBack}
|
|
750
|
+
header={
|
|
751
|
+
<Stack.Header title="Prevent going back">
|
|
752
|
+
<Stack.HeaderLeft>
|
|
753
|
+
<TouchableOpacity
|
|
754
|
+
onPress={onPressBackButton}
|
|
755
|
+
style={{ opacity: canGoBack ? 1 : 0.4 }}
|
|
756
|
+
>
|
|
757
|
+
<Text>Back</Text>
|
|
758
|
+
</TouchableOpacity>
|
|
759
|
+
</Stack.HeaderLeft>
|
|
760
|
+
</Stack.Header>
|
|
761
|
+
}
|
|
754
762
|
>
|
|
755
|
-
<Stack.Header title="Prevent going back">
|
|
756
|
-
<Stack.HeaderLeft>
|
|
757
|
-
<TouchableOpacity
|
|
758
|
-
onPress={onPressBackButton}
|
|
759
|
-
style={{ opacity: canGoBack ? 1 : 0.4 }}
|
|
760
|
-
>
|
|
761
|
-
<Text>Back</Text>
|
|
762
|
-
</TouchableOpacity>
|
|
763
|
-
</Stack.HeaderLeft>
|
|
764
|
-
</Stack.Header>
|
|
765
763
|
<View style={{ paddingVertical: 48, paddingHorizontal: 16, gap: 16 }}>
|
|
766
764
|
<Text style={{ fontSize: 22, fontWeight: "medium" }}>
|
|
767
765
|
Enter some text and try to go back
|
package/package.json
CHANGED
package/src/stack.tsx
CHANGED
|
@@ -143,7 +143,9 @@ let defaultScreenStyle: ViewStyle = {
|
|
|
143
143
|
backgroundColor: "white",
|
|
144
144
|
};
|
|
145
145
|
|
|
146
|
-
export type StackScreenProps = RNScreenProps
|
|
146
|
+
export type StackScreenProps = RNScreenProps & {
|
|
147
|
+
header?: React.ReactElement<StackScreenHeaderProps>
|
|
148
|
+
}
|
|
147
149
|
|
|
148
150
|
let HeaderHeightContext = React.createContext<number>(0);
|
|
149
151
|
|
|
@@ -153,6 +155,7 @@ let StackScreen = React.memo(function StackScreen({
|
|
|
153
155
|
gestureEnabled = true,
|
|
154
156
|
onDismissed: onDismissedProp,
|
|
155
157
|
onHeaderHeightChange: onHeaderHeightChangeProp,
|
|
158
|
+
header,
|
|
156
159
|
...props
|
|
157
160
|
}: StackScreenProps) {
|
|
158
161
|
let stackId = React.useContext(StackIdContext);
|
|
@@ -188,19 +191,29 @@ let StackScreen = React.memo(function StackScreen({
|
|
|
188
191
|
};
|
|
189
192
|
}, [gestureEnabled, stack, screenId, isActive, dispatch]);
|
|
190
193
|
|
|
191
|
-
let
|
|
192
|
-
|
|
193
|
-
let [headerHeight, setHeaderHeight] = React.useState(0);
|
|
194
|
+
let parentHeaderHeight = React.useContext(HeaderHeightContext);
|
|
195
|
+
let [headerHeight, setHeaderHeight] = React.useState(parentHeaderHeight);
|
|
194
196
|
|
|
195
197
|
let onHeaderHeightChange: ScreenProps["onHeaderHeightChange"] =
|
|
196
198
|
React.useCallback(
|
|
197
199
|
(e) => {
|
|
198
|
-
Platform.OS === "ios" &&
|
|
200
|
+
Platform.OS === "ios" &&
|
|
201
|
+
e.nativeEvent.headerHeight > 0 &&
|
|
202
|
+
setHeaderHeight(e.nativeEvent.headerHeight);
|
|
199
203
|
onHeaderHeightChangeProp?.(e);
|
|
200
204
|
},
|
|
201
205
|
[onHeaderHeightChangeProp]
|
|
202
206
|
);
|
|
203
207
|
|
|
208
|
+
let style = React.useMemo(
|
|
209
|
+
() => [
|
|
210
|
+
defaultScreenStyle,
|
|
211
|
+
{ paddingTop: headerHeight || parentHeaderHeight },
|
|
212
|
+
styleProp,
|
|
213
|
+
],
|
|
214
|
+
[styleProp, headerHeight, parentHeaderHeight]
|
|
215
|
+
);
|
|
216
|
+
|
|
204
217
|
return (
|
|
205
218
|
<HeaderHeightContext.Provider value={headerHeight}>
|
|
206
219
|
{/* @ts-expect-error - Ref typings in RNScreens */}
|
|
@@ -212,6 +225,7 @@ let StackScreen = React.memo(function StackScreen({
|
|
|
212
225
|
onDismissed={onDismissed}
|
|
213
226
|
onHeaderHeightChange={onHeaderHeightChange}
|
|
214
227
|
>
|
|
228
|
+
{header}
|
|
215
229
|
{children}
|
|
216
230
|
</RNScreen>
|
|
217
231
|
</HeaderHeightContext.Provider>
|
|
@@ -255,20 +269,7 @@ export type StackScreenHeaderProps = RNScreenStackHeaderConfigProps;
|
|
|
255
269
|
let StackScreenHeader = React.memo(function StackScreenHeader({
|
|
256
270
|
...props
|
|
257
271
|
}: StackScreenHeaderProps) {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
let placeholderStyle = React.useMemo<ViewStyle>(() => {
|
|
261
|
-
return {
|
|
262
|
-
height: headerHeight,
|
|
263
|
-
};
|
|
264
|
-
}, [headerHeight]);
|
|
265
|
-
|
|
266
|
-
return (
|
|
267
|
-
<React.Fragment>
|
|
268
|
-
<RNScreenStackHeaderConfig {...props} />
|
|
269
|
-
<View style={placeholderStyle} />
|
|
270
|
-
</React.Fragment>
|
|
271
|
-
);
|
|
272
|
+
return <RNScreenStackHeaderConfig {...props} />;
|
|
272
273
|
});
|
|
273
274
|
|
|
274
275
|
let StackScreenHeaderLeft = React.memo(function StackScreenHeaderLeft({
|