Simultaneous scrolling of page and LinkControl in Gutenberg editor
Description
Hello,
Since updating to WordPress 6.8.1, we've noticed an unexpected scroll behavior in the Gutenberg editor: when LinkControl is open, scrolling inside the modal also scrolls the page in the background.
Context: WordPress 6.8.1
Thanks for your work on Gutenberg !
Step-by-step reproduction instructions
- Go to the post/page editor.
- Add a Paragraph block.
- Select a word and click on the “Insert link” button.
- Try scrolling inside the modal.
- The main page scrolls along with the modal.
Screenshots, screen recording, code snippet
No response
Environment info
No response
Please confirm that you have searched existing issues in the repo.
- [x] Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
- [x] Yes
Please confirm which theme type you used for testing.
- [ ] Block
- [ ] Classic
- [x] Hybrid (e.g. classic with theme.json)
- [ ] Not sure
I was able to reproduce this bug. Below are the details of my environment used for testing:
- WordPress: 6.8.1
- Gutenberg: 20.9.0
- Theme: Twenty Twenty-Five 1.2
- Browser: Chrome 137.0.0.0
Screencast
https://github.com/user-attachments/assets/04d3146d-4d50-4da4-8bc0-a29fb90cf1ec
I was able to reproduce this bug in Playground as well
https://github.com/user-attachments/assets/14b4099a-32ec-49fc-83be-b42355b12823
I’m able to reproduce the issue, and it appears to have been introduced after PR #68075.
@ellatrix @stokesman #68075 enabled scrolling on in-between inserters, but this seems to cause odd behavior when the popover itself is scrollable. Is it possible to check if the popover itself is scrollable and, if so, cancel the scrolling of the editor?
The following fix works for the LinkControl component, but I also agree that something like this would be a better solution:
Is it possible to check if the popover itself is scrollable and, if so, cancel the scrolling of the editor?
LinkControl Fix diff
--- a/packages/block-editor/src/components/link-control/search-results.js
+++ b/packages/block-editor/src/components/link-control/search-results.js
@@ -3,6 +3,7 @@
*/
import { __, sprintf } from '@wordpress/i18n';
import { MenuGroup } from '@wordpress/components';
+import { useRef, useEffect } from '@wordpress/element';
/**
* External dependencies
@@ -30,6 +31,38 @@ function LinkControlSearchResults( {
createSuggestionButtonText,
suggestionsQuery,
} ) {
+ const scrollContainerRef = useRef();
+
+ useEffect( () => {
+ const node = scrollContainerRef.current;
+ if ( ! node ) {
+ return;
+ }
+
+ const onWheelCapture = ( event ) => {
+ const { deltaY } = event;
+ const atTop = node.scrollTop === 0;
+ const atBottom =
+ node.scrollTop + node.clientHeight >= node.scrollHeight;
+ const scrollingUp = deltaY < 0;
+ const scrollingDown = deltaY > 0;
+
+ // If end of the scroll container is reached, allow the event to propagate
+ if (
+ ( scrollingUp && ! atTop ) ||
+ ( scrollingDown && ! atBottom )
+ ) {
+ event.stopPropagation();
+ }
+ };
+
+ node.addEventListener( 'wheel', onWheelCapture );
+
+ return () => {
+ node.removeEventListener( 'wheel', onWheelCapture );
+ };
+ }, [] );
+
const resultsListClasses = clsx(
'block-editor-link-control__search-results',
{
@@ -59,6 +92,7 @@ function LinkControlSearchResults( {
<div className="block-editor-link-control__search-results-wrapper">
<div
{ ...suggestionsListProps }
+ ref={ scrollContainerRef }
className={ resultsListClasses }
aria-label={ labelText }
>
From what I can see, this issue seems to occur on all popovers that have the __unstableSlotName="__unstable-block-tools-after" prop applied. We can solve it by removing the usePopoverScroll hook like so, but this is probably not an ideal approach:
diff --git a/packages/block-editor/src/components/block-tools/index.js b/packages/block-editor/src/components/block-tools/index.js
index 915d6d9a21..73b5438439 100644
--- a/packages/block-editor/src/components/block-tools/index.js
+++ b/packages/block-editor/src/components/block-tools/index.js
@@ -203,7 +203,6 @@ export default function BlockTools( {
}
}
const blockToolbarRef = usePopoverScroll( __unstableContentRef );
- const blockToolbarAfterRef = usePopoverScroll( __unstableContentRef );
return (
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
@@ -239,10 +238,7 @@ export default function BlockTools( {
) }
{ children }
{ /* Used for inline rich text popovers. */ }
- <Popover.Slot
- name="__unstable-block-tools-after"
- ref={ blockToolbarAfterRef }
- />
+ <Popover.Slot name="__unstable-block-tools-after" />
{ isZoomOutMode && ! isDragging && (
<ZoomOutModeInserters
__unstableContentRef={ __unstableContentRef }
Is it possible to check if the popover itself is scrollable and, if so, cancel the scrolling of the editor?
That's pretty much the approach in #71468 and it’s working well in my testing.
We can solve it by removing the
usePopoverScrollhook like so, but this is probably not an ideal approach:
Yes, without further work it would prevent scrolling through popovers in that slot. Still, it appeals to me for code quality reasons. There some redundancy with the hook's usage right now. It's built into BlockPopover and thus in some cases it's invoked twice for popovers in the slots which apply the hook too (e.g. grid or spacing visualizers). It's not causing doubled execution though since in these cases the hook short-circuits in BlockPopover. It seems like it could be nicer to let BlockPopover be solely responsible for this.