Skip to content

Commit 52978d9

Browse files
committed
add initial swap and trigger draft
1 parent 77fc03b commit 52978d9

File tree

9 files changed

+595
-6
lines changed

9 files changed

+595
-6
lines changed

README.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# go-htmx
2-
unobstructive HTMX integration in golang applications.
2+
Seamless HTMX integration in golang applications.
33

44
This package consists of two main parts,
55
1) Middleware to catch the HTMX headers from the request
@@ -8,7 +8,7 @@ This package consists of two main parts,
88
# Getting started
99
`go get github.com/donseba/go-htmx`
1010

11-
initialise the htmx service like so :
11+
initialize the htmx service like so :
1212
```go
1313
package main
1414

@@ -47,11 +47,18 @@ func (a *App) Home(w http.ResponseWriter, r *http.Request) {
4747
h.ReTarget("#ReTarged")
4848

4949
// write the output like you normally do.
50-
// check inspector tool in browser to see that the headers are set.
50+
// check the inspector tool in the browser to see that the headers are set.
5151
_, _ = h.Write([]byte("OK"))
5252
}
5353
```
5454

55+
### Swapping
56+
Swapping is a way to replace the content of a dom element with the content of the response.
57+
This is done by setting the `HX-Swap` header to the id of the dom element you want to swap.
58+
59+
### Trigger Events
60+
Trigger events are a way to trigger events on the dom element.
61+
This is done by setting the `HX-Trigger` header to the event you want to trigger.
5562

5663
echo middleware example:
5764
```go

handler.go

+34
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ const (
1818
StatusStopPolling = 286
1919
)
2020

21+
// IsHxRequest returns true if the request is an htmx request
22+
func (h *Handler) IsHxRequest() bool {
23+
return h.request.HxRequest
24+
}
25+
26+
// IsHxBoosted returns true if the request is an htmx request and the request is boosted
27+
func (h *Handler) IsHxBoosted() bool {
28+
return h.request.HxBoosted
29+
}
30+
2131
// Write writes the data to the connection as part of an HTTP reply.
2232
func (h *Handler) Write(data []byte) (n int, err error) {
2333
return h.w.Write(data)
@@ -89,6 +99,12 @@ func (h *Handler) ReSwap(val string) {
8999
h.response.Set(HXReswap, val)
90100
}
91101

102+
// ReSwapWithObject allows you to specify how the response will be swapped. See hx-swap for possible values
103+
// https://htmx.org/attributes/hx-swap/
104+
func (h *Handler) ReSwapWithObject(s *Swap) {
105+
h.ReSwap(s.String())
106+
}
107+
92108
// ReTarget a CSS selector that updates the target of the content update to a different element on the page
93109
func (h *Handler) ReTarget(val string) {
94110
h.response.Set(HXRetarget, val)
@@ -105,18 +121,36 @@ func (h *Handler) Trigger(val string) {
105121
h.response.Set(HXTrigger, val)
106122
}
107123

124+
// TriggerWithObject triggers events as soon as the response is received.
125+
// https://htmx.org/headers/hx-trigger/
126+
func (h *Handler) TriggerWithObject(t *Trigger) {
127+
h.Trigger(t.String())
128+
}
129+
108130
// TriggerAfterSettle trigger events after the settling step.
109131
// https://htmx.org/headers/hx-trigger/
110132
func (h *Handler) TriggerAfterSettle(val string) {
111133
h.response.Set(HXTriggerAfterSettle, val)
112134
}
113135

136+
// TriggerAfterSettleWithObject trigger events after the settling step.
137+
// https://htmx.org/headers/hx-trigger/
138+
func (h *Handler) TriggerAfterSettleWithObject(t *Trigger) {
139+
h.TriggerAfterSettle(t.String())
140+
}
141+
114142
// TriggerAfterSwap trigger events after the swap step.
115143
// https://htmx.org/headers/hx-trigger/
116144
func (h *Handler) TriggerAfterSwap(val string) {
117145
h.response.Set(HXTriggerAfterSwap, val)
118146
}
119147

148+
// TriggerAfterSwapWithObject trigger events after the swap step.
149+
// https://htmx.org/headers/hx-trigger/
150+
func (h *Handler) TriggerAfterSwapWithObject(t *Trigger) {
151+
h.TriggerAfterSwap(t.String())
152+
}
153+
120154
// Request returns the HxHeaders from the request
121155
func (h *Handler) Request() HxRequestHeader {
122156
return h.request

htmx.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ package htmx
33
import (
44
"net/http"
55
"strings"
6+
"time"
7+
)
8+
9+
var (
10+
DefaultSwapDuration = time.Duration(0 * time.Millisecond)
11+
DefaultSettleDelay = time.Duration(20 * time.Millisecond)
612
)
713

814
type (
@@ -13,11 +19,11 @@ func New() *HTMX {
1319
return &HTMX{}
1420
}
1521

16-
func (s *HTMX) NewHandler(w http.ResponseWriter, r *http.Request) *Handler {
22+
func (h *HTMX) NewHandler(w http.ResponseWriter, r *http.Request) *Handler {
1723
return &Handler{
1824
w: w,
1925
r: r,
20-
request: s.HxHeader(r.Context()),
26+
request: h.HxHeader(r.Context()),
2127
response: &HxResponseHeader{
2228
headers: w.Header(),
2329
},

request.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type (
1919
}
2020
)
2121

22-
func (s *HTMX) HxHeader(ctx context.Context) HxRequestHeader {
22+
func (h *HTMX) HxHeader(ctx context.Context) HxRequestHeader {
2323
header := ctx.Value(ContextRequestHeader)
2424

2525
if val, ok := header.(HxRequestHeader); ok {

suite/suite_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"net/http/httptest"
88
"testing"
9+
"time"
910

1011
"github.com/donseba/go-htmx"
1112
"github.com/donseba/go-htmx/middleware"
@@ -134,3 +135,31 @@ func TestStopPolling(t *testing.T) {
134135

135136
assert.Equal(t, htmx.StatusStopPolling, resp.StatusCode)
136137
}
138+
139+
func TestSwap(t *testing.T) {
140+
h := htmx.New()
141+
142+
svr := httptest.NewServer(middleware.MiddleWare(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
143+
handler := h.NewHandler(w, r)
144+
145+
_ = handler.Location(location)
146+
handler.ReSwapWithObject(htmx.NewSwap().ScrollTop().Settle(1 * time.Second))
147+
148+
_, err := handler.Write([]byte("hi"))
149+
if err != nil {
150+
t.Error(err)
151+
}
152+
})))
153+
defer svr.Close()
154+
155+
resp, err := http.Get(svr.URL)
156+
if err != nil {
157+
t.Error("an error occurred while making the request")
158+
return
159+
}
160+
defer resp.Body.Close()
161+
162+
t.Log(resp.Header.Get(htmx.HXReswap.String()))
163+
164+
assert.Equal(t, "innerHTML scroll:top settle:1s", resp.Header.Get(htmx.HXReswap.String()))
165+
}

0 commit comments

Comments
 (0)