Skip to content

Commit 0d47ffe

Browse files
Kiryousniros1
authored andcommitted
fix: show browser prompt if user close the page with unsaved changes (#235324)
## Before https://github.com/user-attachments/assets/5a7d4fd4-c185-4c25-a931-38c5d47d0a7d ## After https://github.com/user-attachments/assets/65f3f9f9-32d8-4bc9-88f4-0e57e2851d81 ### Checklist Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [ ] 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/src/platform/packages/shared/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 - [ ] 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 was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The `release_note:breaking` label should be applied in these situations. - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [ ] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) - [ ] Review the [backport guidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing) and apply applicable `backport:*` labels. ### Identify risks Does this PR introduce any risks? For example, consider risks like hard to test bugs, performance regression, potential of data loss. Describe the risk, its severity, and mitigation for each identified risk. Invite stakeholders and evaluate how to proceed before merging. - [ ] [See some risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) - [ ] ...
1 parent 16e7c21 commit 0d47ffe

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/platform/plugins/shared/workflows_management/public/shared/ui/unsaved_changes_prompt.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ export const UnsavedChangesPrompt: React.FC<Props> = ({
2727
currentPathRef.current = location.pathname;
2828
}, [location.pathname]);
2929

30+
useEffect(() => {
31+
const handler = (event: BeforeUnloadEvent) => {
32+
if (hasUnsavedChanges) {
33+
// These 2 lines of code are the recommendation from MDN for triggering a browser prompt for confirming
34+
// whether or not a user wants to leave the current site.
35+
event.preventDefault();
36+
event.returnValue = '';
37+
}
38+
};
39+
// Adding this handler will prompt users if they are navigating to a new page, outside of the Kibana SPA
40+
window.addEventListener('beforeunload', handler);
41+
return () => window.removeEventListener('beforeunload', handler);
42+
}, [hasUnsavedChanges]);
43+
3044
const message = (nextLocation: any) => {
3145
const nextPath: string | undefined = nextLocation?.pathname;
3246
const currentPath = currentPathRef.current;

0 commit comments

Comments
 (0)