lmscn

Quiz

A multi-question quiz supporting single-choice, multiple-choice, and true/false question types.

Installation

npx shadcn@latest add https://lmscn.vercel.app/r/quiz.json

Required primitives: button, card, progress, badge

npx shadcn@latest add button card progress badge

Usage

import { Quiz } from "@/components/lms/quiz"
import type { QuizData, QuizResult } from "@/components/lms/quiz"

const data: QuizData = {
  title: "JavaScript Basics",
  description: "Test your knowledge of JS fundamentals.",
  passingScore: 80,
  showExplanations: true,
  questions: [
    {
      id: "q1",
      type: "single",
      question: "What does `typeof null` return?",
      options: [
        { id: "a", label: "null" },
        { id: "b", label: "object", explanation: "A historical quirk in the JS spec." },
        { id: "c", label: "undefined" },
      ],
      correctIds: ["b"],
      hint: "It's a well-known JavaScript oddity.",
    },
    {
      id: "q2",
      type: "multiple",
      question: "Which are falsy values in JavaScript?",
      options: [
        { id: "a", label: "0" },
        { id: "b", label: '""' },
        { id: "c", label: '"false"' },
        { id: "d", label: "null" },
      ],
      correctIds: ["a", "b", "d"],
      points: 2,
    },
  ],
}

export function MyQuiz() {
  return (
    <Quiz
      quizData={data}
      onComplete={(result: QuizResult) => {
        console.log(`${result.percentage}% — ${result.passed ? "Passed" : "Failed"}`)
      }}
    />
  )
}

Props

QuizProps

PropTypeRequiredDescription
quizDataQuizDataQuiz configuration and questions
onComplete(result: QuizResult) => voidCalled when the last question is answered
classNamestringAdditional CSS classes

Data shape

QuizData

FieldTypeDefaultDescription
titlestringDisplayed in the card header
descriptionstringSubtitle below the title
questionsQuizQuestion[]List of questions
showExplanationsbooleantrueShow per-option explanations after answering
shufflebooleanfalseRandomise question order on mount
passingScorenumber70Minimum percentage (0–100) required to pass

QuizQuestion

FieldTypeDescription
idstringUnique identifier
type"single" | "multiple" | "true-false"Selection mode
questionstringThe question text
optionsQuizOption[]Answer options
correctIdsstring[]IDs of the correct option(s)
hintstringOptional hint shown before submitting
pointsnumberPoint value — defaults to 1

QuizOption

FieldTypeDescription
idstringUnique identifier
labelstringDisplayed text
explanationstringShown beneath the option after the answer is revealed

Result

onComplete receives a QuizResult object:

FieldTypeDescription
scorenumberTotal points earned
maxScorenumberTotal points available
percentagenumberRounded score as a percentage
passedbooleantrue if percentage >= passingScore
answersRecord<string, string[]>Map of question ID → selected option IDs

Question types

single — the learner picks exactly one option. Clicking another option deselects the previous one.

multiple — the learner picks all that apply. A "Select all that apply" label is shown automatically.

true-false — identical to single but conventionally used with two options labelled "True" and "False".

On this page