1
0
gomponents/README.md

147 lines
3.9 KiB
Markdown
Raw Normal View History

# gomponents
2020-09-14 01:51:29 -07:00
[![GoDoc](https://godoc.org/github.com/maragudk/gomponents?status.svg)](https://godoc.org/github.com/maragudk/gomponents)
2020-09-18 05:05:53 -07:00
[![codecov](https://codecov.io/gh/maragudk/gomponents/branch/master/graph/badge.svg)](https://codecov.io/gh/maragudk/gomponents)
2020-09-14 01:51:29 -07:00
gomponents are declarative view components in Go, that can render to HTML5.
gomponents aims to make it easy to build HTML5 pages of reusable components,
without the use of a template language. Think server-side-rendered React,
but without the virtual DOM and diffing.
The implementation is very usable, but the API may change until version 1 is reached.
2020-09-21 07:30:29 -07:00
Check out the blog post [gomponents: declarative view components in Go](https://www.maragu.dk/blog/gomponents-declarative-view-components-in-go/)
for background.
## Features
- Build reusable view components
- Write declarative HTML5 in Go without all the strings, so you get
- Type safety
- Auto-completion
- Nice formatting with `gofmt`
- Simple API that's easy to learn and use (you know most already if you know HTML)
- No external dependencies
2020-09-21 07:30:29 -07:00
## Usage
Get the library using `go get`:
```shell script
go get -u github.com/maragudk/gomponents
```
The preferred way to use gomponents is with so-called dot-imports (note the dot before the `gomponents/html` import),
to give you that smooth, native HTML feel:
2020-09-21 07:30:29 -07:00
```go
2020-09-23 13:05:59 -07:00
package main
2020-09-21 07:30:29 -07:00
import (
2020-09-23 13:05:59 -07:00
"net/http"
2020-09-21 07:30:29 -07:00
2020-09-23 13:05:59 -07:00
g "github.com/maragudk/gomponents"
c "github.com/maragudk/gomponents/components"
. "github.com/maragudk/gomponents/html"
2020-09-21 07:30:29 -07:00
)
func main() {
_ = http.ListenAndServe("localhost:8080", http.HandlerFunc(handler))
2020-09-21 07:30:29 -07:00
}
func handler(w http.ResponseWriter, r *http.Request) {
_ = Page("Hi!", r.URL.Path).Render(w)
2020-09-21 07:30:29 -07:00
}
2020-09-23 13:05:59 -07:00
func Page(title, currentPath string) g.Node {
2020-12-10 04:13:10 -08:00
return Doctype(
HTML(
Lang("en"),
Head(
TitleEl(title),
StyleEl(Type("text/css"), g.Raw(".is-active{ font-weight: bold }")),
2020-09-23 13:05:59 -07:00
),
Body(
Navbar(currentPath),
H1(title),
P(g.Textf("Welcome to the page at %v.", currentPath)),
2020-09-23 13:05:59 -07:00
),
),
)
}
func Navbar(currentPath string) g.Node {
return Nav(
NavbarLink("/", "Home", currentPath),
NavbarLink("/about", "About", currentPath),
2020-09-23 13:05:59 -07:00
)
}
func NavbarLink(href, name, currentPath string) g.Node {
return A(Href(href), c.Classes{"is-active": currentPath == href}, g.Text(name))
}
2020-09-21 07:30:29 -07:00
```
Some people don't like dot-imports, and luckily it's completely optional.
If you don't like dot-imports, just use regular imports.
You could also use the provided HTML5 document template to simplify your code a bit:
2020-10-29 04:03:43 -07:00
```go
package main
import (
"net/http"
g "github.com/maragudk/gomponents"
2020-12-10 04:13:10 -08:00
c "github.com/maragudk/gomponents/components"
. "github.com/maragudk/gomponents/html"
2020-10-29 04:03:43 -07:00
)
func main() {
_ = http.ListenAndServe("localhost:8080", http.HandlerFunc(handler))
2020-10-29 04:03:43 -07:00
}
func handler(w http.ResponseWriter, r *http.Request) {
_ = Page("Hi!", r.URL.Path).Render(w)
2020-10-29 04:03:43 -07:00
}
func Page(title, currentPath string) g.Node {
2020-12-10 04:13:10 -08:00
return c.HTML5(c.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)),
2020-10-29 04:03:43 -07:00
},
})
}
func Navbar(currentPath string) g.Node {
return Nav(
NavbarLink("/", "Home", currentPath),
NavbarLink("/about", "About", currentPath),
2020-10-29 04:03:43 -07:00
)
}
func NavbarLink(href, name, currentPath string) g.Node {
return A(Href(href), c.Classes{"is-active": currentPath == href}, g.Text(name))
}
2020-10-29 04:03:43 -07:00
```
2020-09-21 07:30:29 -07:00
For more complete examples, see [the examples directory](examples/).
### What's up with the specially named elements and attributes?
Unfortunately, there are four main name clashes in HTML elements and attributes, so they need an `El` or `Attr` suffix,
respectively, to be able to co-exist in the same package in Go:
- `data` (`DataEl`/`DataAttr`)
- `form` (`FormEl`/`FormAttr`)
- `style` (`StyleEl`/`StyleAttr`)
- `title` (`TitleEl`/`TitleAttr`)