mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Navigation: Improve docked auto scroll behaviour (#77117)
ensure expand works on any location change, only scroll when docked
This commit is contained in:
parent
bc9fab6f30
commit
49f8838b62
@ -1,9 +1,11 @@
|
|||||||
import { css, cx } from '@emotion/css';
|
import { css, cx } from '@emotion/css';
|
||||||
import React, { useEffect, useRef } from 'react';
|
import React, { useEffect, useRef } from 'react';
|
||||||
|
import { useLocation } from 'react-router-dom';
|
||||||
import { useLocalStorage } from 'react-use';
|
import { useLocalStorage } from 'react-use';
|
||||||
|
|
||||||
import { GrafanaTheme2, NavModelItem, toIconName } from '@grafana/data';
|
import { GrafanaTheme2, NavModelItem, toIconName } from '@grafana/data';
|
||||||
import { useStyles2, Text, IconButton, Icon } from '@grafana/ui';
|
import { useStyles2, Text, IconButton, Icon } from '@grafana/ui';
|
||||||
|
import { useGrafana } from 'app/core/context/GrafanaContext';
|
||||||
|
|
||||||
import { Indent } from '../../Indent/Indent';
|
import { Indent } from '../../Indent/Indent';
|
||||||
|
|
||||||
@ -21,6 +23,10 @@ interface Props {
|
|||||||
const MAX_DEPTH = 2;
|
const MAX_DEPTH = 2;
|
||||||
|
|
||||||
export function MegaMenuItem({ link, activeItem, level = 0, onClick }: Props) {
|
export function MegaMenuItem({ link, activeItem, level = 0, onClick }: Props) {
|
||||||
|
const { chrome } = useGrafana();
|
||||||
|
const state = chrome.useState();
|
||||||
|
const menuIsDocked = state.megaMenu === 'docked';
|
||||||
|
const location = useLocation();
|
||||||
const FeatureHighlightWrapper = link.highlightText ? FeatureHighlight : React.Fragment;
|
const FeatureHighlightWrapper = link.highlightText ? FeatureHighlight : React.Fragment;
|
||||||
const hasActiveChild = hasChildMatch(link, activeItem);
|
const hasActiveChild = hasChildMatch(link, activeItem);
|
||||||
const isActive = link === activeItem || (level === MAX_DEPTH && hasActiveChild);
|
const isActive = link === activeItem || (level === MAX_DEPTH && hasActiveChild);
|
||||||
@ -38,16 +44,16 @@ export function MegaMenuItem({ link, activeItem, level = 0, onClick }: Props) {
|
|||||||
if (hasActiveChild) {
|
if (hasActiveChild) {
|
||||||
setSectionExpanded(true);
|
setSectionExpanded(true);
|
||||||
}
|
}
|
||||||
}, [hasActiveChild, setSectionExpanded]);
|
}, [hasActiveChild, location, menuIsDocked, setSectionExpanded]);
|
||||||
|
|
||||||
// scroll active element into center if it's offscreen
|
// scroll active element into center if it's offscreen
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isActive && item.current && isElementOffscreen(item.current)) {
|
if (menuIsDocked && isActive && item.current && isElementOffscreen(item.current)) {
|
||||||
item.current.scrollIntoView({
|
item.current.scrollIntoView({
|
||||||
block: 'center',
|
block: 'center',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [isActive]);
|
}, [isActive, menuIsDocked]);
|
||||||
|
|
||||||
if (!link.url) {
|
if (!link.url) {
|
||||||
return null;
|
return null;
|
||||||
|
Loading…
Reference in New Issue
Block a user