Skip to content

Commit c0df782

Browse files
Add support for the "_" Hyperscript attribute. (#13)
* Added the '_' attribute for Hyperscript support. * fix * fix * fix * Update Readme. * renamed to `hyperscript` --------- Co-authored-by: Simon Leeb <[email protected]>
1 parent 0a34f2f commit c0df782

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

Package.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ let package = Package(
2020
.library(name: "ElementaryHTMX", targets: ["ElementaryHTMX"]),
2121
.library(name: "ElementaryHTMXSSE", targets: ["ElementaryHTMXSSE"]),
2222
.library(name: "ElementaryHTMXWS", targets: ["ElementaryHTMXWS"]),
23+
.library(name: "ElementaryHyperscript", targets: ["ElementaryHyperscript"]),
2324
],
2425
dependencies: [
2526
.package(url: "https://github.com/sliemeobn/elementary.git", from: "0.3.0"),
@@ -48,6 +49,13 @@ let package = Package(
4849
],
4950
swiftSettings: featureFlags
5051
),
52+
.target(
53+
name: "ElementaryHyperscript",
54+
dependencies: [
55+
.product(name: "Elementary", package: "elementary"),
56+
],
57+
swiftSettings: featureFlags
58+
),
5159
.testTarget(
5260
name: "TestUtilities",
5361
dependencies: [
@@ -79,5 +87,13 @@ let package = Package(
7987
],
8088
swiftSettings: featureFlags
8189
),
90+
.testTarget(
91+
name: "ElementaryHyperscriptTest",
92+
dependencies: [
93+
.target(name: "ElementaryHyperscript"),
94+
.target(name: "TestUtilities"),
95+
],
96+
swiftSettings: featureFlags
97+
),
8298
]
8399
)

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ div(.hx.ext(.ws), .ws.connect("/echo")) {
4747
}
4848
```
4949

50+
```swift
51+
import Elementary
52+
import ElementaryHyperscript
53+
54+
// Hyperscript extension
55+
button(.hyperscript("on click send hello to <form />")) {
56+
"Send"
57+
}
58+
```
59+
5060
## Play with it
5161

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

6070
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)
6171

72+
The package also supports the [Hyperscript](https://hyperscript.org) `_` attribute as `.hyperscript`.
73+
6274
## Future directions
6375

6476
- Add module (or separate package?) for HTMX Request and Response headers
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Elementary
2+
3+
public extension HTMLAttribute where Tag: HTMLTrait.Attributes.Global {
4+
static func hyperscript(_ script: String) -> Self {
5+
.init(name: "_", value: script)
6+
}
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Elementary
2+
import ElementaryHyperscript
3+
import TestUtilities
4+
import XCTest
5+
6+
final class elementary_hyperscriptTests: XCTestCase {
7+
func testScript() {
8+
HTMLAttributeAssertEqual(.hyperscript("on click send hello to <form />"), "_", "on click send hello to <form />")
9+
}
10+
}

0 commit comments

Comments
 (0)