Skip to content

Commit b7074ff

Browse files
committed
Add initial scaffold for PlaygroundWidget
1 parent dcad394 commit b7074ff

File tree

6 files changed

+149
-3
lines changed

6 files changed

+149
-3
lines changed

src/bindings/Next.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,5 @@ module Dynamic = {
138138
@bs.module("next/dynamic")
139139
external dynamic: (unit => Js.Promise.t<'a>, options) => 'a = "default"
140140

141-
@bs.val external \"import": string => Js.Promise.t<'a> = "import"
141+
@bs.val external import_: string => Js.Promise.t<'a> = "import"
142142
}

src/bindings/Next.resi

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,5 @@ module Dynamic: {
130130
external dynamic: (unit => Js.Promise.t<'a>, options) => 'a = "default"
131131

132132
@bs.val
133-
external \"import": string => Js.Promise.t<'a> = "import"
133+
external import_: string => Js.Promise.t<'a> = "import"
134134
}

src/components/PlaygroundWidget.js

+80
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/PlaygroundWidget.res

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
type state =
2+
| Init
3+
| Edit
4+
5+
@react.component
6+
let make = (~initialCode="", ~height="10rem") => {
7+
let (state, setState) = React.useState(_ => Init)
8+
let editorCode = React.useRef(initialCode)
9+
let typingTimer = React.useRef(None)
10+
11+
let timeoutCompile = React.useRef(() => ())
12+
13+
let codeMirrorComponent: React.component<'props> = Next.Dynamic.dynamic(() => {
14+
Next.Dynamic.import_("src/components/CodeMirror.js")->Promise.then(m => {
15+
m["make"]
16+
})
17+
}, Next.Dynamic.options(
18+
~ssr=false,
19+
~loading=() => <CodeExample code="Loading compiler..." lang="text" showLabel=false />,
20+
(),
21+
))
22+
23+
let codeMirrorEl = React.createElement(
24+
codeMirrorComponent,
25+
CodeMirror.makeProps(
26+
~className="w-full py-4",
27+
~mode="reason",
28+
~errors=[],
29+
~minHeight=height,
30+
~maxHeight=height,
31+
~value=editorCode.current,
32+
~onChange=value => {
33+
editorCode.current = value
34+
35+
switch typingTimer.current {
36+
| None => ()
37+
| Some(timer) => Js.Global.clearTimeout(timer)
38+
}
39+
40+
let timer = Js.Global.setTimeout(() => {
41+
timeoutCompile.current()
42+
typingTimer.current = None
43+
}, 100)
44+
typingTimer.current = Some(timer)
45+
},
46+
(),
47+
),
48+
)
49+
50+
<div>
51+
<div style={ReactDOMStyle.make(~height, ())}>
52+
{switch state {
53+
| Init =>
54+
<div>
55+
<CodeExample code=initialCode lang="res" showLabel=false />
56+
<button onClick={evt => setState(_ => Edit)}> {React.string("Edit")} </button>
57+
</div>
58+
| Edit => codeMirrorEl
59+
}}
60+
</div>
61+
</div>
62+
}

src/layouts/LandingPageLayout.js

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/layouts/LandingPageLayout.res

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ let make = (~components=Markdown.default, ~children) => {
7979
</a>
8080
</Link>
8181
</div>
82+
<PlaygroundWidget initialCode="let a = 1" />
8283
children
8384
</div>
8485
</div>

0 commit comments

Comments
 (0)