|
5 | 5 |
|
6 | 6 | [HTMX](https://htmx.org) attributes and helpers for [gomponents](https://www.gomponents.com).
|
7 | 7 |
|
| 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 | + |
8 | 91 | Made in 🇩🇰 by [maragu](https://www.maragu.dk/), maker of [online Go courses](https://www.golang.dk/).
|
0 commit comments