Skip to content

Commit 31d1343

Browse files
Add all core attributes (#2)
See https://htmx.org/reference/#attributes
2 parents 4c86ded + a49e29c commit 31d1343

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

htmx.go

+51-3
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,72 @@ import (
66
g "github.com/maragudk/gomponents"
77
)
88

9-
// Boost for links and forms.
9+
// Boost to add or remove progressive enhancement for links and forms.
1010
// See https://htmx.org/attributes/hx-boost
1111
func Boost(v string) g.Node {
1212
return attr("boost", v)
1313
}
1414

15-
// Get the url.
15+
// Get from the specified URL.
1616
// See https://htmx.org/attributes/hx-get
1717
func Get(url string) g.Node {
1818
return attr("get", url)
1919
}
2020

21-
// Post to the url.
21+
// Post to the specified URL.
2222
// See https://htmx.org/attributes/hx-post
2323
func Post(url string) g.Node {
2424
return attr("post", url)
2525
}
2626

27+
// PushURL into the browser location bar, creating a new history entry.
28+
// See https://htmx.org/attributes/hx-push-url
29+
func PushURL(v string) g.Node {
30+
return attr("push-url", v)
31+
}
32+
33+
// Select content to swap in from a response.
34+
// See https://htmx.org/attributes/hx-select
35+
func Select(v string) g.Node {
36+
return attr("select", v)
37+
}
38+
39+
// SelectOOB content to swap in from a response, out of band (somewhere other than the target).
40+
// See https://htmx.org/attributes/hx-select-oob
41+
func SelectOOB(v string) g.Node {
42+
return attr("select-oob", v)
43+
}
44+
45+
// Swap controls how content is swapped in.
46+
// See https://htmx.org/attributes/hx-swap
47+
func Swap(v string) g.Node {
48+
return attr("swap", v)
49+
}
50+
51+
// SwapOOB marks content in a response to be out of band (should swap in somewhere other than the target).
52+
// See https://htmx.org/attributes/hx-swap-oob
53+
func SwapOOB(v string) g.Node {
54+
return attr("swap-oob", v)
55+
}
56+
57+
// Target specifies the target element to be swapped.
58+
// See https://htmx.org/attributes/hx-target
59+
func Target(v string) g.Node {
60+
return attr("target", v)
61+
}
62+
63+
// Trigger specifies the event that triggers the request.
64+
// See https://htmx.org/attributes/hx-trigger
65+
func Trigger(v string) g.Node {
66+
return attr("trigger", v)
67+
}
68+
69+
// Vals adds values to the parameters to submit with the request (JSON-formatted).
70+
// See https://htmx.org/attributes/hx-vals
71+
func Vals(v string) g.Node {
72+
return attr("vals", v)
73+
}
74+
2775
func attr(name, value string) g.Node {
2876
return g.Attr("hx-"+name, value)
2977
}

htmx_test.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,29 @@ package htmx_test
22

33
import (
44
"fmt"
5+
"os"
56
"testing"
67

78
g "github.com/maragudk/gomponents"
9+
. "github.com/maragudk/gomponents/html"
810

9-
. "github.com/maragudk/gomponents-htmx"
11+
hx "github.com/maragudk/gomponents-htmx"
1012
"github.com/maragudk/gomponents-htmx/internal/assert"
1113
)
1214

1315
func TestAttributes(t *testing.T) {
1416
cases := map[string]func(string) g.Node{
15-
"boost": Boost,
16-
"get": Get,
17-
"post": Post,
17+
"boost": hx.Boost,
18+
"get": hx.Get,
19+
"post": hx.Post,
20+
"push-url": hx.PushURL,
21+
"select": hx.Select,
22+
"select-oob": hx.SelectOOB,
23+
"swap": hx.Swap,
24+
"swap-oob": hx.SwapOOB,
25+
"target": hx.Target,
26+
"trigger": hx.Trigger,
27+
"vals": hx.Vals,
1828
}
1929

2030
for name, fn := range cases {
@@ -24,3 +34,9 @@ func TestAttributes(t *testing.T) {
2434
})
2535
}
2636
}
37+
38+
func ExampleGet() {
39+
n := Button(hx.Post("/clicked"), hx.Swap("outerHTML"))
40+
_ = n.Render(os.Stdout)
41+
// Output: <button hx-post="/clicked" hx-swap="outerHTML"></button>
42+
}

0 commit comments

Comments
 (0)