Skip to content

Commit

Permalink
Add all core attributes (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
markuswustenberg committed Feb 24, 2023
2 parents 4c86ded + a49e29c commit 31d1343
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
54 changes: 51 additions & 3 deletions htmx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,72 @@ import (
g "github.com/maragudk/gomponents"
)

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

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

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

// PushURL into the browser location bar, creating a new history entry.
// See https://htmx.org/attributes/hx-push-url
func PushURL(v string) g.Node {
return attr("push-url", v)
}

// Select content to swap in from a response.
// See https://htmx.org/attributes/hx-select
func Select(v string) g.Node {
return attr("select", v)
}

// SelectOOB content to swap in from a response, out of band (somewhere other than the target).
// See https://htmx.org/attributes/hx-select-oob
func SelectOOB(v string) g.Node {
return attr("select-oob", v)
}

// Swap controls how content is swapped in.
// See https://htmx.org/attributes/hx-swap
func Swap(v string) g.Node {
return attr("swap", v)
}

// SwapOOB marks content in a response to be out of band (should swap in somewhere other than the target).
// See https://htmx.org/attributes/hx-swap-oob
func SwapOOB(v string) g.Node {
return attr("swap-oob", v)
}

// Target specifies the target element to be swapped.
// See https://htmx.org/attributes/hx-target
func Target(v string) g.Node {
return attr("target", v)
}

// Trigger specifies the event that triggers the request.
// See https://htmx.org/attributes/hx-trigger
func Trigger(v string) g.Node {
return attr("trigger", v)
}

// Vals adds values to the parameters to submit with the request (JSON-formatted).
// See https://htmx.org/attributes/hx-vals
func Vals(v string) g.Node {
return attr("vals", v)
}

func attr(name, value string) g.Node {
return g.Attr("hx-"+name, value)
}
24 changes: 20 additions & 4 deletions htmx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@ package htmx_test

import (
"fmt"
"os"
"testing"

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

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

func TestAttributes(t *testing.T) {
cases := map[string]func(string) g.Node{
"boost": Boost,
"get": Get,
"post": Post,
"boost": hx.Boost,
"get": hx.Get,
"post": hx.Post,
"push-url": hx.PushURL,
"select": hx.Select,
"select-oob": hx.SelectOOB,
"swap": hx.Swap,
"swap-oob": hx.SwapOOB,
"target": hx.Target,
"trigger": hx.Trigger,
"vals": hx.Vals,
}

for name, fn := range cases {
Expand All @@ -24,3 +34,9 @@ func TestAttributes(t *testing.T) {
})
}
}

func ExampleGet() {
n := Button(hx.Post("/clicked"), hx.Swap("outerHTML"))
_ = n.Render(os.Stdout)
// Output: <button hx-post="/clicked" hx-swap="outerHTML"></button>
}

0 comments on commit 31d1343

Please sign in to comment.