-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathMenu.js
More file actions
150 lines (128 loc) · 3.73 KB
/
Menu.js
File metadata and controls
150 lines (128 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/** @jsx React.DOM */
var React = require('react');
var cloneWithProps = React.addons.cloneWithProps;
var MenuTrigger = require('./MenuTrigger');
var MenuOptions = require('./MenuOptions');
var MenuOption = require('./MenuOption');
var uuid = require('../helpers/uuid');
var injectCSS = require('../helpers/injectCSS');
var buildClassName = require('../mixins/buildClassName');
var Menu = module.exports = React.createClass({
displayName: 'Menu',
statics: {
injectCSS: injectCSS
},
mixins: [buildClassName],
childContextTypes: {
id: React.PropTypes.string,
active: React.PropTypes.bool
},
getChildContext: function () {
return {
id: this.state.id,
active: this.state.active
};
},
getInitialState: function(){
return {
id: uuid(),
active: false,
selectedIndex: 0,
horizontalPlacement: 'right', // only 'right' || 'left'
verticalPlacement: 'bottom' // only 'top' || 'bottom'
};
},
closeMenu: function() {
this.setState({active: false}, this.focusTrigger);
},
focusTrigger: function() {
this.refs.trigger.getDOMNode().focus();
},
handleBlur: function(e) {
// give next element a tick to take focus
setTimeout(function() {
if (!this.getDOMNode().contains(document.activeElement) && this.state.active){
this.closeMenu();
}
}.bind(this), 1);
},
handleTriggerToggle: function() {
this.setState({active: !this.state.active}, this.afterTriggerToggle);
},
afterTriggerToggle: function() {
if (this.state.active) {
this.refs.options.focusOption(0);
this.updatePositioning();
}
},
updatePositioning: function() {
var triggerRect = this.refs.trigger.getDOMNode().getBoundingClientRect();
var optionsRect = this.refs.options.getDOMNode().getBoundingClientRect();
var positionState = {};
// horizontal = left if it wont fit on left side
if (triggerRect.left + optionsRect.width > window.innerWidth) {
positionState.horizontalPlacement = 'left';
} else {
positionState.horizontalPlacement = 'right';
}
if (triggerRect.top + optionsRect.height > window.innerHeight) {
positionState.verticalPlacement = 'top';
} else {
positionState.verticalPlacement = 'bottom';
}
this.setState(positionState);
},
handleKeys: function(e) {
if (e.key === 'Escape') {
this.closeMenu();
}
},
verifyTwoChildren: function() {
var ok = (React.Children.count(this.props.children) === 2);
if (!ok)
throw 'react-menu can only take two children, a MenuTrigger, and a MenuOptions';
return ok;
},
renderTrigger: function() {
var trigger;
if(this.verifyTwoChildren()) {
React.Children.forEach(this.props.children, function(child){
if (child === MenuTrigger) {
trigger = cloneWithProps(child, {
ref: 'trigger',
onToggleActive: this.handleTriggerToggle
});
}
}.bind(this));
}
return trigger;
},
renderMenuOptions: function() {
var options;
if(this.verifyTwoChildren()) {
React.Children.forEach(this.props.children, function(child){
if (child === MenuOptions) {
options = cloneWithProps(child, {
ref: 'options',
horizontalPlacement: this.state.horizontalPlacement,
verticalPlacement: this.state.verticalPlacement,
onSelectionMade: this.closeMenu
});
}
}.bind(this));
}
return options;
},
render: function() {
return (
<div
className={this.buildClassName('Menu')}
onKeyDown={this.handleKeys}
onBlur={this.handleBlur}
>
{this.renderTrigger()}
{this.renderMenuOptions()}
</div>
)
}
});