diff --git a/packages/presentation/presentation_containers/interfaces/panel_management.ts b/packages/presentation/presentation_containers/interfaces/panel_management.ts index 1d18f43bc444..834c0871ca58 100644 --- a/packages/presentation/presentation_containers/interfaces/panel_management.ts +++ b/packages/presentation/presentation_containers/interfaces/panel_management.ts @@ -19,7 +19,7 @@ export const apiCanDuplicatePanels = ( }; export interface CanExpandPanels { - expandPanel: (panelId?: string) => void; + expandPanel: (panelId: string) => void; expandedPanelId: PublishingSubject; } diff --git a/src/plugins/dashboard/public/dashboard_actions/expand_panel_action.tsx b/src/plugins/dashboard/public/dashboard_actions/expand_panel_action.tsx index dd43e5967d6c..adc0c8389e2c 100644 --- a/src/plugins/dashboard/public/dashboard_actions/expand_panel_action.tsx +++ b/src/plugins/dashboard/public/dashboard_actions/expand_panel_action.tsx @@ -50,8 +50,6 @@ export class ExpandPanelAction implements Action { public async execute({ embeddable }: EmbeddableApiContext) { if (!isApiCompatible(embeddable)) throw new IncompatibleActionError(); - embeddable.parentApi.expandPanel( - embeddable.parentApi.expandedPanelId.value ? undefined : embeddable.uuid - ); + embeddable.parentApi.expandPanel(embeddable.uuid); } } diff --git a/src/plugins/dashboard/public/dashboard_container/component/grid/_dashboard_grid.scss b/src/plugins/dashboard/public/dashboard_container/component/grid/_dashboard_grid.scss index b33e35647e21..f6e7918fb1b0 100644 --- a/src/plugins/dashboard/public/dashboard_container/component/grid/_dashboard_grid.scss +++ b/src/plugins/dashboard/public/dashboard_container/component/grid/_dashboard_grid.scss @@ -40,7 +40,6 @@ * Shifting the rendered panels offscreen prevents a quick flash when redrawing the panels on minimize */ .dshDashboardGrid__item--hidden { - position: absolute; top: -9999px; left: -9999px; } diff --git a/src/plugins/dashboard/public/dashboard_container/component/grid/dashboard_grid_item.tsx b/src/plugins/dashboard/public/dashboard_container/component/grid/dashboard_grid_item.tsx index 7d4a87f7209e..d4fd84618747 100644 --- a/src/plugins/dashboard/public/dashboard_container/component/grid/dashboard_grid_item.tsx +++ b/src/plugins/dashboard/public/dashboard_container/component/grid/dashboard_grid_item.tsx @@ -64,14 +64,15 @@ export const Item = React.forwardRef( useLayoutEffect(() => { if (typeof ref !== 'function' && ref?.current) { + const panelRef = ref.current; if (scrollToPanelId === id) { - container.scrollToPanel(ref.current); + container.scrollToPanel(panelRef); } if (highlightPanelId === id) { - container.highlightPanel(ref.current); + container.highlightPanel(panelRef); } - ref.current.querySelectorAll('*').forEach((e) => { + panelRef.querySelectorAll('*').forEach((e) => { if (blurPanel) { // remove blurred panels and nested elements from tab order e.setAttribute('tabindex', '-1'); diff --git a/src/plugins/dashboard/public/dashboard_container/embeddable/dashboard_container.tsx b/src/plugins/dashboard/public/dashboard_container/embeddable/dashboard_container.tsx index 1bfa289a5e91..45f3b2535ff6 100644 --- a/src/plugins/dashboard/public/dashboard_container/embeddable/dashboard_container.tsx +++ b/src/plugins/dashboard/public/dashboard_container/embeddable/dashboard_container.tsx @@ -170,6 +170,7 @@ export class DashboardContainer public creationEndTime?: number; public firstLoad: boolean = true; private hadContentfulRender = false; + private scrollPosition?: number; // cleanup public stopSyncingWithUnifiedSearch?: () => void; @@ -585,11 +586,18 @@ export class DashboardContainer return panel; }; - public expandPanel = (panelId?: string) => { - this.setExpandedPanelId(panelId); + public expandPanel = (panelId: string) => { + const isPanelExpanded = Boolean(this.getExpandedPanelId()); - if (!panelId) { + if (isPanelExpanded) { + this.setExpandedPanelId(undefined); this.setScrollToPanelId(panelId); + return; + } + + this.setExpandedPanelId(panelId); + if (window.scrollY > 0) { + this.scrollPosition = window.scrollY; } }; @@ -765,6 +773,17 @@ export class DashboardContainer this.untilEmbeddableLoaded(id).then(() => { this.setScrollToPanelId(undefined); + if (this.scrollPosition) { + panelRef.ontransitionend = () => { + // Scroll to the last scroll position after the transition ends to ensure the panel is back in the right position before scrolling + // This is necessary because when an expanded panel collapses, it takes some time for the panel to return to its original position + window.scrollTo({ top: this.scrollPosition }); + this.scrollPosition = undefined; + panelRef.ontransitionend = null; + }; + return; + } + panelRef.scrollIntoView({ block: 'center' }); }); };