fix(ui): document drawers now prompt "Leave without saving" modal #13978
+165
−9
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Document drawers were not prompting users with the "Leave without saving" modal when closing with unsaved changes, making it easy to accidentally lose edits.
Problem
The
usePreventLeave
hook only intercepted:<a>
) tagsHowever, document drawer close buttons use
<button>
elements that callcloseModal()
directly, bypassing the prevention logic entirely.Solution
Extended the
usePreventLeave
hook to also intercept document drawer close button clicks by:findClosestButton()
to traverse the DOM tree for button elementsisDrawerCloseButton()
to identify document drawer close buttons specificallyhandleClick()
to prevent button clicks when the form has unsaved changesThe implementation targets these specific button classes and IDs:
drawer__close
- Generic drawer close buttonsdrawer__header__close
- Drawer header close buttonsdoc-drawer__header-close
- Document drawer specific close buttonsclose-drawer__
- Programmatically generated close buttonsTesting
Created comprehensive validation that confirms:
Added comprehensive e2e tests in
test/admin/e2e/document-view/e2e.spec.ts
that cover:The fix uses the same event capture mechanism as the existing implementation, ensuring consistent behavior across all close methods.
Screenshots
Before Fix: Document drawer would close immediately losing unsaved changes
After Fix: Close buttons are intercepted when form has modifications
Demo showing both close buttons successfully intercepted when form has unsaved changes. The log entries show "Click intercepted by usePreventLeave hook" proving the fix is working.
Impact
This change prevents accidental data loss in document drawers and relationship field editing scenarios while maintaining backward compatibility with all existing functionality.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.