@@ -28,7 +28,7 @@ class LanguageMenu extends React.PureComponent {
28
28
componentDidUpdate ( prevProps ) {
29
29
// If the submenu has been toggled open, try scrolling the selected option into view.
30
30
if ( ! prevProps . menuOpen && this . props . menuOpen && this . selectedRef ) {
31
- this . selectedRef . scrollIntoView ( { block : 'center' } ) ;
31
+ this . scrollSelectedIntoView ( ) ;
32
32
}
33
33
}
34
34
@@ -39,10 +39,25 @@ class LanguageMenu extends React.PureComponent {
39
39
handleMouseOver ( ) {
40
40
// If we are using hover rather than clicks for submenus, scroll the selected option into view
41
41
if ( ! this . props . menuOpen && this . selectedRef ) {
42
- this . selectedRef . scrollIntoView ( { block : 'center' } ) ;
42
+ this . scrollSelectedIntoView ( ) ;
43
43
}
44
44
}
45
45
46
+ scrollSelectedIntoView ( ) {
47
+ // the native scrollIntoView() scrolls the entire page when used outside the editor,
48
+ // so we do this manually instead.
49
+ // selectedRef is the checkmark <img>, its parent is a <div> from <MenuItem>, then a <div> from <SubMenu>
50
+
51
+ const menuItem = this . selectedRef . parentNode ;
52
+ const scrollContainer = menuItem . parentNode ;
53
+
54
+ const itemHeight = menuItem . offsetHeight ;
55
+ const selectedItemPosition = menuItem . offsetTop ;
56
+ const visibleHeight = scrollContainer . offsetHeight ;
57
+
58
+ scrollContainer . scrollTop = selectedItemPosition - ( visibleHeight / 2 ) + ( itemHeight / 2 ) ;
59
+ }
60
+
46
61
render ( ) {
47
62
return (
48
63
< MenuItem
0 commit comments