1.0.4 • Published 1 year ago

@brnho/react-native-context-menu v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

react-native-context-menu

React Native implementation of iOS context menu - replicates the full set of animations (bounce, haptics, background shrink and blur, etc.), and provides extensive support for customization of menu items and animations.

https://user-images.githubusercontent.com/17365107/211397217-63fa501c-481e-4434-9e52-1e8b491f21c9.mov

Setup

The library is available on npm. Install with npm i @brnho/react-native-context-menu or yarn add @brnho/react-native-context-menu.

Usage

First, wrap the root view of your app with ContextMenuProvider. Then, wrap the component that you want to provide a context menu to with ContextMenuContainer.

import { ContextMenuProvider, ContextMenuContainer } from "@brnho/react-native-context-menu";

export default function App() {
  return (
    <ContextMenuProvider>
      <View style={styles.container}>
        <ContextMenuContainer menuItems={menuItems}>
          <MyExampleComponent />
        </ContextMenuContainer
      </View>
    </ContextMenuProvider>
  );
}

You can customize the context menu via the menuItems prop of ContextMenuContainer. For example:

const menuItems = [
  { text: "Action", isTitle: true },
  {
    text: "Edit",
    icon: {
      type: "Feather",
      name: "edit",
      size: 18,
    },
    onPress: () => {
      alert("Edit pressed");
    },
  },
  {
    text: "Delete",
    icon: {
      type: "Feather",
      name: "trash",
      size: 18,
    },
    withSeparator: true,
    isDestructive: true,
    onPress: () => {
      alert("Delete pressed");
    },
  },
];

Tips/Caveats

  • For the positioning of the animations to work properly, do not apply any margins to the target component. Instead, apply margins directly to the ContextMenuContainer wrapper via the margin props (see Props section).

  • If your target component has a nonzero border radius, then apply the same amount of border radius to ContextMenuContainer via the borderRadius prop. This ensures that ContextMenuContainer precisely covers the component.

  • If you are applying a context menu to an item in a Flatlist, to prevent the user from being able to scroll the list during a long press event, set the setScrollEnabled prop of ContextMenuProvider equal to a state function. For more details, refer to the following example:

export default function App() {
  const [scrollEnabled, setScrollEnabled] = useState(true);
  return (
    <ContextMenuProvider setScrollEnabled={setScrollEnabled}>
      <View style={styles.container}>
        <FlatList
          scrollEnabled={scrollEnabled}
          data={data}
          renderItem={({ item }) => <ListItem item={item} />}
          keyExtractor={(_, index) => index}
        />
      </View>
    </ContextMenuProvider>
  );
}

Props

ContextMenuProvider Props

NameTypeDefaultRequiredDescription
setScrollEnabledfunctionnullNoFunction executed on long press to prevent user from being able to scroll
SCREEN_SHRINK_FACTORnumber0.97NoAmount by which to shrink the background when the component pops out
EXPAND_FACTORnumber1.05NoAmount by which to expand the component when it pops out
FADE_SPEEDnumber200NoAnimation speed in MS of the component "un-popping"
APPEAR_SPEEDnumber200NoAnimation speed in MS of the component popping out
BLUR_INTENSITYnumber30NoDegree to which the background is blurred when the component pops out (value is in range 0, 100)
MENU_ITEM_HEIGHTnumber40NoHeight of a single context menu item
DIVIDER_HEIGHTnumber1NoHeight of the dividers separating context menu items
MENU_WIDTHnumber200NoWidth of a single context menu item
MENU_MARGINnumber7NoMargin between the component and the context menu

ContextMenuContainer Props

NameTypeRequiredDescription
marginTopnumberNoAmount of margin to apply to the top of the component
marginRightnumberNoAmount of margin to apply to the right of the component
marginBottomnumberNoAmount of margin to apply to the bottom of the component
marginLeftnumberNoAmount of margin to apply to the left of the component
borderRadiusnumberNoRadius of component corners
menuItemsarrayYesSee below for additional details

menuItems (See Usage section for an example)

menuItems is an array of objects, each representing an item of the context menu. The fields of a menu item object are as follows:

NameTypeRequiredDescription
textstringYesMenu item text
iconobjectNoSee below for additional details
onPressfunctionNoFunction to be called when the menu item is pressed
isTitlestringNoApplies different styling to a menu item designated as the title
isDestructivebooleanNoColors text and icon of item in red

The icon prop is a object with the following fields:

NameTypeRequiredDescription
typestringYesIcon family (must be one of 'AntDesign', 'Entypo', 'Feather', 'FontAwesome5', 'Ionicons', 'MaterialIcons')
namestringYesName of icon
sizenumberYesSize of icon

(See https://icons.expo.fyi/ for a list of icon types and names)