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.
1 file changed, 25 insertions(+), 18 deletions(-)
changed files
M examples/dot-import/dot-import.go → examples/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)) }