diff --git a/Package.swift b/Package.swift index 64974a2..c7ef2ed 100644 --- a/Package.swift +++ b/Package.swift @@ -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"), @@ -48,6 +49,13 @@ let package = Package( ], swiftSettings: featureFlags ), + .target( + name: "ElementaryHyperscript", + dependencies: [ + .product(name: "Elementary", package: "elementary"), + ], + swiftSettings: featureFlags + ), .testTarget( name: "TestUtilities", dependencies: [ @@ -79,5 +87,13 @@ let package = Package( ], swiftSettings: featureFlags ), + .testTarget( + name: "ElementaryHyperscriptTest", + dependencies: [ + .target(name: "ElementaryHyperscript"), + .target(name: "TestUtilities"), + ], + swiftSettings: featureFlags + ), ] ) diff --git a/README.md b/README.md index 1b22df1..5742518 100644 --- a/README.md +++ b/README.md @@ -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
")) { + "Send" +} +``` + ## Play with it 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 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 diff --git a/Sources/ElementaryHyperscript/HTMLAttribute+Hyperscript.swift b/Sources/ElementaryHyperscript/HTMLAttribute+Hyperscript.swift new file mode 100644 index 0000000..91a5079 --- /dev/null +++ b/Sources/ElementaryHyperscript/HTMLAttribute+Hyperscript.swift @@ -0,0 +1,7 @@ +import Elementary + +public extension HTMLAttribute where Tag: HTMLTrait.Attributes.Global { + static func hyperscript(_ script: String) -> Self { + .init(name: "_", value: script) + } +} diff --git a/Tests/ElementaryHyperscriptTest/ElementaryHyperscriptTest.swift b/Tests/ElementaryHyperscriptTest/ElementaryHyperscriptTest.swift new file mode 100644 index 0000000..c57b3d3 --- /dev/null +++ b/Tests/ElementaryHyperscriptTest/ElementaryHyperscriptTest.swift @@ -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 "), "_", "on click send hello to ") + } +}