Skip to content

Commit c7c12c2

Browse files
committed
refactored header component using clsx to create conditional classnames
1 parent fd9a20d commit c7c12c2

File tree

6 files changed

+41
-19
lines changed

6 files changed

+41
-19
lines changed

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"dependencies": {
1313
"@react-three/drei": "^9.120.4",
1414
"@react-three/fiber": "^8.17.10",
15+
"clsx": "^2.1.1",
1516
"react": "^18.3.1",
1617
"react-dom": "^18.3.1",
1718
"three": "^0.171.0"

src/App.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ import React from 'react';
22
import About from './components/About';
33
import Header from './components/Header';
44

5-
65
const App: React.FC = () => {
76
return (
87

9-
<div className="bg-black min-h-screen mt-0 flex items-center justify-center w-full h-full">
8+
<div className="bg-black min-h-screen mt-0 flex items-center justify-center w-full h-full">
109

1110
<div className="absolute top-0 left-0 p-5 hidden md:block">
12-
<Header text="trsctr.github.io" textSize="text-2xl" className="tracking-widest font-mono" hasGradient gradientColors="from-primary to-secondary"/>
11+
<Header text="trsctr.github.io" textSize="text-2xl" className="tracking-widest font-mono" hasGradient/>
1312
</div>
1413
<About title="Hello, my name is Otto" imageUrl="src/assets/meika.jpg" imageOnRight>
1514
<p className="mb-2">
@@ -23,8 +22,9 @@ const App: React.FC = () => {
2322
<p className="mt-2">I'm beginning to like Tailwind. It's useful and makes CSS less nervewracking than it could be.</p>
2423

2524
<p className="mt-2">Which is nice.</p>
26-
</About>
27-
</div>
25+
</About>
26+
</div>
27+
2828
);
2929
};
3030

src/components/About.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ const About: React.FC<AboutProps> = ( {title, imageUrl, children, imageOnRight =
2323
return (
2424
<div className="absolute flex left-0 top-0 md:top-1/4 md:left-32 w-full md:w-9/12 max-w-[900px]">
2525
<ContentBox className="rounded-lg shadow-lg">
26-
{title && <Header text={title} gradientColors="from-primary to-secondary" hasGradient/>}
26+
{title && <Header text={title} hasGradient/>}
2727
<TextImageBox imageUrl={imageUrl} imageOnRight={imageOnRight}>
2828
{children}
2929
</TextImageBox>
3030
<Footer/>
3131
</ContentBox>
32-
3332
</div>
34-
3533
);
3634
};
3735

src/components/Header.tsx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import clsx from 'clsx';
23

34
interface HeaderProps {
45
text: string;
@@ -19,16 +20,30 @@ interface HeaderProps {
1920
* @param hasGradient Whether to apply a gradient to the text.
2021
* @param textSize The size of the text.
2122
*/
22-
const Header: React.FC<HeaderProps> = ({ text, color = "text-text", gradientColors, className, hasGradient = false, textSize = "text-4xl lg:text-6xl" }) => {
23-
// tsekkaa clsx
24-
let gradientSettings = ""
25-
if (hasGradient && gradientColors)
26-
gradientSettings = "text-transparent bg-clip-text bg-gradient-to-r " + gradientColors;
27-
return (
28-
<h1 className={`${textSize} ${gradientSettings} ${color} font-bold leading-tight ${className}`}>
23+
const Header: React.FC<HeaderProps> = ({
24+
text, color = "text-text",
25+
gradientColors = "from-primary to-secondary",
26+
className,
27+
hasGradient = false,
28+
textSize = "text-4xl lg:text-6xl"}) => {
29+
30+
return (
31+
<h1 className={clsx(
32+
'font-bold leading-tight',
33+
className,
34+
textSize,
35+
{
36+
'text-transparent bg-clip-text bg-gradient-to-r': hasGradient,
37+
[gradientColors || '']: hasGradient,
38+
[color]: !hasGradient
39+
}
40+
)}>
2941
{text}
3042
</h1>
3143
);
3244
}
3345

34-
export default Header;
46+
export default Header;
47+
48+
// <h1 className={`${textSize} ${gradientSettings} ${color} font-bold leading-tight ${className}`}>
49+
// {text}

src/components/TextImageBox.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const TextImage: React.FC<Pick<TextImageBoxProps, 'imageUrl' | 'altText'>> = ({
2525
*/
2626
const TextImageBox: React.FC<TextImageBoxProps> = ({ imageUrl, children, imageOnRight = false, altText = "Image"}) => {
2727
return (
28-
<div className="flex flex-col md:flex-row text-text text-lg items-center md:items-start pt-5">
28+
<div className="flex flex-col md:flex-row text-text text-md items-center md:items-start pt-5">
2929
{!imageOnRight && imageUrl && (
3030
<TextImage imageUrl={imageUrl} altText={altText}/>
3131
)}
@@ -38,6 +38,4 @@ return (
3838
</div>)
3939
};
4040

41-
42-
4341
export default TextImageBox;

0 commit comments

Comments
 (0)