Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix(select): Position incorrect if selection scrolled.
Browse files Browse the repository at this point in the history
If the currently selected object scrolled into view because it
was further down in the list, the position of the menu was
incorrect upon opening because the `scrollTop` of the `contentNode`
was always `0` due to the ARIA changes which set it to
`display: none` upon closing.

Fix by modifying the display to `block` when we run the `isScrollable`
check and back afterward (usually `display: none` but we account for
other options).

Also fixes a slight `2px` positioning issue which made the menu not
perfectly line up with the underlying selection.

Fixes #6190.
  • Loading branch information
topherfangio committed Dec 17, 2015
1 parent b8a9624 commit b8e92d7
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/components/select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ function SelectProvider($$interimElementProvider) {
* Calculate the
*/
function calculateMenuPositions(scope, element, opts) {
var
var
containerNode = element[0],
targetNode = opts.target[0].children[0], // target the label
parentNode = $document[0].body,
Expand All @@ -1298,11 +1298,12 @@ function SelectProvider($$interimElementProvider) {
bottom: bounds.bottom - (targetRect.top + targetRect.height)
},
maxWidth = parentRect.width - SELECT_EDGE_MARGIN * 2,
isScrollable = contentNode.scrollHeight > contentNode.offsetHeight,
selectedNode = selectNode.querySelector('md-option[selected]'),
optionNodes = selectNode.getElementsByTagName('md-option'),
optgroupNodes = selectNode.getElementsByTagName('md-optgroup');

var isScrollable = calculateScrollable(element, contentNode);

var loading = isPromiseLike(opts.loadingAsync);
var centeredNode;
if (!loading) {
Expand Down Expand Up @@ -1386,7 +1387,7 @@ function SelectProvider($$interimElementProvider) {
} else {
left = (targetRect.left + centeredRect.left - centeredRect.paddingLeft) + 2;
top = Math.floor(targetRect.top + targetRect.height / 2 - centeredRect.height / 2 -
centeredRect.top + contentNode.scrollTop) + 4;
centeredRect.top + contentNode.scrollTop) + 2;

transformOrigin = (centeredRect.left + targetRect.width / 2) + 'px ' +
(centeredRect.top + centeredRect.height / 2 - contentNode.scrollTop) + 'px 0px';
Expand Down Expand Up @@ -1437,5 +1438,24 @@ function SelectProvider($$interimElementProvider) {
height: node.offsetHeight
} : {left: 0, top: 0, width: 0, height: 0};
}

function calculateScrollable(element, contentNode) {
var isScrollable = false;

try {
var oldDisplay = element[0].style.display;

// Set the element's display to block so that this calculation is correct
element[0].style.display = 'block';

isScrollable = contentNode.scrollHeight > contentNode.offsetHeight;

// Reset it back afterwards
element[0].style.display = oldDisplay;
} finally {
// Nothing to do
}
return isScrollable;
}
}

0 comments on commit b8e92d7

Please sign in to comment.