From a11c99e89a5b46910a30c7ad936a9318dba59f31 Mon Sep 17 00:00:00 2001 From: Catherine Liu Date: Thu, 20 Jun 2024 09:31:54 -0700 Subject: [PATCH] [Dashboard] Scroll to panel on minimize (#184696) ## Summary Closes #184695. This stores the user's last scroll position on the dashboard container when they maximize a panel. On minimize, we scroll back to their original position on the dashboard. ![Jun-03-2024 17-52-35](https://github.com/elastic/kibana/assets/1697105/af37a8fc-d9d5-42cb-b7e8-58e064035012) ### Checklist Delete any items that are not applicable to this PR. - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [ ] Any UI touched in this PR is usable by keyboard only (learn more about [keyboard accessibility](https://webaim.org/techniques/keyboard/)) - [ ] Any UI touched in this PR does not create any new axe failures (run axe in browser: [FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/), [Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US)) - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This renders correctly on smaller devices using a responsive layout. (You can test this [in your browser](https://www.browserstack.com/guide/responsive-testing-on-local-server)) - [ ] This was checked for [cross-browser compatibility](https://www.elastic.co/support/matrix#matrix_browsers) ### Risk Matrix Delete this section if it is not applicable to this PR. Before closing this PR, invite QA, stakeholders, and other developers to identify risks that should be tested prior to the change/feature release. When forming the risk matrix, consider some of the following examples and how they may potentially impact the change: | Risk | Probability | Severity | Mitigation/Notes | |---------------------------|-------------|----------|-------------------------| | Multiple Spaces—unexpected behavior in non-default Kibana Space. | Low | High | Integration tests will verify that all features are still supported in non-default Kibana Space and when user switches between spaces. | | Multiple nodes—Elasticsearch polling might have race conditions when multiple Kibana nodes are polling for the same tasks. | High | Low | Tasks are idempotent, so executing them multiple times will not result in logical error, but will degrade performance. To test for this case we add plenty of unit tests around this logic and document manual testing procedure. | | Code should gracefully handle cases when feature X or plugin Y are disabled. | Medium | High | Unit tests will verify that any feature flag or plugin combination still results in our service operational. | | [See more potential risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) | ### For maintainers - [ ] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) --- .../interfaces/panel_management.ts | 2 +- .../dashboard_actions/expand_panel_action.tsx | 4 +-- .../component/grid/_dashboard_grid.scss | 1 - .../component/grid/dashboard_grid_item.tsx | 7 +++--- .../embeddable/dashboard_container.tsx | 25 ++++++++++++++++--- 5 files changed, 28 insertions(+), 11 deletions(-) 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' }); }); };