SPB Git

Repository Public

QuizBlock.tsx 3.6 KB 79 lines tsx

1"use client";2import { useState } from "react";3import type { BlockOf, Locale, Source } from "@/schema";4import { useHydrated } from "../hooks";5import { Inline } from "../inline";6import { t } from "../i18n";78export interface QuizBlockProps { block: BlockOf<"quiz">; locale: Locale; mode: "public" | "editor" | "print"; sources?: Source[] }910const Mark = ({ ok }: { ok: boolean }) => (11  <svg className="sh-quiz-mark" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true" focusable="false">12    <path d={ok ? "m5 12.5 4.5 4.5L19 7.5" : "M6.5 6.5l11 11M17.5 6.5l-11 11"} />13  </svg>14);1516/**17 * A self-check question. Interactive once hydrated: picking a choice reveals whether it is right, with its18 * explanation. Without JavaScript and in print the answers sit in a <details>.19 */20export function QuizBlock({ block, locale, mode, sources }: QuizBlockProps) {21  const hydrated = useHydrated();22  const [picked, setPicked] = useState<number[]>([]);23  const letter = (i: number) => String.fromCharCode(65 + i);24  const question = (25    <p className="sh-quiz-question"><span className="sh-quiz-label">{t(locale, "question")}</span><span><Inline text={block.question} sources={sources} /></span></p>26  );2728  if (!hydrated || mode === "print") {29    return (30      <section className="sh-quiz sh-quiz--static">31        {question}32        <ol className="sh-quiz-choices">{block.choices.map((c, i) => <li key={i}><span className="sh-quiz-letter">{letter(i)}</span><span><Inline text={c.text} sources={sources} /></span></li>)}</ol>33        <details className="sh-quiz-answers">34          <summary>{t(locale, "showAnswers")}</summary>35          <ul>36            {block.choices.map((c, i) => (37              <li key={i} className={c.correct ? "sh-quiz-ok" : "sh-quiz-ko"}>38                <strong>{letter(i)} — {t(locale, c.correct ? "correct" : "incorrect")}.</strong>{c.explanation ? <> <Inline text={c.explanation} sources={sources} /></> : null}39              </li>40            ))}41          </ul>42        </details>43      </section>44    );45  }4647  const last = picked.length ? picked[picked.length - 1] : null;48  return (49    <section className="sh-quiz">50      {question}51      <ul className="sh-quiz-choices">52        {block.choices.map((c, i) => {53          const shown = picked.includes(i);54          return (55            <li key={i}>56              <button type="button" className={`sh-quiz-choice${shown ? (c.correct ? " sh-quiz-ok" : " sh-quiz-ko") : ""}`} aria-pressed={shown}57                onClick={() => setPicked((p) => (p.includes(i) ? p : [...p, i]))}>58                <span className="sh-quiz-letter">{shown ? <Mark ok={c.correct} /> : letter(i)}</span>59                <span className="sh-quiz-choice-text"><Inline text={c.text} sources={sources} /></span>60              </button>61              {shown ? (62                <p className={`sh-quiz-feedback ${c.correct ? "sh-quiz-ok" : "sh-quiz-ko"}`}>63                  <strong>{t(locale, c.correct ? "correct" : "incorrect")}.</strong>{c.explanation ? <> <Inline text={c.explanation} sources={sources} /></> : null}64                </p>65              ) : null}66            </li>67          );68        })}69      </ul>70      <div className="sh-quiz-foot">71        <p className="sh-quiz-status" aria-live="polite">72          {last === null ? t(locale, "pickOne") : `${letter(last)} — ${t(locale, block.choices[last].correct ? "correct" : "incorrect")}`}73        </p>74        <button type="button" className="sh-btn" onClick={() => setPicked([])} disabled={picked.length === 0}>{t(locale, "retry")}</button>75      </div>76    </section>77  );78}79
Files main
Menu

Theme