diff --git a/packages/grafana-ui/src/components/Select/SelectMenu.tsx b/packages/grafana-ui/src/components/Select/SelectMenu.tsx index 73fda63e4b6..9b4f6f5d19f 100644 --- a/packages/grafana-ui/src/components/Select/SelectMenu.tsx +++ b/packages/grafana-ui/src/components/Select/SelectMenu.tsx @@ -1,6 +1,6 @@ import { cx } from '@emotion/css'; import { max } from 'lodash'; -import { RefCallback, useEffect, useMemo, useRef } from 'react'; +import { RefCallback, useLayoutEffect, useMemo, useRef } from 'react'; import * as React from 'react'; import { MenuListProps } from 'react-select'; import { FixedSizeList as List } from 'react-window'; @@ -52,6 +52,7 @@ const VIRTUAL_LIST_WIDTH_EXTRA = 36; export const VirtualizedSelectMenu = ({ children, maxHeight, + innerRef: scrollRef, options, focusedOption, }: MenuListProps) => { @@ -71,7 +72,7 @@ export const VirtualizedSelectMenu = ({ const focusedIndex = flattenedOptions.findIndex( (option: SelectableValue) => option.value === focusedOption?.value ); - useEffect(() => { + useLayoutEffect(() => { listRef.current?.scrollToItem(focusedIndex); }, [focusedIndex]); @@ -111,6 +112,7 @@ export const VirtualizedSelectMenu = ({ return (