Skip to content

Commit

Permalink
Auto scroll menu container if list extends beyond menu height on key … (
Browse files Browse the repository at this point in the history
#46)

* Auto scroll menu container if list extends beyond menu height on key up and key down.

* Small syntax cleanup.
  • Loading branch information
mrsweaters authored Dec 28, 2016
1 parent 16b19d1 commit c135366
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tribute",
"description": "Native ES6 @mentions",
"version": "2.2.0",
"version": "2.3.0",
"main": [
"dist/tribute.js",
"dist/tribute.css"
Expand Down
28 changes: 27 additions & 1 deletion dist/tribute.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/tribute.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/tribute.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tributejs",
"version": "2.2.0",
"version": "2.3.0",
"description": "Native ES6 @mentions",
"main": "dist/tribute.js",
"devDependencies": {
Expand Down
25 changes: 25 additions & 0 deletions src/TributeEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,43 @@ class TributeEvents {
let lis = this.tribute.menu.querySelectorAll('li'),
length = lis.length >>> 0

// get heights
let menuFullHeight = this.getFullHeight(this.tribute.menu),
liHeight = this.getFullHeight(lis[0])

if (index) this.tribute.menuSelected = index;

for (let i = 0; i < length; i++) {
let li = lis[i]
if (i === this.tribute.menuSelected) {
let offset = liHeight * (i+1)
let scrollTop = this.tribute.menu.scrollTop
let totalScroll = scrollTop + menuFullHeight

if (offset > totalScroll) {
this.tribute.menu.scrollTop += liHeight
} else if (offset < totalScroll) {
this.tribute.menu.scrollTop -= liHeight
}

li.className = this.tribute.current.collection.selectClass
} else {
li.className = ''
}
}
}

getFullHeight(elem, includeMargin) {
let height = elem.getBoundingClientRect().height

if (includeMargin) {
let style = elem.currentStyle || window.getComputedStyle(elem)
return height + parseFloat(style.marginTop) + parseFloat(style.marginBottom)
}

return height
}

}

export default TributeEvents;

0 comments on commit c135366

Please sign in to comment.