-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
61 lines (50 loc) · 1.37 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* External dependencies
*/
import scrollIntoView from 'dom-scroll-into-view';
/**
* WordPress dependencies
*/
import { useEffect } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { getScrollContainer } from '@wordpress/dom';
/**
* Internal dependencies
*/
import { getBlockDOMNode } from '../../utils/dom';
/**
* Scrolls the multi block selection end into view if not in view already. This
* is important to do after selection by keyboard.
*/
export default function MultiSelectScrollIntoView() {
const selector = ( select ) => {
const {
getBlockSelectionEnd,
isMultiSelecting,
} = select( 'core/block-editor' );
return {
selectionEnd: getBlockSelectionEnd(),
isMultiSelecting: isMultiSelecting(),
};
};
const { selectionEnd, isMultiSelecting } = useSelect( selector, [] );
useEffect( () => {
if ( ! selectionEnd || isMultiSelecting ) {
return;
}
const extentNode = getBlockDOMNode( selectionEnd );
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,
} );
}, [ selectionEnd, isMultiSelecting ] );
return null;
}