-
-
Notifications
You must be signed in to change notification settings - Fork 835
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework Composer exit prevention logic
- The main breakthrough was moving the logic from Composer to ComposerBody. The former had to consult the state for logic and values readily available inside the ComposerBody (and its subclasses). - The state is no longer involved in the browser-level unload event handling. - It still has to take care of the manual confirmation message that is shown when the composer itself is closed while drafting a message. - The former does not need a custom message (it would ignore it anyway), the latter does. - The callback and the text that should be displayed are now passed to the state with a nicely encapsulating method. - While I was at it, I extracted a simple helper component for handling the event (de)registration. JSX composition is so nice!
- Loading branch information
1 parent
9429fb2
commit 050081c
Showing
5 changed files
with
79 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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,37 @@ | ||
import Component from '../Component'; | ||
|
||
/** | ||
* The `ConfirmDocumentUnload` component can be used to register a global | ||
* event handler that prevents closing the browser window/tab based on the | ||
* return value of a given callback prop. | ||
* | ||
* ### Props | ||
* | ||
* - `when` - a callback returning true when the browser should prompt for | ||
* confirmation before closing the window/tab | ||
* | ||
* ### Children | ||
* | ||
* NOTE: Only the first child will be rendered. (Use this component to wrap | ||
* another component / DOM element.) | ||
* | ||
*/ | ||
export default class ConfirmDocumentUnload extends Component { | ||
config(isInitialized, context) { | ||
if (isInitialized) return; | ||
|
||
const handler = () => this.props.when() || undefined; | ||
|
||
$(window).on('beforeunload', handler); | ||
|
||
context.onunload = () => { | ||
$(window).off('beforeunload', handler); | ||
}; | ||
} | ||
|
||
view() { | ||
// To avoid having to render another wrapping <div> here, we assume that | ||
// this component is only wrapped around a single element / component. | ||
return this.props.children[0]; | ||
} | ||
} |
This file contains 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 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 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 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