How can I style my HTML elements when using Kotlinx-html
, I've my app working fine, then I tried to add styling using AZA-Kotlin
, but once I imported azadev.kotlin
It gave me error build
My full code is a s below:
Main.kt
:
import azadev.kotlin.css.Stylesheet
import azadev.kotlin.css.color
import azadev.kotlin.css.dimens.px
import azadev.kotlin.css.opacity
import azadev.kotlin.css.width
import kotlinx.html.*
import kotlinx.html.js.*
import kotlinx.html.dom.create
import kotlin.browser.*
import kotlinx.html.dom.append
import org.w3c.dom.HTMLButtonElement
fun main(args: Array<String>) {
println("Hello JavaScript!")
val myDiv = document.create.div("panel") { // class = "panel"
p {
+"Here is "
a("http://kotlinlang.org") { +"official Kotlin site" }
}
}
val button = BUTTON()
button!!.innerText = "Click me"
button!!.onclick = { println("Button clicked!") }
val btn = document.create.button {
text("click me")
onClickFunction = { _ -> window.alert("Kotlin!") }
Stylesheet {
color = 0xffffff
width = 10.px
opacity = .8
hover {
color = 0xf2cacf
}
}
}
document.getElementById("container")!!.appendChild(myDiv)
document.getElementById("container")!!.appendChild(btn)
document.getElementById("container")!!.appendChild(button)
document.getElementById("container")!!.append {
div {
+"added it"
}
}
}
fun BUTTON(): HTMLButtonElement {return document.create.button()}
My gradle.build
is:
group 'org.example'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.1.51'
ext.kotlinx_html_version = '0.6.4'
ext.aza_kotlin_css = '1.0'
ext.web_dir = 'web'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin2js'
repositories {
mavenCentral()
jcenter()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
compile "org.jetbrains.kotlinx:kotlinx-html-js:$kotlinx_html_version"
compile "azadev.kotlin:aza-kotlin-css:$aza_kotlin_css"
}
compileKotlin2Js {
kotlinOptions.outputFile = "${projectDir}/web/scripts/main.js"
kotlinOptions.moduleKind = "umd"
kotlinOptions.sourceMap = true
}
clean.doFirst() {
delete("${web_dir}")
}
build.doLast() {
// Copy kotlin.js and kotlin-meta.js from jar into web directory
configurations.compile.each { File file ->
copy {
includeEmptyDirs = false
from zipTree(file.absolutePath)
into "${projectDir}/${web_dir}/lib"
include { fileTreeElement ->
def path = fileTreeElement.path
path.endsWith(".js") && (path.startsWith("META-INF/resources/") || !path.startsWith("META-INF/"))
}
}
}
// Copy scripts to web directory
copy {
includeEmptyDirs = false
from new File("build/classes/main")
into "${web_dir}/lib"
}
// Copy resources to web directory
copy {
includeEmptyDirs = false
from new File("src/main/kotlin/resources")
into "${web_dir}"
}
}
My index.html
is:
<html>
<head>
<meta charset="UTF-8">
<title>Sample Default</title>
</head>
<body id="BODY">
<h1>Kotlin 1.1 Example</h1>
<div id="container"/>
<input type="text" name="email" id="email"/>
<script type="text/javascript" src="lib/kotlin.js"></script>
<script type="text/javascript" src="lib/kotlinx-html-js.js"></script>
<script type="text/javascript" src="scripts/main.js"></script>
</body>
</html>
How can I style my elements, regardless using Aza-kotlin
or any other way.
As of today, something wrong with the
aza_kotlin_css
and the app worked with me as below [detailed steps for Mac newbie :)]Installed
Homebrew
as:/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Installed
gradle
as:brew install gradle
Found the installed path by running
brew info gradle
Added the gradle manually to `Android Studio/Preferences'
Created the projected folder, and moved in
cd myapp
Initiated new java gradle project as
gradle init --type java-library
Deleted the
src/main
and thesrc/test
foldersCreated the
src/kotlin
andsrc/resources
foldersReplaced the content of the
build.gradle
file by:Created the
src/kotlin/Main.Kt
file with the following content:Created another file
src/kotlin/Fib.tk
with the following content, which is called from theMain.kt
file, and all are compiled issingle
JavaScript file:Created the
src/resources/index.html
file with the following content:Created the
src/resources/styles/main.css
file with the following content:Built the project using
gradle build
and it functioned perfectly.The app structure is as below:
And the app run with me as:
For working with the server side, I've a question here and here and here about creating
jar
file of it, hope you find it useful and complimentary of this.