A well-tested, adaptable, lightweight <Popover> component for react-native
A well-tested, adaptable, lightweight <Popover> component for react-native
A well-tested, adaptable, lightweight `` component for react-native with no dependencies. Tested and working on iOS and Android. May work on Web, but not officially supported.
It is written entirely in TypeScript and uses React Native's native driver for responsive animations, even when the JS thread is busy.
Open the snack to play with all the features!
I no longer have any React Native apps that I actively maintain, and so am thus detached from the community and need to focus my attention elsewhere. If you actively use this project and are interested in maintaining it, please reach out and let me know!
Touchable) or to a point on the screen, it will automatically find the best placement to accommodate the popover content.npm i react-native-popover-viewor
yarn add react-native-popover-viewFor the simplest usage, just pass your Touchable into the from prop. The Popover will automatically be shown when the Touchable is pressed.
import React from 'react';
import Popover from 'react-native-popover-view';
function App() {
return (
Press here to open popover!
)}>
This is the contents of the popover
);
}Note that if you pass an onPress or ref prop to the Touchable it will be overwritten.
For more advanced usage, pass in a function that returns any React element. You control which element the popover anchors on (using the sourceRef) and when the popover will be shown (using the showPopover callback). In this example, the Popover will appear to originate from the text inside the popover, and will only be shown when the Touchable is held down.
import React from 'react';
import Popover from 'react-native-popover-view';
function App() {
return (
(
Press here to open popover!
)}>
This is the contents of the popover
);
}You can control visibility yourself instead of letting the Popover manage it automatically by using the isVisible and onRequestClose prop. This would allow you to manually dismiss the Popover. onRequestClose is called when the user taps outside the Popover. If you want to force the user to tap a button inside the Popover to dismiss, you could omit onRequestClose and change the state manually.
import React, { useState, useEffect } from 'react';
import Popover from 'react-native-popover-view';
function App() {
const [showPopover, setShowPopover] = useState(false);
useEffect(() => {
setTimeout(() => setShowPopover(false), 2000);
}, []);
return (
setShowPopover(false)}
from={(
setShowPopover(true)}>
Press here to open popover!
)}>
This popover will be dismissed automatically after 2 seconds
);
}If you prefer to let the Popover handle opening from an element but need to close it programmatically, you can also use the exposed method requestClose to close it manually.
import React, { useRef } from 'react';
import Popover from 'react-native-popover-view';
function App() {
const popoverRef = useRef();
return (
Press here to open popover!
)}>
popoverRef.current.requestClose()}>
Tap to close me
);
}If you need even more control (e.g. having the Popover and Touchable in complete different parts of the node hierarchy), you can just pass in a normal ref.
import React, { useRef, useState } from 'react';
import Popover from 'react-native-popover-view';
function App() {
const touchable = useRef();
const [showPopover, setShowPopover] = useState(false);
return (
<>
setShowPopover(true)}>
Press here to open popover!
setShowPopover(false)}>
This is the contents of the popover
);
}If you already know the exact location of the place you want the Popover to anchor, you can create a Rect(x, y, width, height) object, and show from that Rect. Note that Rect(x, y, 0, 0) is equivalent to showing from the point (x, y).
import React, { useState } from 'react';
import Popover, { Rect } from 'react-native-popover-view';
function App() {
const [showPopover, setShowPopover] = useState(false);
return (
<>
setShowPopover(true)}>
Press here to open popover!
setShowPopover(false)}>
This is the contents of the popover
);
}If you just want the popover to be floating on the screen, not anchored to anything, you can omit the from prop altogether.
import React, { useState } from 'react';
import Popover from 'react-native-popover-view';
function App() {
const [showPopover, setShowPopover] = useState(false);
return (
<>
setShowPopover(true)}>
Press here to open popover!
setShowPopover(false)}>
This popover will stay centered on the screen, even when the device is rotated!
);
}Normally, the Popover will automatically pick the direction it pops out based on where it would fit best on the screen, even showing centered and unanchored if the contents would be compressed otherwise. If you would like to force a direction, you can pass in the placement prop.
import React from 'react';
import Popover, { PopoverPlacement } from 'react-native-popover-view';
function App() {
return (
Press here to open popover!
)}>
This is the contents of the popover
);
}Normally, the popover creates a background that dims the content behind it. You can also show a tooltip without fading the background. Read more about the available modes below. Note that when using TOOLTIP mode, you must control the visiblity manually (onRequestClose will never be called).
import React, { useRef, useState } from 'react';
import Popover, { PopoverMode, PopoverPlacement } from 'react-native-popover-view';
function App() {
const [showPopover, setShowPopover] = useState(false);
return (
setShowPopover(true)}>
Press here to open popover!
)}>
<>
This is the contents of the popover
setShowPopover(false)}>
Dismiss
);
}If you are not using functional components and hooks yet, you can still use class components in almost every case outlined above. Here is an example of using a class component and a ref, which is slightly different when using class components.
import React, { createRef } from 'react';
import Popover from 'react-native-popover-view';
class App extends React.Component {
constructor(props) {
super(props);
this.touchable = createRef();
this.state = {
showPopover: false
}
}
render() {
return (
<>
this.setState({ showPopover: true })}>
Press here to open popover!
this.setState({ showPopover: false })}>
This is the contents of the popover
);
}
}All props are optional
| Prop | Type | Default | Description |
|---|---|---|---|
| from | multiple | null | Popover source. See From section below. |
| isVisible | bool | false | Show/Hide the popover. Required if from is not a Touchable or function that uses showPopover call (see examples). If supplied, takes precedence regardless of from. |
| mode | string | 'rn-modal' | One of: 'rn-modal', 'js-modal', 'tooltip'. See Mode section below for details. |
| placement | string OR string list | 'auto' | How to position the popover, one of 'top', 'bottom', 'left', 'right', 'floating', or 'auto'. When 'auto' is specified, it will try to determine the best placement so that the popover is fully visible within displayArea. If an array of options is passed in, it will pick the first option that can accommodate the content. |
| offset | number | 0 | The amount to shift the popover away from the source. Does not apply if the popover is centered. |
| popoverStyle | object | The style of the popover itself. You can override the borderRadius, backgroundColor, or any other style prop for a View. |
|
| popoverShift | object | How much to shift the popover in each direction, as a multiplier. Object of shape { x: -1 to 1, y: -1 to 1 }, where both x and y are optional. -1 shifts the popover all the way to the left/top and 1 shifts it to the right/bottom. Currently only applies when placement is floating, but this will apply to all placements in a future version. |
|
| backgroundStyle | object | The style of the background view. Default is a black background with 0.5 opacity. | |
| arrowSize | object | { width: 16, height: 8 } |
The size of the arrow, as an object with width & height properties. The width of the arrow is the size of the arrow on the edge that touches the popover (base of isosceles triangle), while the height covers the distance from the popover to the source view, regardless of the placement of the popover. You can use { width: 0, height: 0 } to hide the arrow completely. |
| arrowShift | number | 0 | How much to shift the arrow to either side, as a multiplier. -1 will shift it all the way to the left (or top) corner of the source view, while 1 will shift all the way to the right (or bottom) corner. A value of 0.5 or -0.8 will shift it partly to one side. |
| onOpenStart | function | Callback to be fired when the open animation starts (before animation) | |
| onOpenComplete | function | Callback to be fired when the open animation ends (after animation) | |
| onRequestClose | function | Callback to be fired when the user taps outside the popover (on the background) or taps the system back button on Android | |
| onCloseStart | function | Callback to be fired when the popover starts closing (before animation) | |
| onCloseComplete | function | Callback to be fired when the popover is finished closing (after animation) | |
| onPositionChange | function | Callback to be fired when the popover position finishes moving position (after animation) | |
| animationConfig | object | An object contai |
No open issues yet, or sync has not completed.