@molecule/app-quiz-card-angular
Feature · quiz-card · App (browser) · v1.1.4 · Apache-2.0
Question card with multiple-choice options, progress, optional timer, correct/incorrect feedback
npm install @molecule/app-quiz-card-angularHow it works
@molecule/app-quiz-card-angular is a ready-made quiz-card feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.
import { Component } from '@angular/core'
import { QuizCard } from '@molecule/app-quiz-card-angular'
@Component({
selector: 'app-question',
standalone: true,
imports: [QuizCard],
template: `
<div mol-quiz-card [progress]="'1 / 10'" [correctId]="'b'" [onAnswer]="onAnswer">
<span slot="question">What is the capital of France?</span>
<span slot="explanation">Paris has been the capital of France since 987 AD.</span>
</div>
`,
})
export class Question {
options = [
{ id: 'a', label: 'London' },
{ id: 'b', label: 'Paris' },
{ id: 'c', label: 'Berlin' },
]
onAnswer(id: string, correct?: boolean): void {
console.log(id, correct)
}
}Works with: @molecule/app-angular, @molecule/app-ui, @molecule/app-ui-angular
Reference
Angular quiz / multiple-choice question card — the counterpart of
@molecule/app-quiz-card-react: the same props and the same DOM for the
same input.
Exports <QuizCard> and the QuizOption type.
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
Quick Start
import { Component } from '@angular/core'
import { QuizCard } from '@molecule/app-quiz-card-angular'
@Component({
selector: 'app-question',
standalone: true,
imports: [QuizCard],
template: `
<div mol-quiz-card [progress]="'1 / 10'" [correctId]="'b'" [onAnswer]="onAnswer">
<span slot="question">What is the capital of France?</span>
<span slot="explanation">Paris has been the capital of France since 987 AD.</span>
</div>
`,
})
export class Question {
options = [
{ id: 'a', label: 'London' },
{ id: 'b', label: 'Paris' },
{ id: 'c', label: 'Berlin' },
]
onAnswer(id: string, correct?: boolean): void {
console.log(id, correct)
}
}
Type
feature
Installation
npm install @molecule/app-quiz-card-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-ui @molecule/app-ui-angular
API
Interfaces
QuizOption
A single selectable answer option in a quiz question.
label is a string (rendered as text) or an embedded template (a node
input cannot travel through an Angular input, so callers pass nodes as
templates where the framework supplies them).
interface QuizOption {
/** The option's identifier, used for selection and as the rendered key. */
id: string
/** The option's label: a plain string, or a template rendering the nodes. */
label: unknown
}
QuizCardProps
Props accepted by QuizCard — the same props
@molecule/app-quiz-card-react accepts. Node props (question, progress,
timer, explanation) are projected content in Angular (an input cannot
carry a node), so they are typed unknown: pass them as
[slot=question] / [slot=progress] / [slot=timer] /
[slot=explanation] content, or as plain strings.
interface QuizCardProps {
/** The question text, projected as `[slot=question]` content or a string. */
question?: unknown
/** Answer options. */
options: QuizOption[]
/** Id of the correct option — when provided, the component reveals correct/incorrect on submit. */
correctId?: string
/** Called when the user submits an answer. */
onAnswer?: (optionId: string, correct?: boolean) => void
/** Progress display — e.g. `2 / 10` — projected as `[slot=progress]` content or a string. */
progress?: unknown
/** Optional countdown / timer node, projected as `[slot=timer]` content or a string. */
timer?: unknown
/** Explanation shown after submit, projected as `[slot=explanation]` content or a string. */
explanation?: unknown
/** Extra classes. */
className?: string
}
Classes
QuizCard
QuizCard component.
Single quiz question card with multiple-choice options. The host IS React's
root <div> (div[mol-quiz-card]), carrying the Card classes exactly as
React's Card does. Tracks its own submit state and reveals
correct/incorrect when correctId is provided.
The node-valued question, progress, timer and explanation props
arrive as projected content ([slot=question], [slot=progress],
[slot=timer], [slot=explanation]), as embedded templates, or as plain
string inputs. A wrapper React would not render at all (the progress span,
the explanation paragraph) leaves the page and keeps its capture point, so
content a consumer projects later renders in it.
@Component({
selector: 'div[mol-quiz-card]',
standalone: true,
hostDirectives: [MolPlacement],
imports: [Button, NgTemplateOutlet],
inputs: [
'question',
'options',
'correctId',
'onAnswer',
'progress',
'timer',
'explanation',
'className',
],
host: {
'[class]': 'cardClasses()',
},
// prettier-ignore
template: `<div #stack [class]="stackClasses()"><header #header [class]="headerClasses()"><span #progressEl [class]="progressClasses()">@if (isText(progress)) {<ng-container>{{ progress }}</ng-container>}<ng-container [ngTemplateOutlet]="templateOf(progress)"></ng-container><ng-content select="[slot=progress]"></ng-content></span>@if (isText(timer)) {<ng-container>{{ timer }}</ng-container>}<ng-container [ngTemplateOutlet]="templateOf(timer)"></ng-container><ng-content select="[slot=timer]"></ng-content></header><h2 [class]="questionClasses()">@if (isText(question)) {<ng-container>{{ question }}</ng-container>}<ng-container [ngTemplateOutlet]="templateOf(question)"></ng-container><ng-content select="[slot=question]"></ng-content></h2><div [class]="optionsClasses()">@for (option of options; track option.id) {<button type="button" (click)="select(option)" [disabled]="submitted()" [attr.aria-pressed]="isSelected(option) ? 'true' : 'false'" data-mol-id="quiz-card-option" [attr.data-correct]="isCorrect(option) ? 'true' : null" [attr.data-wrong]="isWrong(option) ? 'true' : null" [class]="optionClasses(option)" [style]="optionStyle(option)"><span>@if (isText(option.label)) {<ng-container>{{ option.label }}</ng-container>}@else {<ng-container [ngTemplateOutlet]="templateOf(option.label)"></ng-container>}</span>@if (isCorrect(option)) {<span aria-hidden="true">✓</span>}@if (isWrong(option)) {<span aria-hidden="true">×</span>}</button>}</div>@if (!submitted()) {<button mol-button [variant]="'solid'" [color]="'primary'" (click)="submit()" [disabled]="!selected()">{{ submitLabel() }}</button>}<p #explanationEl [class]="explanationClasses()">@if (isText(explanation)) {<ng-container>{{ explanation }}</ng-container>}<ng-container [ngTemplateOutlet]="templateOf(explanation)"></ng-container><ng-content select="[slot=explanation]"></ng-content></p></div>`,
})
export class QuizCard {
question?: unknown
/** The answer options, rendered in order and keyed by `id`. */
options: QuizOption[] = []
correctId?: string
/** Called when the user submits an answer. */
onAnswer?: (optionId: string, correct?: boolean) => void
progress?: unknown
timer?: unknown
explanation?: unknown
/** Extra classes on the card. */
className?: string
/** The progress span (always in the template; detached when empty). */
@ViewChild('progressEl') private progressEl?: ElementRef<HTMLSpanElement>
@ViewChild('explanationEl') private explanationEl?: ElementRef<HTMLParagraphElement>
/** The i18n provider above the card (the submit label). */
private readonly i18n = useTranslation()
/** The selected option id. */
private readonly selectedState = signal<string | null>(null)
/** Whether the answer was submitted (the card is single-shot). */
private readonly submittedState = signal(false)
constructor() {
usePlacement((hasContent) => this.syncWrappers(hasContent))
}
/** The current class map. */
cm(): UIClassMap {
return getClassMap()
}
cardClasses(): string {
const cm = this.cm()
return cm.cn(cm.card({ variant: 'elevated' }), cm.cardPadding('md'), this.className)
}
/** The outer stack class list. */
stackClasses(): string {
return this.cm().stack(4)
}
/** The header class list. */
headerClasses(): string {
return this.cm().flex({ justify: 'between', align: 'center', gap: 'sm' })
}
/** The progress row class list. */
progressClasses(): string {
const cm = this.cm()
return cm.cn(cm.textSize('xs'), cm.fontWeight('semibold'))
}
/** The question class list. */
questionClasses(): string {
const cm = this.cm()
return cm.cn(cm.textSize('lg'), cm.fontWeight('bold'))
}
/** The options container class list. */
optionsClasses(): string {
return this.cm().stack(2)
}
/** The explanation class list. */
explanationClasses(): string {
return this.cm().textSize('sm')
}
/** One answer button's class list (React's weight switch on selection). */
optionClasses(option: QuizOption): string {
const cm = this.cm()
return cm.cn(
cm.flex({ align: 'center', gap: 'sm' }),
cm.sp('px', 3),
cm.sp('py', 2),
cm.textSize('sm'),
cm.cursorPointer,
cm.touchTargetCompact,
this.isSelected(option) ? cm.fontWeight('semibold') : cm.fontWeight('medium'),
)
}
optionStyle(option: QuizOption): string {
const success = 'var(--mol-color-success, #22c55e)'
const error = 'var(--mol-color-error, #ef4444)'
const correct = this.isCorrect(option)
const wrong = this.isWrong(option)
const border = correct
? success
: wrong
? error
: this.isSelected(option)
? 'currentColor'
: 'var(--mol-color-border, rgba(128,128,128,0.35))'
const background = correct
? `; background: color-mix(in srgb, ${success} 12%, transparent)`
: wrong
? `; background: color-mix(in srgb, ${error} 12%, transparent)`
: ''
return `border-radius: 8px; border: 1px solid ${border}${background}`
}
/** Whether an option is the current selection. */
isSelected(option: QuizOption): boolean {
return option.id === this.selectedState()
}
isCorrect(option: QuizOption): boolean {
return this.submittedState() && !!this.correctId && option.id === this.correctId
}
isWrong(option: QuizOption): boolean {
return (
this.submittedState() &&
!!this.correctId &&
this.isSelected(option) &&
option.id !== this.correctId
)
}
/** Whether the answer was submitted. */
submitted(): boolean {
return this.submittedState()
}
/** The selected option id, if any. */
selected(): string | null {
return this.selectedState()
}
isText(value: unknown): value is string | number {
return typeof value === 'string' || typeof value === 'number'
}
/** Narrows a value to an embedded template. */
templateOf(value: unknown): TemplateRef<unknown> | null {
return value instanceof TemplateRef ? value : null
}
/** Whether the progress row renders at all — React's `{progress && …}`. */
hasProgress(projected: boolean): boolean {
return (
(this.isText(this.progress) && this.progress !== '') ||
this.templateOf(this.progress) !== null ||
projected
)
}
/** Whether the explanation renders — React's `explanation && <p>`. */
hasExplanation(projected: boolean): boolean {
return (
(this.isText(this.explanation) && this.explanation !== '') ||
this.templateOf(this.explanation) !== null ||
projected
)
}
/** The submit button's label. */
submitLabel(): string {
return this.i18n.t('quizCard.submit', {}, { defaultValue: 'Submit answer' })
}
/** Selects an option (React ignores the click after submit). */
select(option: QuizOption): void {
if (!this.submittedState()) this.selectedState.set(option.id)
}
/** Validates the selection and fires onAnswer with the correctness flag. */
submit(): void {
const optionId = this.selectedState()
if (!optionId || this.submittedState()) return
this.submittedState.set(true)
this.onAnswer?.(optionId, this.correctId ? optionId === this.correctId : undefined)
}
private syncWrappers(hasContent: SlotReader): void {
const progress = this.progressEl?.nativeElement
setElementPresent(progress, this.hasProgress(!!progress && hasContent(progress)))
const explanation = this.explanationEl?.nativeElement
setElementPresent(
explanation,
this.submittedState() && this.hasExplanation(!!explanation && hasContent(explanation)),
)
}
}
(Member comments are shortened to their first sentence, and the 13 longest are left out to fit; the full declaration is in node_modules/@molecule/app-quiz-card-angular/dist/quiz-card.component.d.ts.)
Injection Notes
Requirements
Peer dependencies:
@angular/common^22.0.0@angular/core^22.0.0@angular/platform-browser^22.0.0@molecule/app-angular^3.13.0 || ^4.0.0@molecule/app-ui^1.1.0@molecule/app-ui-angular^1.1.0
Runtime Dependencies
@angular/common@angular/core@angular/platform-browser@molecule/app-angular@molecule/app-ui@molecule/app-ui-angular
Companion locale bond: @molecule/app-locales-quiz-card (quizCard.submit).
The card is single-shot: after submit the options lock and the explanation
replaces the button — there is no reset prop, so advance quizzes by
remounting with a fresh key per question. Without correctId no
correct/incorrect state is revealed and onAnswer's second argument is
undefined. Requires the app-angular i18n provider and a wired ClassMap
bond.
The host element is the card surface itself (div[mol-quiz-card]), carrying
the Card classes React renders. The node-valued question, progress,
timer and explanation props are projected content
([slot=question], [slot=progress], [slot=timer],
[slot=explanation]) or plain strings.
Translations
Translation strings are provided by @molecule/app-locales-quiz-card.