examples/simple/simple.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | //go:build go1.18
// +build go1.18
package main
import (
"net/http"
g "github.com/maragudk/gomponents"
c "github.com/maragudk/gomponents/components"
. "github.com/maragudk/gomponents/html"
)
func main() {
_ = http.ListenAndServe("localhost:8080", http.HandlerFunc(handler))
}
func handler(w http.ResponseWriter, r *http.Request) {
_ = Page(props{
title: r.URL.Path,
path: r.URL.Path,
}).Render(w)
}
type props struct {
title string
path string
}
// Page is a whole document to output.
func Page(p props) g.Node {
return c.HTML5(c.HTML5Props{
Title: p.title,
Language: "en",
Head: []g.Node{
StyleEl(Type("text/css"),
g.Raw("html { font-family: sans-serif; }"),
g.Raw("ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; }"),
g.Raw("ul li { display: block; padding: 8px; float: left; }"),
g.Raw(".is-active { font-weight: bold; }"),
),
},
Body: []g.Node{
Navbar(p.path, []PageLink{
{Path: "/foo", Name: "Foo"},
{Path: "/bar", Name: "Bar"},
}),
H1(g.Text(p.title)),
P(g.Textf("Welcome to the page at %v.", p.path)),
},
})
}
type PageLink struct {
Path string
Name string
}
func Navbar(currentPath string, links []PageLink) g.Node {
return Div(
Ul(
NavbarLink("/", "Home", currentPath),
g.Group(g.Map(links, func(pl PageLink) g.Node {
return NavbarLink(pl.Path, pl.Name, currentPath)
})),
),
Hr(),
)
}
func NavbarLink(href, name, currentPath string) g.Node {
return Li(A(Href(href), c.Classes{"is-active": currentPath == href}, g.Text(name)))
}
|