Skip to content

Commit

Permalink
Add example application (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
markuswustenberg committed Feb 27, 2023
2 parents 4752527 + da517a0 commit 66db7e0
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
83 changes: 83 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,87 @@

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

## Usage

```shell
go get github.com/maragudk/gomponents-htmx
```

Example app:

```go
package main

import (
"errors"
"log"
"net/http"
"time"

g "github.com/maragudk/gomponents"
c "github.com/maragudk/gomponents/components"
. "github.com/maragudk/gomponents/html"
ghttp "github.com/maragudk/gomponents/http"
hx "github.com/maragudk/gomponents-htmx"
hxhttp "github.com/maragudk/gomponents-htmx/http"
)

func main() {
if err := start(); err != nil {
log.Fatalln("Error:", err)
}
}

func start() error {
now := time.Now()
mux := http.NewServeMux()
mux.HandleFunc("/", ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (g.Node, error) {
if r.Method == http.MethodPost && hxhttp.IsBoosted(r.Header) {
now = time.Now()

hxhttp.SetPushURL(w.Header(), "/?time="+now.Format(timeFormat))

return partial(now), nil
}
return page(now), nil
}))

log.Println("Starting on http://localhost:8080")
if err := http.ListenAndServe("localhost:8080", mux); err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
}

const timeFormat = "15:04:05"

func page(now time.Time) g.Node {
return c.HTML5(c.HTML5Props{
Title: now.Format(timeFormat),
Head: []g.Node{
Script(Src("https://cdn.tailwindcss.com?plugins=forms,typography")),
Script(Src("https://unpkg.com/htmx.org")),
},
Body: []g.Node{
Div(Class("max-w-7xl mx-auto p-4 prose lg:prose-lg xl:prose-xl"),
H1(g.Text(`gomponents + HTMX`)),
P(g.Textf(`Time at last full page refresh was %v.`, now.Format(timeFormat))),
partial(now),
FormEl(Method("post"), Action("/"), hx.Boost("true"), hx.Target("#partial"), hx.Swap("outerHTML"),
Button(Type("submit"), g.Text(`Update time`),
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"),
),
),
),
},
})
}

func partial(now time.Time) g.Node {
return P(ID("partial"), g.Textf(`Time was last updated at %v.`, now.Format(timeFormat)))
}

```


Made in 🇩🇰 by [maragu](https://www.maragu.dk/), maker of [online Go courses](https://www.golang.dk/).
71 changes: 71 additions & 0 deletions cmd/example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"errors"
"log"
"net/http"
"time"

g "github.com/maragudk/gomponents"
c "github.com/maragudk/gomponents/components"
. "github.com/maragudk/gomponents/html"
ghttp "github.com/maragudk/gomponents/http"

hx "github.com/maragudk/gomponents-htmx"
hxhttp "github.com/maragudk/gomponents-htmx/http"
)

func main() {
if err := start(); err != nil {
log.Fatalln("Error:", err)
}
}

func start() error {
now := time.Now()
mux := http.NewServeMux()
mux.HandleFunc("/", ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (g.Node, error) {
if r.Method == http.MethodPost && hxhttp.IsBoosted(r.Header) {
now = time.Now()

hxhttp.SetPushURL(w.Header(), "/?time="+now.Format(timeFormat))

return partial(now), nil
}
return page(now), nil
}))

log.Println("Starting on http://localhost:8080")
if err := http.ListenAndServe("localhost:8080", mux); err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
}

const timeFormat = "15:04:05"

func page(now time.Time) g.Node {
return c.HTML5(c.HTML5Props{
Title: now.Format(timeFormat),
Head: []g.Node{
Script(Src("https://cdn.tailwindcss.com?plugins=forms,typography")),
Script(Src("https://unpkg.com/htmx.org")),
},
Body: []g.Node{
Div(Class("max-w-7xl mx-auto p-4 prose lg:prose-lg xl:prose-xl"),
H1(g.Text(`gomponents + HTMX`)),
P(g.Textf(`Time at last full page refresh was %v.`, now.Format(timeFormat))),
partial(now),
FormEl(Method("post"), Action("/"), hx.Boost("true"), hx.Target("#partial"), hx.Swap("outerHTML"),
Button(Type("submit"), g.Text(`Update time`),
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"),
),
),
),
},
})
}

func partial(now time.Time) g.Node {
return P(ID("partial"), g.Textf(`Time was last updated at %v.`, now.Format(timeFormat)))
}
6 changes: 6 additions & 0 deletions http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,9 @@ func ExampleIsBoosted() {
}
})
}

func ExampleSetRefresh() {
_ = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hxhttp.SetRefresh(w.Header())
})
}

0 comments on commit 66db7e0

Please sign in to comment.