Skip to content

Instantly share code, notes, and snippets.

@dennisslimmers
Last active April 1, 2024 19:51
Show Gist options
  • Save dennisslimmers/c7c257f8e214bc52ff530463aa2e16dc to your computer and use it in GitHub Desktop.
Save dennisslimmers/c7c257f8e214bc52ff530463aa2e16dc to your computer and use it in GitHub Desktop.
This is an example how to properly use the 'Menu' element from React Native Paper
import React, { useState } from 'react';
import Icon from 'react-native-vector-icons/FontAwesome5';
import {
View,
StyleSheet,
} from 'react-native';
import {
Title,
Paragraph,
Menu,
} from 'react-native-paper';
const FoodScheduleCard = ({ icon, title, time }) => {
const [showMenu, setShowMenu] = useState(false);
const [menuAnchor, setMenuAnchor] = useState({ x: 0, y: 0 })
/**
* Summary. Update showMenu state to true
*
* @return {Void}
*/
const openMenu = () => setShowMenu(true);
/**
* Summary. Update showMenu state to false
*
* @return {Void}
*/
const closeMenu = () => setShowMenu(false);
/**
* Summary. Retrieve the coords from the pressed icon. The menuAnchor
* state will be updated with these values and the menu will be shown
*
* @param {Object} event: GestureResponderEvent
* @return {Void}
*/
const onIconPress = (event) => {
const { nativeEvent } = event;
const anchor = {
x: nativeEvent.pageX,
y: nativeEvent.pageY,
};
setMenuAnchor(anchor);
openMenu();
}
return(
<View style={styles.card}>
<View style={styles.cardIcon}>
<Icon
name={icon}
size={24}
color="red"
style={styles.icon}
/>
</View>
<View style={styles.cardContent}>
<Title>{title}</Title>
<Paragraph>{time}</Paragraph>
</View>
<Icon
name="ellipsis-v"
size={24}
color="gray"
style={styles.menuIcon}
onPress={onIconPress}
/>
<Menu
visible={showMenu}
onDismiss={closeMenu}
anchor={menuAnchor}
>
<Menu.Item onPress={() => {}} title="View" />
<Menu.Item onPress={() => {}} title="Edit" />
<Menu.Item onPress={() => {}} title="Delete" />
</Menu>
</View>
)
};
const styles = StyleSheet.create({
icon: {
backgroundColor: 'red',
color: '#fff',
width: 45,
padding: 10,
borderRadius: 50,
textAlign: 'center',
},
menuIcon: {
marginLeft: 290,
marginTop: 26,
},
card: {
height: 80,
marginBottom: 10,
backgroundColor: '#fff',
borderRadius: 5,
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 1,
},
shadowOpacity: 0.18,
shadowRadius: 1.00,
elevation: 0.5,
},
cardContent: {
position: 'absolute',
left: 85,
bottom: 13
},
cardIcon: {
position: 'absolute',
top: 16,
left: 20
}
});
export default FoodScheduleCard;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment