-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
Wait for editor changes to be debounced before closing modal #38262
Conversation
Hi , @woocommerce/mothra Apart from reviewing the code changes, please make sure to review the testing instructions as well. You can follow this guide to find out what good testing instructions should look like: |
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.
Thanks for the updates, I did leave some suggestions to do this differently, basically two things:
- Call
debouncedOnChange
for bothonInput
&onChange
(this is actually necessary :/ see inline comment for more info) - Make use of the
flush()
method thatuseDebounce
returns.
shouldCloseOnClickOutside={ false } | ||
> | ||
<IframeEditor | ||
initialBlocks={ initialBlocks } | ||
onChange={ onChange } | ||
onInput={ handleInput } |
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.
It actually looks like you need both onChange
and onInput
:/ they are both being called for different circumstances.
For example, onChange
only gets called when new blocks are created, but onInput
when something within the block is updated. You can pretty easily see this if you console log both functions.
Also see this example: https://github.com/WordPress/gutenberg/blob/trunk/packages/block-editor/README.md?plain=1#L34-L35
And the description of the outputs: https://github.com/WordPress/gutenberg/blob/trunk/packages/block-editor/src/components/provider/README.md?plain=1#L14-L32
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.
Good call. I consoled the outputs and noticed this as well.
A callback invoked when the blocks have been modified in a non-persistent manner.
I had read the above and re-reading it I'm still convinced the documentation is semantically incorrect. I would expect the non-persistent callback (onInput
) to always fire while the persistent callback (onChange
) would fire when those changes were persisted.
This behavior is all-around a little odd and does not match the browser API as stated in the docs. I'll open an issue upstream to get this fixed.
useEffect( () => { | ||
// Only close the modal once editor changes have persisted after debouncing. | ||
if ( isClosing && ! isDebouncing ) { | ||
onClose(); |
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.
This is a rather hacky way of doing this, and at-least at one point I managed to trigger the onClose
but it wasn't being triggered until I made another block change (probably related to the difference between onInput
and onChange
.
But there is a much easier way to handle this, the useDebounce
hook has a flush
method that returns the last block result. So you can just do this within the handleClose
:
const blocks = debouncedOnChange.flush();
if ( blocks ) {
onChange( blocks );
}
onClose();
This way you can remove both the isClosing
and isDebouncing
states :)
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.
This is a rather hacky way of doing this, and at-least at one point I managed to trigger the onClose but it wasn't being triggered until I made another block change (probably related to the difference between onInput and onChange.
I suspect you managed to do this based on the buggy behavior of onChange
and onInput
discussed above.
useDebounce hook has a flush method that returns the last block result
TIL! Thanks for sharing this- this is a much easier way to implement this.
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.
Thanks for the updates, I did leave some suggestions to do this differently, basically two things:
- Call
debouncedOnChange
for bothonInput
&onChange
(this is actually necessary :/ see inline comment for more info) - Make use of the
flush()
method thatuseDebounce
returns.
Test Results SummaryCommit SHA: e6ceafb
To view the full API test report, click here. To view the full E2E test report, click here. To view all test reports, visit the WooCommerce Test Reports Dashboard. |
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.
Thanks for the updates, this tested well 🚀
Submission Review Guidelines:
Changes proposed in this Pull Request:
Making changes within the modal editor and quickly closing the editor previously would result in some of the changes being missed.
This PR capture the changes immediately, debounces on the onchange handler, and only closes the modal once the debouncing is complete.
Closes #38020 .
How to test the changes in this Pull Request:
Using the WooCommerce Testing Instructions Guide, include your detailed testing instructions: