generated from Weaverse/pilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblogs.tsx
More file actions
189 lines (182 loc) · 5.21 KB
/
blogs.tsx
File metadata and controls
189 lines (182 loc) · 5.21 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { createSchema } from "@weaverse/hydrogen";
import { useLoaderData } from "react-router";
import type { ArticleFragment, BlogQuery } from "storefront-api.generated";
import { Image } from "~/components/image";
import { Link } from "~/components/link";
import { RevealUnderline } from "~/components/reveal-underline";
import { layoutInputs, Section, type SectionProps } from "~/components/section";
import type { ImageAspectRatio } from "~/types/others";
import { cn } from "~/utils/cn";
import { calculateAspectRatio, getImageLoadingPriority } from "~/utils/image";
interface BlogsProps
extends Omit<ArticleCardProps, "article" | "blogHandle" | "loading">,
SectionProps {
ref: React.Ref<HTMLElement>;
layout: "blog" | "default";
}
export default function Blogs(props: BlogsProps) {
const {
ref,
layout,
showExcerpt,
showAuthor,
showDate,
showReadmore,
imageAspectRatio,
...rest
} = props;
const { blog, articles } = useLoaderData<
BlogQuery & { articles: ArticleFragment[] }
>();
if (blog) {
return (
<Section ref={ref} {...rest}>
<h4 className="text-center font-medium">{blog.title}</h4>
<div className="grid grid-cols-1 gap-x-4 gap-y-12 lg:grid-cols-3">
{articles.map((article, i) => (
<ArticleCard
key={article.id}
blogHandle={blog.handle}
article={article}
loading={getImageLoadingPriority(i, 2)}
showAuthor={showAuthor}
showExcerpt={showExcerpt}
showDate={showDate}
showReadmore={showReadmore}
imageAspectRatio={imageAspectRatio}
/>
))}
</div>
</Section>
);
}
return <Section ref={ref} {...rest} />;
}
export interface ArticleCardProps {
article: ArticleFragment;
blogHandle: string;
loading?: HTMLImageElement["loading"];
showDate: boolean;
showExcerpt: boolean;
showAuthor: boolean;
showReadmore: boolean;
imageAspectRatio: ImageAspectRatio;
className?: string;
}
export function ArticleCard({
blogHandle,
article,
loading,
showExcerpt,
showAuthor,
showDate,
showReadmore,
imageAspectRatio,
className,
}: ArticleCardProps) {
return (
<div className={cn("flex flex-col gap-5", className)}>
{article.image && (
<Link
to={`/blogs/${blogHandle}/${article.handle}`}
className="flex flex-col gap-5"
>
<Image
alt={article.image.altText || article.title}
data={article.image}
aspectRatio={calculateAspectRatio(article.image, imageAspectRatio)}
loading={loading}
sizes="(min-width: 768px) 50vw, 100vw"
/>
</Link>
)}
<div className="space-y-2.5">
<Link
to={`/blogs/${blogHandle}/${article.handle}`}
className="inline-block"
>
<RevealUnderline className="text-xl leading-6">
{article.title}
</RevealUnderline>
</Link>
<div className="flex items-center gap-2 text-gray-600 empty:hidden">
{showDate && <span className="block">{article.publishedAt}</span>}
{showDate && showAuthor && <span>•</span>}
{showAuthor && <span className="block">{article.author?.name}</span>}
</div>
{showExcerpt && (
<div className="line-clamp-2 text-gray-700 lg:line-clamp-4">
{article.excerpt}
</div>
)}
{showReadmore && (
<div>
<Link
to={`/blogs/${blogHandle}/${article.handle}`}
variant="underline"
>
Read more →
</Link>
</div>
)}
</div>
</div>
);
}
export const schema = createSchema({
type: "blogs",
title: "Blogs",
limit: 1,
enabledOn: {
pages: ["BLOG"],
},
settings: [
{ group: "Layout", inputs: layoutInputs },
{
group: "Article card",
inputs: [
{
type: "select",
name: "imageAspectRatio",
label: "Image aspect ratio",
defaultValue: "adapt",
configs: {
options: [
{ value: "adapt", label: "Adapt to image" },
{ value: "1/1", label: "Square (1/1)" },
{ value: "3/4", label: "Portrait (3/4)" },
{ value: "4/3", label: "Landscape (4/3)" },
{ value: "16/9", label: "Widescreen (16/9)" },
],
},
helpText:
'Learn more about image <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio" target="_blank" rel="noopener noreferrer">aspect ratio</a> property.',
},
{
type: "switch",
name: "showExcerpt",
label: "Show excerpt",
defaultValue: true,
},
{
type: "switch",
name: "showDate",
label: "Show date",
defaultValue: false,
},
{
type: "switch",
name: "showAuthor",
label: "Show author",
defaultValue: false,
},
{
type: "switch",
name: "showReadmore",
label: "Show read more",
defaultValue: true,
},
],
},
],
});