Skip to content

Commit 487d379

Browse files
committed
Document exported hooks for each navigator
1 parent 8636893 commit 487d379

File tree

4 files changed

+121
-27
lines changed

4 files changed

+121
-27
lines changed

versioned_docs/version-7.x/bottom-tab-navigator.md

+40-26
Original file line numberDiff line numberDiff line change
@@ -375,31 +375,7 @@ To show your screen under the tab bar, you can set the `position` style to absol
375375
>
376376
```
377377

378-
You also might need to add a bottom margin to your content if you have a absolutely positioned tab bar. React Navigation won't do it automatically.
379-
380-
To get the height of the bottom tab bar, you can use `BottomTabBarHeightContext` with [React's Context API](https://reactjs.org/docs/context.html#contextconsumer) or `useBottomTabBarHeight`:
381-
382-
```js
383-
import { BottomTabBarHeightContext } from '@react-navigation/bottom-tabs';
384-
385-
// ...
386-
387-
<BottomTabBarHeightContext.Consumer>
388-
{tabBarHeight => (
389-
/* render something */
390-
)}
391-
</BottomTabBarHeightContext.Consumer>
392-
```
393-
394-
or
395-
396-
```js
397-
import { useBottomTabBarHeight } from '@react-navigation/bottom-tabs';
398-
399-
// ...
400-
401-
const tabBarHeight = useBottomTabBarHeight();
402-
```
378+
You also might need to add a bottom margin to your content if you have an absolutely positioned tab bar. React Navigation won't do it automatically. See [`useBottomTabBarHeight`](#usebottomtabbarheight) for more details.
403379

404380
#### `tabBarBackground`
405381

@@ -420,7 +396,7 @@ import { BlurView } from 'expo-blur';
420396
>
421397
```
422398

423-
When using `BlurView`, make sure to set `position: 'absolute'` in `tabBarStyle` as well. You'd also need to use `useBottomTabBarHeight()` to add a bottom padding to your content.
399+
When using `BlurView`, make sure to set `position: 'absolute'` in `tabBarStyle` as well. You'd also need to use [`useBottomTabBarHeight`](#usebottomtabbarheight) to add a bottom padding to your content.
424400

425401
#### `tabBarPosition`
426402

@@ -581,6 +557,44 @@ Navigates to an existing screen in the tab navigator. The method accepts followi
581557
navigation.jumpTo('Profile', { owner: 'Michaś' });
582558
```
583559

560+
### Hooks
561+
562+
The bottom tab navigator exports the following hooks:
563+
564+
#### `useBottomTabBarHeight`
565+
566+
This hook returns the height of the bottom tab bar. By default, the screen content doesn't go under the tab bar. However, if you want to make the tab bar absolutely positioned and have the content go under it (e.g. to show a blur effect), it's necessary to adjust the content to take the tab bar height into account.
567+
568+
Example:
569+
570+
```js
571+
import { useBottomTabBarHeight } from '@react-navigation/bottom-tabs';
572+
573+
function MyComponent() {
574+
const tabBarHeight = useBottomTabBarHeight();
575+
576+
return (
577+
<ScrollView contentStyle={{ paddingBottom: tabBarHeight }}>
578+
{/* Content */}
579+
</ScrollView>
580+
);
581+
}
582+
```
583+
584+
Alternatively, you can use the `BottomTabBarHeightContext` directly if you are using a class component or need it in a reusable component that can be used outside the bottom tab navigator:
585+
586+
```js
587+
import { BottomTabBarHeightContext } from '@react-navigation/bottom-tabs';
588+
589+
// ...
590+
591+
<BottomTabBarHeightContext.Consumer>
592+
{tabBarHeight => (
593+
/* render something */
594+
)}
595+
</BottomTabBarHeightContext.Consumer>
596+
```
597+
584598
## Animations
585599

586600
By default, switching between tabs doesn't have any animation. You can specify the `animation` option to customize the transition animation.

versioned_docs/version-7.x/material-top-tab-navigator.md

+28
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,31 @@ Navigates to an existing screen in the tab navigator. The method accepts followi
517517
```js
518518
navigation.jumpTo('Profile', { name: 'Michaś' });
519519
```
520+
521+
### Hooks
522+
523+
The material top tab navigator exports the following hooks:
524+
525+
#### `useTabAnimation`
526+
527+
This hook returns an object containing an animated value that represents the current position of the tabs. This can be used to animate elements based on the swipe position of the tabs, such as the tab indicator:
528+
529+
```js
530+
import { Animated } from 'react-native';
531+
import { useTabAnimation } from '@react-navigation/material-top-tabs';
532+
533+
function MyView() {
534+
const { position } = useTabAnimation();
535+
536+
return (
537+
<Animated.View
538+
style={{
539+
width: '50%',
540+
height: 2,
541+
backgroundColor: 'tomato',
542+
transform: [{ translateX: position }],
543+
}}
544+
/>
545+
);
546+
}
547+
```

versioned_docs/version-7.x/native-stack-navigator.md

+30
Original file line numberDiff line numberDiff line change
@@ -805,3 +805,33 @@ Pops all of the screens in the stack except the first one and navigates to it.
805805
```js
806806
navigation.popToTop();
807807
```
808+
809+
### Hooks
810+
811+
The native stack navigator exports the following hooks:
812+
813+
#### `useAnimatedHeaderHeight`
814+
815+
The hook returns an animated value representing the height of the header. This is similar to [`useHeaderHeight`](elements.md#useheaderheight) but returns an animated value that changed as the header height changes, e.g. when expanding or collapsing large title or search bar on iOS.
816+
817+
It can be used to animated content along with header height changes.
818+
819+
```js
820+
import { Animated } from 'react-native';
821+
import { useAnimatedHeaderHeight } from '@react-navigation/native-stack';
822+
823+
const MyView = () => {
824+
const headerHeight = useAnimatedHeaderHeight();
825+
826+
return (
827+
<Animated.View
828+
style={{
829+
height: 100,
830+
aspectRatio: 1,
831+
backgroundColor: 'tomato',
832+
transform: [{ translateY: headerHeight }],
833+
}}
834+
/>
835+
);
836+
};
837+
```

versioned_docs/version-7.x/stack-navigator.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,28 @@ Pops all of the screens in the stack except the first one and navigates to it.
595595
navigation.popToTop();
596596
```
597597

598+
### Hooks
599+
600+
The stack navigator exports the following hooks:
601+
602+
#### `useCardAnimation`
603+
604+
This hook returns values related to the screen's animation. It contains the following properties:
605+
606+
- `current` - Values for the current screen:
607+
- `progress` - Animated node representing the progress value of the current screen.
608+
- `next` - Values for the screen after this one in the stack. This can be `undefined` in case the screen animating is the last one.
609+
- `progress` - Animated node representing the progress value of the next screen.
610+
- `closing` - Animated node representing whether the card is closing. `1` when closing, `0` if not.
611+
- `swiping` - Animated node representing whether the card is being swiped. `1` when swiping, `0` if not.
612+
- `inverted` - Animated node representing whether the card is inverted. `-1` when inverted, `1` if not.
613+
- `index` - The index of the card in the stack.
614+
- `layouts` - Layout measurements for various items we use for animation.
615+
- `screen` - Layout of the whole screen. Contains `height` and `width` properties.
616+
- `insets` - Layout of the safe area insets. Contains `top`, `right`, `bottom` and `left` properties.
617+
618+
See [Transparent modals](#transparent-modals) for an example of how to use this hook.
619+
598620
## Animations
599621

600622
You can specify the `animation` option to customize the transition animation for screens being pushed or popped.
@@ -800,7 +822,7 @@ Stack Navigator exposes various options to configure the transition animation wh
800822
- `title` - Layout of the title element. Might be `undefined` when not rendering a title.
801823
- `leftLabel` - Layout of the back button label. Might be `undefined` when not rendering a back button label.
802824

803-
A config which just fades the elements looks like this:
825+
A config that just fades the elements looks like this:
804826

805827
```js
806828
const forFade = ({ current, next }) => {

0 commit comments

Comments
 (0)