Skip to content

Instantly share code, notes, and snippets.

@kapobajza
Last active November 26, 2023 09:10
Show Gist options
  • Save kapobajza/86e604b69bd27580f31cba50bbbdf10d to your computer and use it in GitHub Desktop.
Save kapobajza/86e604b69bd27580f31cba50bbbdf10d to your computer and use it in GitHub Desktop.
RN Popover component sample
import { Button, StyleSheet, View } from "react-native";
import React, { useState } from "react";
import Popover from "./Popover";
const PopoverUsageSample = () => {
const [popoverVisible, setPopoverVisible] = useState(false);
return (
<Popover
visible={popoverVisible}
setVisible={setPopoverVisible}
PopoverContent={
<View style={styles.popoverContent}>
<Button
title="Close popover"
onPress={() => setPopoverVisible(false)}
/>
</View>
}
>
<Button title="Open popover" onPress={() => setPopoverVisible(true)} />
</Popover>
);
};
export default PopoverUsageSample;
const styles = StyleSheet.create({
popoverContent: {
alignItems: "center",
height: 200,
backgroundColor: "white",
justifyContent: "center",
},
});
import { View, StyleSheet, Pressable } from "react-native";
import { Dispatch, ReactElement, ReactNode, SetStateAction } from "react";
const Popover = ({
visible,
setVisible,
children,
PopoverContent,
}: {
visible: boolean;
setVisible: Dispatch<SetStateAction<boolean>>;
children: ReactNode;
PopoverContent: ReactElement;
}) => {
const onClosePress = () => setVisible(false);
return (
<View style={styles.container}>
{children}
{visible ? (
<View style={styles.popover}>
<Pressable
style={StyleSheet.absoluteFillObject}
onPress={onClosePress}
/>
{PopoverContent}
</View>
) : null}
</View>
);
};
export default Popover;
const styles = StyleSheet.create({
popover: {
justifyContent: "center",
backgroundColor: "rgba(0,0,0,0.5)",
padding: 16,
flex: 1,
...StyleSheet.absoluteFillObject,
},
container: {
flex: 1,
},
popoverContent: {
alignItems: "center",
height: 200,
backgroundColor: "white",
justifyContent: "center",
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment