Skip to content

Commit e689f5b

Browse files
adding banner
1 parent a6106bb commit e689f5b

File tree

7 files changed

+443
-1066
lines changed

7 files changed

+443
-1066
lines changed

playground/internal/react/banner.go

Lines changed: 0 additions & 124 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package react
2+
3+
func BannerTitle(version string) *Element {
4+
return CreateElement(bannerTitleComponent, Props{
5+
`version`: version,
6+
})
7+
}
8+
9+
func bannerTitleComponent(props Props) *Element {
10+
version := As[string](props, `version`)
11+
return Span(Props{
12+
`id`: `banner-title`,
13+
},
14+
`playground `,
15+
Span(Props{
16+
`id`: `banner-title-sub`,
17+
},
18+
`GopherJS (`, version, `)`,
19+
),
20+
)
21+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package react
2+
3+
import "github.com/gopherjs/gopherjs/js"
4+
5+
func FormatImportsControl(fmtImports bool, setFmtImports func(any)) *Element {
6+
return CreateElement(formatImportsComponent, Props{
7+
`fmtImports`: fmtImports,
8+
`setFmtImports`: setFmtImports,
9+
})
10+
}
11+
12+
func formatImportsComponent(props Props) *Element {
13+
fmtImports := As[bool](props, `fmtImports`)
14+
setFmtImports := AsSetter(props, `setFmtImports`)
15+
16+
onChange := func(e *js.Object) {
17+
setFmtImports(e.Get(`target`).Get(`checked`).Bool())
18+
}
19+
20+
return CreateElement(`label`, Props{
21+
`id`: `format-imports-label`,
22+
`title`: `Rewrite imports on Format`,
23+
},
24+
CreateElement(`input`, Props{
25+
`id`: `format-imports`,
26+
`type`: `checkbox`,
27+
`checked`: fmtImports,
28+
`onChange`: onChange,
29+
}),
30+
`Imports`,
31+
)
32+
}

playground/internal/react/playground.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,43 @@ func Playground() *Element {
66

77
func playgroundComponent() *Element {
88
code, setCode := UseState(``)
9-
output, _ := UseState([]any{})
9+
shareUrl, setShareUrl := UseState(``)
10+
output, setOutput := UseState([]any{})
11+
fmtImports, setFmtImports := UseState(true)
12+
13+
_ = setShareUrl
14+
_ = setOutput
15+
16+
version := `vx.x.x`
17+
18+
onRunClick := func() {
19+
print("Run clicked") // TODO(grantnelson-wf): Implement
20+
}
21+
22+
onFormatClick := func() {
23+
print("Format clicked", fmtImports) // TODO(grantnelson-wf): Implement
24+
}
25+
26+
onShareClick := func() {
27+
print("Share clicked") // TODO(grantnelson-wf): Implement
28+
}
1029

1130
return Div(Props{
1231
`id`: `playground`,
1332
},
33+
Div(Props{
34+
`id`: `banner`,
35+
},
36+
BannerTitle(version),
37+
Span(Props{
38+
`id`: `controls`,
39+
},
40+
Button(`run-button`, `Run`, nil, onRunClick),
41+
Button(`format-button`, `Format`, nil, onFormatClick),
42+
FormatImportsControl(fmtImports, setFmtImports),
43+
ShareUrlControl(shareUrl, onShareClick),
44+
),
45+
),
1446
CodeBox(code, setCode),
1547
OutputBox(output),
1648
)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package react
2+
3+
import "github.com/gopherjs/gopherjs/js"
4+
5+
func ShareUrlControl(shareUrl string, onShare func()) *Element {
6+
return CreateElement(shareUrlComponent, Props{
7+
`shareUrl`: shareUrl,
8+
`onShare`: onShare,
9+
})
10+
}
11+
12+
func shareUrlComponent(props Props) *Element {
13+
shareUrl := As[string](props, `shareUrl`)
14+
onShare := AsFunc(props, `onShare`)
15+
shareUrlRef := UseRef()
16+
17+
UseEffect(func() {
18+
if len(shareUrl) > 0 {
19+
shareUrlRef.Call(`focus`)
20+
}
21+
}, []any{shareUrl, shareUrlRef})
22+
23+
onShareUrlFocus := func(e *js.Object) {
24+
e.Get(`target`).Call(`select`)
25+
}
26+
27+
onShareClick := func() {
28+
onShare()
29+
}
30+
31+
className := `share-url-hidden`
32+
if len(shareUrl) > 0 {
33+
className = `share-url-show`
34+
}
35+
36+
return Fragment(
37+
Button(`share-button`, `Share`, nil, onShareClick),
38+
CreateElement(`input`, Props{
39+
`id`: `share-url`,
40+
`type`: `text`,
41+
`className`: className,
42+
`ref`: shareUrlRef,
43+
`value`: shareUrl,
44+
`readOnly`: true,
45+
`onFocus`: onShareUrlFocus,
46+
}),
47+
)
48+
}

0 commit comments

Comments
 (0)