react-dd-menu-portal
A fork of the react-dd-menu project. With (hard-coded) support for React portals. Undocumented and very dirty implementation. Use at own risk.
Live Example: React Dropdown Menu
NOTE: I am no longer actively developing this project since it has met most of the initial goals and I will be spending most of my time developing the bigger project react-md. I am more than happy to keep review/accepting pull requests with new features/bugfixes though.
Installation
$ npm install -S react-dd-menu \ # If you haven't installed already react \ react-dom \ react-transition-group
Props
DropdownMenu
static MENU_SIZES = 'sm' 'md' 'lg' 'xl' static ALIGNMENTS = 'center' 'right' 'left' static propTypes = isOpen: PropTypesboolisRequired close: PropTypesfuncisRequired toggle: PropTypesnodeisRequired children: PropTypesnode inverse: PropTypesbool align: PropTypes animAlign: PropTypes textAlign: PropTypes menuAlign: PropTypes className: PropTypesstring size: PropTypes upwards: PropTypesbool animate: PropTypesbool enterTimeout: PropTypesnumber leaveTimeout: PropTypesnumber closeOnInsideClick: PropTypesbool closeOnOutsideClick: PropTypesbool static defaultProps = inverse: false align: 'center' animAlign: null textAlign: null menuAlign: null className: null size: null upwards: false animate: true enterTimeout: 150 leaveTimeout: 150 closeOnInsideClick: true closeOnOutsideClick: true
isOpen
- Boolean for telling if the menu is open. This was passed in as a prop instead of having the component's own state so you can decide when to close the menu on your own.close
- a function to call that turns theisOpen
boolean to falsetoggle
- any renderable item that will be used to toggle the menu open. So normally a button or any other content.inverse
- boolean if it is an inversed color menualign
- the alignment for the animation, text, and menu if the specific props are not given. Defaults tocenter
animAlign
- the alignment/direction that the menu will appear fromtextAlign
- the alignment of each list item's textmenuAlign
- the alignment of the menu to thetoggle
elementsize
- the size of the menu. Defaults to auto size.className
- any additional css classes to add the the dropdown menu container. (.dd-menu
)upwards
- boolean if the menu should go upwards. Defaults tofalse
animate
- boolean if the menu should animate on open and close. Defaults totrue
enterTimeout
- the amount of time in ms to end the CSSTransitionGroup. Defaults to150
leaveTimeout
- the amount of time in ms to end the CSSTransitionGroup. Defaults to150
closeOnInsideClick
- a boolean if the menu should close when you click inside the menu. Defaults totrue
closeOnOutsideClick
- a boolean if the menu should close when you click elsewhere on the page. Defaults totrue
NestedDropdownMenu
static propTypes = toggle: PropTypesnodeisRequired children: PropTypesnode nested: PropTypes animate: PropTypesbool direction: PropTypes upwards: PropTypesbool delay: PropTypesnumber enterTimeout: PropTypesnumber leaveTimeout: PropTypesnumber openOnMouseover: PropTypesbool static defaultProps = nested: 'reverse' animate: false direction: 'right' upwards: false delay: 500 enterTimeout: 150 leaveTimeout: 150 openOnMouseover: true
toggle
- an renderable item that will open the nested menu on hover. It gets wrapped in ali
element, so it might be best to have a button or a link tag.nested
- the nested menu's expansion direction. The default case should hopefully be the only used case.- Inherit - If the main dropdown menu is aligned left, the nested menu will appear to the left as well.
- Reverse - If the main dropdown menu is aligned left, the nested menu will appear to the right.
- Left - Force the menu to appear to the left of the menu.
- Right - Force the menu to appear to the right of the menu.
animate
- boolean if the nested menu should animate when appearing. Defaults tofalse
direction
- The animation direction.upwards
- boolean if the nested menu should render upwards. Defaults tofalse
delay
- A number in ms to allow the mouse to be off of the dropdown menu to close it. Defaults to500ms
enterTimeout
- the amount of time in ms to end the CSSTransitionGroup. Defaults to150
leaveTimeout
- the amount of time in ms to end the CSSTransitionGroup. Defaults to150
openOnMouseover
- boolean if the menu can be opened/close by mouseover/mouseleave events
Styling
In the dist
folder, there is a react-dd-menu.css
and a react-dd-menu.min.css
with the default css stylings. If you have SASS, the source is located in src/scss
.
If you don't want the default css or to edit the default, the layout is this:
.dd-menu
| -- toggle component
| -- .dd-menu-items
| | -- ul
| | | [role="separator"], .separator
The separator can be any element with a classname of .separator
or any element with a role of separator (or both). To get the best styling, it should probably be applied to an li
element.
Usage
;; Component { super; thisstate = isMenuOpen: false ; thisclick = thisclick; thistoggle = thistoggle; thisclose = thisclose; } { this; } { this; } { console; } { const menuOptions = isOpen: thisstateisMenuOpen close: thisclose toggle: <button type="button" onClick=thistoggle>Click me!</button> align: 'right' ; return <DropdownMenu ...menuOptions> <li><a href="#">Example 1</a></li> <li><button type="button" onClick=thisclick>Example 2</button></li> </DropdownMenu> ; }
or..
var React = ;var DropdownMenu = ; var Example = React;
Nested Menu Example
'use strict'; ;; Component state = isMenuOpen: false ; { this; } { this; }; { console; }; { const menuOptions = isOpen: thisstateisMenuOpen close: thisclose toggle: <button type="button" onClick=thistoggle>Click me!</button> align: 'right' ; const nestedProps = toggle: <a href="#">Hover me for Nested Menu!</a> animate: true ; return <DropdownMenu ...menuOptions> <li><a href="#">Example 1</a></li> <li><button type="button" onClick=thisclick>Example 2</button></li> <li role="separator" className="separator" /> <NestedDropdownMenu ...nestedProps> <li><a href="#">I am in a Nested Menu!</a></li> </NestedDropdownMenu> </DropdownMenu> ; }
Contributors/Local Changes
To rebuild the source:
$ npm run build
This will output all the css and js files into ./dist
;
Versions
- 0.0.1 - add React portal support