|
| 1 | +import React from "react"; |
| 2 | + |
| 3 | +import PropTypes from "prop-types"; |
| 4 | + |
| 5 | +function classNames(...classes) { |
| 6 | + return classes.filter(Boolean).join(" "); |
| 7 | +} |
| 8 | + |
| 9 | +export default function BlogSection({ |
| 10 | + title, |
| 11 | + subtitle, |
| 12 | + posts = [], |
| 13 | + variants = "grid-image", |
| 14 | + orientation = "row", |
| 15 | +}) { |
| 16 | + const containerClasses = classNames( |
| 17 | + "bg-white py-20 sm:py-28", |
| 18 | + orientation === "column" ? "max-w-2xl lg:max-w-4xl" : "max-w-7xl" |
| 19 | + ); |
| 20 | + |
| 21 | + const headerClasses = classNames( |
| 22 | + "text-balance text-4xl font-semibold tracking-tight text-gray-900 sm:text-5xl", |
| 23 | + orientation !== "column" && "text-center" |
| 24 | + ); |
| 25 | + |
| 26 | + const gridClasses = classNames( |
| 27 | + orientation === "column" |
| 28 | + ? "mt-10 space-y-16 border-t border-gray-200 pt-10 sm:mt-16 sm:pt-16" |
| 29 | + : "mx-auto mt-16 grid max-w-2xl grid-cols-1 gap-x-8 gap-y-20 lg:mx-0 lg:max-w-none lg:grid-cols-3", |
| 30 | + variants === "grid-text" && "border-t border-gray-200" |
| 31 | + ); |
| 32 | + |
| 33 | + return ( |
| 34 | + <div className={containerClasses}> |
| 35 | + <div className="mx-auto px-6 lg:px-8"> |
| 36 | + <div className="mx-auto"> |
| 37 | + <h2 className={headerClasses}>{title}</h2> |
| 38 | + {subtitle && ( |
| 39 | + <p className="mt-2 text-lg/8 text-gray-600">{subtitle}</p> |
| 40 | + )} |
| 41 | + </div> |
| 42 | + <div className={gridClasses}> |
| 43 | + {posts?.map((post) => ( |
| 44 | + <article |
| 45 | + key={post.id} |
| 46 | + className={classNames( |
| 47 | + variants === "grid-image" && orientation === "column" |
| 48 | + ? "relative isolate flex flex-col gap-8 lg:flex-row" |
| 49 | + : "flex flex-col items-start justify-between" |
| 50 | + )} |
| 51 | + > |
| 52 | + {variants === "grid-image" && ( |
| 53 | + <div |
| 54 | + className={classNames( |
| 55 | + orientation === "column" |
| 56 | + ? "relative aspect-[16/9] sm:aspect-[2/1] lg:aspect-square lg:w-64 lg:shrink-0" |
| 57 | + : "relative w-full" |
| 58 | + )} |
| 59 | + > |
| 60 | + <img |
| 61 | + alt={post.title} |
| 62 | + src={post.imageUrl} |
| 63 | + className={classNames( |
| 64 | + orientation === "column" |
| 65 | + ? "absolute inset-0 h-full w-full rounded-2xl bg-gray-50 object-cover" |
| 66 | + : "aspect-[16/9] w-full rounded-2xl bg-gray-100 object-cover sm:aspect-[2/1] lg:aspect-[3/2]" |
| 67 | + )} |
| 68 | + /> |
| 69 | + <div className="absolute inset-0 rounded-2xl ring-1 ring-inset ring-gray-900/10" /> |
| 70 | + </div> |
| 71 | + )} |
| 72 | + <div className="max-w-xl"> |
| 73 | + <div className="mt-8 flex items-center gap-x-4 text-xs"> |
| 74 | + <time dateTime={post.datetime} className="text-gray-500"> |
| 75 | + {post.date} |
| 76 | + </time> |
| 77 | + <a |
| 78 | + href={post.category.href} |
| 79 | + className="relative z-10 rounded-full bg-gray-50 px-3 py-1.5 font-medium text-gray-600 hover:bg-gray-100" |
| 80 | + > |
| 81 | + {post.category.title} |
| 82 | + </a> |
| 83 | + </div> |
| 84 | + <div className="group relative"> |
| 85 | + <h3 className="mt-3 text-lg/6 font-semibold text-gray-900 group-hover:text-gray-600"> |
| 86 | + <a href={post.href}> |
| 87 | + <span className="absolute inset-0" /> |
| 88 | + {post.title} |
| 89 | + </a> |
| 90 | + </h3> |
| 91 | + <p className="mt-5 line-clamp-3 text-sm/6 text-gray-600"> |
| 92 | + {post.description} |
| 93 | + </p> |
| 94 | + </div> |
| 95 | + <div className="relative mt-8 flex items-center gap-x-4"> |
| 96 | + <img |
| 97 | + alt={post.author.name} |
| 98 | + src={post.author.imageUrl} |
| 99 | + className="h-10 w-10 rounded-full bg-gray-100" |
| 100 | + /> |
| 101 | + <div className="text-sm/6"> |
| 102 | + <p className="font-semibold text-gray-900"> |
| 103 | + <a href={post.author.href}> |
| 104 | + <span className="absolute inset-0" /> |
| 105 | + {post.author.name} |
| 106 | + </a> |
| 107 | + </p> |
| 108 | + <p className="text-gray-600">{post.author.role}</p> |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + </div> |
| 112 | + </article> |
| 113 | + ))} |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + ); |
| 118 | +} |
| 119 | + |
| 120 | +BlogSection.propTypes = { |
| 121 | + title: PropTypes.string.isRequired, |
| 122 | + subtitle: PropTypes.string, |
| 123 | + posts: PropTypes.arrayOf( |
| 124 | + PropTypes.shape({ |
| 125 | + id: PropTypes.number.isRequired, |
| 126 | + title: PropTypes.string.isRequired, |
| 127 | + href: PropTypes.string.isRequired, |
| 128 | + description: PropTypes.string.isRequired, |
| 129 | + imageUrl: PropTypes.string.isRequired, |
| 130 | + date: PropTypes.string.isRequired, |
| 131 | + datetime: PropTypes.string.isRequired, |
| 132 | + category: PropTypes.shape({ |
| 133 | + title: PropTypes.string.isRequired, |
| 134 | + href: PropTypes.string.isRequired, |
| 135 | + }).isRequired, |
| 136 | + author: PropTypes.shape({ |
| 137 | + name: PropTypes.string.isRequired, |
| 138 | + role: PropTypes.string.isRequired, |
| 139 | + href: PropTypes.string.isRequired, |
| 140 | + imageUrl: PropTypes.string.isRequired, |
| 141 | + }).isRequired, |
| 142 | + }) |
| 143 | + ).isRequired, |
| 144 | + variants: PropTypes.oneOf(["grid-image", "grid-text"]), |
| 145 | + orientation: PropTypes.oneOf(["row", "column"]), |
| 146 | +}; |
| 147 | + |
| 148 | +// ImageGrid.defaultProps = { |
| 149 | +// subtitle: "", |
| 150 | +// variants: "grid-image", |
| 151 | +// orientation: "row", |
| 152 | +// }; |
0 commit comments