Skip to content

Commit 45efd8c

Browse files
committed
add random color gradient
1 parent 84879da commit 45efd8c

File tree

4 files changed

+105
-2
lines changed

4 files changed

+105
-2
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { html } from 'satori-html';
2+
import colors from 'tailwindcss/colors';
3+
4+
import { getRandomElementFromArray, limitString } from '@/utils/strings';
5+
6+
export interface TemplateProps {
7+
title: string;
8+
heroImageUrl: string;
9+
avatarImageUrl: string;
10+
siteUrl: string;
11+
}
12+
13+
const clrs = ['gray', 'indigo', 'yellow', 'blue', 'cyan', 'lime', 'sky'];
14+
const directions = ['to right', 'to bottom', '45deg'];
15+
const shades = [50, 100, 200];
16+
const rnd = getRandomElementFromArray;
17+
18+
const getRandomGradient = () =>
19+
`background: linear-gradient(${rnd(directions)}, ${colors[rnd(clrs)][rnd(shades)]}, ${colors[rnd(clrs)][rnd(shades)]})`;
20+
21+
const gradients = [
22+
`background: linear-gradient(to right, ${colors.gray[100]}, ${colors.gray[300]})`,
23+
`background: linear-gradient(to right, ${colors.indigo[200]}, ${colors.yellow[100]})`,
24+
`background: linear-gradient(to right, ${colors.blue[100]}, ${colors.blue[200]})`,
25+
`background: linear-gradient(to right, ${colors.fuchsia[200]}, ${colors.cyan[200]})`,
26+
`background: linear-gradient(to right, ${colors.teal[200]}, ${colors.lime[100]})`,
27+
`background: linear-gradient(to right, ${colors.sky[200]}, ${colors.sky[100]})`,
28+
`background: linear-gradient(to right, ${colors.sky[200]}, ${colors.cyan[100]})`,
29+
];
30+
31+
const templateHtml = ({ title, heroImageUrl, avatarImageUrl, siteUrl }: TemplateProps) => {
32+
// 2 rows - max 30 chars
33+
// 1 row - max 18 chars
34+
const isLongSiteUrl = siteUrl.length > 17;
35+
36+
// max 6 rows x 10-15 chars
37+
const limitedTitle = limitString(title, 70);
38+
39+
// const randomGradient = getRandomElementFromArray(gradients);
40+
const randomGradient = getRandomGradient();
41+
42+
return html`
43+
<div class="flex p-8 h-full" style="${randomGradient}">
44+
<div class="flex w-full flex-row justify-between text-slate-900">
45+
<!-- left column -->
46+
<div class="w-[550px] flex flex-col justify-between mr-6">
47+
<!-- title -->
48+
<div class="flex flex-grow text-6xl font-semibold mb-4">${limitedTitle}</div>
49+
50+
<!-- avatar and site -->
51+
<div class="flex items-center ${isLongSiteUrl ? 'flex-col justify-end items-start' : ''}">
52+
<img
53+
src=${avatarImageUrl}
54+
alt="Nemanja Mitic"
55+
width="120"
56+
height="120"
57+
class="rounded-full mr-8"
58+
/>
59+
<div class="flex items-center ${isLongSiteUrl ? 'mt-4 text-3xl' : 'text-4xl'}">
60+
<div>${siteUrl}</div>
61+
</div>
62+
</div>
63+
</div>
64+
65+
<!-- right column -->
66+
<div class="w-[550px] flex items-center">
67+
<img src="${heroImageUrl}" class="h-full w-full rounded-md" style="object-fit: cover" />
68+
</div>
69+
</div>
70+
</div>
71+
`;
72+
};
73+
74+
export default templateHtml;

src/libs/api/open-graph/template-html.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { html } from 'satori-html';
22

3+
import { getRandomGradient } from '@/utils/gradients';
34
import { limitString } from '@/utils/strings';
45

56
export interface TemplateProps {
@@ -17,8 +18,10 @@ const templateHtml = ({ title, heroImageUrl, avatarImageUrl, siteUrl }: Template
1718
// max 6 rows x 10-15 chars
1819
const limitedTitle = limitString(title, 70);
1920

21+
const randomGradient = getRandomGradient();
22+
2023
return html`
21-
<div class="flex p-8 h-full" style="background: linear-gradient(to right, #D1D5DB, #F3F4F6)">
24+
<div class="flex p-8 h-full" style="${randomGradient}">
2225
<div class="flex w-full flex-row justify-between text-slate-900">
2326
<!-- left column -->
2427
<div class="w-[550px] flex flex-col justify-between mr-6">
@@ -32,7 +35,7 @@ const templateHtml = ({ title, heroImageUrl, avatarImageUrl, siteUrl }: Template
3235
alt="Nemanja Mitic"
3336
width="120"
3437
height="120"
35-
class="rounded-full mr-8"
38+
class="rounded-full mr-8 border-2 border-gray-300"
3639
/>
3740
<div class="flex items-center ${isLongSiteUrl ? 'mt-4 text-3xl' : 'text-4xl'}">
3841
<div>${siteUrl}</div>

src/utils/gradients.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { default as twColors } from 'tailwindcss/colors';
2+
3+
import { getRandomElementFromArray as rnd } from '@/utils/strings';
4+
5+
import type { DefaultColors } from 'tailwindcss/types/generated/colors';
6+
7+
type ColorKeys = keyof DefaultColors;
8+
type ShadeKeys = keyof DefaultColors[ColorKeys];
9+
10+
const colors = ['gray', 'indigo', 'yellow', 'blue', 'cyan', 'lime', 'sky', 'white'] as ColorKeys[];
11+
const shades = [50, 100, 200] as ShadeKeys[];
12+
const directions = ['to right', 'to bottom', '45deg'];
13+
14+
// to support white
15+
const getRandomColor = () => {
16+
const rndColor = rnd(colors);
17+
return rndColor === 'white' ? rndColor : twColors[rndColor][rnd(shades)];
18+
};
19+
20+
export const getRandomGradient = () =>
21+
`background: linear-gradient(${rnd(directions)}, ${getRandomColor()}, ${getRandomColor()})`;
22+
23+
export const grayGradient = `background: linear-gradient(to right, ${twColors.gray[100]}, ${twColors.gray[300]})`;

src/utils/strings.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export const getRandomLengthSubstring = (inputString: string, length: number, ma
88
export const limitString = (str: string, maxLength: number) =>
99
str.length > maxLength ? str.slice(0, maxLength) + '...' : str;
1010

11+
export const getRandomElementFromArray = <T>(arr: T[]): T =>
12+
arr[Math.floor(Math.random() * arr.length)];
13+
1114
export const trimHttpProtocol = (url: string) => {
1215
const trailingSlashRegex = /\/$/;
1316
const protocolRegex = /^(https?:\/\/)/i;

0 commit comments

Comments
 (0)