Skip to content

Conversation

akansjain
Copy link

What?

A basic browser alert has been created before unloading

Why?

To prevent accidental page refreshes which would lose the user's data and progress.

How?

A basic browser alert is implemented using window.addEventListener('beforeunload')

Testing Instructions

@akansjain akansjain requested a review from adamziel as a code owner June 3, 2023 17:36
@akansjain
Copy link
Author

Hey @adamziel ,
Please review this PR and provide your feedback.

@eliot-akira
Copy link
Collaborator

eliot-akira commented Jun 3, 2023

Note that the embedded site can have its own event handlers for window beforeunload event. For example, the post edit screen in WP admin:

	/*
	 * When the user is trying to load another page, or reloads current page
	 * show a confirmation dialog when there are unsaved changes.
	 */
	$( window ).on( 'beforeunload.edit-post', function( event ) {

https://github.com/WordPress/wordpress-develop/blob/933537c35a9ebaba561562652d76a1ff2e988731/src/js/_enqueues/admin/post.js#LL495C1-L498C63

This is actually for the iframe's window, so maybe that event fires before the Playground host's window.

I also wonder if there could be a more descriptive alert message that explains how refreshing the page or closing the browser tab may risk losing unsaved data. WP core's message is:

The changes you made will be lost if you navigate away from this page.

@adamziel
Copy link
Collaborator

adamziel commented Jun 5, 2023

Thank you @akansjain! @eliot-akira brought up an interesting scenario – that is, we're in a post editor with an unsaved content and closing it would trigger an alert. What happens then?

@akansjain
Copy link
Author

Hey @eliot-akira @adamziel ,
If you want to handle the beforeunload event specifically for an iframe's window, you can access it using the contentWindow property of the iframe element.

@adamziel I think when we're in a post editor with an unsaved content and closing it would trigger an alert then:-

If the user chooses to confirm the action (e.g., by clicking "Leave" or "OK"), the browser will proceed with closing the window or navigating away from the page, effectively discarding the unsaved content.

If the user chooses to cancel the action (e.g., by clicking "Stay" or "Cancel"), the browser will prevent the default behavior of closing the window or navigating away, allowing the user to continue editing and saving the content.

@adamziel
Copy link
Collaborator

adamziel commented Jun 7, 2023

@akansjain Thank you! I'm at the WordCamp EU this week and may not be able to process this in the next couple of days, but I will follow up after that.

@akansjain
Copy link
Author

@adamziel Sure.

@dmsnell
Copy link
Member

dmsnell commented Jun 10, 2023

@akansjain thanks for the PR!

I like the idea of warning people before they lose content, while at the same time I wonder about the need because part of what makes the Playground different is its ephemerality.

In the patch there's no message on the unload handler. I worry that what this could do is unintentionally harass people who want to quickly open and close Playground windows, people who aren't concerned with saving data.

Do you have any ideas for:

  • communicating why we're trying to interrupt the page close
  • avoid running the unload handler if there's nothing to save

@adamziel
Copy link
Collaborator

Noting this should only run when using temporary storage (persistent Playground storage was added recently)

@adamziel
Copy link
Collaborator

adamziel commented Dec 1, 2023

This one seems stale so I'll close it. Feel free to reopen!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants