-
Notifications
You must be signed in to change notification settings - Fork 206
Warn when closing tab if there is an unsaved, non-empty draft #7083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robertknight
wants to merge
1
commit into
main
Choose a base branch
from
unsaved-draft-warning
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { mount } from '@hypothesis/frontend-testing'; | ||
|
||
import { useUnsavedChanges, hasUnsavedChanges } from '../unsaved-changes'; | ||
|
||
function TestUseUnsavedChanges({ unsaved, fakeWindow }) { | ||
useUnsavedChanges(unsaved, fakeWindow); | ||
return <div />; | ||
} | ||
|
||
describe('useUnsavedChanges', () => { | ||
let fakeWindow; | ||
|
||
function dispatchBeforeUnload() { | ||
const event = new Event('beforeunload', { cancelable: true }); | ||
fakeWindow.dispatchEvent(event); | ||
return event; | ||
} | ||
|
||
function createWidget(unsaved) { | ||
return mount( | ||
<TestUseUnsavedChanges fakeWindow={fakeWindow} unsaved={unsaved} />, | ||
); | ||
} | ||
|
||
beforeEach(() => { | ||
// Use a dummy window to avoid triggering any handlers that respond to | ||
// "beforeunload" on the real window. | ||
fakeWindow = new EventTarget(); | ||
}); | ||
|
||
it('does not increment unsaved-changes count if argument is false', () => { | ||
createWidget(false); | ||
assert.isFalse(hasUnsavedChanges()); | ||
}); | ||
|
||
it('does not register "beforeunload" handler if argument is false', () => { | ||
createWidget(false); | ||
const event = dispatchBeforeUnload(); | ||
assert.isFalse(event.defaultPrevented); | ||
}); | ||
|
||
it('increments unsaved-changes count if argument is true', () => { | ||
const wrapper = createWidget(true); | ||
assert.isTrue(hasUnsavedChanges()); | ||
wrapper.unmount(); | ||
assert.isFalse(hasUnsavedChanges()); | ||
}); | ||
|
||
it('registers "beforeunload" handler if argument is true', () => { | ||
const wrapper = createWidget(true); | ||
const event = dispatchBeforeUnload(); | ||
assert.isTrue(event.defaultPrevented); | ||
|
||
// We don't test `event.returnValue` here because it returns `false` after | ||
// assignment in Chrome, even though the handler assigns it `true`. | ||
|
||
// Unmount the widget, this should remove the handler. | ||
wrapper.unmount(); | ||
const event2 = dispatchBeforeUnload(); | ||
assert.isFalse(event2.defaultPrevented); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useEffect } from 'preact/hooks'; | ||
|
||
/** Count of components with unsaved changes. */ | ||
let unsavedCount = 0; | ||
|
||
function preventUnload(e: BeforeUnloadEvent) { | ||
// `preventDefault` is the modern API for preventing unload. | ||
e.preventDefault(); | ||
|
||
// Setting `returnValue` to a truthy value is a legacy method needed for | ||
// Firefox. Note that in Chrome, reading `returnValue` will return false | ||
// afterwards. | ||
e.returnValue = true; | ||
} | ||
|
||
/** | ||
* Return true if any active components have indicated they have unsaved changes | ||
* using {@link useUnsavedChanges}. | ||
*/ | ||
export function hasUnsavedChanges() { | ||
return unsavedCount > 0; | ||
} | ||
|
||
/** | ||
* Hook that registers the current component as having unsaved changes that | ||
* would be lost in the event of a navigation. | ||
* | ||
* WARNING: As of May 2025, this works in Chrome and Firefox, but not Safari. | ||
* See https://github.com/hypothesis/support/issues/59#issuecomment-2886335334. | ||
* | ||
* @param hasUnsavedChanges - True if current component has unsaved changes | ||
* @param window_ - Test seam | ||
*/ | ||
export function useUnsavedChanges( | ||
hasUnsavedChanges: boolean, | ||
/* istanbul ignore next - test seam */ | ||
window_ = window, | ||
) { | ||
useEffect(() => { | ||
if (!hasUnsavedChanges) { | ||
return () => {}; | ||
} | ||
|
||
unsavedCount += 1; | ||
if (unsavedCount === 1) { | ||
window_.addEventListener('beforeunload', preventUnload); | ||
} | ||
return () => { | ||
unsavedCount -= 1; | ||
if (unsavedCount === 0) { | ||
window_.removeEventListener('beforeunload', preventUnload); | ||
} | ||
}; | ||
}, [hasUnsavedChanges, window_]); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Next week I will look at upstreaming this into the frontend-shared library.