[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](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)
This commit is contained in:
Catherine Liu 2024-06-20 09:31:54 -07:00 committed by GitHub
parent ffa943c8be
commit a11c99e89a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 28 additions and 11 deletions

View file

@ -19,7 +19,7 @@ export const apiCanDuplicatePanels = (
};
export interface CanExpandPanels {
expandPanel: (panelId?: string) => void;
expandPanel: (panelId: string) => void;
expandedPanelId: PublishingSubject<string | undefined>;
}

View file

@ -50,8 +50,6 @@ export class ExpandPanelAction implements Action<EmbeddableApiContext> {
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);
}
}

View file

@ -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;
}

View file

@ -64,14 +64,15 @@ export const Item = React.forwardRef<HTMLDivElement, Props>(
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');

View file

@ -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' });
});
};