Skip to content

Commit

Permalink
Move to maragu.dev/gomponents-htmx namespace (#16)
Browse files Browse the repository at this point in the history
Also upgrade to maragu.dev/[email protected]
  • Loading branch information
markuswustenberg authored Oct 9, 2024
1 parent 15d81a4 commit b85d5b8
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 57 deletions.
56 changes: 30 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<img src="logo.png" alt="Logo" width="300" align="right">

[![GoDoc](https://pkg.go.dev/badge/github.com/maragudk/gomponents-htmx)](https://pkg.go.dev/github.com/maragudk/gomponents-htmx)
[![GoDoc](https://pkg.go.dev/badge/maragu.dev/gomponents-htmx)](https://pkg.go.dev/maragu.dev/gomponents-htmx)
[![CI](https://github.com/maragudk/gomponents-htmx/actions/workflows/ci.yml/badge.svg)](https://github.com/maragudk/gomponents-htmx/actions/workflows/ci.yml)

[HTMX](https://htmx.org) attributes and helpers for [gomponents](https://www.gomponents.com).
Expand All @@ -14,7 +14,7 @@ Does your company depend on this project? [Contact me at [email protected]](mailt
## Usage

```shell
go get github.com/maragudk/gomponents-htmx
go get maragu.dev/gomponents-htmx@v1.0.0-beta1
```

```go
Expand All @@ -26,12 +26,13 @@ import (
"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"
. "maragu.dev/gomponents"
. "maragu.dev/gomponents/components"
. "maragu.dev/gomponents/html"
. "maragu.dev/gomponents/http"

hx "maragu.dev/gomponents-htmx"
hxhttp "maragu.dev/gomponents-htmx/http"
)

func main() {
Expand All @@ -43,11 +44,11 @@ func main() {
func start() error {
now := time.Now()
mux := http.NewServeMux()
mux.HandleFunc("/", ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (g.Node, error) {
mux.HandleFunc("/", Adapt(func(w http.ResponseWriter, r *http.Request) (Node, error) {
if r.Method == http.MethodPost && hxhttp.IsBoosted(r.Header) {
now = time.Now()

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

return partial(now), nil
}
Expand All @@ -61,22 +62,29 @@ func start() error {
return nil
}

const timeFormat = "15:04:05"
const timeOnly = "15:04:05"

func page(now time.Time) Node {
return HTML5(HTML5Props{
Title: now.Format(timeOnly),

func page(now time.Time) g.Node {
return c.HTML5(c.HTML5Props{
Title: now.Format(timeFormat),
Head: []g.Node{
Head: []Node{
Script(Src("https://cdn.tailwindcss.com?plugins=forms,typography")),
Script(Src("https://unpkg.com/htmx.org")),
},
Body: []g.Node{

Body: []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))),
H1(Text(`gomponents + HTMX`)),

P(Textf(`Time at last full page refresh was %v.`, now.Format(timeOnly))),

partial(now),
FormEl(Method("post"), Action("/"), hx.Boost("true"), hx.Target("#partial"), hx.Swap("outerHTML"),
Button(Type("submit"), g.Text(`Update time`),

Form(Method("post"), Action("/"),
hx.Boost("true"), hx.Target("#partial"), hx.Swap("outerHTML"),

Button(Type("submit"), 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"),
),
),
Expand All @@ -85,11 +93,7 @@ func page(now time.Time) g.Node {
})
}

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

```


Made in 🇩🇰 by [maragu](https://www.maragu.dk/), maker of [online Go courses](https://www.golang.dk/).
47 changes: 27 additions & 20 deletions cmd/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"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"
. "maragu.dev/gomponents"
. "maragu.dev/gomponents/components"
. "maragu.dev/gomponents/html"
. "maragu.dev/gomponents/http"

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

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

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

return partial(now), nil
}
Expand All @@ -42,22 +42,29 @@ func start() error {
return nil
}

const timeFormat = "15:04:05"
const timeOnly = "15:04:05"

func page(now time.Time) g.Node {
return c.HTML5(c.HTML5Props{
Title: now.Format(timeFormat),
Head: []g.Node{
func page(now time.Time) Node {
return HTML5(HTML5Props{
Title: now.Format(timeOnly),

Head: []Node{
Script(Src("https://cdn.tailwindcss.com?plugins=forms,typography")),
Script(Src("https://unpkg.com/htmx.org")),
},
Body: []g.Node{

Body: []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))),
H1(Text(`gomponents + HTMX`)),

P(Textf(`Time at last full page refresh was %v.`, now.Format(timeOnly))),

partial(now),
FormEl(Method("post"), Action("/"), hx.Boost("true"), hx.Target("#partial"), hx.Swap("outerHTML"),
Button(Type("submit"), g.Text(`Update time`),

Form(Method("post"), Action("/"),
hx.Boost("true"), hx.Target("#partial"), hx.Swap("outerHTML"),

Button(Type("submit"), 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"),
),
),
Expand All @@ -66,6 +73,6 @@ func page(now time.Time) g.Node {
})
}

func partial(now time.Time) g.Node {
return P(ID("partial"), g.Textf(`Time was last updated at %v.`, now.Format(timeFormat)))
func partial(now time.Time) Node {
return P(ID("partial"), Textf(`Time was last updated at %v.`, now.Format(timeOnly)))
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/maragudk/gomponents-htmx
module maragu.dev/gomponents-htmx

go 1.18

require github.com/maragudk/gomponents v0.20.1
require maragu.dev/gomponents v1.0.0-beta1

require maragu.dev/is v0.2.0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github.com/maragudk/gomponents v0.20.1 h1:TeJY1fXEcfUvzmvjeUgxol42dvkYMggK1c0V67crWWs=
github.com/maragudk/gomponents v0.20.1/go.mod h1:nHkNnZL6ODgMBeJhrZjkMHVvNdoYsfmpKB2/hjdQ0Hg=
maragu.dev/gomponents v1.0.0-beta1 h1:I51NqKfrtQC4GxuWShqW5CT5BrfToMEueLD76IhdSXs=
maragu.dev/gomponents v1.0.0-beta1/go.mod h1:oEDahza2gZoXDoDHhw8jBNgH+3UR5ni7Ur648HORydM=
maragu.dev/is v0.2.0 h1:poeuVEA5GG3vrDpGmzo2KjWtIMZmqUyvGnOB0/pemig=
maragu.dev/is v0.2.0/go.mod h1:bviaM5S0fBshCw7wuumFGTju/izopZ/Yvq4g7Klc7y8=
2 changes: 1 addition & 1 deletion htmx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package htmx
import (
"io"

g "github.com/maragudk/gomponents"
g "maragu.dev/gomponents"
)

// Boost to add or remove progressive enhancement for links and forms.
Expand Down
8 changes: 4 additions & 4 deletions htmx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"os"
"testing"

g "github.com/maragudk/gomponents"
. "github.com/maragudk/gomponents/html"
g "maragu.dev/gomponents"
. "maragu.dev/gomponents/html"

hx "github.com/maragudk/gomponents-htmx"
"github.com/maragudk/gomponents-htmx/internal/assert"
hx "maragu.dev/gomponents-htmx"
"maragu.dev/gomponents-htmx/internal/assert"
)

func TestAttributes(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"maragu.dev/is"

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

func TestBoolGetters(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"testing"

g "github.com/maragudk/gomponents"
g "maragu.dev/gomponents"
)

// Equal checks for equality between the given expected string and the rendered Node string.
Expand Down

0 comments on commit b85d5b8

Please sign in to comment.