Skip to content

A Kotlin Multiplatform htmx library

License

Notifications You must be signed in to change notification settings

silas00301/kotlin-htmx

Repository files navigation

Kotlin-Htmx

Kotlin-Htmx is a Kotlin DSL for the htmx library. It provides a type-safe way to generate htmx attributes and configuration.

Usage

As the goal of this library is to be usable with any Kotlin web framework, it does not provide any specific integration. Instead, it provides a few code snippets that have to be integrated into your project.

kotlinx-html

If you are using kotlinx-html the following snippets can be used to integrate Kotlin-Htmx into your project.

Attributes

If you want to use a scope function to set the headers, you can use the following code snippet.

fun HTMLTag.hx(block: HtmxHtmlAttributes.() -> Unit) = HtmxHtmlAttributes(attributes).block()

If you want to set the headers using a property, you can use the following code snippet.

val HTMLTag.hx
    get() = HtmxHtmlAttributes(attributes)

Configuration

fun HEAD.htmxConfig(block: HtmxConfiguration.() -> Unit) = meta {
    name = "htmx-config"
    content = HtmxConfiguration().apply(block).toConfigString()
}

CSS Classes

fun HTMLTag.hxClasses(block: HtmxCSSClasses.() -> Unit) {
    attributes["class"] += HtmxCSSClasses().apply(block).classString
}

Headers

Response Headers

fun RoutingResponse.hx(block: HtmxResponseHeaders.() -> Unit) = mutableMapOf<String, String>().also {
    HtmxResponseHeaders(it).apply(block)
}.forEach(headers::append)

Request Headers

val RoutingRequest.hx
    get() = HtmxRequestHeaders(headers.flattenEntries().toMap())

Releases

No releases published

Packages

No packages published

Languages