-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
List View: Scroll selected block into view when single block selectio…
…n changes (#46895) * List View: Try scrolling selected blocks into view when single block selection changes * Use getScrollContainer and calculate real top position of scrollable area instead of using a hard-coded value * Try rearranging things so that the ref is always attached at the row level * Move placeholder to its own file * Tidy up a little * Tidy comments * Remove unneeded optional chaining Co-authored-by: Kai Hao <[email protected]> * Simplify and improve logic based on feedback Co-authored-by: Kai Hao <[email protected]> * Remove unneeded optional chaining Co-authored-by: Kai Hao <[email protected]> * Revert placeholder component, update showBlock logic so that selected blocks are rendered as real ListViewBlock components --------- Co-authored-by: Kai Hao <[email protected]>
- Loading branch information
1 parent
2cc9840
commit b87e855
Showing
4 changed files
with
104 additions
and
31 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
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
48 changes: 48 additions & 0 deletions
48
packages/block-editor/src/components/list-view/use-list-view-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,48 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { getScrollContainer } from '@wordpress/dom'; | ||
import { useLayoutEffect } from '@wordpress/element'; | ||
|
||
export default function useListViewScrollIntoView( { | ||
isSelected, | ||
selectedClientIds, | ||
rowItemRef, | ||
} ) { | ||
const isSingleSelection = selectedClientIds.length === 1; | ||
|
||
useLayoutEffect( () => { | ||
// Skip scrolling into view if this particular block isn't selected, | ||
// or if more than one block is selected overall. This is to avoid | ||
// scrolling the view in a multi selection where the user has intentionally | ||
// selected multiple blocks within the list view, but the initially | ||
// selected block may be out of view. | ||
if ( ! isSelected || ! isSingleSelection || ! rowItemRef.current ) { | ||
return; | ||
} | ||
|
||
const scrollContainer = getScrollContainer( rowItemRef.current ); | ||
const { ownerDocument } = rowItemRef.current; | ||
|
||
const windowScroll = | ||
scrollContainer === ownerDocument.body || | ||
scrollContainer === ownerDocument.documentElement; | ||
|
||
// If the there is no scroll container, of if the scroll container is the window, | ||
// do not scroll into view, as the block is already in view. | ||
if ( windowScroll || ! scrollContainer ) { | ||
return; | ||
} | ||
|
||
const rowRect = rowItemRef.current.getBoundingClientRect(); | ||
const scrollContainerRect = scrollContainer.getBoundingClientRect(); | ||
|
||
// If the selected block is not currently visible, scroll to it. | ||
if ( | ||
rowRect.top < scrollContainerRect.top || | ||
rowRect.bottom > scrollContainerRect.bottom | ||
) { | ||
rowItemRef.current.scrollIntoView(); | ||
} | ||
}, [ isSelected, isSingleSelection, rowItemRef ] ); | ||
} |
b87e855
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.
Flaky tests detected in b87e855.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4188870157
📝 Reported issues:
specs/editor/various/switch-to-draft.test.js
/test/e2e/specs/editor/various/a11y.spec.js
specs/editor/various/block-hierarchy-navigation.test.js
specs/editor/various/switch-to-draft.test.js