From a126c07e541733462c6b69970bec829f93a4c68b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 14 Apr 2022 19:58:08 +0200 Subject: [PATCH] DashboardPage: Remember scroll position when coming back panel edit / view panel (#47639) * DashboardPage: Remember scroll position when coming back panel edit / view panel * Use scollElement callback * Fixed ts issue --- .../dashboard/containers/DashboardPage.tsx | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/public/app/features/dashboard/containers/DashboardPage.tsx b/public/app/features/dashboard/containers/DashboardPage.tsx index 860e645f6bb..c73c0c6fb16 100644 --- a/public/app/features/dashboard/containers/DashboardPage.tsx +++ b/public/app/features/dashboard/containers/DashboardPage.tsx @@ -4,7 +4,6 @@ import { connect, ConnectedProps } from 'react-redux'; import { locationService } from '@grafana/runtime'; import { selectors } from '@grafana/e2e-selectors'; import { CustomScrollbar, stylesFactory, Themeable2, withTheme2 } from '@grafana/ui'; - import { createErrorNotification } from 'app/core/copy/appNotification'; import { Branding } from 'app/core/components/Branding/Branding'; import { DashboardGrid } from '../dashgrid/DashboardGrid'; @@ -78,6 +77,7 @@ export interface State { showLoadingState: boolean; panelNotFound: boolean; editPanelAccessDenied: boolean; + scrollElement?: HTMLDivElement; } export class UnthemedDashboardPage extends PureComponent { @@ -224,14 +224,14 @@ export class UnthemedDashboardPage extends PureComponent { } if (dashboard.canEditPanel(panel)) { - return { ...state, editPanel: panel }; + return { ...state, editPanel: panel, rememberScrollTop: state.scrollElement?.scrollTop }; } else { return { ...state, editPanelAccessDenied: true }; } } // Leaving edit mode else if (state.editPanel && !urlEditPanelId) { - return { ...state, editPanel: null }; + return { ...state, editPanel: null, updateScrollTop: state.rememberScrollTop }; } // Entering view mode @@ -245,11 +245,7 @@ export class UnthemedDashboardPage extends PureComponent { // Should move this state out of dashboard in the future dashboard.initViewPanel(panel); - return { - ...state, - viewPanel: panel, - updateScrollTop: 0, - }; + return { ...state, viewPanel: panel, rememberScrollTop: state.scrollElement?.scrollTop, updateScrollTop: 0 }; } // Leaving view mode else if (state.viewPanel && !urlViewPanelId) { @@ -290,6 +286,10 @@ export class UnthemedDashboardPage extends PureComponent { this.setState({ updateScrollTop: 0 }); }; + setScrollRef = (scrollElement: HTMLDivElement): void => { + this.setState({ scrollElement }); + }; + getInspectPanel() { const { dashboard, queryParams } = this.props; @@ -346,6 +346,7 @@ export class UnthemedDashboardPage extends PureComponent {