Skip to content

Commit c9e5fc2

Browse files
Ciphroxxkeshav
authored andcommitted
fixed: back button, typing animation playground, added: image on letter change
1 parent 1d9a816 commit c9e5fc2

File tree

3 files changed

+113
-17
lines changed

3 files changed

+113
-17
lines changed

src/assets/styles/alphabets/[alphabet]/playground.css

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
display: grid;
99
grid-template-columns: repeat(2, 1fr);
1010
grid-template-rows: 35% auto auto;
11-
grid-template-areas:
12-
"letter emoji"
11+
grid-template-areas:
12+
"letter content-display"
1313
"spelling spelling"
1414
"description description";
1515
grid-auto-flow: row;
@@ -26,17 +26,46 @@
2626
padding: 0;
2727
transition: color 0.5s ease-in;
2828
text-align: center;
29-
width: 100%;
29+
width: 100%;
30+
line-height: 1;
31+
}
32+
33+
& .content__display {
34+
grid-area: content-display;
35+
display: flex;
36+
justify-content: center;
37+
align-items: center;
38+
width: 100%;
39+
height: 100%;
40+
overflow: hidden;
41+
padding: 1rem;
3042
}
3143

3244
& .emoji {
33-
grid-area: emoji;
3445
font-size: clamp(6em, 15vw, 18em);
3546
}
3647

48+
& .image__container {
49+
width: 100%;
50+
height: 100%;
51+
display: flex;
52+
justify-content: center;
53+
align-items: center;
54+
overflow: hidden;
55+
display: none;
56+
}
57+
58+
& .image__container img {
59+
max-width: 100%;
60+
max-height: 100%;
61+
object-fit: contain;
62+
border-radius: 8px;
63+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
64+
}
65+
3766
& .animation {
3867
animation:
39-
typing 1s steps(var(--steps, 10)),
68+
typing 1.5s steps(var(--steps, 10)),
4069
blink 0.5s step-end infinite alternate;
4170
}
4271

@@ -52,6 +81,8 @@
5281
font-size: clamp(4em, 10vw, 12em);
5382
transition: all 0.5s ease-in-out;
5483
color: var(--spelling__txt--3);
84+
max-width: fit-content;
85+
margin: 0 auto;
5586
}
5687

5788
& .description {
@@ -65,12 +96,33 @@
6596
color: var(--spelling__txt--3);
6697
min-height: 2em;
6798
}
99+
100+
& .description.animation {
101+
display: inline-block;
102+
animation:
103+
typing 2s steps(var(--steps-desc, 30)) forwards,
104+
blink 0.5s step-end infinite alternate;
105+
white-space: nowrap;
106+
overflow: hidden;
107+
width: 0;
108+
border-right: 3px solid;
109+
max-width: fit-content;
110+
margin: 0 auto;
111+
}
112+
113+
& .description:empty {
114+
border-right: none;
115+
}
68116
}
69117

70118
@keyframes typing {
71119
from {
72120
width: 0;
73121
}
122+
123+
to {
124+
width: 100%;
125+
}
74126
}
75127

76128
@keyframes blink {

src/data/alphabets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"id": 1,
88
"letter": "A",
99
"code": 65,
10-
"text": "Apple",
10+
"text": "Apple",
1111
"description": "",
1212
"image": "apple.jpg"
1313
},
@@ -1128,7 +1128,7 @@
11281128
}
11291129
]
11301130
},
1131-
"indian_mythology": {
1131+
"Indian": {
11321132
"description": "Mythological Indian culture Alphabets",
11331133
"icon": "🕉️",
11341134
"data": [

src/pages/alphabets/[alphabet]/playground.astro

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,18 @@ const currentCategory = alphabetData[alphabet as keyof AlphabetsData];
3232
<BackButton />
3333
<article class="tv container__typing">
3434
<div class="letter">A</div>
35-
<div class="emoji">{currentCategory.icon}</div>
36-
<div class="spelling animation" id="spelling"></div>
35+
<div class="content__display">
36+
<div class="emoji">{currentCategory.icon}</div>
37+
<div class="image__container"></div>
38+
</div>
39+
<div class="spelling" id="spelling"></div>
3740
<div class="description" id="description"></div>
3841
</article>
3942

4043
<script define:vars={{ alphabetData: currentCategory.data, category: alphabet }}>
4144
const letterDiv = document.querySelector(".letter");
4245
const emojiDiv = document.querySelector(".emoji");
46+
const imageContainer = document.querySelector(".image__container");
4347
const spellDiv = document.querySelector(".spelling");
4448
const descriptionDiv = document.querySelector(".description");
4549

@@ -59,22 +63,62 @@ const currentCategory = alphabetData[alphabet as keyof AlphabetsData];
5963
const drawInfo = (letter, color) => {
6064
const info = alphabetData.find((item) => item.letter === letter);
6165
if (info) {
62-
// Display text
6366
spellDiv.innerHTML = info.text;
6467
spellDiv.style.color = color;
65-
document.documentElement.style.setProperty("--steps", info.text.length);
6668

67-
// Display description
68-
descriptionDiv.innerHTML = info.description;
69-
descriptionDiv.style.color = color;
69+
document.documentElement.style.setProperty("--steps", info.text.length * 2);
7070

71-
// Animation handling
71+
// Animation handling to spelling
7272
spellDiv.classList.remove("animation");
73-
void spellDiv.offsetWidth;
7473
spellDiv.classList.add("animation");
74+
75+
if (info.description && info.description.trim() !== "") {
76+
descriptionDiv.innerHTML = info.description;
77+
descriptionDiv.style.color = color;
78+
document.documentElement.style.setProperty("--steps-desc", info.description.length * 2);
79+
80+
// Animation handling for description
81+
descriptionDiv.classList.remove("animation");
82+
descriptionDiv.classList.add("animation");
83+
} else {
84+
descriptionDiv.classList.remove("animation");
85+
descriptionDiv.innerHTML = "";
86+
}
87+
88+
if (info.image) {
89+
const img = new Image();
90+
img.onload = function () {
91+
emojiDiv.style.display = "none";
92+
imageContainer.style.display = "flex";
93+
imageContainer.innerHTML = "";
94+
imageContainer.appendChild(img);
95+
96+
img.style.width = "auto";
97+
img.style.height = "auto";
98+
img.style.maxWidth = "100%";
99+
img.style.maxHeight = "100%";
100+
};
101+
img.onerror = function () {
102+
emojiDiv.style.display = "block";
103+
imageContainer.style.display = "none";
104+
};
105+
img.src = `/assets/images/alphabets/${category}/${info.image}`;
106+
img.alt = info.text;
107+
108+
emojiDiv.style.display = "block";
109+
imageContainer.style.display = "none";
110+
} else {
111+
emojiDiv.style.display = "block";
112+
imageContainer.style.display = "none";
113+
}
75114
}
76115
};
77116

117+
window.addEventListener("DOMContentLoaded", () => {
118+
spellDiv.classList.remove("animation");
119+
descriptionDiv.classList.remove("animation");
120+
});
121+
78122
const colorBox = ["#FF6B6B", "#4ECDC4", "#45B7D1", "#96CEB4", "#FFEEAD", "#D4A5A5", "#9B59B6", "#3498DB"];
79123
const random = (arr) => arr[Math.floor(Math.random() * arr.length)];
80124
const isAlphabet = (keyCode) => keyCode >= 65 && keyCode <= 90;
@@ -88,7 +132,7 @@ const currentCategory = alphabetData[alphabet as keyof AlphabetsData];
88132
if (isAlphabet(keyCode)) {
89133
const keyCapital = key.toUpperCase();
90134
drawLetter(keyCapital, color);
91-
drawInfo(keyCapital, color); // Pass color as parameter
135+
drawInfo(keyCapital, color);
92136
}
93137
},
94138
false

0 commit comments

Comments
 (0)