Hey. One more (stupid) question: I have a org.w3c....
# kvision
j
Hey. One more (stupid) question: I have a org.w3c.dom.HTMLDivElement. How can I add this to a kvision div?
r
you need to get access to the native dom element inside
afterInsertHook
Copy code
val el = document.createElement("div")
        el.textContent = "test"
        root("kvapp") {
            div {
                addAfterInsertHook {
                    getElement()?.appendChild(el)
                }
            }
        }
👍 1
j
Very well. Maybe an extension method would be great for that?
r
You can do this directly if you are sure the element is rendered:
Copy code
val el = document.createElement("div")
        el.textContent = "test"
        root("kvapp") {
            val div = div {
                +"some content"
            }
            button("test").onClick {
                div.getElement()?.appendChild(el)
            }
        }
Direct access to the DOM is possible but you need to be careful because KVision works with virtual dom and your direct changes will disappear (e.g. after hiding and showing again):
Copy code
val el = document.createElement("div")
        el.textContent = "test"
        root("kvapp") {
            val div = div {
                +"some content"
            }
            button("test").onClick {
                div.getElement()?.appendChild(el)
            }
            button("toggle visibility").onClick {
                div.toggleVisible()
            }
        }
I think it should be used explicitly and extension function could hide what's going on and lead to errors.