react-native-popover-view
This allows the popover view from react-native-popover-view to be used with React Navigation, just like the Stack Navigator, Drawer Navigator, and others.
It works by modifying the stack navigator to show the child views in a popover if desired, which is great for making your app adaptable to tablets, where full-screen views in a stack can be a bit much.
See the documentation for react-native-popover-view for details on how the Popover
component itself works.
This project is not officially affiliated with React Navigation, but integrates well and borrows code from react-navigation-stack. Thanks to all the guys at React Navigation for all the work they do.
Table of Contents
Demo App
You can play around with the various features using the Expo test app. Source Code: react-native-popover-view-test-app
Installation
npm i react-navigation-popover
or
yarn add react-navigation-popover
Usage with React Navigation
Support
react-navigation-popover
v1.1.x supportsreact-navigation
v3.0.0 and forwardsreact-navigation-popover
v1.0.x supportsreact-navigation
v2.12.0 to v2.14.2- For older versions of
react-navigation
, instead installreact-native-popover-view
v1.0.5 or ealier, and use the documentation at that repository for the corresponding tag
If react-navigation
changes in a future versions and breaks compatibility with this library, please open an issue.
Basic Setup
createStackNavigator
to createPopoverStackNavigator
1) Change createPopoverStackNavigator
is a drop-in replacement for react-navigation's createStackNavigator
. It assumes the first view in your routeConfigMap
is the base view, and every other view should be shown in a Popover when the showInPopover
prop is true
(see step #2).
You can pass a few (optional) per-screen options through your routeConfigMap
or globally through your stackConfig
:
Option | Type | Default | Description |
---|---|---|---|
placement |
PLACEMENT_OPTIONS | PLACEMENT_OPTIONS.AUTO | Passed through to Popover . |
contentContainerStyle |
number | {width: 380} | The style for the internal view that wraps the Popover . |
showInModal |
boolean | true | Passed through to Popover . If you want to stack multiple Popover 's, only the bottom one can be shown in a Modal on iOS. |
showBackground |
boolean | true | Passed through to Popover |
arrowStyle |
object | {} | Passed through to Popover |
popoverStyle |
object | {} | Passed through to Popover |
animationConfig |
object | Passed through to Popover |
|
verticalOffset |
number | 0 | Passed through to Popover |
Note: If you pass a value through the stackConfig
, and pass the same option for an individual screen, the value passed for the screen overrides.
Example:
import createPopoverStackNavigator from 'react-navigation-popover'; let stack = ;
2) Define when Popover should be shown
By default, views will be shown in a Popover view on tablets, and normally on phones. To override this behavior, you can pass the showInPopover
option in the screenProps
to the class returned by createPopoverStackNavigator
:
let Stack = ;... { let smallScreen = thispropswidth < 500; return <Stack = />; }
This sort of width-based test is needed if your app can be launched in split-screen mode on tablets, because the default value is always true
on tablets regardless of the actual display width of your app.
3) (Optional) Set Popover Source
There are several ways to make sure the Popover
shows from the button that triggered it:
I. (Recommended) Register Refs for Views
You can register the button as the source of the Popover
for a particular route. Check out this example:
We first register the ref for a view:
<TouchableHighlight = />
Then, if View1
is a route name in a createPopoverStackNavigator
...
import View1 from './views/View1';...let stack = ;
When we navigate to the view, the Popover
will originate from the associated TouchableHighlight
:
thispropsnavigation;
You can register any type of view, not only a TouchableHighlight
, and the Popover
will point to the outside of the bounds of that view.
Note: The map is stored statically, so you cannot register two views with the same name, even if they are in different createPopoverStackNavigator
's.
II. Send showFromView
If you need even more fine-grained control, such as wanting to open the same child but have it originate from different views at different times, you can pass the showFromView
param in your navigate
call:
thispropsnavigation;
where this.storedView
is a ref of a component (obtained through a ref
callback).
III. Use a Custum Rect
See "Show Popover from custom rect" in the Advanced Usage section below.
Full Example
import React Component from 'react';import createPopoverStackNavigator from 'react-navigation-popover';import MoreHeaderView ExtraInfoView MoreOptionView from './myOtherViews';import Colors from './Colors';import DeviceInfo from 'react-native-device-info'; { return <View => <MoreHeaderView /> <View> <TouchableHighlight = = => <Text>About the App</Text> </TouchableHighlight> <TouchableHighlight = = => <Text>Content Settings</Text> </TouchableHighlight> <TouchableHighlight = = => <Text>Account Details</Text> </TouchableHighlight> </View> <ExtraInfoView /> </View> } let MoreStack = ; state = width: DeviceInfo { return <View = => <MoreStack = /> </View> ; } let styles = buttonStyle: width: 100 height: 40 marginBottom: 50 viewStyle: alignItems: 'center' headerStyle: headerStyle: backgroundColor: ColorsbackgroundColor headerTintColor: ColorstintColor headerTitleStyle: color: ColorsheaderTextColor fullScreenViewStyle: position: 'absolute' left: 0 right: 0 top: 0 bottom: 0
Advanced Usage
Custumize Display Area used by Popovers
By default, Popover's will query RN's SafeAreaView
to get the allowed display area on the device, and then add a 10pt padding around all the edges, and set this as the display area. If you want to inject a custum display area to a specific popover, you can do so either through the createPopoverStackNavigator
's RouteConfigs
or through params in the navigate
call:
let Stack = ;
OR
thispropsnavigation;
Show Popover from custom rect
There may be situations in which you want to show a Popover
with a custom fromRect, not tied to any view. Instead of using createPopoverStackNavigator.registerRefForView
, you can pass in a custom fromRect
as params to the navigate()
call. For example:
import Rect from 'react-navigation-popover';... thispropsnavigation;
If the rect uses variables that could change when the display area changes, you should instead use calculateRect
, and pass in a function that will return the rect. For example, if your popover originates from a button that is always centered, regardless of screen size, you could use the following:
import Rect from 'react-navigation-popover';... thispropsnavigation;
Now, if your app is put into split-screen mode while the popover is still showing, calculateRect
will be called again, and the popover will shift to point to the new rect.
Contributing
Pull requests are welcome; if you find that you are having to bend over backwards to make this work for you, feel free to open an issue or PR! Of course, try to keep the same coding style if possible and I'll try to get back to you as soon as I can.
MIT Licensed