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

Commit de5237f

Browse files
topherfangioThomasBurleson
authored andcommitted
fix(select): Position incorrect if selection scrolled.
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. Closes #6354
1 parent f9b0602 commit de5237f

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/components/select/select.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ function SelectProvider($$interimElementProvider) {
12761276
* Calculate the
12771277
*/
12781278
function calculateMenuPositions(scope, element, opts) {
1279-
var
1279+
var
12801280
containerNode = element[0],
12811281
targetNode = opts.target[0].children[0], // target the label
12821282
parentNode = $document[0].body,
@@ -1298,13 +1298,13 @@ function SelectProvider($$interimElementProvider) {
12981298
bottom: bounds.bottom - (targetRect.top + targetRect.height)
12991299
},
13001300
maxWidth = parentRect.width - SELECT_EDGE_MARGIN * 2,
1301-
isScrollable = contentNode.scrollHeight > contentNode.offsetHeight,
13021301
selectedNode = selectNode.querySelector('md-option[selected]'),
13031302
optionNodes = selectNode.getElementsByTagName('md-option'),
1304-
optgroupNodes = selectNode.getElementsByTagName('md-optgroup');
1303+
optgroupNodes = selectNode.getElementsByTagName('md-optgroup'),
1304+
isScrollable = calculateScrollable(element, contentNode),
1305+
centeredNode;
13051306

13061307
var loading = isPromiseLike(opts.loadingAsync);
1307-
var centeredNode;
13081308
if (!loading) {
13091309
// If a selected node, center around that
13101310
if (selectedNode) {
@@ -1386,7 +1386,7 @@ function SelectProvider($$interimElementProvider) {
13861386
} else {
13871387
left = (targetRect.left + centeredRect.left - centeredRect.paddingLeft) + 2;
13881388
top = Math.floor(targetRect.top + targetRect.height / 2 - centeredRect.height / 2 -
1389-
centeredRect.top + contentNode.scrollTop) + 4;
1389+
centeredRect.top + contentNode.scrollTop) + 2;
13901390

13911391
transformOrigin = (centeredRect.left + targetRect.width / 2) + 'px ' +
13921392
(centeredRect.top + centeredRect.height / 2 - contentNode.scrollTop) + 'px 0px';
@@ -1437,5 +1437,24 @@ function SelectProvider($$interimElementProvider) {
14371437
height: node.offsetHeight
14381438
} : {left: 0, top: 0, width: 0, height: 0};
14391439
}
1440+
1441+
function calculateScrollable(element, contentNode) {
1442+
var isScrollable = false;
1443+
1444+
try {
1445+
var oldDisplay = element[0].style.display;
1446+
1447+
// Set the element's display to block so that this calculation is correct
1448+
element[0].style.display = 'block';
1449+
1450+
isScrollable = contentNode.scrollHeight > contentNode.offsetHeight;
1451+
1452+
// Reset it back afterwards
1453+
element[0].style.display = oldDisplay;
1454+
} finally {
1455+
// Nothing to do
1456+
}
1457+
return isScrollable;
1458+
}
14401459
}
14411460

0 commit comments

Comments
 (0)