PluginProbe
Extendify / 3.1.0
Extendify v3.1.0
3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 0.7.0 All 126 releases
extendify / src / Library / components / ModalContent.jsx

ModalContent.jsx in Extendify 3.1.0, at src/Library/components/ModalContent.jsx

96 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { BlockPreviewButton } from '@library/components/BlockPreviewButton';
2 import { usePatterns } from '@library/hooks/usePatterns';
3 import { useSiteImages } from '@library/hooks/useSiteImages';
4 import { replacePatternImages } from '@library/util/replace-pattern-images';
5 import { Spinner } from '@wordpress/components';
6 import { useEffect, useMemo, useState } from '@wordpress/element';
7 import { __ } from '@wordpress/i18n';
8 import { useInView } from 'react-intersection-observer';
9 import Masonry from 'react-masonry-css';
10
11 export const ModalContent = ({ insertPattern, category }) => {
12 const { data, isLoading, setSize } = usePatterns({
13 category,
14 });
15 const { siteImages } = useSiteImages();
16 const sessionSeed = useMemo(() => Math.random().toString(36), []);
17 const [showLoading, setShowLoading] = useState(true);
18 const [loadMoreRef, inView] = useInView();
19 const noMore = data?.at(-1)?.length < 9; // hard coded for now
20
21 useEffect(() => {
22 if (isLoading) {
23 return setShowLoading(true);
24 }
25 const id = setTimeout(() => {
26 setShowLoading(false);
27 }, 750);
28 return () => clearTimeout(id);
29 }, [isLoading]);
30
31 useEffect(() => {
32 if (!inView || isLoading) return;
33 setSize((size) => size + 1);
34 }, [inView, isLoading, setSize]);
35
36 if (isLoading || !data?.length) {
37 return (
38 <div className="absolute inset-0 flex flex-col items-center justify-center text-center">
39 <Spinner />
40 <span className="sr-only">
41 {__('Loading Patterns...', 'extendify-local')}
42 </span>
43 </div>
44 );
45 }
46
47 return (
48 <>
49 <Masonry
50 breakpointCols={{
51 default: 3,
52 1600: 2,
53 1000: 1,
54 783: 2,
55 600: 1,
56 }}
57 columnClassName=""
58 className="relative flex w-full gap-6 p-8 pt-2"
59 >
60 {data.map((p) =>
61 p.map(({ id, code, patternReplacementCode }) => (
62 <BlockPreviewButton
63 key={id}
64 insertPattern={insertPattern}
65 code={replacePatternImages(
66 patternReplacementCode ?? code,
67 siteImages,
68 `${id}-${sessionSeed}`,
69 )}
70 />
71 )),
72 )}
73 </Masonry>
74 {showLoading ? (
75 <div className="absolute inset-0 flex flex-col items-center justify-center text-center">
76 <Spinner />
77 <span className="sr-only">
78 {__('Loading Patterns...', 'extendify-local')}
79 </span>
80 </div>
81 ) : null}
82 {showLoading || noMore ? null : (
83 <div
84 ref={loadMoreRef}
85 className="mb-6 mt-6 flex w-full justify-center md:mt-2"
86 >
87 <Spinner />
88 <span className="sr-only">
89 {__('Loading more patterns...', 'extendify-local')}
90 </span>
91 </div>
92 )}
93 </>
94 );
95 };
96