2016-05-27 13:19:48 +00:00
# API
## MenuContext
It provides methods to handle popup menus imperatively. The same methods are exposed to the child context with name `menuActions` .
2016-10-10 15:33:17 +00:00
**Note:** It is important that `<MenuContext />` is on the top of the component hierarchy (e.g. `ScrollView` should be inside of `MenuContext` ) and wraps all `<Menu />` components.
2016-06-06 14:58:21 +00:00
This is needed in order to solve z-index issues.
2016-10-10 15:33:17 +00:00
The only known exception is when you use [Modal ](https://facebook.github.io/react-native/docs/modal.html ) - you need to place (additional) 'MenuContext' inside of 'Modal' (see our [ModalExample ](../examples/ModalExample.js ))
2016-06-06 14:58:21 +00:00
2016-05-27 13:19:48 +00:00
### Methods, `menuActions` context
| Method Name | Arguments | Notes
|---|---|---|
|`openMenu`|`name`|Opens menu by name|
|`toggleMenu`|`name`|Toggle menu by name|
|`closeMenu`||Closes currently opened menu|
|`isMenuOpen`||Returns `true` if any menu is open|
## Menu
Root menu component defining menu name and providing menu events.
### Options
| Option | Type | Opt/Required | Default | Note |
|---|---|---|---|---|
|`name`|`String`|Optional|`auto-generated`|Unique name of menu|
|`opened`|`Boolean`|Optional||Declaratively states if menu is opened. When this prop is provided, menu is controlled and imperative API won't work.|
2016-06-15 10:08:13 +00:00
|`renderer`|`Function`|Optional|`ContextMenu`|Defines position, animation and basic menu styles. See [renderers section ](#renderers ) for more details|
2016-05-27 13:19:48 +00:00
### Events
2016-05-30 07:17:00 +00:00
| Event Name | Arguments | Notes |
2016-05-27 13:19:48 +00:00
|---|---|---|
|`onSelect`|`optionValue`|Triggered when menu option is selected. When event handler returns `false` , the popup menu remains open|
|`onOpen`||Triggered when menu is opened|
|`onClose`||Triggered when menu is closed|
|`onBackdropPress`||Triggered when user press backdrop (outside of the opened menu)|
### Static Properties
| Property name | Type | Opt/Required | Default | Note |
|---|---|---|---|---|
|`debug`|`Boolean`|Optional|`false`|This property enables debug logs|
2016-06-15 10:08:13 +00:00
### Static Functions
| Function name | Arguments | Returns | Note |
|---|---|---|---|
|`setDefaultRenderer`| `Function` | | Sets new default renderer. See [renderers section ](#renderers ) for more details |
2016-05-27 13:19:48 +00:00
## MenuTrigger
It defines position where the popup menu will be rendered.
Menu can by opened by clicking on `<MenuTrigger />` or by calling context methods.
2016-06-06 14:58:21 +00:00
**Note:** It is necessary that `<MenuTrigger />` is a direct child of `<Menu />` .
2016-05-27 13:19:48 +00:00
### Options
| Option | Type | Opt/Required | Default | Note |
|---|---|---|---|---|
|`disabled`|`Boolean`|Optional|`false`|Indicates if trigger can be pressed|
|`text`|`String`|Optional||Text to be rendered. When this prop is provided, trigger's children won't be rendered|
2016-06-06 14:58:21 +00:00
|`customStyles`|`Object`|Optional||Object defining wrapper, touchable and text styles|
2016-05-27 13:19:48 +00:00
### Events
2016-05-30 07:17:00 +00:00
| Event Name | Arguments | Notes |
2016-05-27 13:19:48 +00:00
|---|---|---|
|`onPress`||Triggered when trigger is pressed|
2016-06-06 14:58:21 +00:00
### Custom styles
To style `<MenuTrigger />` component you can pass `customStyles` object prop with following keys:
| Object key | Type | Notes |
|---|---|---|
|`triggerWrapper`|`Object`|Style of wrapping `View` component|
|`triggerTouchable`|`Object`|Style props of `TouchableHighlight` . Supported keys: `activeOpacity` , `underlayColor` |
|`triggerText`|`Object`|Style of `Text` component (used when `text` shorthand option is defined)|
2016-06-06 15:03:08 +00:00
See more in custom styling [example ](../examples/StylingExample.js ).
2016-05-27 13:19:48 +00:00
## MenuOptions
This component wrapps all menu options.
**Note:** It is necessary that `<MenuOptions />` is a direct child of `<Menu />` .
### Options
| Option | Type | Opt/Required | Default | Note |
|---|---|---|---|---|
2016-06-06 14:58:21 +00:00
|`optionsContainerStyle`|`Style`|Optional||Custom styles for options container. Note: this option is deprecated, use `customStyles` option instead|
2016-05-27 13:19:48 +00:00
|`renderOptionsContainer`|`Func`|Optional|`options => options`|Custom render function for `<MenuOptions />` . It takes options component as argument and returns component. E.g.: `options => <SomeCustomContainer>{options}</SomeCustomContainer>` |
2016-06-06 14:58:21 +00:00
|`customStyles`|`Object`|Optional||Object defining wrapper, touchable and text styles|
### Custom styles
To style `<MenuOptions />` and it's `<MenuOption />` components you can pass `customStyles` object prop with following keys:
| Object key | Type | Notes |
|---|---|---|
2016-07-27 14:08:20 +00:00
|`optionsWrapper`|`Object`|Style of `View` component wrapping all options|
2016-06-06 14:58:21 +00:00
|`optionsContainer`|`Object`|Style of wrapping `AnimatedView` component|
2016-07-27 14:08:20 +00:00
|`optionWrapper`|`Object`|Style of `View` component wrapping single option|
2016-06-06 14:58:21 +00:00
|`optionTouchable`|`Object`|Style props of `TouchableHighlight` . Supported keys: `activeOpacity` , `underlayColor` |
|`optionText`|`Object`|Style of `Text` component (when `text` shorthand option is defined)|
**Note:** `optionWrapper` , `optionTouchable` and `optionText` styles of particular menu option can be overriden by `customStyles` prop of `<MenuOption />` component.
2016-06-06 15:03:08 +00:00
See more in custom styling [example ](../examples/StylingExample.js ).
2016-05-27 13:19:48 +00:00
## MenuOption
Wrapper component of menu option.
2016-05-27 13:25:45 +00:00
### Options
2016-05-27 13:19:48 +00:00
| Option | Type | Opt/Required | Default | Note |
|---|---|---|---|---|
|`value`|`Any`|Optional||Value of option|
|`text`|`String`|Optional||Text to be rendered. When this prop is provided, option's children won't be rendered|
|`disabled`|`Boolean`|Optional|`false`|Indicates if option can be pressed|
2016-06-06 14:58:21 +00:00
|`customStyles`|`Object`|Optional||Object defining wrapper, touchable and text styles|
2016-05-27 13:19:48 +00:00
### Events
2016-05-30 07:17:00 +00:00
| Event Name | Arguments | Notes |
2016-05-27 13:19:48 +00:00
|---|---|---|
|`onSelect`||Triggered when option is selected. When event handler returns `false` , the popup menu remains open. Note: If this event handler is defined, it suppress `onSelect` handler of `<Menu />` |
2016-06-06 14:58:21 +00:00
### Custom styles
To style `<MenuOption />` component you can pass `customStyles` object prop with following keys:
| Object key | Type | Notes |
|---|---|---|
|`optionWrapper`|`Object`|Style of wrapping `View` component.|
|`optionTouchable`|`Object`|Style props of `TouchableHighlight` . Supported keys: `activeOpacity` , `underlayColor` |
|`optionText`|`Object`|Style of `Text` component (when `text` shorthand option is defined)|
2016-06-06 15:03:08 +00:00
See more in custom styling [example ](../examples/StylingExample.js ).
2016-06-06 14:58:21 +00:00
2016-05-31 11:14:24 +00:00
## renderers
Renderers are react components which wraps `MenuOptions` and are responsible for menu position and animation.
2016-06-15 10:08:13 +00:00
The `renderers` module provides following renderers
* `ContextMenu` (default) - opens (animated) context menu over the trigger position. The `ContextMenu.computePosition` exports function for position calculation in case you would like to implement your own renderer (without special position calculation).
* `NotAnimatedContextMenu` - same as ContextMenu but without any animation.
* `SlideInMenu` - slides in the menu from the bottom of the screen.
It is possible to extend menu and use custom renderer (see implementation of existing renderers or [extension guide ](extensions.md )).
2016-05-31 11:14:24 +00:00
2016-06-15 10:08:13 +00:00
**Note:** If you only need to add styles or wrap `MenuOptions` with your own component, use `customStyles` or `renderOptionsContainer` options of `MenuOptions` instead.