Hey. One more (stupid) question: I have a org.w3c.dom.HTMLDivElement. How can I add this to a kvision div?
r
Robert Jaros
02/11/2021, 1:59 PM
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
jschneider
02/11/2021, 2:01 PM
Very well. Maybe an extension method would be great for that?
r
Robert Jaros
02/11/2021, 2:02 PM
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)
}
}
Robert Jaros
02/11/2021, 2:08 PM
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()
}
}
Robert Jaros
02/11/2021, 2:10 PM
I think it should be used explicitly and extension function could hide what's going on and lead to errors.