PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 4.0.7
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v4.0.7
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 2.3.5 2.3.6 2.4.0 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.15 2.4.16 2.4.17 2.4.18 2.4.19 2.4.2 2.4.20 2.4.21 All 88 releases
post-carousel / src / blocks / post-grid-three / loopContent.js

loopContent.js in Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News 4.0.7, at src/blocks/post-grid-three/loopContent.js

112 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { Fragment, useEffect, useState } from "@wordpress/element";
2 import { skipItemsForGridThreeTopSection } from "../../controls/constants";
3 import { useDeviceType } from "../../controls/controls";
4 import TemplateOne from "../shared/templates/templateCards/templateOne";
5
6 const LoopContent = ({
7 posts,
8 attributes,
9 bottomStyle = false,
10 Masonry = false,
11 ResponsiveMasonry = Fragment,
12 containerRef,
13 }) => {
14 const { postGridLayout, contentAreaHeight, contentVerticalPosition, gridThreeColumns, masonryGap } = attributes;
15
16 const deviceType = useDeviceType();
17
18 const skipPostForLargeLayouts = "Mobile" !== deviceType ? skipItemsForGridThreeTopSection[postGridLayout] : 0;
19
20 const masonryGapCss = `${masonryGap?.device?.Desktop}${masonryGap?.unit?.Desktop}`;
21
22 const columnsBreakPoints = Masonry
23 ? {
24 columnsCountBreakPoints: {
25 350: gridThreeColumns?.device?.Mobile,
26 750: gridThreeColumns?.device?.Tablet,
27 900: gridThreeColumns?.device?.Desktop,
28 },
29 }
30 : {};
31
32 const gutter = Masonry
33 ? {
34 gutter: masonryGapCss,
35 }
36 : {};
37 const [selectedPostId, setSelectedPostId] = useState(null);
38
39 useEffect(() => {
40 const handleClickOutside = (event) => {
41 if (containerRef.current && !containerRef.current.contains(event.target)) {
42 setSelectedPostId(null);
43 }
44 const targetPostId = parseInt(event.target.closest(".sp-smart-post-card-image")?.dataset?.post_id);
45 if (targetPostId && targetPostId !== selectedPostId) {
46 setSelectedPostId(null);
47 }
48 };
49
50 document.addEventListener("mousedown", handleClickOutside);
51 return () => document.removeEventListener("mousedown", handleClickOutside);
52 }, [selectedPostId]);
53
54 return (
55 <>
56 {Masonry && (
57 <ResponsiveMasonry {...columnsBreakPoints}>
58 <Masonry {...gutter}>
59 {posts
60 ?.slice(
61 bottomStyle ? skipPostForLargeLayouts : 0,
62 bottomStyle ? posts?.length : skipPostForLargeLayouts
63 )
64 ?.map((post, i) => (
65 <TemplateOne
66 key={post?.post_id}
67 data={post}
68 posts={posts}
69 containerVAlign={false}
70 horizontalAlign={false}
71 contentPosition={contentVerticalPosition}
72 attributes={attributes}
73 contentHeight={`${
74 contentAreaHeight?.device?.[deviceType] + contentAreaHeight?.unit?.[deviceType]
75 }`}
76 layoutName={postGridLayout}
77 isSelected={selectedPostId === post?.post_id}
78 onSelect={() => setSelectedPostId(post?.post_id)}
79 />
80 ))}
81 </Masonry>
82 </ResponsiveMasonry>
83 )}
84 {!Masonry &&
85 posts
86 ?.slice(
87 bottomStyle ? skipPostForLargeLayouts : 0,
88 bottomStyle ? posts?.length : skipPostForLargeLayouts
89 )
90 ?.map((post, i) => (
91 <TemplateOne
92 key={post?.post_id}
93 data={post}
94 posts={posts}
95 containerVAlign={false}
96 horizontalAlign={false}
97 contentPosition={contentVerticalPosition}
98 attributes={attributes}
99 contentHeight={`${
100 contentAreaHeight?.device?.[deviceType] + contentAreaHeight?.unit?.[deviceType]
101 }`}
102 layoutName={postGridLayout}
103 isSelected={selectedPostId === post?.post_id}
104 onSelect={() => setSelectedPostId(post?.post_id)}
105 />
106 ))}
107 </>
108 );
109 };
110
111 export default LoopContent;
112