Skip to content

Commit

Permalink
add see extension
Browse files Browse the repository at this point in the history
  • Loading branch information
will-wow committed May 19, 2024
1 parent ab21d33 commit e1a5fed
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,14 @@ var hx = htmx.NewTempl()

### Current Extensions supported

See [htmx/ext](./htmx/ext) for a full list of extensions.
Some of the more complex extensions supported:

- [`class-tools`](https://pkg.go.dev/github.com/will-wow/typed-htmx-go/htmx/ext/classtools)
- [`preload`](https://pkg.go.dev/github.com/will-wow/typed-htmx-go/htmx/ext/preload)
- [`response-targets`](https://pkg.go.dev/github.com/will-wow/typed-htmx-go/htmx/ext/responsetargets)
- [`loading-states`](https://pkg.go.dev/github.com/will-wow/typed-htmx-go/htmx/ext/loadingstates)

- [`class-tools`](https://htmx.org/extensions/class-tools/)
- [`preload`](https://htmx.org/extensions/preload/)
- [`response-targets`](https://htmx.org/extensions/response-targets/)
- [`event-header`](https://htmx.org/extensions/event-header/)
- [`remove-me`](https://htmx.org/extensions/remove-me/)
See [htmx/ext](./htmx/ext) for a full list of extensions.

## Examples

Expand Down
42 changes: 42 additions & 0 deletions htmx/ext/sse/sse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// package sse connects to an EventSource directly from HTML. It manages the connections to your web server, listens for server events, and then swaps their contents into your htmx webpage in real-time.
//
// [EventSource]: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
package sse

import (
"fmt"

"github.com/will-wow/typed-htmx-go/htmx"
)

// Extension connects to an EventSource directly from HTML. It manages the connections to your web server, listens for server events, and then swaps their contents into your htmx webpage in real-time.
//
// # Install
//
// <script src="https://unpkg.com/[email protected]/dist/ext/sse.js"></script>
//
// Extension: [server-sent-events]
//
// [server-sent-events]: https://htmx.org/extensions/server-sent-events/
//
// [EventSource]: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
const Extension htmx.Extension = "sse"

// Message is the the default name of an empty SSE event.
const Message = "message"

// Connect
func Connect[T any](hx htmx.HX[T], url string) T {
return hx.Attr("sse-connect", url)
}

// Swap
func Swap[T any](hx htmx.HX[T], messageName string) T {
return hx.Attr("sse-swap", messageName)
}

// Trigger allows SSE messages to trigger HTTP callbacks using the [htmx.HX.Trigger()] attribute.
func Trigger[T any](hx htmx.HX[T], event string) T {
prefixedEvent := fmt.Sprintf("sse:%s", event)
return hx.Attr(htmx.Trigger, prefixedEvent)
}
40 changes: 40 additions & 0 deletions htmx/ext/sse/sse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package sse_test

import (
"fmt"

"github.com/will-wow/typed-htmx-go/htmx"
"github.com/will-wow/typed-htmx-go/htmx/ext/sse"
)

var hx = htmx.NewStringAttrs()

func ExampleExtension() {
attr := hx.Ext(sse.Extension)
fmt.Println(attr)
// Output: hx-ext='sse'
}

func ExampleMessage() {
attr := sse.Swap(hx, sse.Message)
fmt.Println(attr)
// Output: sse-swap='message'
}

func ExampleConnect() {
attr := sse.Connect(hx, "/chatroom")
fmt.Println(attr)
// Output: sse-connect='/chatroom'
}

func ExampleSwap() {
attr := sse.Swap(hx, "event")
fmt.Println(attr)
// Output: sse-swap='event'
}

func ExampleTrigger() {
attr := sse.Trigger(hx, "event")
fmt.Println(attr)
// Output: hx-trigger='sse:event'
}

0 comments on commit e1a5fed

Please sign in to comment.