Skip to content

Commit 8e1a359

Browse files
committed
fixed dropdown menu positioning bug #275
1 parent f9356b4 commit 8e1a359

File tree

5 files changed

+24
-24
lines changed

5 files changed

+24
-24
lines changed

examples/css-js/dropdowns.html

+13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<h1>Dropdowns</h1>
2525
<div class="mui-panel">
2626
<h2>CSS</h2>
27+
2728
<div class="mui-dropdown">
2829
<button class="mui-btn mui-btn--primary" data-mui-toggle="dropdown">
2930
Dropdown
@@ -36,6 +37,7 @@ <h2>CSS</h2>
3637
<li><a href="#">Option 4</a></li>
3738
</ul>
3839
</div>
40+
3941
<div class="mui-dropdown">
4042
<button class="mui-btn mui-btn--primary" data-mui-toggle="dropdown">
4143
Dropdown
@@ -48,6 +50,17 @@ <h2>CSS</h2>
4850
<li><a href="#">Option 4</a></li>
4951
</ul>
5052
</div>
53+
54+
<div class="mui-dropdown">
55+
<a data-mui-toggle="dropdown">Click me</a>
56+
<ul class="mui-dropdown__menu">
57+
<li><a href="#">Option 1</a></li>
58+
<li><a href="#">Option 2</a></li>
59+
<li><a href="#">Option 3</a></li>
60+
<li><a href="#">Option 4</a></li>
61+
</ul>
62+
</div>
63+
5164
</div>
5265
</div>
5366
</body>

src/angular/dropdown.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,13 @@ angular.module(moduleName, [])
3838
rightClass = 'mui-dropdown__menu--right',
3939
isUndef = angular.isUndefined,
4040
menuEl,
41-
buttonEl;
41+
buttonEl,
42+
marginTop;
4243

4344
// save references
4445
menuEl = angular.element(element[0].querySelector('.' + menuClass));
4546
buttonEl = angular.element(element[0].querySelector('.mui-btn'));
4647

47-
menuEl.css('margin-top', '-3px');
48-
4948
// handle is-open
5049
if (!isUndef(attrs.open)) scope.open = true;
5150

src/js/dropdown.js

-7
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,6 @@ function toggleDropdown(toggleEl) {
8585

8686
// method to open dropdown
8787
function openDropdownFn() {
88-
// position menu element below toggle button
89-
var wrapperRect = wrapperEl.getBoundingClientRect(),
90-
toggleRect = toggleEl.getBoundingClientRect();
91-
92-
var top = toggleRect.top - wrapperRect.top + toggleRect.height;
93-
jqLite.css(menuEl, 'top', top + 'px');
94-
9588
// add open class to wrapper
9689
jqLite.addClass(menuEl, openClass);
9790

src/react/dropdown.jsx

+4-14
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class Dropdown extends React.Component {
3030
super(props);
3131

3232
this.state = {
33-
opened: false,
34-
menuTop: 0
35-
}
33+
opened: false
34+
};
35+
3636
let cb = util.callback;
3737
this.selectCB = cb(this, 'select');
3838
this.onClickCB = cb(this, 'onClick');
@@ -97,16 +97,7 @@ class Dropdown extends React.Component {
9797
}
9898

9999
open() {
100-
// position menu element below toggle button
101-
let wrapperRect = this.wrapperElRef.getBoundingClientRect(),
102-
toggleRect;
103-
104-
toggleRect = this.buttonElRef.buttonElRef.getBoundingClientRect();
105-
106-
this.setState({
107-
opened: true,
108-
menuTop: toggleRect.top - wrapperRect.top + toggleRect.height
109-
});
100+
this.setState({ opened: true });
110101
}
111102

112103
close() {
@@ -175,7 +166,6 @@ class Dropdown extends React.Component {
175166
<ul
176167
ref={el => { this.menuElRef = el }}
177168
className={cs}
178-
style={{ top: this.state.menuTop }}
179169
onClick={this.selectCB}
180170
>
181171
{children}

src/sass/mui/_dropdown.scss

+5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@
9494
}
9595
}
9696

97+
// margin after mui-btn
98+
.mui-btn + .mui-dropdown__menu {
99+
margin-top: -4px;
100+
}
101+
97102
// right align
98103
.mui-dropdown__menu--right {
99104
left: auto;

0 commit comments

Comments
 (0)