From 8609318fed89c0b6cb887ac1acf273256bc97354 Mon Sep 17 00:00:00 2001 From: Ryan Schmukler Date: Sun, 12 Jul 2015 18:03:13 -0400 Subject: [PATCH] fix(menu): fix menu positioning when using display:none items closes #3444 --- src/components/menu/menu-interim-element.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/components/menu/menu-interim-element.js b/src/components/menu/menu-interim-element.js index 36f4bf20aa1..f8d51266d8c 100644 --- a/src/components/menu/menu-interim-element.js +++ b/src/components/menu/menu-interim-element.js @@ -284,7 +284,12 @@ function MenuProvider($$interimElementProvider) { if (positionMode.top == 'target' || positionMode.left == 'target' || positionMode.left == 'target-right') { // TODO: Allow centering on an arbitrary node, for now center on first menu-item's child - alignTarget = openMenuNode.firstElementChild.firstElementChild || openMenuNode.firstElementChild; + alignTarget = firstVisibleChild(); + if (!alignTarget) { + throw Error('Error positioning menu. No visible children.'); + } + + alignTarget = alignTarget.firstElementChild || alignTarget; alignTarget = alignTarget.querySelector('[md-menu-align-target]') || alignTarget; alignTargetRect = alignTarget.getBoundingClientRect(); @@ -363,6 +368,18 @@ function MenuProvider($$interimElementProvider) { pos.top = Math.max(Math.min(pos.top, bounds.bottom - containerNode.offsetHeight), bounds.top); pos.left = Math.max(Math.min(pos.left, bounds.right - containerNode.offsetWidth), bounds.left); } + + /** + * Gets the first visible child in the openMenuNode + * Necessary incase menu nodes are being dynamically hidden + */ + function firstVisibleChild() { + for (var i = 0; i < openMenuNode.children.length; ++i) { + if (window.getComputedStyle(openMenuNode.children[i]).display != 'none') { + return openMenuNode.children[i]; + } + } + } } } }