diff --git a/pkg/ui/components/head.go b/pkg/ui/components/head.go index b767a0b..e4016b4 100644 --- a/pkg/ui/components/head.go +++ b/pkg/ui/components/head.go @@ -1,7 +1,6 @@ package components import ( - "fmt" "strings" "github.com/mikestefanello/pagoda/pkg/ui" @@ -10,30 +9,9 @@ import ( ) func JS(r *ui.Request) Node { - const htmxErr = ` - document.body.addEventListener('htmx:beforeSwap', function(evt) { - if (evt.detail.xhr.status >= 400){ - evt.detail.shouldSwap = true; - evt.detail.target = htmx.find("body"); - } - }); - ` - - const htmxCSRF = ` - document.body.addEventListener('htmx:configRequest', function(evt) { - if (evt.detail.verb !== "get") { - evt.detail.parameters['csrf'] = '%s'; - } - }) - ` - return Group{ Script(Src("https://unpkg.com/htmx.org@2.0.0/dist/htmx.min.js")), Script(Src("https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"), Defer()), - Script(Raw(htmxErr)), - Iff(len(r.CSRF) > 0, func() Node { - return Script(Raw(fmt.Sprintf(htmxCSRF, r.CSRF))) - }), } } diff --git a/pkg/ui/components/htmx.go b/pkg/ui/components/htmx.go index d62cb58..e4f97f0 100644 --- a/pkg/ui/components/htmx.go +++ b/pkg/ui/components/htmx.go @@ -1,9 +1,39 @@ package components import ( + "fmt" + + "github.com/mikestefanello/pagoda/pkg/ui" . "maragu.dev/gomponents" + . "maragu.dev/gomponents/html" ) +func HtmxListeners(r *ui.Request) Node { + const htmxErr = ` + document.body.addEventListener('htmx:beforeSwap', function(evt) { + if (evt.detail.xhr.status >= 400){ + evt.detail.shouldSwap = true; + evt.detail.target = htmx.find("body"); + } + }); + ` + + const htmxCSRF = ` + document.body.addEventListener('htmx:configRequest', function(evt) { + if (evt.detail.verb !== "get") { + evt.detail.parameters['csrf'] = '%s'; + } + }) + ` + + return Group{ + Script(Raw(htmxErr)), + Iff(len(r.CSRF) > 0, func() Node { + return Script(Raw(fmt.Sprintf(htmxCSRF, r.CSRF))) + }), + } +} + func HxBoost() Node { return Attr("hx-boost", "true") } diff --git a/pkg/ui/layouts/auth.go b/pkg/ui/layouts/auth.go index 719d3ff..a7586f9 100644 --- a/pkg/ui/layouts/auth.go +++ b/pkg/ui/layouts/auth.go @@ -41,6 +41,7 @@ func Auth(r *ui.Request, content Node) Node { ), ), ), + HtmxListeners(r), ), ), ) diff --git a/pkg/ui/layouts/primary.go b/pkg/ui/layouts/primary.go index e92e562..a314d62 100644 --- a/pkg/ui/layouts/primary.go +++ b/pkg/ui/layouts/primary.go @@ -36,6 +36,7 @@ func Primary(r *ui.Request, content Node) Node { ), ), ), + HtmxListeners(r), ), ), )