about summary refs log tree commit diff stats
path: root/internal/examples/app
diff options
context:
space:
mode:
Diffstat (limited to 'internal/examples/app')
-rw-r--r--internal/examples/app/cmd/app/main.go13
-rw-r--r--internal/examples/app/go.mod5
-rw-r--r--internal/examples/app/go.sum2
-rw-r--r--internal/examples/app/html/about.go25
-rw-r--r--internal/examples/app/html/components.go62
-rw-r--r--internal/examples/app/html/home.go20
-rw-r--r--internal/examples/app/http/pages.go24
-rw-r--r--internal/examples/app/http/server.go18
8 files changed, 169 insertions, 0 deletions
diff --git a/internal/examples/app/cmd/app/main.go b/internal/examples/app/cmd/app/main.go
new file mode 100644
index 0000000..0c01576
--- /dev/null
+++ b/internal/examples/app/cmd/app/main.go
@@ -0,0 +1,13 @@
+package main
+
+import (
+	"log/slog"
+
+	"app/http"
+)
+
+func main() {
+	if err := http.Start(); err != nil {
+		slog.Info("Error", 1, "error", err)
+	}
+}
diff --git a/internal/examples/app/go.mod b/internal/examples/app/go.mod
new file mode 100644
index 0000000..a86bad6
--- /dev/null
+++ b/internal/examples/app/go.mod
@@ -0,0 +1,5 @@
+module app
+
+go 1.23.1
+
+require github.com/maragudk/gomponents v0.20.5
diff --git a/internal/examples/app/go.sum b/internal/examples/app/go.sum
new file mode 100644
index 0000000..37a89f1
--- /dev/null
+++ b/internal/examples/app/go.sum
@@ -0,0 +1,2 @@
+github.com/maragudk/gomponents v0.20.5 h1:Z8phdQIW+NqAGO0clMh/wyv1/M7viUBf7/EL44gvwtI=
+github.com/maragudk/gomponents v0.20.5/go.mod h1:nHkNnZL6ODgMBeJhrZjkMHVvNdoYsfmpKB2/hjdQ0Hg=
diff --git a/internal/examples/app/html/about.go b/internal/examples/app/html/about.go
new file mode 100644
index 0000000..37b797f
--- /dev/null
+++ b/internal/examples/app/html/about.go
@@ -0,0 +1,25 @@
+package html
+
+import (
+	"time"
+
+	. "github.com/maragudk/gomponents"
+	. "github.com/maragudk/gomponents/html"
+)
+
+func AboutPage() Node {
+	now := time.Now()
+
+	return page("About",
+		H1(Text("About")),
+
+		P(Textf("Built with gomponents and rendered at %v.", now.Format(time.TimeOnly))),
+
+		P(
+			If(now.Second()%2 == 0, Text("It's an even second!")),
+			If(now.Second()%2 != 0, Text("It's an odd second!")),
+		),
+
+		Img(Class("max-w-sm"), Src("https://www.gomponents.com/images/logo.png"), Alt("gomponents logo")),
+	)
+}
diff --git a/internal/examples/app/html/components.go b/internal/examples/app/html/components.go
new file mode 100644
index 0000000..25880a3
--- /dev/null
+++ b/internal/examples/app/html/components.go
@@ -0,0 +1,62 @@
+package html
+
+import (
+	. "github.com/maragudk/gomponents"
+	. "github.com/maragudk/gomponents/components"
+	. "github.com/maragudk/gomponents/html"
+)
+
+func page(title string, children ...Node) Node {
+	return HTML5(HTML5Props{
+		Title:    title,
+		Language: "en",
+		Head: []Node{
+			Script(Src("https://cdn.tailwindcss.com?plugins=typography")),
+		},
+		Body: []Node{Class("bg-gradient-to-b from-white to-indigo-100 bg-no-repeat"),
+			Div(Class("min-h-screen flex flex-col justify-between"),
+				header(),
+				Div(Class("grow"),
+					container(true,
+						Div(Class("prose prose-lg prose-indigo"),
+							Group(children),
+						),
+					),
+				),
+				footer(),
+			),
+		},
+	})
+}
+
+func header() Node {
+	return Div(Class("bg-indigo-600 text-white shadow"),
+		container(false,
+			Div(Class("flex items-center space-x-4 h-8"),
+				headerLink("/", "Home"),
+				headerLink("/about", "About"),
+			),
+		),
+	)
+}
+
+func headerLink(href, text string) Node {
+	return A(Class("hover:text-indigo-300"), Href(href), Text(text))
+}
+
+func container(padY bool, children ...Node) Node {
+	return Div(
+		Classes{
+			"max-w-7xl mx-auto":     true,
+			"px-4 md:px-8 lg:px-16": true,
+			"py-4 md:py-8":          padY,
+		},
+		Group(children),
+	)
+}
+
+func footer() Node {
+	return Div(Class("bg-gray-900 text-white shadow text-center h-16 flex items-center justify-center"),
+		A(Href("https://www.gomponents.com"), Text("gomponents")),
+	)
+}
diff --git a/internal/examples/app/html/home.go b/internal/examples/app/html/home.go
new file mode 100644
index 0000000..d4e192c
--- /dev/null
+++ b/internal/examples/app/html/home.go
@@ -0,0 +1,20 @@
+package html
+
+import (
+	. "github.com/maragudk/gomponents"
+	. "github.com/maragudk/gomponents/html"
+)
+
+func HomePage(items []string) Node {
+	return page("Home",
+		H1(Text("Home")),
+
+		P(Text("This is a gomponents example app!")),
+
+		P(Raw(`Have a look at the <a href="https://github.com/maragudk/gomponents/tree/main/internal/examples/app">source code</a> to see how it’s structured.`)),
+
+		Ul(Group(Map(items, func(s string) Node {
+			return Li(Text(s))
+		}))),
+	)
+}
diff --git a/internal/examples/app/http/pages.go b/internal/examples/app/http/pages.go
new file mode 100644
index 0000000..dd686c0
--- /dev/null
+++ b/internal/examples/app/http/pages.go
@@ -0,0 +1,24 @@
+package http
+
+import (
+	"net/http"
+
+	g "github.com/maragudk/gomponents"
+	ghttp "github.com/maragudk/gomponents/http"
+
+	"app/html"
+)
+
+func Home(mux *http.ServeMux) {
+	mux.Handle("GET /", ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (g.Node, error) {
+		// Let's pretend this comes from a db or something.
+		items := []string{"Super", "Duper", "Nice"}
+		return html.HomePage(items), nil
+	}))
+}
+
+func About(mux *http.ServeMux) {
+	mux.Handle("GET /about", ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (g.Node, error) {
+		return html.AboutPage(), nil
+	}))
+}
diff --git a/internal/examples/app/http/server.go b/internal/examples/app/http/server.go
new file mode 100644
index 0000000..8264fcf
--- /dev/null
+++ b/internal/examples/app/http/server.go
@@ -0,0 +1,18 @@
+package http
+
+import (
+	"net/http"
+)
+
+func Start() error {
+	return http.ListenAndServe(":8080", setupRoutes())
+}
+
+func setupRoutes() http.Handler {
+	mux := http.NewServeMux()
+
+	Home(mux)
+	About(mux)
+
+	return mux
+}