You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Pure JavaScript material
menu component for React
Native.
Install
npm install react-native-enhanced-popup-menu --save
or
yarn add react-native-enhanced-popup-menu
Usage example
importReactfrom'react';import{View,Text,ButtonasRNButton,StyleProp,ViewStyle,}from'react-native';import{Menu,MenuItem,MenuDivider,Position,}from'react-native-enhanced-popup-menu';declareconstglobal: {HermesInternal: null|{}};interfaceElementToStickProps{style?: StyleProp<ViewStyle>;}constElementToStick=React.forwardRef<View,ElementToStickProps>(({style},ref)=>{return(<Viewref={ref}style={[{padding: 16,borderColor: 'grey',borderWidth: 2,justifyContent: 'center',alignItems: 'center',},style,]}><Text>Element to which menu is sticked</Text></View>);},);interfaceButtonProps{title: string;style?: StyleProp<ViewStyle>;onPress: ()=>void;}constButton=({title, style, onPress}: ButtonProps)=>{return(<Viewstyle={style}><RNButtontitle={title}onPress={onPress}/></View>);};constApp=()=>{letelementRef=React.createRef<View>();letmenuRef: Menu|null=null;constsetMenuRef: (instance: Menu|null)=>void=(ref)=>(menuRef=ref);consthideMenu=()=>menuRef?.hide();constshowMenu=()=>{menuRef?.show(elementRef.current,Position.TOP_LEFT);};constonPress=()=>showMenu();return(<><Viewstyle={{width: '100%',height: '100%',alignItems: 'center',justifyContent: 'center',}}><ElementToStickref={elementRef}/><Buttonstyle={{position: 'absolute',bottom: 64}}title={'Press to show menu'}onPress={onPress}/><Menuref={setMenuRef}><MenuItemonPress={hideMenu}>Item 1</MenuItem><MenuItemonPress={hideMenu}>Item 2</MenuItem><MenuItemonPress={hideMenu}disabled>
Item 3
</MenuItem><MenuDivider/><MenuItemonPress={hideMenu}>Item 4</MenuItem></Menu></View></>);};exportdefaultApp;
Menu
Properties
name
description
type
default
children
Components rendered in menu (required)
Node
-
style
Menu style (optional)
Style
-
onHidden
Callback when menu has become hidden (optional)
Function
-
Methods
name
description
show()
Shows menu
hide()
Hides menu
show method parameters
name
description
type
default
ref
React reference to component (required)
Reference
-
stickTo
To which component border(s) we will stick menu (optional)
Position
Position.TOP_LEFT
extraOffset
Additional offset to stickTo (optional)
Object
{ left: 0, top: 0 }
computeOffset
Additional computed offset to stickTo (optional)
Function
{ left: 0, top: 0 }
stickTo parameter set relative base position of menu, it is always relative to component.
Position enum values
value
description
TOP_LEFT
Show the menu at the top left of the component
TOP_RIGHT
Show the menu at the top rigth of the component
TOP_CENTER
Show the menu at the top center of the component
BOTTOM_LEFT
Show the menu at the bottom left of the component
BOTTOM_RIGHT
Show the menu at the bottom right of the component
BOTTOM_CENTER
Show the menu at the bottom center of the component
extraOffset parameter set additional offset to base position of menu. It's used if you want customize stickTo, adding additional offset. extraOffset is an Object with the following allowed properties.
extraOffset can have duplicate properties (they all will be applied correctly)
{ top: 10, top: -5, top: 15 }
extraOffset values can be negative too
{ top: 10, top: -5, top: 15 }
computeOffset parameter is a callback function which will be called with position and size of component (computeOffset(left, top, width, height)). It's used if you want to customize stickTo dynamically and your computed offset depends on component position / size (for example to show menu centered you need to know component width).
computeOffset callback parameters
name
description
type
left
position of component on the horizontal axis (from top left window corner)
Number
top
position of component on the vertical axis (from top left window corner)
Number
width
width of component
Number
height
height of component
Number
computeOffset callback should return Object with the same properties as extraOffsetObject.
/* Example of computeOffset return value */{top: 10,left: 15,bottom: -3,right: 15,top: 12}
Notes
You can use extraOffset parameter or computeOffset or both parameters simultaneously. So, the final position of menu is calculated as basePosition + extraOffset + computeOffset(left, top, width, height)