@lessonkit/react 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -0
- package/dist/index.cjs +248 -0
- package/dist/index.d.cts +84 -0
- package/dist/index.d.ts +84 -0
- package/dist/index.js +210 -0
- package/package.json +64 -0
package/README.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# `@lessonkit/react`
|
|
2
|
+
|
|
3
|
+
React components and hooks for building learning experiences in LessonKit.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @lessonkit/react react react-dom
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick example
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { Course, Lesson, Quiz, Scenario, ProgressTracker } from "@lessonkit/react";
|
|
15
|
+
|
|
16
|
+
export default function App() {
|
|
17
|
+
return (
|
|
18
|
+
<Course title="Cybersecurity Basics" courseId="cyber-basics">
|
|
19
|
+
<ProgressTracker />
|
|
20
|
+
|
|
21
|
+
<Lesson title="Phishing Awareness" lessonId="phishing-101">
|
|
22
|
+
<Scenario>
|
|
23
|
+
<p>You receive a suspicious email.</p>
|
|
24
|
+
</Scenario>
|
|
25
|
+
|
|
26
|
+
<Quiz
|
|
27
|
+
question="What should you do first?"
|
|
28
|
+
choices={["Open attachment", "Verify sender"]}
|
|
29
|
+
answer="Verify sender"
|
|
30
|
+
/>
|
|
31
|
+
</Lesson>
|
|
32
|
+
</Course>
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## API (0.1.x)
|
|
38
|
+
|
|
39
|
+
### Components
|
|
40
|
+
|
|
41
|
+
- `Course`
|
|
42
|
+
- `Lesson`
|
|
43
|
+
- `Scenario`
|
|
44
|
+
- `Quiz`
|
|
45
|
+
- `Reflection`
|
|
46
|
+
- `KnowledgeCheck`
|
|
47
|
+
- `ProgressTracker`
|
|
48
|
+
|
|
49
|
+
### Hooks
|
|
50
|
+
|
|
51
|
+
- `useProgress`
|
|
52
|
+
- `useTracking`
|
|
53
|
+
- `useQuizState`
|
|
54
|
+
- `useCompletion`
|
|
55
|
+
|
|
56
|
+
## Notes
|
|
57
|
+
|
|
58
|
+
- `@lessonkit/react` ships **framework primitives**, not content. You bring your own layout/content
|
|
59
|
+
and compose interactions as React components.
|
|
60
|
+
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.tsx
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
Course: () => Course,
|
|
24
|
+
KnowledgeCheck: () => KnowledgeCheck,
|
|
25
|
+
Lesson: () => Lesson,
|
|
26
|
+
ProgressTracker: () => ProgressTracker,
|
|
27
|
+
Quiz: () => Quiz,
|
|
28
|
+
Reflection: () => Reflection,
|
|
29
|
+
Scenario: () => Scenario,
|
|
30
|
+
useCompletion: () => useCompletion,
|
|
31
|
+
useLessonkit: () => useLessonkit,
|
|
32
|
+
useProgress: () => useProgress,
|
|
33
|
+
useQuizState: () => useQuizState,
|
|
34
|
+
useTracking: () => useTracking
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
|
|
38
|
+
// src/components.tsx
|
|
39
|
+
var import_react3 = require("react");
|
|
40
|
+
|
|
41
|
+
// src/context.tsx
|
|
42
|
+
var import_react = require("react");
|
|
43
|
+
var import_core = require("@lessonkit/core");
|
|
44
|
+
var import_xapi = require("@lessonkit/xapi");
|
|
45
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
46
|
+
var LessonkitContext = (0, import_react.createContext)(null);
|
|
47
|
+
function LessonkitProvider(props) {
|
|
48
|
+
const config = props.config ?? {};
|
|
49
|
+
const tracking = (0, import_react.useMemo)(() => {
|
|
50
|
+
if (config.tracking?.enabled === false) return (0, import_core.createTrackingClient)();
|
|
51
|
+
return (0, import_core.createTrackingClient)({ sink: config.tracking?.sink });
|
|
52
|
+
}, [config.tracking?.enabled, config.tracking?.sink]);
|
|
53
|
+
const xapi = (0, import_react.useMemo)(() => {
|
|
54
|
+
if (config.xapi?.enabled === false) return null;
|
|
55
|
+
return config.xapi?.client ?? (0, import_xapi.createXAPIClient)();
|
|
56
|
+
}, [config.xapi?.enabled, config.xapi?.client]);
|
|
57
|
+
const [completedLessonIds, setCompletedLessonIds] = (0, import_react.useState)(() => /* @__PURE__ */ new Set());
|
|
58
|
+
const [activeLessonId, setActiveLessonId] = (0, import_react.useState)(void 0);
|
|
59
|
+
const [courseCompleted, setCourseCompleted] = (0, import_react.useState)(false);
|
|
60
|
+
const courseIdRef = (0, import_react.useRef)(config.courseId);
|
|
61
|
+
courseIdRef.current = config.courseId;
|
|
62
|
+
const track = (0, import_react.useCallback)(
|
|
63
|
+
(name, data) => {
|
|
64
|
+
tracking.track({
|
|
65
|
+
name,
|
|
66
|
+
timestamp: (0, import_core.nowIso)(),
|
|
67
|
+
courseId: courseIdRef.current,
|
|
68
|
+
lessonId: activeLessonId,
|
|
69
|
+
data
|
|
70
|
+
});
|
|
71
|
+
},
|
|
72
|
+
[tracking, activeLessonId]
|
|
73
|
+
);
|
|
74
|
+
const setActiveLesson = (0, import_react.useCallback)(
|
|
75
|
+
(lessonId) => {
|
|
76
|
+
setActiveLessonId(lessonId);
|
|
77
|
+
track("lesson_started", { lessonId });
|
|
78
|
+
xapi?.startedLesson({ lessonId });
|
|
79
|
+
},
|
|
80
|
+
[track, xapi]
|
|
81
|
+
);
|
|
82
|
+
const completeLesson = (0, import_react.useCallback)(
|
|
83
|
+
(lessonId) => {
|
|
84
|
+
setCompletedLessonIds((prev) => new Set(prev).add(lessonId));
|
|
85
|
+
track("lesson_completed", { lessonId });
|
|
86
|
+
xapi?.completeLesson({ lessonId });
|
|
87
|
+
},
|
|
88
|
+
[track, xapi]
|
|
89
|
+
);
|
|
90
|
+
const completeCourse = (0, import_react.useCallback)(() => {
|
|
91
|
+
setCourseCompleted(true);
|
|
92
|
+
track("course_completed");
|
|
93
|
+
xapi?.completeCourse({});
|
|
94
|
+
}, [track, xapi]);
|
|
95
|
+
const runtime = (0, import_react.useMemo)(
|
|
96
|
+
() => ({
|
|
97
|
+
config,
|
|
98
|
+
tracking,
|
|
99
|
+
xapi,
|
|
100
|
+
progress: { activeLessonId, completedLessonIds, courseCompleted },
|
|
101
|
+
setActiveLesson,
|
|
102
|
+
completeLesson,
|
|
103
|
+
completeCourse,
|
|
104
|
+
track
|
|
105
|
+
}),
|
|
106
|
+
[
|
|
107
|
+
config,
|
|
108
|
+
tracking,
|
|
109
|
+
xapi,
|
|
110
|
+
activeLessonId,
|
|
111
|
+
completedLessonIds,
|
|
112
|
+
courseCompleted,
|
|
113
|
+
setActiveLesson,
|
|
114
|
+
completeLesson,
|
|
115
|
+
completeCourse,
|
|
116
|
+
track
|
|
117
|
+
]
|
|
118
|
+
);
|
|
119
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LessonkitContext.Provider, { value: runtime, children: props.children });
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// src/hooks.ts
|
|
123
|
+
var import_react2 = require("react");
|
|
124
|
+
function useLessonkit() {
|
|
125
|
+
const ctx = (0, import_react2.useContext)(LessonkitContext);
|
|
126
|
+
if (!ctx) throw new Error("LessonKit: missing LessonkitProvider");
|
|
127
|
+
return ctx;
|
|
128
|
+
}
|
|
129
|
+
function useProgress() {
|
|
130
|
+
const { progress } = useLessonkit();
|
|
131
|
+
return progress;
|
|
132
|
+
}
|
|
133
|
+
function useTracking() {
|
|
134
|
+
const { track } = useLessonkit();
|
|
135
|
+
return (0, import_react2.useMemo)(() => ({ track }), [track]);
|
|
136
|
+
}
|
|
137
|
+
function useCompletion() {
|
|
138
|
+
const { completeLesson, completeCourse } = useLessonkit();
|
|
139
|
+
return (0, import_react2.useMemo)(() => ({ completeLesson, completeCourse }), [completeLesson, completeCourse]);
|
|
140
|
+
}
|
|
141
|
+
function useQuizState() {
|
|
142
|
+
const { track } = useLessonkit();
|
|
143
|
+
return (0, import_react2.useMemo)(
|
|
144
|
+
() => ({
|
|
145
|
+
answer: (opts) => {
|
|
146
|
+
track("quiz_answered", opts);
|
|
147
|
+
},
|
|
148
|
+
complete: (opts) => {
|
|
149
|
+
track("quiz_completed", opts);
|
|
150
|
+
}
|
|
151
|
+
}),
|
|
152
|
+
[track]
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// src/components.tsx
|
|
157
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
158
|
+
function Course(props) {
|
|
159
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(LessonkitProvider, { config: { courseId: props.courseId }, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("section", { "aria-label": props.title, children: [
|
|
160
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("h1", { children: props.title }),
|
|
161
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { children: props.children })
|
|
162
|
+
] }) });
|
|
163
|
+
}
|
|
164
|
+
function Lesson(props) {
|
|
165
|
+
const { setActiveLesson } = useLessonkit();
|
|
166
|
+
const { completeLesson } = useCompletion();
|
|
167
|
+
const id = props.lessonId ?? (0, import_react3.useMemo)(() => `lesson-${cryptoRandomId()}`, []);
|
|
168
|
+
(0, import_react3.useEffect)(() => {
|
|
169
|
+
setActiveLesson(id);
|
|
170
|
+
return () => {
|
|
171
|
+
completeLesson(id);
|
|
172
|
+
};
|
|
173
|
+
}, [id, setActiveLesson, completeLesson]);
|
|
174
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("article", { "aria-label": props.title, children: [
|
|
175
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("h2", { children: props.title }),
|
|
176
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { children: props.children })
|
|
177
|
+
] });
|
|
178
|
+
}
|
|
179
|
+
function Scenario(props) {
|
|
180
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("section", { "aria-label": "Scenario", children: props.children });
|
|
181
|
+
}
|
|
182
|
+
function Reflection(props) {
|
|
183
|
+
const promptId = (0, import_react3.useId)();
|
|
184
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("section", { "aria-label": "Reflection", children: [
|
|
185
|
+
props.prompt ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { id: promptId, children: props.prompt }) : null,
|
|
186
|
+
props.children,
|
|
187
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("textarea", { "aria-labelledby": props.prompt ? promptId : void 0 })
|
|
188
|
+
] });
|
|
189
|
+
}
|
|
190
|
+
function KnowledgeCheck(props) {
|
|
191
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Quiz, { question: props.question, choices: props.choices, answer: props.answer });
|
|
192
|
+
}
|
|
193
|
+
function Quiz(props) {
|
|
194
|
+
const quiz = useQuizState();
|
|
195
|
+
const [selected, setSelected] = (0, import_react3.useState)(null);
|
|
196
|
+
const questionId = (0, import_react3.useId)();
|
|
197
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("section", { "aria-label": "Quiz", children: [
|
|
198
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { id: questionId, children: props.question }),
|
|
199
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("fieldset", { "aria-labelledby": questionId, children: [
|
|
200
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("legend", { className: "sr-only", children: "Quiz choices" }),
|
|
201
|
+
props.choices.map((c) => /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("label", { style: { display: "block" }, children: [
|
|
202
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
203
|
+
"input",
|
|
204
|
+
{
|
|
205
|
+
type: "radio",
|
|
206
|
+
name: questionId,
|
|
207
|
+
value: c,
|
|
208
|
+
checked: selected === c,
|
|
209
|
+
onChange: () => {
|
|
210
|
+
setSelected(c);
|
|
211
|
+
quiz.answer({ question: props.question, choice: c, correct: c === props.answer });
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
),
|
|
215
|
+
c
|
|
216
|
+
] }, c))
|
|
217
|
+
] }),
|
|
218
|
+
selected ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { role: "status", "aria-live": "polite", children: selected === props.answer ? "Correct" : "Try again" }) : null
|
|
219
|
+
] });
|
|
220
|
+
}
|
|
221
|
+
function ProgressTracker() {
|
|
222
|
+
const { progress } = useLessonkit();
|
|
223
|
+
const completed = progress.completedLessonIds.size;
|
|
224
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("aside", { "aria-label": "Progress", children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("p", { children: [
|
|
225
|
+
"Lessons completed: ",
|
|
226
|
+
completed
|
|
227
|
+
] }) });
|
|
228
|
+
}
|
|
229
|
+
function cryptoRandomId() {
|
|
230
|
+
const g = globalThis;
|
|
231
|
+
if (g.crypto?.randomUUID) return g.crypto.randomUUID();
|
|
232
|
+
return Math.random().toString(16).slice(2);
|
|
233
|
+
}
|
|
234
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
235
|
+
0 && (module.exports = {
|
|
236
|
+
Course,
|
|
237
|
+
KnowledgeCheck,
|
|
238
|
+
Lesson,
|
|
239
|
+
ProgressTracker,
|
|
240
|
+
Quiz,
|
|
241
|
+
Reflection,
|
|
242
|
+
Scenario,
|
|
243
|
+
useCompletion,
|
|
244
|
+
useLessonkit,
|
|
245
|
+
useProgress,
|
|
246
|
+
useQuizState,
|
|
247
|
+
useTracking
|
|
248
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import * as _lessonkit_core from '@lessonkit/core';
|
|
4
|
+
import { LessonId, CourseId, TelemetryEvent, TrackingClient } from '@lessonkit/core';
|
|
5
|
+
import { XAPIClient } from '@lessonkit/xapi';
|
|
6
|
+
|
|
7
|
+
declare function Course(props: {
|
|
8
|
+
title: string;
|
|
9
|
+
courseId?: string;
|
|
10
|
+
children: React.ReactNode;
|
|
11
|
+
}): react_jsx_runtime.JSX.Element;
|
|
12
|
+
declare function Lesson(props: {
|
|
13
|
+
title: string;
|
|
14
|
+
lessonId?: LessonId;
|
|
15
|
+
children: React.ReactNode;
|
|
16
|
+
}): react_jsx_runtime.JSX.Element;
|
|
17
|
+
declare function Scenario(props: {
|
|
18
|
+
children: React.ReactNode;
|
|
19
|
+
}): react_jsx_runtime.JSX.Element;
|
|
20
|
+
declare function Reflection(props: {
|
|
21
|
+
prompt?: string;
|
|
22
|
+
children?: React.ReactNode;
|
|
23
|
+
}): react_jsx_runtime.JSX.Element;
|
|
24
|
+
declare function KnowledgeCheck(props: {
|
|
25
|
+
question: string;
|
|
26
|
+
choices: string[];
|
|
27
|
+
answer: string;
|
|
28
|
+
}): react_jsx_runtime.JSX.Element;
|
|
29
|
+
declare function Quiz(props: {
|
|
30
|
+
question: string;
|
|
31
|
+
choices: string[];
|
|
32
|
+
answer: string;
|
|
33
|
+
}): react_jsx_runtime.JSX.Element;
|
|
34
|
+
declare function ProgressTracker(): react_jsx_runtime.JSX.Element;
|
|
35
|
+
|
|
36
|
+
type LessonkitConfig = {
|
|
37
|
+
courseId?: CourseId;
|
|
38
|
+
tracking?: {
|
|
39
|
+
enabled?: boolean;
|
|
40
|
+
sink?: (event: TelemetryEvent) => void | Promise<void>;
|
|
41
|
+
};
|
|
42
|
+
xapi?: {
|
|
43
|
+
enabled?: boolean;
|
|
44
|
+
client?: XAPIClient;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
type ProgressState = {
|
|
48
|
+
activeLessonId?: LessonId;
|
|
49
|
+
completedLessonIds: Set<LessonId>;
|
|
50
|
+
courseCompleted: boolean;
|
|
51
|
+
};
|
|
52
|
+
type LessonkitRuntime = {
|
|
53
|
+
config: LessonkitConfig;
|
|
54
|
+
tracking: TrackingClient;
|
|
55
|
+
xapi: XAPIClient | null;
|
|
56
|
+
progress: ProgressState;
|
|
57
|
+
setActiveLesson: (lessonId: LessonId) => void;
|
|
58
|
+
completeLesson: (lessonId: LessonId) => void;
|
|
59
|
+
completeCourse: () => void;
|
|
60
|
+
track: (name: TelemetryEvent["name"], data?: TelemetryEvent["data"]) => void;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
declare function useLessonkit(): LessonkitRuntime;
|
|
64
|
+
declare function useProgress(): ProgressState;
|
|
65
|
+
declare function useTracking(): {
|
|
66
|
+
track: (name: _lessonkit_core.TelemetryEvent["name"], data?: _lessonkit_core.TelemetryEvent["data"]) => void;
|
|
67
|
+
};
|
|
68
|
+
declare function useCompletion(): {
|
|
69
|
+
completeLesson: (lessonId: _lessonkit_core.LessonId) => void;
|
|
70
|
+
completeCourse: () => void;
|
|
71
|
+
};
|
|
72
|
+
declare function useQuizState(): {
|
|
73
|
+
answer: (opts: {
|
|
74
|
+
question: string;
|
|
75
|
+
choice: string;
|
|
76
|
+
correct: boolean;
|
|
77
|
+
}) => void;
|
|
78
|
+
complete: (opts?: {
|
|
79
|
+
score?: number;
|
|
80
|
+
maxScore?: number;
|
|
81
|
+
}) => void;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export { Course, KnowledgeCheck, Lesson, ProgressTracker, Quiz, Reflection, Scenario, useCompletion, useLessonkit, useProgress, useQuizState, useTracking };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import * as _lessonkit_core from '@lessonkit/core';
|
|
4
|
+
import { LessonId, CourseId, TelemetryEvent, TrackingClient } from '@lessonkit/core';
|
|
5
|
+
import { XAPIClient } from '@lessonkit/xapi';
|
|
6
|
+
|
|
7
|
+
declare function Course(props: {
|
|
8
|
+
title: string;
|
|
9
|
+
courseId?: string;
|
|
10
|
+
children: React.ReactNode;
|
|
11
|
+
}): react_jsx_runtime.JSX.Element;
|
|
12
|
+
declare function Lesson(props: {
|
|
13
|
+
title: string;
|
|
14
|
+
lessonId?: LessonId;
|
|
15
|
+
children: React.ReactNode;
|
|
16
|
+
}): react_jsx_runtime.JSX.Element;
|
|
17
|
+
declare function Scenario(props: {
|
|
18
|
+
children: React.ReactNode;
|
|
19
|
+
}): react_jsx_runtime.JSX.Element;
|
|
20
|
+
declare function Reflection(props: {
|
|
21
|
+
prompt?: string;
|
|
22
|
+
children?: React.ReactNode;
|
|
23
|
+
}): react_jsx_runtime.JSX.Element;
|
|
24
|
+
declare function KnowledgeCheck(props: {
|
|
25
|
+
question: string;
|
|
26
|
+
choices: string[];
|
|
27
|
+
answer: string;
|
|
28
|
+
}): react_jsx_runtime.JSX.Element;
|
|
29
|
+
declare function Quiz(props: {
|
|
30
|
+
question: string;
|
|
31
|
+
choices: string[];
|
|
32
|
+
answer: string;
|
|
33
|
+
}): react_jsx_runtime.JSX.Element;
|
|
34
|
+
declare function ProgressTracker(): react_jsx_runtime.JSX.Element;
|
|
35
|
+
|
|
36
|
+
type LessonkitConfig = {
|
|
37
|
+
courseId?: CourseId;
|
|
38
|
+
tracking?: {
|
|
39
|
+
enabled?: boolean;
|
|
40
|
+
sink?: (event: TelemetryEvent) => void | Promise<void>;
|
|
41
|
+
};
|
|
42
|
+
xapi?: {
|
|
43
|
+
enabled?: boolean;
|
|
44
|
+
client?: XAPIClient;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
type ProgressState = {
|
|
48
|
+
activeLessonId?: LessonId;
|
|
49
|
+
completedLessonIds: Set<LessonId>;
|
|
50
|
+
courseCompleted: boolean;
|
|
51
|
+
};
|
|
52
|
+
type LessonkitRuntime = {
|
|
53
|
+
config: LessonkitConfig;
|
|
54
|
+
tracking: TrackingClient;
|
|
55
|
+
xapi: XAPIClient | null;
|
|
56
|
+
progress: ProgressState;
|
|
57
|
+
setActiveLesson: (lessonId: LessonId) => void;
|
|
58
|
+
completeLesson: (lessonId: LessonId) => void;
|
|
59
|
+
completeCourse: () => void;
|
|
60
|
+
track: (name: TelemetryEvent["name"], data?: TelemetryEvent["data"]) => void;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
declare function useLessonkit(): LessonkitRuntime;
|
|
64
|
+
declare function useProgress(): ProgressState;
|
|
65
|
+
declare function useTracking(): {
|
|
66
|
+
track: (name: _lessonkit_core.TelemetryEvent["name"], data?: _lessonkit_core.TelemetryEvent["data"]) => void;
|
|
67
|
+
};
|
|
68
|
+
declare function useCompletion(): {
|
|
69
|
+
completeLesson: (lessonId: _lessonkit_core.LessonId) => void;
|
|
70
|
+
completeCourse: () => void;
|
|
71
|
+
};
|
|
72
|
+
declare function useQuizState(): {
|
|
73
|
+
answer: (opts: {
|
|
74
|
+
question: string;
|
|
75
|
+
choice: string;
|
|
76
|
+
correct: boolean;
|
|
77
|
+
}) => void;
|
|
78
|
+
complete: (opts?: {
|
|
79
|
+
score?: number;
|
|
80
|
+
maxScore?: number;
|
|
81
|
+
}) => void;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export { Course, KnowledgeCheck, Lesson, ProgressTracker, Quiz, Reflection, Scenario, useCompletion, useLessonkit, useProgress, useQuizState, useTracking };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// src/components.tsx
|
|
2
|
+
import { useEffect, useId, useMemo as useMemo3, useState as useState2 } from "react";
|
|
3
|
+
|
|
4
|
+
// src/context.tsx
|
|
5
|
+
import { createContext, useCallback, useMemo, useRef, useState } from "react";
|
|
6
|
+
import { createTrackingClient, nowIso } from "@lessonkit/core";
|
|
7
|
+
import { createXAPIClient } from "@lessonkit/xapi";
|
|
8
|
+
import { jsx } from "react/jsx-runtime";
|
|
9
|
+
var LessonkitContext = createContext(null);
|
|
10
|
+
function LessonkitProvider(props) {
|
|
11
|
+
const config = props.config ?? {};
|
|
12
|
+
const tracking = useMemo(() => {
|
|
13
|
+
if (config.tracking?.enabled === false) return createTrackingClient();
|
|
14
|
+
return createTrackingClient({ sink: config.tracking?.sink });
|
|
15
|
+
}, [config.tracking?.enabled, config.tracking?.sink]);
|
|
16
|
+
const xapi = useMemo(() => {
|
|
17
|
+
if (config.xapi?.enabled === false) return null;
|
|
18
|
+
return config.xapi?.client ?? createXAPIClient();
|
|
19
|
+
}, [config.xapi?.enabled, config.xapi?.client]);
|
|
20
|
+
const [completedLessonIds, setCompletedLessonIds] = useState(() => /* @__PURE__ */ new Set());
|
|
21
|
+
const [activeLessonId, setActiveLessonId] = useState(void 0);
|
|
22
|
+
const [courseCompleted, setCourseCompleted] = useState(false);
|
|
23
|
+
const courseIdRef = useRef(config.courseId);
|
|
24
|
+
courseIdRef.current = config.courseId;
|
|
25
|
+
const track = useCallback(
|
|
26
|
+
(name, data) => {
|
|
27
|
+
tracking.track({
|
|
28
|
+
name,
|
|
29
|
+
timestamp: nowIso(),
|
|
30
|
+
courseId: courseIdRef.current,
|
|
31
|
+
lessonId: activeLessonId,
|
|
32
|
+
data
|
|
33
|
+
});
|
|
34
|
+
},
|
|
35
|
+
[tracking, activeLessonId]
|
|
36
|
+
);
|
|
37
|
+
const setActiveLesson = useCallback(
|
|
38
|
+
(lessonId) => {
|
|
39
|
+
setActiveLessonId(lessonId);
|
|
40
|
+
track("lesson_started", { lessonId });
|
|
41
|
+
xapi?.startedLesson({ lessonId });
|
|
42
|
+
},
|
|
43
|
+
[track, xapi]
|
|
44
|
+
);
|
|
45
|
+
const completeLesson = useCallback(
|
|
46
|
+
(lessonId) => {
|
|
47
|
+
setCompletedLessonIds((prev) => new Set(prev).add(lessonId));
|
|
48
|
+
track("lesson_completed", { lessonId });
|
|
49
|
+
xapi?.completeLesson({ lessonId });
|
|
50
|
+
},
|
|
51
|
+
[track, xapi]
|
|
52
|
+
);
|
|
53
|
+
const completeCourse = useCallback(() => {
|
|
54
|
+
setCourseCompleted(true);
|
|
55
|
+
track("course_completed");
|
|
56
|
+
xapi?.completeCourse({});
|
|
57
|
+
}, [track, xapi]);
|
|
58
|
+
const runtime = useMemo(
|
|
59
|
+
() => ({
|
|
60
|
+
config,
|
|
61
|
+
tracking,
|
|
62
|
+
xapi,
|
|
63
|
+
progress: { activeLessonId, completedLessonIds, courseCompleted },
|
|
64
|
+
setActiveLesson,
|
|
65
|
+
completeLesson,
|
|
66
|
+
completeCourse,
|
|
67
|
+
track
|
|
68
|
+
}),
|
|
69
|
+
[
|
|
70
|
+
config,
|
|
71
|
+
tracking,
|
|
72
|
+
xapi,
|
|
73
|
+
activeLessonId,
|
|
74
|
+
completedLessonIds,
|
|
75
|
+
courseCompleted,
|
|
76
|
+
setActiveLesson,
|
|
77
|
+
completeLesson,
|
|
78
|
+
completeCourse,
|
|
79
|
+
track
|
|
80
|
+
]
|
|
81
|
+
);
|
|
82
|
+
return /* @__PURE__ */ jsx(LessonkitContext.Provider, { value: runtime, children: props.children });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// src/hooks.ts
|
|
86
|
+
import { useContext, useMemo as useMemo2 } from "react";
|
|
87
|
+
function useLessonkit() {
|
|
88
|
+
const ctx = useContext(LessonkitContext);
|
|
89
|
+
if (!ctx) throw new Error("LessonKit: missing LessonkitProvider");
|
|
90
|
+
return ctx;
|
|
91
|
+
}
|
|
92
|
+
function useProgress() {
|
|
93
|
+
const { progress } = useLessonkit();
|
|
94
|
+
return progress;
|
|
95
|
+
}
|
|
96
|
+
function useTracking() {
|
|
97
|
+
const { track } = useLessonkit();
|
|
98
|
+
return useMemo2(() => ({ track }), [track]);
|
|
99
|
+
}
|
|
100
|
+
function useCompletion() {
|
|
101
|
+
const { completeLesson, completeCourse } = useLessonkit();
|
|
102
|
+
return useMemo2(() => ({ completeLesson, completeCourse }), [completeLesson, completeCourse]);
|
|
103
|
+
}
|
|
104
|
+
function useQuizState() {
|
|
105
|
+
const { track } = useLessonkit();
|
|
106
|
+
return useMemo2(
|
|
107
|
+
() => ({
|
|
108
|
+
answer: (opts) => {
|
|
109
|
+
track("quiz_answered", opts);
|
|
110
|
+
},
|
|
111
|
+
complete: (opts) => {
|
|
112
|
+
track("quiz_completed", opts);
|
|
113
|
+
}
|
|
114
|
+
}),
|
|
115
|
+
[track]
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// src/components.tsx
|
|
120
|
+
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
121
|
+
function Course(props) {
|
|
122
|
+
return /* @__PURE__ */ jsx2(LessonkitProvider, { config: { courseId: props.courseId }, children: /* @__PURE__ */ jsxs("section", { "aria-label": props.title, children: [
|
|
123
|
+
/* @__PURE__ */ jsx2("h1", { children: props.title }),
|
|
124
|
+
/* @__PURE__ */ jsx2("div", { children: props.children })
|
|
125
|
+
] }) });
|
|
126
|
+
}
|
|
127
|
+
function Lesson(props) {
|
|
128
|
+
const { setActiveLesson } = useLessonkit();
|
|
129
|
+
const { completeLesson } = useCompletion();
|
|
130
|
+
const id = props.lessonId ?? useMemo3(() => `lesson-${cryptoRandomId()}`, []);
|
|
131
|
+
useEffect(() => {
|
|
132
|
+
setActiveLesson(id);
|
|
133
|
+
return () => {
|
|
134
|
+
completeLesson(id);
|
|
135
|
+
};
|
|
136
|
+
}, [id, setActiveLesson, completeLesson]);
|
|
137
|
+
return /* @__PURE__ */ jsxs("article", { "aria-label": props.title, children: [
|
|
138
|
+
/* @__PURE__ */ jsx2("h2", { children: props.title }),
|
|
139
|
+
/* @__PURE__ */ jsx2("div", { children: props.children })
|
|
140
|
+
] });
|
|
141
|
+
}
|
|
142
|
+
function Scenario(props) {
|
|
143
|
+
return /* @__PURE__ */ jsx2("section", { "aria-label": "Scenario", children: props.children });
|
|
144
|
+
}
|
|
145
|
+
function Reflection(props) {
|
|
146
|
+
const promptId = useId();
|
|
147
|
+
return /* @__PURE__ */ jsxs("section", { "aria-label": "Reflection", children: [
|
|
148
|
+
props.prompt ? /* @__PURE__ */ jsx2("p", { id: promptId, children: props.prompt }) : null,
|
|
149
|
+
props.children,
|
|
150
|
+
/* @__PURE__ */ jsx2("textarea", { "aria-labelledby": props.prompt ? promptId : void 0 })
|
|
151
|
+
] });
|
|
152
|
+
}
|
|
153
|
+
function KnowledgeCheck(props) {
|
|
154
|
+
return /* @__PURE__ */ jsx2(Quiz, { question: props.question, choices: props.choices, answer: props.answer });
|
|
155
|
+
}
|
|
156
|
+
function Quiz(props) {
|
|
157
|
+
const quiz = useQuizState();
|
|
158
|
+
const [selected, setSelected] = useState2(null);
|
|
159
|
+
const questionId = useId();
|
|
160
|
+
return /* @__PURE__ */ jsxs("section", { "aria-label": "Quiz", children: [
|
|
161
|
+
/* @__PURE__ */ jsx2("p", { id: questionId, children: props.question }),
|
|
162
|
+
/* @__PURE__ */ jsxs("fieldset", { "aria-labelledby": questionId, children: [
|
|
163
|
+
/* @__PURE__ */ jsx2("legend", { className: "sr-only", children: "Quiz choices" }),
|
|
164
|
+
props.choices.map((c) => /* @__PURE__ */ jsxs("label", { style: { display: "block" }, children: [
|
|
165
|
+
/* @__PURE__ */ jsx2(
|
|
166
|
+
"input",
|
|
167
|
+
{
|
|
168
|
+
type: "radio",
|
|
169
|
+
name: questionId,
|
|
170
|
+
value: c,
|
|
171
|
+
checked: selected === c,
|
|
172
|
+
onChange: () => {
|
|
173
|
+
setSelected(c);
|
|
174
|
+
quiz.answer({ question: props.question, choice: c, correct: c === props.answer });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
),
|
|
178
|
+
c
|
|
179
|
+
] }, c))
|
|
180
|
+
] }),
|
|
181
|
+
selected ? /* @__PURE__ */ jsx2("p", { role: "status", "aria-live": "polite", children: selected === props.answer ? "Correct" : "Try again" }) : null
|
|
182
|
+
] });
|
|
183
|
+
}
|
|
184
|
+
function ProgressTracker() {
|
|
185
|
+
const { progress } = useLessonkit();
|
|
186
|
+
const completed = progress.completedLessonIds.size;
|
|
187
|
+
return /* @__PURE__ */ jsx2("aside", { "aria-label": "Progress", children: /* @__PURE__ */ jsxs("p", { children: [
|
|
188
|
+
"Lessons completed: ",
|
|
189
|
+
completed
|
|
190
|
+
] }) });
|
|
191
|
+
}
|
|
192
|
+
function cryptoRandomId() {
|
|
193
|
+
const g = globalThis;
|
|
194
|
+
if (g.crypto?.randomUUID) return g.crypto.randomUUID();
|
|
195
|
+
return Math.random().toString(16).slice(2);
|
|
196
|
+
}
|
|
197
|
+
export {
|
|
198
|
+
Course,
|
|
199
|
+
KnowledgeCheck,
|
|
200
|
+
Lesson,
|
|
201
|
+
ProgressTracker,
|
|
202
|
+
Quiz,
|
|
203
|
+
Reflection,
|
|
204
|
+
Scenario,
|
|
205
|
+
useCompletion,
|
|
206
|
+
useLessonkit,
|
|
207
|
+
useProgress,
|
|
208
|
+
useQuizState,
|
|
209
|
+
useTracking
|
|
210
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lessonkit/react",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "React components and hooks for building learning experiences with LessonKit.",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/eddiethedean/lessonkit.git",
|
|
10
|
+
"directory": "packages/react"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/eddiethedean/lessonkit",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/eddiethedean/lessonkit/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"lessonkit",
|
|
18
|
+
"react",
|
|
19
|
+
"learning",
|
|
20
|
+
"training",
|
|
21
|
+
"lms",
|
|
22
|
+
"scorm",
|
|
23
|
+
"xapi"
|
|
24
|
+
],
|
|
25
|
+
"type": "module",
|
|
26
|
+
"main": "./dist/index.cjs",
|
|
27
|
+
"module": "./dist/index.js",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"import": "./dist/index.js",
|
|
33
|
+
"require": "./dist/index.cjs"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist"
|
|
38
|
+
],
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsup src/index.tsx --format esm,cjs --dts --external react --external react-dom",
|
|
41
|
+
"dev": "tsup src/index.tsx --format esm,cjs --dts --watch --external react --external react-dom",
|
|
42
|
+
"prepublishOnly": "npm run build",
|
|
43
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
44
|
+
"test": "vitest run --passWithNoTests",
|
|
45
|
+
"lint": "echo \"(no lint configured yet)\""
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"react": ">=18",
|
|
49
|
+
"react-dom": ">=18"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"@lessonkit/core": "0.1.0",
|
|
53
|
+
"@lessonkit/xapi": "0.1.0"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@testing-library/react": "^16.3.0",
|
|
57
|
+
"@types/react": "^18.3.23",
|
|
58
|
+
"@types/react-dom": "^18.3.7",
|
|
59
|
+
"jsdom": "^26.1.0",
|
|
60
|
+
"tsup": "^8.5.0",
|
|
61
|
+
"typescript": "^5.8.3",
|
|
62
|
+
"vitest": "^3.2.4"
|
|
63
|
+
}
|
|
64
|
+
}
|