/swift-htmx

HTMX extensions for swift-http-types and swift-html

Primary LanguageSwiftMIT LicenseMIT

swift-htmx

Typed htmx values for swift on server

Libraries

HtmlHtmx

If you're using pointfreeco/swift-html, HtmlHtmx will add htmx extentions to Attributes.

import Htmx
import HtmlHtmx

func example() -> Node {
  .div(
    .button(
      attributes: [
        .hxGet("/my-endpoint"),
        .hxTarget("#myDiv"),
        .hxSwap(.outerHTML),
      ],
      .text("Swapping html via hx-swap and hx-target")
    ),
    .div(attributes: [.id("myDiv")], .text("I'm getting replaced"))
  )
}

HTTPTypesHtmx

HTTPTypesHtmx adds extensions to swift-http-types's types HTTPField and HTTPField.Name

import Htmx
import HTTPTypesHtmx

let headers = HTTPFields([
  .hxRetarget("#myDiv"),
  .hxReswap(.outerHTML),
])

Example Project

Check the example project here

Installation

Add the dependency to Package.swift

dependencies: [
  .package(url: "https://github.com/alephao/swift-htmx.git", from: "0.3.0")
],

Add the products you want to use to the target dependencies

.target(
  name: "MyTarget",
  dependencies: [
    .product(name: "Htmx", package: "swift-htmx"),
    .product(name: "HTTPTypesHtmx", package: "swift-htmx"),
    .product(name: "HtmlHtmx", package: "swift-htmx"),
  ]
)