-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Block editor: scroll selected block only if it has no focus #30924
Merged
Merged
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 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
64 changes: 64 additions & 0 deletions
64
packages/block-editor/src/components/block-list/use-block-props/use-scroll-into-view.js
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,64 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import scrollIntoView from 'dom-scroll-into-view'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect, useRef } from '@wordpress/element'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { getScrollContainer } from '@wordpress/dom'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as blockEditorStore } from '../../../store'; | ||
|
||
export function useScrollIntoView( clientId ) { | ||
const ref = useRef(); | ||
const isSelectionEnd = useSelect( | ||
( select ) => { | ||
const { isBlockSelected, getBlockSelectionEnd } = select( | ||
blockEditorStore | ||
); | ||
|
||
return ( | ||
isBlockSelected( clientId ) || | ||
getBlockSelectionEnd() === clientId | ||
); | ||
}, | ||
[ clientId ] | ||
); | ||
|
||
useEffect( () => { | ||
if ( ! isSelectionEnd ) { | ||
return; | ||
} | ||
|
||
const extentNode = ref.current; | ||
|
||
// If the block is focused, the browser will already have scrolled into | ||
// view if necessary. | ||
if ( extentNode.contains( extentNode.ownerDocument.activeElement ) ) { | ||
return; | ||
} | ||
|
||
const scrollContainer = getScrollContainer( extentNode ); | ||
|
||
// If there's no scroll container, it follows that there's no scrollbar | ||
// and thus there's no need to try to scroll into view. | ||
if ( ! scrollContainer ) { | ||
return; | ||
} | ||
|
||
scrollIntoView( extentNode, scrollContainer, { | ||
onlyScrollIfNeeded: true, | ||
} ); | ||
}, [ isSelectionEnd ] ); | ||
|
||
return ref; | ||
} |
67 changes: 7 additions & 60 deletions
67
packages/block-editor/src/components/selection-scroll-into-view/index.js
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 |
---|---|---|
@@ -1,70 +1,17 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import scrollIntoView from 'dom-scroll-into-view'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect, useRef } from '@wordpress/element'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { getScrollContainer } from '@wordpress/dom'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getBlockDOMNode } from '../../utils/dom'; | ||
import { store as blockEditorStore } from '../../store'; | ||
|
||
export function useScrollSelectionIntoView( ref ) { | ||
// Although selectionRootClientId isn't used directly in calculating | ||
// whether scrolling should occur, it is used as a dependency of the | ||
// effect to take into account situations where a block might be moved | ||
// to a different parent. In this situation, the selectionEnd clientId | ||
// remains the same, so the rootClientId is used to trigger the effect. | ||
const { selectionRootClientId, selectionEnd } = useSelect( ( select ) => { | ||
const selectors = select( blockEditorStore ); | ||
const selectionEndClientId = selectors.getBlockSelectionEnd(); | ||
return { | ||
selectionEnd: selectionEndClientId, | ||
selectionRootClientId: selectors.getBlockRootClientId( | ||
selectionEndClientId | ||
), | ||
}; | ||
}, [] ); | ||
|
||
useEffect( () => { | ||
if ( ! selectionEnd ) { | ||
return; | ||
} | ||
|
||
const { ownerDocument } = ref.current; | ||
const extentNode = getBlockDOMNode( selectionEnd, ownerDocument ); | ||
|
||
if ( ! extentNode ) { | ||
return; | ||
} | ||
|
||
const scrollContainer = getScrollContainer( extentNode ); | ||
|
||
// If there's no scroll container, it follows that there's no scrollbar | ||
// and thus there's no need to try to scroll into view. | ||
if ( ! scrollContainer ) { | ||
return; | ||
} | ||
|
||
scrollIntoView( extentNode, scrollContainer, { | ||
onlyScrollIfNeeded: true, | ||
} ); | ||
}, [ selectionRootClientId, selectionEnd ] ); | ||
} | ||
import deprecated from '@wordpress/deprecated'; | ||
|
||
/** | ||
* Scrolls the multi block selection end into view if not in view already. This | ||
* is important to do after selection by keyboard. | ||
* | ||
* @deprecated | ||
*/ | ||
export function MultiSelectScrollIntoView() { | ||
const ref = useRef(); | ||
useScrollSelectionIntoView( ref ); | ||
return <div ref={ ref } />; | ||
deprecated( 'wp.blockEditor.MultiSelectScrollIntoView', { | ||
hint: 'This behaviour is now built-in.', | ||
} ); | ||
return null; | ||
} |
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.
Ideally we have a hook that does combines this: if a selected block has no focus, either focus the first element (if so desired) or scroll the block into view.
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.
(If we do set focus, the browser will automatically scroll it into view if needed.)