Skip to content

Commit 6e52411

Browse files
committed
dynamic to static import
Signed-off-by: Muhammad Umer Farooq <[email protected]>
1 parent c7c40d8 commit 6e52411

File tree

1 file changed

+63
-51
lines changed

1 file changed

+63
-51
lines changed

src/components/Home/ToggleSwitcher.astro

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,64 +12,76 @@ import Spacer from './Spacer.astro';
1212
// Props for sections
1313
let { sections } = Astro.props;
1414
15-
// Mapping for dynamic rendering
16-
const componentMap : any = {
17-
'Skills': Skills,
18-
'Companies': Companies,
19-
'RecentProjects': RecentProjects,
20-
'Projects': Projects,
21-
'Education': Education,
22-
'Certificate': Certificate
23-
};
2415
---
2516

2617
<div class="flex justify-center gap-6 mt-6 mb-4">
27-
{sections.map((section:any , index:any) => (
28-
29-
<a href="javascript:void(0)"
30-
class={`${section.key}-toggle toggle ${index === 0 ? 'active' : ''} float-right prose dark:text-gray-300 font-bold hover:text-blue-500`}
31-
data-key={section.key}
32-
data-id={section.id}
33-
> <i class={`fa-solid ${section.icon}`}></i>
34-
{section.title}
35-
</a>
36-
))}
18+
{
19+
sections.map((section: any, index: any) => (
20+
<a
21+
href="javascript:void(0)"
22+
class={`${section.key}-toggle toggle ${
23+
index === 0 ? 'active' : ''
24+
} float-right prose dark:text-gray-300 font-bold hover:text-blue-500`}
25+
data-key={section.key}
26+
data-id={section.id}
27+
>
28+
{' '}
29+
<i class={`fa-solid ${section.icon}`} />
30+
{section.title}
31+
</a>
32+
))
33+
}
3734
</div>
3835
<hr class="short" />
3936

40-
{sections.map((section:any, index:any) => (
41-
<div id={section.key} class={`${index !== 0 ? 'hidden' : ''} toggle-elems`} data-key={section.key} data-id={section.id}>
42-
{componentMap[section.component] ? <componentMap[section.component] /> : <div>Component not found</div>}
43-
</div>
44-
))}
37+
{
38+
sections.map((section: any, index: any) => {
39+
40+
return (
41+
<div
42+
id={section.key}
43+
class={`${index !== 0 ? 'hidden' : ''} toggle-elems`}
44+
data-key={section.key}
45+
data-id={section.id}
46+
>
47+
{section.component === 'Skills' && <Skills />}
48+
{section.component === 'Companies' && <Companies />}
49+
{section.component === 'RecentProjects' && <RecentProjects />}
50+
{section.component === 'Projects' && <Projects />}
51+
{section.component === 'Education' && <Education />}
52+
{section.component === 'Certificate' && <Certificate />}
53+
</div>
54+
);
55+
})
56+
}
4557

4658
<script lang="js" is:inline>
47-
document.querySelectorAll('.toggle').forEach(toggle => {
48-
toggle.addEventListener('click', function () {
49-
const key = this.getAttribute('data-key');
50-
const id = this.getAttribute('data-id');
51-
const elem = document.getElementById(key);
52-
const sections = document.querySelectorAll('.toggle-elems');
53-
const toggles = document.querySelectorAll('.toggle');
54-
// remove active class from all.
55-
toggles.forEach(tgl => {
56-
let dataID = tgl.getAttribute('data-id');
57-
if (id === dataID) {
58-
tgl.classList.remove('active');
59-
}
60-
});
59+
document.querySelectorAll('.toggle').forEach((toggle) => {
60+
toggle.addEventListener('click', function () {
61+
const key = this.getAttribute('data-key');
62+
const id = this.getAttribute('data-id');
63+
const elem = document.getElementById(key);
64+
const sections = document.querySelectorAll('.toggle-elems');
65+
const toggles = document.querySelectorAll('.toggle');
66+
// remove active class from all.
67+
toggles.forEach((tgl) => {
68+
let dataID = tgl.getAttribute('data-id');
69+
if (id === dataID) {
70+
tgl.classList.remove('active');
71+
}
72+
});
6173

62-
toggle.classList.add('active');
63-
// hide all.
64-
sections.forEach(section => {
65-
const dataID = section.getAttribute('data-id');
66-
console.log ("keys", id, dataID, dataID === id);
67-
if (id == dataID) {
68-
section.classList.add('hidden');
69-
}
70-
});
71-
// toggle class of elem hidden.
72-
elem.classList.toggle('hidden');
73-
});
74-
});
74+
toggle.classList.add('active');
75+
// hide all.
76+
sections.forEach((section) => {
77+
const dataID = section.getAttribute('data-id');
78+
console.log('keys', id, dataID, dataID === id);
79+
if (id == dataID) {
80+
section.classList.add('hidden');
81+
}
82+
});
83+
// toggle class of elem hidden.
84+
elem.classList.toggle('hidden');
85+
});
86+
});
7587
</script>

0 commit comments

Comments
 (0)