Skip to content

Commit 66db7e0

Browse files
Add example application (#8)
2 parents 4752527 + da517a0 commit 66db7e0

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

README.md

+83
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,87 @@
55

66
[HTMX](https://htmx.org) attributes and helpers for [gomponents](https://www.gomponents.com).
77

8+
## Usage
9+
10+
```shell
11+
go get github.com/maragudk/gomponents-htmx
12+
```
13+
14+
Example app:
15+
16+
```go
17+
package main
18+
19+
import (
20+
"errors"
21+
"log"
22+
"net/http"
23+
"time"
24+
25+
g "github.com/maragudk/gomponents"
26+
c "github.com/maragudk/gomponents/components"
27+
. "github.com/maragudk/gomponents/html"
28+
ghttp "github.com/maragudk/gomponents/http"
29+
hx "github.com/maragudk/gomponents-htmx"
30+
hxhttp "github.com/maragudk/gomponents-htmx/http"
31+
)
32+
33+
func main() {
34+
if err := start(); err != nil {
35+
log.Fatalln("Error:", err)
36+
}
37+
}
38+
39+
func start() error {
40+
now := time.Now()
41+
mux := http.NewServeMux()
42+
mux.HandleFunc("/", ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (g.Node, error) {
43+
if r.Method == http.MethodPost && hxhttp.IsBoosted(r.Header) {
44+
now = time.Now()
45+
46+
hxhttp.SetPushURL(w.Header(), "/?time="+now.Format(timeFormat))
47+
48+
return partial(now), nil
49+
}
50+
return page(now), nil
51+
}))
52+
53+
log.Println("Starting on http://localhost:8080")
54+
if err := http.ListenAndServe("localhost:8080", mux); err != nil && !errors.Is(err, http.ErrServerClosed) {
55+
return err
56+
}
57+
return nil
58+
}
59+
60+
const timeFormat = "15:04:05"
61+
62+
func page(now time.Time) g.Node {
63+
return c.HTML5(c.HTML5Props{
64+
Title: now.Format(timeFormat),
65+
Head: []g.Node{
66+
Script(Src("https://cdn.tailwindcss.com?plugins=forms,typography")),
67+
Script(Src("https://unpkg.com/htmx.org")),
68+
},
69+
Body: []g.Node{
70+
Div(Class("max-w-7xl mx-auto p-4 prose lg:prose-lg xl:prose-xl"),
71+
H1(g.Text(`gomponents + HTMX`)),
72+
P(g.Textf(`Time at last full page refresh was %v.`, now.Format(timeFormat))),
73+
partial(now),
74+
FormEl(Method("post"), Action("/"), hx.Boost("true"), hx.Target("#partial"), hx.Swap("outerHTML"),
75+
Button(Type("submit"), g.Text(`Update time`),
76+
Class("rounded-md border border-transparent bg-orange-600 px-4 py-2 text-sm font-medium text-white shadow-sm hover:bg-orange-700 focus:outline-none focus:ring-2 focus:ring-orange-500 focus:ring-offset-2"),
77+
),
78+
),
79+
),
80+
},
81+
})
82+
}
83+
84+
func partial(now time.Time) g.Node {
85+
return P(ID("partial"), g.Textf(`Time was last updated at %v.`, now.Format(timeFormat)))
86+
}
87+
88+
```
89+
90+
891
Made in 🇩🇰 by [maragu](https://www.maragu.dk/), maker of [online Go courses](https://www.golang.dk/).

cmd/example/main.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"log"
6+
"net/http"
7+
"time"
8+
9+
g "github.com/maragudk/gomponents"
10+
c "github.com/maragudk/gomponents/components"
11+
. "github.com/maragudk/gomponents/html"
12+
ghttp "github.com/maragudk/gomponents/http"
13+
14+
hx "github.com/maragudk/gomponents-htmx"
15+
hxhttp "github.com/maragudk/gomponents-htmx/http"
16+
)
17+
18+
func main() {
19+
if err := start(); err != nil {
20+
log.Fatalln("Error:", err)
21+
}
22+
}
23+
24+
func start() error {
25+
now := time.Now()
26+
mux := http.NewServeMux()
27+
mux.HandleFunc("/", ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (g.Node, error) {
28+
if r.Method == http.MethodPost && hxhttp.IsBoosted(r.Header) {
29+
now = time.Now()
30+
31+
hxhttp.SetPushURL(w.Header(), "/?time="+now.Format(timeFormat))
32+
33+
return partial(now), nil
34+
}
35+
return page(now), nil
36+
}))
37+
38+
log.Println("Starting on http://localhost:8080")
39+
if err := http.ListenAndServe("localhost:8080", mux); err != nil && !errors.Is(err, http.ErrServerClosed) {
40+
return err
41+
}
42+
return nil
43+
}
44+
45+
const timeFormat = "15:04:05"
46+
47+
func page(now time.Time) g.Node {
48+
return c.HTML5(c.HTML5Props{
49+
Title: now.Format(timeFormat),
50+
Head: []g.Node{
51+
Script(Src("https://cdn.tailwindcss.com?plugins=forms,typography")),
52+
Script(Src("https://unpkg.com/htmx.org")),
53+
},
54+
Body: []g.Node{
55+
Div(Class("max-w-7xl mx-auto p-4 prose lg:prose-lg xl:prose-xl"),
56+
H1(g.Text(`gomponents + HTMX`)),
57+
P(g.Textf(`Time at last full page refresh was %v.`, now.Format(timeFormat))),
58+
partial(now),
59+
FormEl(Method("post"), Action("/"), hx.Boost("true"), hx.Target("#partial"), hx.Swap("outerHTML"),
60+
Button(Type("submit"), g.Text(`Update time`),
61+
Class("rounded-md border border-transparent bg-orange-600 px-4 py-2 text-sm font-medium text-white shadow-sm hover:bg-orange-700 focus:outline-none focus:ring-2 focus:ring-orange-500 focus:ring-offset-2"),
62+
),
63+
),
64+
),
65+
},
66+
})
67+
}
68+
69+
func partial(now time.Time) g.Node {
70+
return P(ID("partial"), g.Textf(`Time was last updated at %v.`, now.Format(timeFormat)))
71+
}

http/http_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,9 @@ func ExampleIsBoosted() {
8888
}
8989
})
9090
}
91+
92+
func ExampleSetRefresh() {
93+
_ = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
94+
hxhttp.SetRefresh(w.Header())
95+
})
96+
}

0 commit comments

Comments
 (0)