-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathfonts-grid.js
59 lines (53 loc) · 1.37 KB
/
fonts-grid.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
/**
* WordPress dependencies
*/
import {
__experimentalVStack as VStack,
__experimentalText as Text,
__experimentalSpacer as Spacer,
} from '@wordpress/components';
import { useState, useEffect } from '@wordpress/element';
function FontsGrid( { title, children, pageSize = 32 } ) {
const [ lastItem, setLastItem ] = useState( null );
const [ page, setPage ] = useState( 1 );
const itemsLimit = page * pageSize;
const items = children.slice( 0, itemsLimit );
useEffect( () => {
if ( lastItem ) {
const observer = new window.IntersectionObserver( ( [ entry ] ) => {
if ( entry.isIntersecting ) {
setPage( ( prevPage ) => prevPage + 1 );
}
} );
observer.observe( lastItem );
return () => observer.disconnect();
}
}, [ lastItem ] );
return (
<div className="font-library-modal__fonts-grid">
<VStack spacing={ 0 }>
{ title && (
<>
<Text className="font-library-modal__subtitle">
{ title }
</Text>
<Spacer margin={ 2 } />
</>
) }
<div className="font-library-modal__fonts-grid__main">
{ items.map( ( child, i ) => {
if ( i === itemsLimit - 1 ) {
return (
<div key={ child.key } ref={ setLastItem }>
{ child }
</div>
);
}
return <div key={ child.key }>{ child }</div>;
} ) }
</div>
</VStack>
</div>
);
}
export default FontsGrid;