Skip to content

Commit 90b8d20

Browse files
authored
Merge pull request #11 from T-Thiry/progress-bar
Progress bar
2 parents c381b59 + 639aa11 commit 90b8d20

File tree

3 files changed

+120
-18
lines changed

3 files changed

+120
-18
lines changed

css/styles.css

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,52 @@ input[type="radio"]:focus-visible+label {
281281
margin: 0 auto 0.75rem auto;
282282
}
283283

284-
#summary-card h3 {
284+
input[type="radio"]:focus-visible+label {
285+
outline: var(--focus-outline);
286+
border-radius: 0.25rem;
287+
}
288+
289+
/* Progress bar */
290+
.quiz-progress {
291+
margin: 2rem 0;
292+
}
293+
294+
.progress-bar {
295+
background-color: var(--progress-bg);
296+
border-radius: 8px;
297+
height: 20px;
298+
margin: 0 2rem 0 2rem;
299+
overflow: hidden;
300+
position: relative;
301+
}
302+
303+
.progress-fill {
304+
background-color: var(--primary-color);
305+
height: 100%;
306+
transition: width 0.3s ease;
307+
border-radius: 8px;
308+
}
309+
310+
.progress-text {
311+
margin-top: 0.5rem;
285312
text-align: center;
286-
word-wrap: break-word;
287-
margin: 2rem auto 0.5rem auto;
313+
font-weight: 500;
288314
}
289315

290-
#summary-card p {
316+
.error-message {
317+
font-size: 1rem;
318+
font-weight: 600;
319+
/* not final color, just testing */
320+
color: red;
321+
}
322+
323+
.error-container {
324+
margin-top: 2rem;
325+
}
326+
327+
/* Submit button Jasmin commented out because we now have it as a nav button
328+
.form-controls {
329+
margin-top: 2rem;
291330
text-align: center;
292331
margin: 0 auto 0.5rem auto;
293332
}

index.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ <h2>
6161
>
6262
<form id="accessibility-quiz">
6363
<h2>Accessibility Quiz</h2>
64+
<!-- Progress bar -->
65+
<div class="quiz-progress" role="status" aria-live="polite">
66+
<div class="progress-bar">
67+
<div class="progress-fill" style="width: 0%"></div>
68+
</div>
69+
<p class="progress-text">0 of 6 questions answered</p>
70+
</div>
6471

6572
<fieldset
6673
class="quiz-card"
@@ -94,6 +101,8 @@ <h2>Accessibility Quiz</h2>
94101
/>
95102
<label for="question-one-c">Auto playing videos</label>
96103

104+
<div class="error-container" role="status" aria-live="assertive"></div>
105+
97106
<div class="nav-buttons">
98107
<button
99108
type="button"
@@ -135,6 +144,8 @@ <h2>Accessibility Quiz</h2>
135144
/>
136145
<label for="question-two-c">Long, dense text</label>
137146

147+
<div class="error-container" aria-live="polite"></div>
148+
138149
<div class="nav-buttons">
139150
<button
140151
type="button"
@@ -179,6 +190,9 @@ <h2>Accessibility Quiz</h2>
179190
value="Text over background images"
180191
/>
181192
<label for="question-three-c">Text over background images</label>
193+
194+
<div class="error-container" aria-live="polite"></div>
195+
182196
<div class="nav-buttons">
183197
<button
184198
type="button"
@@ -223,6 +237,9 @@ <h2>Accessibility Quiz</h2>
223237
value="Complex text layout"
224238
/>
225239
<label for="question-four-c">Complex text layout</label>
240+
241+
<div class="error-container" aria-live="polite"></div>
242+
226243
<div class="nav-buttons">
227244
<button
228245
type="button"
@@ -268,6 +285,8 @@ <h2>Accessibility Quiz</h2>
268285
/>
269286
<label for="question-five-c">Sound-only cues</label>
270287

288+
<div class="error-container" aria-live="polite"></div>
289+
271290
<div class="nav-buttons">
272291
<button
273292
type="button"
@@ -313,6 +332,8 @@ <h2>Accessibility Quiz</h2>
313332
/>
314333
<label for="question-six-c">Tiny clickable areas</label>
315334

335+
<div class="error-container" aria-live="polite"></div>
336+
316337
<div class="nav-buttons">
317338
<button
318339
type="button"

js/main.js

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,56 @@ document.addEventListener('DOMContentLoaded', function () {
5151
return selectedOption ? selectedOption.value : null;
5252
};
5353

54+
// Progress bar logic
55+
56+
const form = document.getElementById('accessibility-quiz');
57+
const progressBar = document.querySelector(".quiz-progress");
58+
const progressFill = document.querySelector('.progress-fill');
59+
const progressText = document.querySelector('.progress-text');
60+
let answeredQuestions = new Set();
61+
62+
const updateProgress = () => {
63+
const totalQuestions = 6;
64+
const answeredCount = answeredQuestions.size;
65+
const percentage = (answeredCount / totalQuestions) * 100;
66+
67+
progressFill.style.width = `${percentage}%`;
68+
progressText.textContent = `${answeredCount} of ${totalQuestions} questions answered`;
69+
announcer.textContent = `${answeredCount} of ${totalQuestions} questions answered`;
70+
}
71+
72+
form.querySelectorAll('input[type="radio"]').forEach((radio) => {
73+
radio.addEventListener('change', () => {
74+
const questionName = radio.name;
75+
answeredQuestions.add(questionName);
76+
updateProgress();
77+
});
78+
});
79+
80+
// const createErrorMessage = () => {
81+
// const errorMessage = document.createElement("div");
82+
// errorMessage.classList.add("error-message");
83+
// errorMessage.setAttribute("aria-live", "polite");
84+
// errorMessage.textContent = "Please select an answer before proceeding.";
85+
// return errorMessage;
86+
// };
5487

5588
const createErrorMessage = () => {
56-
const errorMessage = document.createElement("div");
57-
errorMessage.classList.add("error-message");
58-
errorMessage.setAttribute("aria-live", "polite");
59-
errorMessage.textContent = "Please select an answer before proceeding.";
60-
return errorMessage;
61-
};
89+
const errorContainer = document.querySelectorAll('.error-container')[currentIndex];
90+
errorContainer.textContent = "Please select an answer before proceeding";
91+
}
6292

6393
const showSummary = () => {
6494
let summaryCard = document.getElementById("summary-card");
95+
progressBar.setAttribute("hidden", "true");
6596

6697
if (!summaryCard) {
6798
summaryCard = document.createElement("fieldset");
6899
summaryCard.id = "summary-card";
69100
summaryCard.classList.add("quiz-card");
70101
summaryCard.innerHTML = `
71102
<h2>Your results:</h2>
72-
<div id="summary-content"></div>
103+
<div id="summary-content" aria-live="polite"></div>
73104
<button type="button" id="restart-btn">Restart Quiz</button>
74105
`;
75106
document.getElementById("accessibility-quiz").appendChild(summaryCard);
@@ -99,15 +130,20 @@ document.addEventListener('DOMContentLoaded', function () {
99130
restartButton.addEventListener("click", () => {
100131
currentIndex = 0;
101132
userAnswers = {};
102-
133+
134+
progressBar.removeAttribute("hidden");
103135
summaryCard.classList.remove("active");
104136

105137
document.querySelectorAll('input[type="radio"]:checked').forEach((radio) => {
106138
radio.checked = false;
107139
});
108140

141+
answeredQuestions.clear();
142+
109143
showCard(currentIndex);
110144

145+
updateProgress();
146+
111147
});
112148

113149
showCard(cards.length);
@@ -118,17 +154,23 @@ document.addEventListener('DOMContentLoaded', function () {
118154

119155
const currentCard = cards[currentIndex];
120156
const selectedAnswer = getSelectedAnswer();
157+
const errorContainer = document.querySelectorAll('.error-container')[currentIndex];
121158

122159
// Check if error message already exists and remove it if it does
123-
const existingError = currentCard.querySelector(".error-message");
124-
if (existingError) {
125-
existingError.remove();
126-
}
160+
// const existingError = currentCard.querySelector(".error-message");
161+
// if (existingError) {
162+
// existingError.remove();
163+
// }
127164

128165
// If no answer selected create an error message
166+
// if (!selectedAnswer) {
167+
// const errorMessage = createErrorMessage();
168+
// errorContainer.appendChild(errorMessage);
169+
// return;
170+
// }
171+
129172
if (!selectedAnswer) {
130-
const errorMessage = createErrorMessage();
131-
currentCard.appendChild(errorMessage);
173+
createErrorMessage();
132174
return;
133175
}
134176

0 commit comments

Comments
 (0)