Skip to content

Commit b05fc55

Browse files
committed
docs: add example of tutorialkit:store usage
1 parent f49e910 commit b05fc55

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

docs/tutorialkit.dev/astro.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ export default defineConfig({
6767
label: 'Overriding Components',
6868
link: '/guides/overriding-components/',
6969
},
70+
{
71+
label: 'Using low level APIs',
72+
link: '/guides/using-low-level-apis/',
73+
},
7074
],
7175
},
7276
{
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Dialog } from '@tutorialkit/react';
2+
import { useState } from 'react';
3+
4+
export function HelpButton() {
5+
const [message, setMessage] = useState<string | null>(null);
6+
7+
async function onClick() {
8+
setMessage('Your index.js should have a default export');
9+
}
10+
11+
return (
12+
<>
13+
<button
14+
onClick={onClick}
15+
className="px-4 py-1 my-3 cursor-pointer border-1 border-tk-border-primary rounded-md bg-tk-elements-primaryButton-backgroundColor text-tk-elements-primaryButton-textColor"
16+
>
17+
Help
18+
</button>
19+
20+
{message && (
21+
<Dialog title="Help" confirmText="OK" onClose={() => setMessage(null)}>
22+
{message}
23+
</Dialog>
24+
)}
25+
</>
26+
);
27+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
---
2+
title: Using low level APIs
3+
---
4+
5+
import { HelpButton } from "@components/react-examples/HelpButton";
6+
import '@tutorialkit/astro/default-theme.css';
7+
8+
TutorialKit exposes low level APIs that authors can utilize to provide highly custom experience in their tutorials.
9+
10+
## Tutorial Store
11+
12+
You can access Tutorial Store by importing the `tutorialkit:store` entrypoint.
13+
14+
```ts
15+
import tutorialStore from "tutorialkit:store";
16+
```
17+
18+
The Tutorial Store provides access to current state of the Tutorial, for example the files and their contents.
19+
20+
```ts
21+
import tutorialStore from "tutorialkit:store";
22+
23+
console.log(tutorialStore.documents.value);
24+
{
25+
"/index.js": {
26+
"value": "export default 'hello from index.js';\n",
27+
"type": "file",
28+
},
29+
"/vite.config.ts": {
30+
"value": "import { defineConfig } from \"vite\";\n\nexport default defineConfig({});\n",
31+
"type": "file",
32+
}
33+
}
34+
```
35+
36+
It also exposes methods that can be used to modify lesson files:
37+
38+
```ts
39+
import tutorialStore from "tutorialkit:store";
40+
41+
await tutorialStore.addFolder("/src");
42+
await tutorialStore.addFile("/src/new-file.js");
43+
tutorialStore.updateFile("/src/new-file.js", "console.log('Hello world');");
44+
```
45+
46+
### Example usage in React component
47+
48+
Here is an example of custom React component that reads contents of `index.js` and gives user hints about the expected content.
49+
50+
<HelpButton client:load />
51+
52+
```tsx title="src/components/HelpButton.tsx"
53+
import tutorialStore from "tutorialkit:store";
54+
import { Dialog } from "@tutorialkit/react";
55+
import { useState } from "react";
56+
57+
export function HelpButton() {
58+
const [message, setMessage] = useState<string | null>(null);
59+
60+
async function onClick() {
61+
const files = tutorialStore.documents.value;
62+
const index = files["/index.js"].value as string;
63+
64+
if (index.length === 0) {
65+
setMessage("Your index.js should not be empty");
66+
} else if (!index.includes("export default")) {
67+
setMessage("Your index.js should have a default export");
68+
} else {
69+
setMessage("All good");
70+
}
71+
}
72+
73+
return (
74+
<>
75+
<button
76+
onClick={onClick}
77+
className="px-4 py-1 rounded-md bg-tk-elements-primaryButton-backgroundColor text-tk-elements-primaryButton-textColor"
78+
>
79+
Help
80+
</button>
81+
82+
{message && (
83+
<Dialog title="Help" confirmText="OK" onClose={() => setMessage(null)}>
84+
{message}
85+
</Dialog>
86+
)}
87+
</>
88+
);
89+
}
90+
```
91+
92+
```mdx title="content.mdx"
93+
---
94+
type: lesson
95+
title: Lesson Topic
96+
---
97+
98+
import { HelpButton } from "@/components/HelpButton";
99+
100+
# Lesson Topic
101+
102+
In this lesson we'll learn about...
103+
Click this button if you need help: <HelpButton client:load />
104+
```

0 commit comments

Comments
 (0)