all repos — gomponents @ a76262652b227c95ce140f3698c46f59b79354ac

HTML components in pure Go

Move elements and attributes into html package (#52) This makes it easier to use dot-imports. Also updated the readme and examples with new usage, and move the `Classes` helper into the `components` package.

Markus Wüstenberg
commit

a76262652b227c95ce140f3698c46f59b79354ac

parent

a7e24c6cddaafb98091c3989c9da7779eeba30b5

1 file changed, 25 insertions(+), 18 deletions(-)

changed files
M examples/dot-import/dot-import.goexamples/dot-import/dot-import.go
@@ -3,9 +3,9 @@
import ( "net/http" - . "github.com/maragudk/gomponents" + g "github.com/maragudk/gomponents" . "github.com/maragudk/gomponents/components" - . "github.com/maragudk/gomponents/el" + . "github.com/maragudk/gomponents/html" ) func main() {
@@ -13,25 +13,32 @@ _ = http.ListenAndServe("localhost:8080", http.HandlerFunc(handler))
} func handler(w http.ResponseWriter, r *http.Request) { - p := page(props{ - title: r.URL.Path, - path: r.URL.Path, + 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)), + }, }) - _ = p.Render(w) } -type props struct { - title string - path string +func Navbar(currentPath string) g.Node { + return Nav( + NavbarLink("/", "Home", currentPath), + NavbarLink("/about", "About", currentPath), + ) } -func page(p props) Node { - return HTML5(DocumentProps{ - Title: p.title, - Language: "en", - Body: []Node{ - H1(p.title), - P(Textf("Welcome to the page at %v.", p.path)), - }, - }) +func NavbarLink(href, name, currentPath string) g.Node { + return A(href, Classes{"is-active": currentPath == href}, g.Text(name)) }