Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the "_" Hyperscript attribute. #13

Merged
merged 6 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 />")
}
}
Loading