Skip to content

Commit

Permalink
Merge pull request #2170 from mtlewis/only-scroll-when-necessary
Browse files Browse the repository at this point in the history
Add conditional scrolling into view of focused option
  • Loading branch information
JedWatson authored Nov 26, 2017
2 parents b5b2ff7 + 6eb31be commit 1e8b26b
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,19 @@ class Select extends React.Component {
if (this.menu && this.focused && this.state.isOpen && !this.hasScrolledToOption) {
let focusedOptionNode = findDOMNode(this.focused);
let menuNode = findDOMNode(this.menu);
menuNode.scrollTop = focusedOptionNode.offsetTop;

const scrollTop = menuNode.scrollTop;
const scrollBottom = scrollTop + menuNode.offsetHeight;
const optionTop = focusedOptionNode.offsetTop;
const optionBottom = optionTop + focusedOptionNode.offsetHeight;

if (scrollTop > optionTop || scrollBottom < optionBottom) {
menuNode.scrollTop = focusedOptionNode.offsetTop;
}

// We still set hasScrolledToOption to true even if we didn't
// actually need to scroll, as we've still confirmed that the
// option is in view.
this.hasScrolledToOption = true;
} else if (!this.state.isOpen) {
this.hasScrolledToOption = false;
Expand Down

0 comments on commit 1e8b26b

Please sign in to comment.