about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorMarkus Wüstenberg2020-12-10 14:27:10 +0100
committerGitHub2020-12-10 14:27:10 +0100
commit428a2519eaa8e16767f86b417abd5af007f24fc6 (patch)
tree7458b63507259b0225c0e41d249aeb7b36ebeff8
parentfebffb600db7fbfd81827ded1b8fa3774bf42107 (diff)
downloadgomponents-428a2519eaa8e16767f86b417abd5af007f24fc6.tar.lz
gomponents-428a2519eaa8e16767f86b417abd5af007f24fc6.tar.zst
gomponents-428a2519eaa8e16767f86b417abd5af007f24fc6.zip
Change main example to be the dot-import version (#56)
-rw-r--r--examples/dot-import/dot-import.go44
-rw-r--r--examples/simple/simple.go85
2 files changed, 43 insertions, 86 deletions
diff --git a/examples/dot-import/dot-import.go b/examples/dot-import/dot-import.go
deleted file mode 100644
index d735785..0000000
--- a/examples/dot-import/dot-import.go
+++ /dev/null
@@ -1,44 +0,0 @@
-package main
-
-import (
-	"net/http"
-
-	g "github.com/maragudk/gomponents"
-	. "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 := Page("Hi!", r.URL.Path)
-	_ = page.Render(w)
-}
-
-func Page(title, currentPath string) g.Node {
-	return HTML5(HTML5Props{
-		Title:    title,
-		Language: "en",
-		Head: []g.Node{
-			StyleEl(Type("text/css"), g.Raw(".is-active{ font-weight: bold }")),
-		},
-		Body: []g.Node{
-			Navbar(currentPath),
-			H1(title),
-			P(g.Textf("Welcome to the page at %v.", currentPath)),
-		},
-	})
-}
-
-func Navbar(currentPath string) g.Node {
-	return Nav(
-		NavbarLink("/", "Home", currentPath),
-		NavbarLink("/about", "About", currentPath),
-	)
-}
-
-func NavbarLink(href, name, currentPath string) g.Node {
-	return A(Href(href), Classes{"is-active": currentPath == href}, g.Text(name))
-}
diff --git a/examples/simple/simple.go b/examples/simple/simple.go
index c872512..a6877f5 100644
--- a/examples/simple/simple.go
+++ b/examples/simple/simple.go
@@ -2,11 +2,10 @@ package main
 
 import (
 	"net/http"
-	"time"
 
 	g "github.com/maragudk/gomponents"
 	c "github.com/maragudk/gomponents/components"
-	h "github.com/maragudk/gomponents/html"
+	. "github.com/maragudk/gomponents/html"
 )
 
 func main() {
@@ -14,11 +13,10 @@ func main() {
 }
 
 func handler(w http.ResponseWriter, r *http.Request) {
-	p := page(props{
+	_ = Page(props{
 		title: r.URL.Path,
 		path:  r.URL.Path,
-	})
-	_ = p.Render(w)
+	}).Render(w)
 }
 
 type props struct {
@@ -26,46 +24,49 @@ type props struct {
 	path  string
 }
 
-func page(p props) g.Node {
-	return h.Doctype(
-		h.HTML(h.Lang("en"),
-			h.Head(
-				h.TitleEl(p.title),
-				h.StyleEl(h.Type("text/css"),
-					g.Raw(".is-active{font-weight: bold}"),
-					g.Raw("ul.nav { list-style-type: none; margin: 0; padding: 0; overflow: hidden; }"),
-					g.Raw("ul.nav li { display: block;  padding: 8px; float: left; }"),
-				),
-			),
-			h.Body(
-				navbar(navbarProps{path: p.path}),
-				h.Hr(),
-				h.H1(p.title),
-				h.P(g.Textf("Welcome to the page at %v.", p.path)),
-				h.P(g.Textf("Rendered at %v", time.Now())),
+// 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(p.title),
+			P(g.Textf("Welcome to the page at %v.", p.path)),
+		},
+	})
 }
 
-type navbarProps struct {
-	path string
+type PageLink struct {
+	Path string
+	Name string
 }
 
-func navbar(props navbarProps) g.Node {
-	items := []struct {
-		path string
-		text string
-	}{
-		{"/", "Home"},
-		{"/foo", "Foo"},
-		{"/bar", "Bar"},
-	}
-	lis := g.Map(len(items), func(i int) g.Node {
-		item := items[i]
-		return h.Li(
-			h.A(h.Href(item.path), c.Classes(map[string]bool{"is-active": props.path == item.path}), g.Text(item.text)),
-		)
-	})
-	return h.Ul(h.Class("nav"), g.Group(lis))
+func Navbar(currentPath string, links []PageLink) g.Node {
+	return Div(
+		Ul(
+			NavbarLink("/", "Home", currentPath),
+
+			g.Group(g.Map(len(links), func(i int) g.Node {
+				return NavbarLink(links[i].Path, links[i].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)))
 }