Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/silas00301/kotlin-htmx

A Kotlin Multiplatform htmx library
https://github.com/silas00301/kotlin-htmx

htmx kotlin kotlin-multiplatform kotlinx-html ktor

Last synced: 10 days ago
JSON representation

A Kotlin Multiplatform htmx library

Awesome Lists containing this project

README

        

# Kotlin-Htmx

Kotlin-Htmx is a Kotlin DSL for the [htmx](https://htmx.org) 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](#kotlinx-html)

## 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.

```kotlin
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.

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

### Configuration

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

### CSS Classes

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

### Headers

#### Response Headers

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

#### Request Headers

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