Skip to content

Commit ab6a3a5

Browse files
committed
made the Codeblock copying option more accessible
1 parent 16883a6 commit ab6a3a5

File tree

5 files changed

+56
-56
lines changed

5 files changed

+56
-56
lines changed

client/src/App.tsx

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,18 @@ import { useState } from "react";
22
import Header from "./sections/Header";
33
import Options from "./sections/Options";
44
import Preview from "./sections/Preview";
5-
import { Line } from "./types/line";
65

76
const App = () => {
8-
const BASE_LINK = "https://github-readme-tech-stack.vercel.app/api/cards";
9-
const [link, setLink] = useState<string>(BASE_LINK);
10-
11-
const generateLink = (
12-
title: string,
13-
lineCount: string,
14-
theme: string,
15-
align: string,
16-
lines: Line[]
17-
): string => {
18-
title = title.replace(/[ ]/g, "%20");
19-
let res = `${BASE_LINK}?title=${title}&lineCount=${lineCount}&theme=${theme}&align=${align}`;
20-
21-
for (const l of lines) {
22-
if (l.badges.length < 1) {
23-
continue;
24-
}
25-
26-
let line = `&line${l.lineNumber}=`;
27-
for (const b of l.badges) {
28-
const color = b.color.replace("#", "");
29-
line += `${b.iconName},${b.label},${color};`;
30-
}
31-
32-
res += line;
33-
}
34-
35-
return res;
36-
};
7+
const [link, setLink] = useState<string>(
8+
"https://github-readme-tech-stack.vercel.app/api/cards"
9+
);
3710

3811
return (
3912
<div className="bg-gh-bg w-screen min-h-screen overflow-x-hidden select-none pb-5">
4013
<Header />
4114

4215
<div className="flex gap-4 mx-10 md:mx-48 mt-6 flex-col lg:flex-row">
43-
<Options
44-
generateLink={generateLink}
45-
setLink={(link: string) => setLink(link)}
46-
/>
16+
<Options setLink={(link: string) => setLink(link)} />
4717
<Preview link={link} />
4818
</div>
4919
</div>

client/src/components/input/LineInput.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FC, useState } from "react";
22
import { Badge, Line } from "../../types/line";
3-
import { AiOutlinePlusCircle } from "react-icons/ai";
3+
import { AiOutlinePlus } from "react-icons/ai";
44
import BlurOverlay from "../popups/BlurOverlay";
55
import LinePopup from "../popups/LinePopup";
66

@@ -49,12 +49,11 @@ const LineInput: FC<InputProps> = (props) => {
4949

5050
<button
5151
type="button"
52-
title="Edit Line"
5352
className="cursor-pointer ml-auto text-gh-text-secondary
5453
hover:text-gh-blue transition-all duration-150"
5554
onClick={() => setIsPopupOpen(true)}
5655
>
57-
<AiOutlinePlusCircle />
56+
<AiOutlinePlus />
5857
</button>
5958
</div>
6059
</div>

client/src/components/text/Codeblock.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
11
import { FC, useState } from "react";
2-
import { MdContentCopy } from "react-icons/md";
2+
import { MdContentCopy, MdCheck } from "react-icons/md";
33

44
interface CodeblockProps {
55
code: string;
66
}
77

88
const Codeblock: FC<CodeblockProps> = (props) => {
99
const [hovered, setHovered] = useState<boolean>(false);
10+
const [clicked, setClicked] = useState<boolean>(false);
1011

1112
return (
1213
<div className="relative">
1314
<button
1415
type="button"
1516
onMouseEnter={() => setHovered(true)}
16-
onClick={() => navigator.clipboard.writeText(props.code)}
17+
onClick={() => {
18+
navigator.clipboard.writeText(props.code);
19+
setClicked(true);
20+
}}
21+
onMouseLeave={() => setClicked(false)}
1722
className={`absolute top-1/3 right-5 bg-gh-bg-secondary
18-
border border-solid border-gh-button-border cursor-pointer
19-
transition-all duration-75 p-2 -translate-y-1/2 text-base rounded-md
20-
text-gh-text-secondary hover:border-gh-button-border-active
21-
hover:bg-gh-button shadow-xl z-10
22-
${hovered ? "scale-100 opacity-100" : "scale-75 opacity-0"}`}
23+
border border-solid cursor-pointer -translate-y-1/2 text-base rounded-md
24+
hover:bg-gh-button shadow-xl z-10 transition-all duration-75 p-2
25+
${hovered ? "scale-100 opacity-100" : "scale-75 opacity-0"}
26+
${
27+
clicked
28+
? "border-gh-green-active hover:border-gh-green-active text-gh-green-active"
29+
: "border-gh-button-border hover:border-gh-button-border-active text-gh-text-secondary "
30+
}`}
2331
>
24-
<MdContentCopy />
32+
{clicked ? <MdCheck /> : <MdContentCopy />}
2533
</button>
2634

2735
<div

client/src/sections/Options.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,14 @@ import { FC, useCallback, useEffect, useState } from "react";
1616
import LineInput from "../components/input/LineInput";
1717
import { Line } from "../types/line";
1818
import axios from "axios";
19+
import { generateLink } from "../utils/generate";
1920

2021
interface OptionsProps {
21-
generateLink: (
22-
title: string,
23-
lineCount: string,
24-
theme: string,
25-
align: string,
26-
lines: Line[]
27-
) => string;
2822
setLink: (link: string) => void;
2923
}
3024

3125
const Options: FC<OptionsProps> = (props) => {
32-
const [themes, setThemes] = useState<string[]>([]);
26+
const [themes, setThemes] = useState<string[]>(["github", "github_dark"]);
3327

3428
useEffect(() => {
3529
axios
@@ -131,9 +125,7 @@ const Options: FC<OptionsProps> = (props) => {
131125
<GreenButton
132126
icon={IoHammerOutline}
133127
onClick={() => {
134-
props.setLink(
135-
props.generateLink(title, lineCount, theme, align, lines)
136-
);
128+
props.setLink(generateLink(title, lineCount, theme, align, lines));
137129
}}
138130
text="Generate"
139131
/>

client/src/utils/generate.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Line } from "../types/line";
2+
3+
export const generateLink = (
4+
title: string,
5+
lineCount: string,
6+
theme: string,
7+
align: string,
8+
lines: Line[]
9+
): string => {
10+
// replace every space with %20
11+
title = title.replace(/[ ]/g, "%20");
12+
13+
let res = `https://github-readme-tech-stack.vercel.app/api/cards?title=${title}&lineCount=${lineCount}&theme=${theme}&align=${align}`;
14+
15+
for (const l of lines) {
16+
// if the line doesn't contain badges
17+
if (l.badges.length < 1) {
18+
continue;
19+
}
20+
21+
let line = `&line${l.lineNumber}=`;
22+
for (const b of l.badges) {
23+
const color = b.color.replace("#", "");
24+
line += `${b.iconName},${b.label},${color};`;
25+
}
26+
27+
res += line;
28+
}
29+
30+
return res;
31+
};

0 commit comments

Comments
 (0)