Skip to content

Request - docs and/or support for JSX-like inclusion of own components in dsl #494

@reubenfirmin

Description

@reubenfirmin

JSX is the part of react that everybody likes. It's too bad that Kotlin can't handle angle braces in identifiers, but that's not something that can change soon. Nevertheless, it'd be great if KVision could provide a way to get closer to the JSX flow of "build a component and include it as a tag".

Right now, the dsl that KVision provides is nice and fluent:

root("kvapp") {
            div("Hello world")
            h2("my heading")  {
                color = Color.name(BLUE)
            }
}

However, while I can make my own component with minimal boilerplate using Kotlin's init:

class MyComponent: Div() {

    init {
        h2("goodbye world")
    }
}

...this does not work in KVision's dsl:

        root("kvapp") {
            div("Hello world")
            h2("my heading") {
                color = Color.name(BLUE)
            }
            MyComponent()
        }

Instead, I have to call add(MyComponent()), which spoils the illusion of the dsl. I think it would be fine for components to have to implement a function in order to support this (rather than init, although init is nicer), but either way, would it be possible to think about a way to more seamlessly include components within the dsl in this manner? Maybe it could be done with receivers on a different entrypoint to root, so as to avoid breaking existing apps, e.g.:

    kv("kvapp") {
      div("hello world") {
          MyComponent()        
       }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions