Skip to content

Commit 5bf1e82

Browse files
fix(a11y): make code examples in quiz accessible (freeCodeCamp#60498)
1 parent 6180702 commit 5bf1e82

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

client/src/templates/Challenges/quiz/show.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,15 @@
99
.quiz-challenge-container div[role='radiogroup'] label p:last-child {
1010
margin-bottom: 0;
1111
}
12+
13+
/* Quiz question text and option text have the <p> tag removed
14+
so the HTML can be semantically correct.
15+
This also removes the `p` styles that the text should have,
16+
so we need to clone the CSS of the `p` element here. */
17+
.quiz-question-label,
18+
.quiz-answer-label {
19+
line-height: 1.5rem;
20+
font-weight: 400;
21+
font-size: 1rem;
22+
margin: 0 0 1.2rem;
23+
}

client/src/templates/Challenges/quiz/show.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ interface ShowQuizProps {
8585
closeFinishQuizModal: () => void;
8686
}
8787

88+
const removeParagraphTags = (text: string) => text.replace(/^<p>|<\/p>$/g, '');
89+
8890
const ShowQuiz = ({
8991
challengeMounted,
9092
data: {
@@ -143,7 +145,12 @@ const ShowQuiz = ({
143145
const distractors = question.distractors.map((distractor, index) => {
144146
return {
145147
label: (
146-
<PrismFormatted className='quiz-answer-label' text={distractor} />
148+
<PrismFormatted
149+
className='quiz-answer-label'
150+
text={removeParagraphTags(distractor)}
151+
useSpan
152+
noAria
153+
/>
147154
),
148155
value: index + 1
149156
};
@@ -153,14 +160,23 @@ const ShowQuiz = ({
153160
label: (
154161
<PrismFormatted
155162
className='quiz-answer-label'
156-
text={question.answer}
163+
text={removeParagraphTags(question.answer)}
164+
useSpan
165+
noAria
157166
/>
158167
),
159168
value: 4
160169
};
161170

162171
return {
163-
question: <PrismFormatted text={question.text} />,
172+
question: (
173+
<PrismFormatted
174+
className='quiz-question-label'
175+
text={removeParagraphTags(question.text)}
176+
useSpan
177+
noAria
178+
/>
179+
),
164180
answers: shuffleArray([...distractors, answer]),
165181
correctAnswer: answer.value
166182
};

0 commit comments

Comments
 (0)