Skip to content

Commit 55e9219

Browse files
committed
🪶 Feat: Dynamic Import Theme Reactive SVG
1 parent f0c8630 commit 55e9219

File tree

5 files changed

+42
-56
lines changed

5 files changed

+42
-56
lines changed
Lines changed: 1 addition & 1 deletion
Loading
Lines changed: 1 addition & 1 deletion
Loading
Lines changed: 1 addition & 1 deletion
Loading
Lines changed: 1 addition & 1 deletion
Loading

‎world/skills.tsx‎

Lines changed: 38 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Image from "next/image";
33
import React, { useEffect, useState } from "react";
44
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
55
import { faToolbox } from "@fortawesome/free-solid-svg-icons";
6-
import IconCloud from "@/world/effects/iconCloud"
6+
import IconCloud from "@/world/effects/iconCloud";
77

88
export default function Skills() {
99
// █▀▀ █▀▀ ▀█▀ █▀▀ █░█ █▀ █▄▀ █ █░░ █░░ █▀
@@ -43,26 +43,18 @@ export default function Skills() {
4343
return <p>Loading...</p>;
4444
}
4545

46-
// █▀█ █▀▀ █▄░█ █▀▄ █▀▀ █▀█ █▀ █▄▀ █ █░░ █░░ █▀
47-
// █▀▄ ██▄ █░▀█ █▄▀ ██▄ █▀▄ ▄█ █░█ █ █▄▄ █▄▄ ▄█
48-
// Prints skills from given category
49-
// Syntax: renderSkills("category", data.category)
50-
// Example: renderSkills("languages", skills.languages)
51-
const renderSkills = (title: string, skillList: Skill[]) => (
52-
<div className="w-full grid sm:grid-cols-4 md:grid-cols-6 lg:grid-cols-8 gap-4 text-center py-2 sm:px-0">
53-
{skillList.map((skill, index) => (
54-
<div key={`skill-${index}`} className="block p-4">
55-
<Image
56-
src={`/ico/${title}/${skill.icon}.svg`}
57-
alt={`${skill.name} icon`}
58-
width={150}
59-
height={150}
60-
data-blobity-tooltip={`${skill.name}`}
61-
/>
62-
</div>
63-
))}
64-
</div>
65-
);
46+
// █▀ █░█ █▀▀ █▀█ █ █▀▄▀█ █▀█ █▀█ █▀█ ▀█▀
47+
// ▄█ ▀▄▀ █▄█ █▀▄ █ █░▀░█ █▀▀ █▄█ █▀▄ ░█░
48+
// Function to dynamically import SVGs as React Component using SVGR
49+
const importSvgIcon = (title: String, icon: String) => {
50+
try {
51+
const IconComponent = require(`/public/ico/${title}/${icon}.svg`).default;
52+
return IconComponent;
53+
} catch (error) {
54+
console.error("Error importing SVG:", error);
55+
return null;
56+
}
57+
};
6658

6759
// █▀█ █▀▀ █▄░█ █▀▄ █▀▀ █▀█ ▄▀█ █░░ █░░ █▀ █▄▀ █ █░░ █░░ █▀
6860
// █▀▄ ██▄ █░▀█ █▄▀ ██▄ █▀▄ █▀█ █▄▄ █▄▄ ▄█ █░█ █ █▄▄ █▄▄ ▄█
@@ -75,23 +67,28 @@ export default function Skills() {
7567
// Using loops over .map() as Heap/GC allocation in JS is expensive.
7668
for (let index = 0; index < skillList.length; index++) {
7769
const skill = skillList[index];
78-
skillElements.push(
79-
<div
80-
key={`${title}-${index}`}
81-
className="place-content-center p-5 bg-white border border-gray-200 rounded-lg shadow hover:bg-gray-100 dark:bg-[#161D1F] dark:border-gray-700 dark:hover:bg-gray-700"
82-
data-blobity-tooltip={`${skill.name}`}
83-
data-blobity-magnetic="false"
84-
>
85-
<Image
86-
src={`/ico/${title}/${skill.icon}.svg`}
87-
alt={`${skill.name} icon`}
70+
const IconComponent = importSvgIcon(title, skill.icon);
71+
72+
if (IconComponent) {
73+
skillElements.push(
74+
<div
75+
key={`${title}-${index}`}
76+
className="place-content-center p-5 bg-white border border-gray-200 rounded-lg shadow hover:bg-gray-100 dark:bg-[#161D1F] dark:border-gray-700 dark:hover:bg-gray-700"
8877
data-blobity-tooltip={`${skill.name}`}
89-
data-blobity-magnetic="true"
90-
width={100}
91-
height={100}
92-
/>
93-
</div>
94-
);
78+
data-blobity-magnetic="false"
79+
>
80+
<IconComponent
81+
className="w-full h-full object-contain text-gray-800 dark:text-gray-200"
82+
src={`/ico/${title}/${skill.icon}.svg`}
83+
alt={`${skill.name} icon`}
84+
data-blobity-tooltip={`${skill.name}`}
85+
data-blobity-magnetic="true"
86+
width={100}
87+
height={100}
88+
/>
89+
</div>,
90+
);
91+
}
9592
}
9693
}
9794
}
@@ -105,9 +102,7 @@ export default function Skills() {
105102
const skillIcons = [];
106103
if (skills) {
107104
for (const skillList of Object.values(skills)) {
108-
skillIcons.push(
109-
...skillList.map((skill) => skill.icon)
110-
);
105+
skillIcons.push(...skillList.map((skill) => skill.icon));
111106
}
112107
}
113108
return skillIcons;
@@ -116,19 +111,14 @@ export default function Skills() {
116111
const skillNames = [];
117112
if (skills) {
118113
for (const skillList of Object.values(skills)) {
119-
skillNames.push(
120-
...skillList.map((skill) => skill.name)
121-
);
114+
skillNames.push(...skillList.map((skill) => skill.name));
122115
}
123116
}
124117
return skillNames;
125118
};
126119

127120
return (
128-
<section
129-
id="skills"
130-
className="px-3 md:px-[100px] py-[100px]"
131-
>
121+
<section id="skills" className="px-3 md:px-[100px] py-[100px]">
132122
<h2>
133123
<FontAwesomeIcon className="pe-4" icon={faToolbox} />
134124
Skills
@@ -137,16 +127,12 @@ export default function Skills() {
137127
<div className="flex gap-10">
138128
{/* CLOUD ONLY FOR PC-MASTER-RACE ;) */}
139129
<div className="w-fit mx-auto hidden lg:block">
140-
<IconCloud
141-
iconSlugs={getSkillIcons()}
142-
names={getSkillNames()}
143-
/>
130+
<IconCloud iconSlugs={getSkillIcons()} names={getSkillNames()} />
144131
</div>
145132
<div className="w-fit mx-auto grid grid-cols-3 sm:grid-cols-4 md:grid-cols-6 lg:grid-cols-5 xl:grid-cols-7 gap-6">
146133
{renderAllSkills()}
147134
</div>
148135
</div>
149-
150136
</section>
151137
);
152138
}

0 commit comments

Comments
 (0)