/kotlin-hyperapp

Hyperapp wrapper for Kotlin/JS.

Primary LanguageKotlinApache License 2.0Apache-2.0

kotlin-hyperapp

Download

Hyperapp wrapper for Kotlin/JS.

Installation

repositories {
    jcenter()
}

dependencies {
    implementation "land.mog:kotlin-hyperapp:${version}"
}

Usage

First, you can define type of state and actions.

class ExampleState(
    val count: Int
) : State

class ExampleActions(
    val down: (Int) -> (ExampleState) -> ExampleState,
    val up: (Int) -> (ExampleState) -> ExampleState
) : Actions

Then, initialize default value and action.

val initialState = ExampleState(
    count = 0
)

val initialActions = ExampleActions(
    up = { value -> { state ->
        ExampleState(state.count + value)
    } },
    down = { value -> { state ->
        ExampleState(state.count - value)
    } }
)

Finally, construct view and run app.

val view =  { state: ExampleState, actions: ExampleActions ->
    val upAttributes = json(
        "onclick" to { actions.up(1) }
    )
    val downAttributes = json(
        "onclick" to { actions.down(1) }
    )

    h("div", null, arrayOf(
        h("h1", null, arrayOf(state.count)),
        h("button", downAttributes, arrayOf("-")),
        h("button", upAttributes, arrayOf("+"))
    ))
}

app(initialState, initialActions, view, document.body!!)

You can also see whole example project here.

Build

To build, you need simply run this command:

./gradlew :kotlin-hyperapp:build

License

This project is licensed under the Apache license 2.0.