|
| 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