Skip to content

Commit 666bf1f

Browse files
Add documentation and stories; update React API (#1)
* Breaking: Flatten options into props for render blocks React * Use TypeDoc to generate docs, tweak README.md * Add usage documentation for VanillaJS and React * Export createMakeCodeRenderBlocks (potentially breaking change)
1 parent 54b4a1a commit 666bf1f

23 files changed

+1079
-558
lines changed

.github/workflows/build-docs.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: build-docs
2+
on:
3+
release:
4+
types: [created]
5+
push:
6+
branches:
7+
- '**'
8+
tags:
9+
- '**'
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
- name: Setup Pages
17+
uses: actions/configure-pages@v5
18+
- run: npm ci
19+
- name: Build docs
20+
run: npm run docs
21+
- name: Upload artifact
22+
uses: actions/upload-pages-artifact@v3
23+
with:
24+
path: ./docs/build
25+
26+
deploy:
27+
if: ${{ startsWith(github.ref, 'refs/tags/') }}
28+
permissions:
29+
pages: write
30+
id-token: write
31+
environment:
32+
name: github-pages
33+
url: ${{ steps.deployment.outputs.page_url }}
34+
runs-on: ubuntu-latest
35+
needs: build
36+
steps:
37+
- name: Deploy to GitHub Pages
38+
id: deployment
39+
uses: actions/deploy-pages@v4

.github/workflows/build.yml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,3 @@ jobs:
3131
if: github.event_name == 'release' && github.event.action == 'created'
3232
env:
3333
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
34-
deploy:
35-
# https://github.com/actions/deploy-pages
36-
runs-on: ubuntu-latest
37-
permissions:
38-
pages: write
39-
id-token: write
40-
needs: build
41-
# Deploy to the github-pages environment
42-
environment:
43-
name: github-pages
44-
url: ${{ steps.deployment.outputs.page_url }}
45-
if: github.ref == 'refs/heads/main'
46-
steps:
47-
- name: Deploy to GitHub Pages
48-
id: deployment
49-
uses: actions/deploy-pages@v4

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ node_modules
1111
dist
1212
dist-ssr
1313
*.local
14+
docs/build
1415

1516
# Editor directories and files
1617
.vscode/*

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) YEAR AUTHOR
3+
Copyright (c) 2024-2025 Micro:bit Educational Foundation and contributors
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
# React/JavaScript library for embedding Microsoft MakeCode as an iframe
1+
# MakeCode Embed
22

3-
This project is a work in progress.
3+
<a href="https://microbit-foundation.github.io/makecode-embed/" class="typedoc-ignore">This documentation is best viewed on the documentation site rather than GitHub or NPM package site.</a>
4+
5+
This is a React/JavaScript library for embedding Microsoft MakeCode as an iframe.
46

57
It is intended to be used by other Micro:bit Educational Foundation projects
68
that need to embed MakeCode and, when the API stabilises, to be useful to others
79
who embed MakeCode.
810

9-
## Documentation
11+
- [StoryBook demo of the components](https://makecode-embed.pages.dev/)
12+
13+
## Usage
1014

11-
There isn't any documentation yet. You can find a StoryBook demo of the
12-
components at https://microbit-foundation.github.io/makecode-embed/
15+
- [React usage documentation](docs/react.md)
16+
- [VanillaJS usage documentation](docs/vanilla.md)
1317

1418
## License
1519

1620
This software is under the MIT open source license.
1721

18-
[SPDX-License-Identifier: MIT](LICENSE)
22+
[SPDX-License-Identifier: MIT](LICENSE.md)
1923

2024
We use dependencies via the NPM registry as specified by the package.json file under common Open Source licenses.
2125

docs/custom.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Styling for hiding elements in the generated TypeDoc site. */
2+
a.typedoc-ignore {
3+
display: none;
4+
}

docs/react.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
title: React Usage
3+
---
4+
5+
# React Usage
6+
7+
<a href="https://microbit-foundation.github.io/makecode-embed/" class="typedoc-ignore">This documentation is best viewed on the documentation site rather than GitHub or NPM package site.</a>
8+
9+
## Blocks rendering
10+
11+
Use {@link react.MakeCodeRenderBlocksProvider | MakeCodeRenderBlocksProvider} and {@link react.MakeCodeBlocksRendering | MakeCodeBlocksRendering} React components to render MakeCode blocks for a MakeCode project. Example MakeCode projects used for the demo are defined in [fixtures.ts](../src/stories/fixtures.ts).
12+
13+
```js
14+
import {
15+
MakeCodeRenderBlocksProvider,
16+
MakeCodeBlocksRendering,
17+
} from '@microbit/makecode-embed/react';
18+
19+
<MakeCodeRenderBlocksProvider>
20+
<MakeCodeBlocksRendering code={project} />
21+
</MakeCodeRenderBlocksProvider>;
22+
```
23+
24+
The provider manages a hidden, embedded MakeCode. If you have more than one code embed then place the provider at a suitable location. You can use the `disabled` prop to avoid loading MakeCode if you know it's not needed.
25+
26+
For more examples, take a look at the [MakeCode blocks rendering demo source code](../src/stories/react/MakeCodeBlocksRendering.stories.tsx).
27+
28+
## Embed MakeCode editor
29+
30+
Use {@link react.MakeCodeFrame | MakeCodeFrame} component to embed MakeCode.
31+
32+
```js
33+
import { MakeCodeFrame } from '@microbit/makecode-embed/react';
34+
35+
<MakeCodeFrame
36+
ref={ref}
37+
controller={1}
38+
controllerId="YOUR APP NAME HERE"
39+
initialProjects={[savedProject]}
40+
onEditorContentLoaded={(e) => console.log("MakeCode is now ready")},
41+
onWorkspaceSave={(e) => {
42+
// Set project as project changes in the editor.
43+
setSavedProject(e.project);
44+
}}
45+
/>;
46+
```
47+
48+
For more examples, take a look at the [MakeCode frame demo source code](../src/stories/react/MakeCodeFrame.stories.tsx).

docs/vanilla.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
title: VanillaJS Usage
3+
---
4+
5+
# VanillaJS Usage
6+
7+
<a href="https://microbit-foundation.github.io/makecode-embed/" class="typedoc-ignore">This documentation is best viewed on the documentation site rather than GitHub or NPM package site.</a>
8+
9+
## Blocks rendering
10+
11+
Use {@link vanilla.createMakeCodeRenderBlocks | createMakeCodeRenderBlocks} to create a MakeCode block renderer. Initialise the renderer before calling `renderBlocks` with a {@link vanilla.RenderBlocksRequest | RenderBlocksRequest}, which includes a MakeCode project ([see examples](../src/stories/fixtures.ts)). The function will return a {@link vanilla.RenderBlocksResponse | RenderBlocksResponse}.
12+
13+
```js
14+
import { createMakeCodeRenderBlocks } from "@microbit/makecode-embed/vanilla";
15+
16+
const renderer = createMakeCodeRenderBlocks({});
17+
renderer.initialize();
18+
const result = await renderer.renderBlocks({ code: makeCodeProject });
19+
20+
document.querySelector<HTMLDivElement>("#app")!.innerHTML = `
21+
<div>
22+
${result.svg}
23+
</div>
24+
`;
25+
```
26+
27+
For more examples, take a look at the [MakeCode blocks rendering demo source code](../src/stories/vanilla/makecode-render-blocks.stories.tsx).
28+
29+
## Embed MakeCode editor
30+
31+
Use {@link vanilla.MakeCodeFrameDriver | MakeCodeFrameDriver} class to create a driverRef for an iframe element. The iframe element src URL can be generated using {@link vanilla.createMakeCodeURL | createMakeCodeURL}.
32+
33+
```js
34+
import {
35+
Project,
36+
MakeCodeFrameDriver,
37+
createMakeCodeURL,
38+
} from "@microbit/makecode-embed/vanilla";
39+
40+
// Set up an iframe element.
41+
const iframe = document.createElement("iframe");
42+
iframe.allow = "usb; autoplay; camera; microphone;";
43+
iframe.src = createMakeCodeURL(
44+
"https://makecode.microbit.org",
45+
undefined, // Version.
46+
undefined, // Language.
47+
1, // Controller.
48+
undefined // Query params.
49+
);
50+
iframe.width = "100%";
51+
iframe.height = "100%";
52+
53+
document.querySelector<HTMLDivElement>("#app")!.appendChild(iframe);
54+
55+
// Create and initialise an instance of MakeCodeFrameDriver.
56+
const driverRef = new MakeCodeFrameDriver(
57+
{
58+
controllerId: "YOUR APP NAME HERE",
59+
initialProjects: async () => [makeCodeProject],
60+
onEditorContentLoaded: (e) => console.log("MakeCode is now ready"),
61+
onWorkspaceSave: (e) => {
62+
console.log(e.project!.header!.id, e.project);
63+
},
64+
},
65+
() => iframe
66+
);
67+
driverRef.initialize();
68+
```
69+
70+
For more examples, take a look at the [MakeCode frame demo source code](../src/stories/vanilla/makecode-frame-driver.stories.tsx).

0 commit comments

Comments
 (0)