Skip to content

Commit

Permalink
Add support for the "_" Hyperscript attribute. (#13)
Browse files Browse the repository at this point in the history
* Added the '_' attribute for Hyperscript support.

* fix

* fix

* fix

* Update Readme.

* renamed to `hyperscript`

---------

Co-authored-by: Simon Leeb <[email protected]>
  • Loading branch information
maciejtrybilo and sliemeobn authored Dec 16, 2024
1 parent 0a34f2f commit c0df782
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ let package = Package(
.library(name: "ElementaryHTMX", targets: ["ElementaryHTMX"]),
.library(name: "ElementaryHTMXSSE", targets: ["ElementaryHTMXSSE"]),
.library(name: "ElementaryHTMXWS", targets: ["ElementaryHTMXWS"]),
.library(name: "ElementaryHyperscript", targets: ["ElementaryHyperscript"]),
],
dependencies: [
.package(url: "https://github.com/sliemeobn/elementary.git", from: "0.3.0"),
Expand Down Expand Up @@ -48,6 +49,13 @@ let package = Package(
],
swiftSettings: featureFlags
),
.target(
name: "ElementaryHyperscript",
dependencies: [
.product(name: "Elementary", package: "elementary"),
],
swiftSettings: featureFlags
),
.testTarget(
name: "TestUtilities",
dependencies: [
Expand Down Expand Up @@ -79,5 +87,13 @@ let package = Package(
],
swiftSettings: featureFlags
),
.testTarget(
name: "ElementaryHyperscriptTest",
dependencies: [
.target(name: "ElementaryHyperscript"),
.target(name: "TestUtilities"),
],
swiftSettings: featureFlags
),
]
)
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ div(.hx.ext(.ws), .ws.connect("/echo")) {
}
```

```swift
import Elementary
import ElementaryHyperscript

// Hyperscript extension
button(.hyperscript("on click send hello to <form />")) {
"Send"
}
```

## Play with it

Check out the [Hummingbird example app](https://github.com/sliemeobn/elementary-htmx/tree/main/Examples/HummingbirdDemo).
Expand All @@ -59,6 +69,8 @@ The package brings the `.hx` syntaxt to all `HTMLElements` - providing a rich AP

There is also an `ElementaryHTMXSSE` module that adds the `.sse` syntax for the [Server Sent Events extensions](https://github.com/bigskysoftware/htmx-extensions/blob/main/src/sse/README.md), as well as `ElementaryHTMXWS` to add the `.ws` syntax for the [WebSockets extensions.](https://github.com/bigskysoftware/htmx-extensions/blob/main/src/ws/README.md)

The package also supports the [Hyperscript](https://hyperscript.org) `_` attribute as `.hyperscript`.

## Future directions

- Add module (or separate package?) for HTMX Request and Response headers
Expand Down
7 changes: 7 additions & 0 deletions Sources/ElementaryHyperscript/HTMLAttribute+Hyperscript.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Elementary

public extension HTMLAttribute where Tag: HTMLTrait.Attributes.Global {
static func hyperscript(_ script: String) -> Self {
.init(name: "_", value: script)
}
}
10 changes: 10 additions & 0 deletions Tests/ElementaryHyperscriptTest/ElementaryHyperscriptTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Elementary
import ElementaryHyperscript
import TestUtilities
import XCTest

final class elementary_hyperscriptTests: XCTestCase {
func testScript() {
HTMLAttributeAssertEqual(.hyperscript("on click send hello to <form />"), "_", "on click send hello to <form />")
}
}

0 comments on commit c0df782

Please sign in to comment.