Skip to content

Commit c135366

Browse files
authored
Auto scroll menu container if list extends beyond menu height on key … (#46)
* Auto scroll menu container if list extends beyond menu height on key up and key down. * Small syntax cleanup.
1 parent 16b19d1 commit c135366

File tree

6 files changed

+56
-5
lines changed

6 files changed

+56
-5
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "tribute",
33
"description": "Native ES6 @mentions",
4-
"version": "2.2.0",
4+
"version": "2.3.0",
55
"main": [
66
"dist/tribute.js",
77
"dist/tribute.css"

dist/tribute.js

Lines changed: 27 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/tribute.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/tribute.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tributejs",
3-
"version": "2.2.0",
3+
"version": "2.3.0",
44
"description": "Native ES6 @mentions",
55
"main": "dist/tribute.js",
66
"devDependencies": {

src/TributeEvents.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,43 @@ class TributeEvents {
213213
let lis = this.tribute.menu.querySelectorAll('li'),
214214
length = lis.length >>> 0
215215

216+
// get heights
217+
let menuFullHeight = this.getFullHeight(this.tribute.menu),
218+
liHeight = this.getFullHeight(lis[0])
219+
216220
if (index) this.tribute.menuSelected = index;
217221

218222
for (let i = 0; i < length; i++) {
219223
let li = lis[i]
220224
if (i === this.tribute.menuSelected) {
225+
let offset = liHeight * (i+1)
226+
let scrollTop = this.tribute.menu.scrollTop
227+
let totalScroll = scrollTop + menuFullHeight
228+
229+
if (offset > totalScroll) {
230+
this.tribute.menu.scrollTop += liHeight
231+
} else if (offset < totalScroll) {
232+
this.tribute.menu.scrollTop -= liHeight
233+
}
234+
221235
li.className = this.tribute.current.collection.selectClass
222236
} else {
223237
li.className = ''
224238
}
225239
}
226240
}
227241

242+
getFullHeight(elem, includeMargin) {
243+
let height = elem.getBoundingClientRect().height
244+
245+
if (includeMargin) {
246+
let style = elem.currentStyle || window.getComputedStyle(elem)
247+
return height + parseFloat(style.marginTop) + parseFloat(style.marginBottom)
248+
}
249+
250+
return height
251+
}
252+
228253
}
229254

230255
export default TributeEvents;

0 commit comments

Comments
 (0)