Skip to content

Commit df0f13a

Browse files
authored
feat(module): improve-styling-for-bash-code-snippets (#272)
* fix(button): fix dark mode toggle click - I201 Signed-off-by: Dharma Teja <[email protected]> * fix(button): fix dark mode toggle click - I201 Signed-off-by: Dharma Teja <[email protected]> * fix(button): fix dark mode toggle click - I201 Signed-off-by: Dharma Teja <[email protected]> * update is checked Signed-off-by: Dharma Teja <[email protected]> * fix(button): fix dark mode toggle click - I201 Signed-off-by: Dharma Teja <[email protected]> * fix(button): fix dark mode toggle click - I201 Signed-off-by: Dharma Teja <[email protected]> * fix(button): fix dark mode toggle click - I201 Signed-off-by: Dharma Teja <[email protected]> * update is checked Signed-off-by: Dharma Teja <[email protected]> * feat(module): improve-styling-for-bash-code-snippets Signed-off-by: Dharma Teja <[email protected]> --------- Signed-off-by: Dharma Teja <[email protected]>
1 parent c0b80b8 commit df0f13a

File tree

2 files changed

+83
-50
lines changed

2 files changed

+83
-50
lines changed

src/components/Content.tsx

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ import {
77
ContentContainer,
88
NavigationButtons,
99
NavigationButton,
10+
CodeBlockContainer,
11+
CopyButton,
1012
} from "../styles/components/Content";
1113
import {
1214
LoadingOutlined,
1315
LeftOutlined,
1416
RightOutlined,
17+
CopyOutlined,
18+
CheckOutlined,
1519
} from "@ant-design/icons";
1620
import { Spin } from "antd";
1721
import fetchContent from "../utils/fetchContent";
@@ -23,6 +27,7 @@ const LearnContent: React.FC<LearnContentProps> = ({ file }) => {
2327
const [content, setContent] = useState<string | null>(null);
2428
const [loading, setLoading] = useState(true);
2529
const [error, setError] = useState<string | null>(null);
30+
const [copied, setCopied] = useState<string | null>(null);
2631
const navigate = useNavigate();
2732

2833
useEffect(() => {
@@ -59,6 +64,12 @@ const LearnContent: React.FC<LearnContentProps> = ({ file }) => {
5964
}
6065
};
6166

67+
const copyToClipboard = (code: string) => {
68+
navigator.clipboard.writeText(code);
69+
setCopied(code);
70+
setTimeout(() => setCopied(null), 2000);
71+
};
72+
6273
if (loading) {
6374
return (
6475
<div
@@ -88,11 +99,20 @@ const LearnContent: React.FC<LearnContentProps> = ({ file }) => {
8899
<ReactMarkdown
89100
rehypePlugins={[rehypeRaw, rehypeHighlight]}
90101
components={{
91-
img: ({ ...props }) => (
92-
<div className="image-container">
93-
<img {...props} alt={props.alt || ""} />
94-
</div>
95-
),
102+
pre: ({ children }) => {
103+
const codeElement = React.Children.toArray(children)[0] as React.ReactElement;
104+
if (!codeElement || typeof codeElement !== "object") return <pre>{children}</pre>;
105+
106+
const codeText = codeElement.props.children || "";
107+
return (
108+
<CodeBlockContainer>
109+
<pre>{children}</pre>
110+
<CopyButton onClick={() => copyToClipboard(codeText)}>
111+
{copied === codeText ? <CheckOutlined /> : <CopyOutlined />}
112+
</CopyButton>
113+
</CodeBlockContainer>
114+
);
115+
},
96116
}}
97117
>
98118
{content}

src/styles/components/Content.ts

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,40 @@
11
import styled from "styled-components";
22

33
export const ContentContainer = styled.div`
4-
padding: 20px;
5-
max-width: 900px;
4+
padding: 1.25rem;
5+
max-width: 56.25rem;
66
width: 100%;
77
margin: 0 auto;
8-
background-color: var(--bg-color) !important;
9-
border-radius: 8px;
10-
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
8+
background-color: var(--bg-color);
9+
border-radius: 0.5rem;
10+
box-shadow: 0 0.25rem 0.5rem rgba(0, 0, 0, 0.1);
1111
box-sizing: border-box;
1212
13-
h1,
14-
h2,
15-
h3,
16-
h4,
17-
h5,
18-
h6 {
13+
h1, h2, h3, h4, h5, h6 {
1914
margin-top: 1em;
2015
margin-bottom: 0.5em;
21-
color: var(--text-color) !important;
16+
color: var(--text-color);
2217
}
2318
2419
p {
2520
line-height: 1.6;
2621
margin-bottom: 1em;
27-
color: var(--text-color) !important;
22+
color: var(--text-color);
2823
}
2924
3025
a {
3126
color: #19c6c7;
3227
text-decoration: none;
33-
3428
&:hover {
3529
text-decoration: underline;
3630
}
3731
}
3832
39-
ul,
40-
ol {
33+
ul, ol {
4134
margin: 1em 0;
42-
4335
li {
4436
margin-bottom: 0.5em;
45-
color: var(--text-color) !important;
37+
color: var(--text-color);
4638
}
4739
}
4840
@@ -58,40 +50,76 @@ export const ContentContainer = styled.div`
5850
justify-content: center;
5951
}
6052
61-
@media (max-width: 1200px) {
53+
pre {
54+
background-color: #282c34;
55+
color: white;
56+
padding: 0.75rem;
57+
border-radius: 0.3125rem;
58+
overflow-x: auto;
59+
font-size: 0.875rem;
60+
margin: 0.625rem 0;
61+
}
62+
63+
code {
64+
font-family: "Fira Code", monospace;
65+
white-space: pre-wrap;
66+
}
67+
68+
@media (max-width: 75rem) {
6269
max-width: 90%;
6370
}
6471
65-
@media (max-width: 768px) {
66-
padding: 15px;
72+
@media (max-width: 48rem) {
73+
padding: 0.9375rem;
74+
}
75+
`;
76+
77+
export const CodeBlockContainer = styled.div`
78+
position: relative;
79+
margin: 0.625rem 0;
80+
`;
81+
82+
export const CopyButton = styled.button`
83+
position: absolute;
84+
top: 0.5rem;
85+
right: 0.625rem;
86+
border: none;
87+
background: rgba(255, 255, 255, 0.2);
88+
color: white;
89+
cursor: pointer;
90+
padding: 0.3125rem;
91+
border-radius: 0.25rem;
92+
font-size: 0.75rem;
93+
94+
&:hover {
95+
background: rgba(255, 255, 255, 0.4);
6796
}
6897
`;
6998

7099
export const NavigationButtons = styled.div`
71-
margin-top: 60px;
72-
margin-bottom: 20px;
100+
margin-top: 3.75rem;
101+
margin-bottom: 1.25rem;
73102
display: flex;
74103
justify-content: space-between;
75104
76-
@media (max-width: 768px) {
105+
@media (max-width: 48rem) {
77106
flex-direction: column;
78107
align-items: stretch;
79-
80108
button {
81-
margin-bottom: 10px;
109+
margin-bottom: 0.625rem;
82110
}
83111
}
84112
`;
85113

86114
export const NavigationButton = styled.button`
87-
padding: 10px 20px;
88-
border: 2px solid #19c6c7;
89-
border-radius: 4px;
115+
padding: 0.625rem 1.25rem;
116+
border: 0.125rem solid #19c6c7;
117+
border-radius: 0.25rem;
90118
background-color: white;
91119
color: #1b2540;
92120
cursor: pointer;
93121
font-weight: 600;
94-
font-size: 16px;
122+
font-size: 1rem;
95123
display: flex;
96124
align-items: center;
97125
justify-content: center;
@@ -108,19 +136,4 @@ export const NavigationButton = styled.button`
108136
color: white;
109137
text-decoration: underline;
110138
}
111-
112-
svg {
113-
margin-right: 8px;
114-
}
115-
116-
&:nth-child(1) svg {
117-
margin-right: 8px;
118-
}
119-
120-
&:nth-child(2) {
121-
svg {
122-
margin-right: 0;
123-
margin-left: 8px;
124-
}
125-
}
126139
`;

0 commit comments

Comments
 (0)