Kotlin has frameworks to represent html, such as kotlinx. How can I represent web component tags in such frameworks? For instance, if I want to use Polymer components, do I have to extend these frameworks to include every Polymer component?
You can Customize Kotlinx (To create a Custom Tag). For a tag called dicom-editor
to be used inside divs
:
class DicomEditor(consumer: TagConsumer<*>) :
HTMLTag("dicom-editor", consumer, emptyMap(),
inlineTag = true,
emptyTag = false),
HtmlInlineTag {}
fun DIV.dicom_editor(block: DicomEditor.() -> Unit = {}) {
DicomEditor(consumer).visit(block)
}
...
div{
dicom_editor {
onMouseDownFunction = {_ ->
window.alert("Dicom Editor")
}
}
}
In the example above, the dicom_editor
call includes a callback for the mouse down event. You can also add atributes: attributes["data-toggle"] = "dropdown"
You can add attributes as fields:
class DicomEditor(consumer: TagConsumer<*>) :
HTMLTag("dicom-editor", consumer, emptyMap(),
inlineTag = true,
emptyTag = false),
HtmlInlineTag {
var data_toggle: String = ""
set(x) {attributes["data-toggle"] = x}
}
fun DIV.dicom_editor(block: DicomEditor.() -> Unit = {}) {
DicomEditor(consumer).visit(block)
}
...
div{
dicom_editor {
data_toggle = "dropdown"
}
}
In the Kotlin code, you have to use _
in the place of -
or you get an error.