Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 30, 2025

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:

  1. Browser beforeunload events (refresh/close browser)
  2. Click events on anchor (<a>) tags

However, document drawer close buttons use <button> elements that call closeModal() directly, bypassing the prevention logic entirely.

Solution

Extended the usePreventLeave hook to also intercept document drawer close button clicks by:

  • Adding findClosestButton() to traverse the DOM tree for button elements
  • Adding isDrawerCloseButton() to identify document drawer close buttons specifically
  • Extending handleClick() to prevent button clicks when the form has unsaved changes
  • Preserving all existing anchor tag navigation prevention logic

The implementation targets these specific button classes and IDs:

  • drawer__close - Generic drawer close buttons
  • drawer__header__close - Drawer header close buttons
  • doc-drawer__header-close - Document drawer specific close buttons
  • IDs starting with close-drawer__ - Programmatically generated close buttons

Testing

Created comprehensive validation that confirms:

  • ✅ Clean forms close immediately (no unsaved changes)
  • ✅ Modified forms are intercepted and prevented from closing
  • ✅ Both header X button and drawer close button work correctly
  • ✅ Existing anchor tag prevention behavior is unchanged
  • ✅ Only document drawer buttons are intercepted, not all modal close buttons

Added comprehensive e2e tests in test/admin/e2e/document-view/e2e.spec.ts that cover:

  • ✅ Leave without saving modal appears when closing drawer with unsaved changes
  • ✅ "Stay on this page" button functionality keeps drawer open
  • ✅ "Leave anyway" button closes drawer and discards changes
  • ✅ No modal appears when closing drawer without changes
  • ✅ Multiple close button types are properly intercepted

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

This section details on the original issue you should resolve

<issue_title>Document drawers don't prompt the "Leave without saving" modal</issue_title>
<issue_description>### Describe the Bug

When making edits inside a document drawer and then closing it, the leave without saving modal never appears. This makes it easy to lose edits when editing inside a modal.

Link to the code that reproduces this issue

N/A

Reproduction Steps

  1. Create a relationship field on a collection
  2. Open the relationship drawer and make edits
  3. Close the drawer
  4. Not prompted with leave without saving modal

Which area(s) are affected? (Select all that apply)

area: ui

Environment Info

Relevant Packages:
  payload: 3.57.0
  next: 15.5.2
  @payloadcms/db-mongodb: 3.57.0
  @payloadcms/next/utilities: 3.57.0
  @payloadcms/ui/shared: 3.57.0
  react: 19.0.0
  react-dom: 19.0.0
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

Fixes #13947

💡 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.

@denolfe denolfe changed the title [WIP] Document drawers don't prompt the "Leave without saving" modal fix(ui): document drawers don't prompt the "Leave without saving" modal Sep 30, 2025
Co-authored-by: denolfe <65888+denolfe@users.noreply.github.com>
@denolfe
Copy link
Member

denolfe commented Sep 30, 2025

@copilot Add an e2e test for this

Co-authored-by: denolfe <65888+denolfe@users.noreply.github.com>
@Copilot Copilot AI changed the title fix(ui): document drawers don't prompt the "Leave without saving" modal fix(ui): document drawers now prompt "Leave without saving" modal Sep 30, 2025
@Copilot Copilot AI requested a review from denolfe September 30, 2025 14:16
Copilot finished work on behalf of denolfe September 30, 2025 14:16
Co-authored-by: denolfe <65888+denolfe@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Sep 30, 2025

@copilot Add an e2e test for this

Added comprehensive e2e tests for the document drawer leave without saving functionality in commit 13e3e99. The tests cover:

  • Prompting modal when closing drawer with unsaved changes
  • Cancel functionality ("Stay on this page")
  • Confirm functionality ("Leave anyway")
  • No prompt when closing drawer without changes
  • Testing multiple close button types

The tests are located in test/admin/e2e/document-view/e2e.spec.ts under the drawers section and follow the existing test patterns in the repository.

Copilot finished work on behalf of denolfe September 30, 2025 14:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document drawers don't prompt the "Leave without saving" modal
2 participants