Skip to content
This repository was archived by the owner on May 17, 2023. It is now read-only.

Commit 398fa83

Browse files
Update "Building Web Applications with React and Kotlin/JS"
Use new React DSL Use new CSS DSL Dependency version updates
1 parent ab01ea6 commit 398fa83

File tree

8 files changed

+193
-204
lines changed

8 files changed

+193
-204
lines changed

Building Web Applications with React and Kotlin JS/01_Introduction.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ It is supported by a large ecosystem of community-created materials and componen
66

77
We will get to reuse our knowledge about Kotlin's language paradigms, syntax, and tooling to build front-end applications for modern browsers.
88

9-
During this hands-on, we will learn how to build our first application with Kotlin/JS and React.
9+
In this hands-on, we will learn how to build our first application with Kotlin/JS and React.
1010
We will go through the usual tasks associated with building a typical, simple React application.
1111

1212
We will explore how [Kotlin's DSLs](https://kotlinlang.org/docs/type-safe-builders.html) can be used to help us write our app completely in Kotlin.
@@ -18,7 +18,7 @@ Prior knowledge of the basic concepts behind React is helpful in understanding s
1818
### What we are going to build
1919

2020
The annual [*KotlinConf*](https://kotlinconf.com/) is the event to visit if you want to learn more about Kotlin and exchange with the community.
21-
With 1300 attendees and a ton of workshops and sessions, KotlinConf 2018 offered a wealth of information.
21+
With 1300 attendees and a ton of workshops and sessions, KotlinConf 2018 offered a wealth of information, much of which is still valuable to this very day!
2222
For anyone needing to still catch up, all the videos are available on YouTube!
2323

2424
The project that we will create throughout this tutorial - *KotlinConf Explorer* - will allow users to get an overview of the talks, quickly mark them as *seen* or *unseen*, and watch them all from one page: perfect for a Kotlin-binge-session!

Building Web Applications with React and Kotlin JS/02_Setting_up.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
To get started, let's make sure we have installed an up-to-date development environment. All we need to get started is:
66

7-
- IntelliJ IDEA (version `2021.2` or above) with the Kotlin plugin (`1.5.31` or above) – [Download/Install](https://www.jetbrains.com/idea/download/)
7+
- IntelliJ IDEA (version `2022.1` or above) with the Kotlin plugin (`1.6.10` or above) – [Download/Install](https://www.jetbrains.com/idea/download/)
88

99

1010
### Setting up the project
@@ -25,26 +25,24 @@ The `dependencies` block in our `build.gradle.kts` file contains everything we'l
2525

2626
```kotlin
2727
dependencies {
28-
2928
//React, React DOM + Wrappers (chapter 3)
30-
implementation("org.jetbrains.kotlin-wrappers:kotlin-react:17.0.2-pre.264-kotlin-1.5.31")
31-
implementation("org.jetbrains.kotlin-wrappers:kotlin-react-dom:17.0.2-pre.264-kotlin-1.5.31")
29+
implementation("org.jetbrains.kotlin-wrappers:kotlin-react:17.0.2-pre.297-kotlin-1.6.10")
30+
implementation("org.jetbrains.kotlin-wrappers:kotlin-react-dom:17.0.2-pre.297-kotlin-1.6.10")
3231
implementation(npm("react", "17.0.2"))
3332
implementation(npm("react-dom", "17.0.2"))
3433

35-
//Kotlin Styled (chapter 3)
36-
implementation("org.jetbrains.kotlin-wrappers:kotlin-styled:5.3.3-pre.264-kotlin-1.5.31")
37-
implementation(npm("styled-components", "~5.3.3"))
34+
//Kotlin React CSS (chapter 3)
35+
implementation("org.jetbrains.kotlin-wrappers:kotlin-react-css:17.0.2-pre.298-kotlin-1.6.10")
3836

3937
//Video Player (chapter 7)
40-
implementation(npm("react-youtube-lite", "1.1.0"))
38+
implementation(npm("react-youtube-lite", "1.5.0"))
4139

4240
//Share Buttons (chapter 7)
4341
implementation(npm("react-share", "4.4.0"))
4442

4543
//Coroutines & serialization (chapter 8)
46-
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2")
47-
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0")
44+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0")
45+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2")
4846
}
4947
```
5048

@@ -75,7 +73,7 @@ As is typical [JavaScript convention](https://faqs.skillcrush.com/article/176-wh
7573
The template also starts us out with a simple code example, just to verify that everything is working fine, and we're good to go. The file `src/main/kotlin/Main.kt` contains the following snippet, which just turns the whole page red:
7674

7775
```kotlin
78-
// . . .
76+
import kotlinx.browser.document
7977

8078
fun main() {
8179
document.bgColor = "red"

Building Web Applications with React and Kotlin JS/03_A_First_Static_Page.md

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,27 @@ Let's start our story with a classic little *Hello World*-type program!
55
Change the code inside `src/main/kotlin/Main.kt` to look like this:
66

77
```kotlin
8-
import react.dom.*
98
import kotlinx.browser.document
10-
import kotlinx.css.*
9+
import react.*
10+
import react.css.css
11+
import react.dom.render
12+
import csstype.Position
13+
import csstype.px
14+
import react.dom.html.ReactHTML.h1
15+
import react.dom.html.ReactHTML.h3
16+
import react.dom.html.ReactHTML.div
17+
import react.dom.html.ReactHTML.p
18+
import react.dom.html.ReactHTML.img
1119
import kotlinx.serialization.Serializable
12-
import styled.*
1320

1421
fun main() {
15-
render(document.getElementById("root")) {
22+
val container = document.getElementById("root") ?: error("Couldn't find root container!")
23+
24+
render(Fragment.create {
1625
h1 {
1726
+"Hello, React+Kotlin/JS!"
1827
}
19-
}
28+
}, container)
2029
}
2130
```
2231

@@ -26,7 +35,7 @@ If you don't have the continuous Gradle build (as described in Chapter 2) still
2635

2736
Congratulations, you've just written your first website using pure Kotlin/JS and React!
2837

29-
In this first snippet, you can see the `render` from [kotlin-react-dom](https://github.com/JetBrains/kotlin-wrappers/tree/master/kotlin-react-dom) render a first little component into an element called `#root`.
38+
In this first snippet, you can see the `render` from [kotlin-react-dom](https://github.com/JetBrains/kotlin-wrappers/tree/master/kotlin-react-dom) render a first HTML element inside a [fragment](https://reactjs.org/docs/fragments.html) to an element called `#root`.
3039
This container element is defined in `/src/main/resources/index.html`, which was included in the template.
3140

3241
The content we render is pretty simple - it is just an `<h1>` headline. However, we use Kotlin's typesafe DSL to render this HTML snippet.
@@ -73,7 +82,7 @@ Let's translate this code into Kotlin. The conversion should be rather straightf
7382

7483
```kotlin
7584
h1 {
76-
+"KotlinConf Explorer"
85+
+"Hello, React+Kotlin/JS!"
7786
}
7887
div {
7988
h3 {
@@ -101,14 +110,12 @@ div {
101110
+"John Doe: Building and breaking things"
102111
}
103112
img {
104-
attrs {
105-
src = "https://via.placeholder.com/640x360.png?text=Video+Player+Placeholder"
106-
}
113+
src = "https://via.placeholder.com/640x360.png?text=Video+Player+Placeholder"
107114
}
108115
}
109116
```
110117

111-
Type or paste the above code as the contents of your `render` call inside the `main` function,
118+
Type or paste the above code as the contents of your `Fragment.create` call inside the `main` function,
112119
replacing our previous `h1` tag.
113120

114121
After saving the file, let's go back to the browser.
@@ -118,7 +125,7 @@ We will see a page that looks like this:
118125

119126
### Using Kotlin language constructs in markup
120127

121-
There are some useful side-effects to writing HTML in Kotlin using this DSL.
128+
There are some useful side effects to writing HTML in Kotlin using this DSL.
122129
For example, we can manipulate our website using regular Kotlin constructs.
123130
We could use loops, conditions, collections, string interpolation, and more.
124131
We'll see how that works in practice in just a bit.
@@ -141,13 +148,13 @@ Then, let's fill up the two lists for unwatched videos and watched videos respec
141148

142149
```kotlin
143150
val unwatchedVideos = listOf(
144-
Video(1, "Building and breaking things", "John Doe", "https://youtu.be/PsaFVLr8t4E"),
145-
Video(2, "The development process", "Jane Smith", "https://youtu.be/PsaFVLr8t4E"),
146-
Video(3, "The Web 7.0", "Matt Miller", "https://youtu.be/PsaFVLr8t4E")
151+
Video(1, "Opening Keynote", "Andrey Breslav", "https://youtu.be/PsaFVLr8t4E"),
152+
Video(2, "Dissecting the stdlib", "Huyen Tue Dao", "https://youtu.be/Fzt_9I733Yg"),
153+
Video(3, "Kotlin and Spring Boot", "Nicolas Frankel", "https://youtu.be/pSiZVAeReeg")
147154
)
148155

149156
val watchedVideos = listOf(
150-
Video(4, "Mouseless development", "Tom Jerry", "https://youtu.be/PsaFVLr8t4E")
157+
Video(4, "Creating Internal DSLs in Kotlin", "Venkat Subramaniam", "https://youtu.be/JzTeAM8N1-o")
151158
)
152159
```
153160

@@ -171,40 +178,38 @@ for(video in watchedVideos) {
171178
}
172179
```
173180

174-
Wait for the browser to reload. If everything went well, we should see the same layout as before (but now, using proper Kotlin objects in the background!)
181+
Wait for the browser to reload. If everything went well, we should see the same layout as before (but now, using proper Kotlin objects behind the scenes!)
175182
To _really_ make sure our loop is actually looping, you can of course add some more videos to the list on your own at this point.
176183

177184
### Writing typesafe CSS
178185

179186
Our little "app" now already has the ability to show watched and unwatched videos in a list.
180-
Unfortuantely, it still looks a bit boring.
187+
Unfortunately, it still looks a bit boring.
181188
To fix that, we could include a CSS file in our `index.html` template.
182189
But we can also use this chance to play with another Kotlin DSL - this time for CSS.
183190

184-
The [kotlin-styled](https://github.com/JetBrains/kotlin-wrappers/tree/master/kotlin-styled) library wraps the [styled-components](https://www.styled-components.com/) JavaScript library.
185-
Using it, we can define styles either [globally](https://github.com/JetBrains/kotlin-wrappers/blob/master/kotlin-styled/README.md#global-styles) or for individual elements of our DOM.
186-
Conceptually, that makes it similar to [CSS-in-JS](https://reactjs.org/docs/faq-styling.html#what-is-css-in-js) – but for Kotlin. Like before, the benefit of using a DSL is that we can use Kotlin code constructs to express our formatting rules.
191+
The [kotlin-react-css](https://github.com/JetBrains/kotlin-wrappers/tree/master/kotlin-react-css) library allows us to specify CSS attributes – even dynamic ones – right alongside our HTML.
192+
Conceptually, that makes it similar to [CSS-in-JS](https://reactjs.org/docs/faq-styling.html#what-is-css-in-js) – but for Kotlin! Like before, the benefit of using a DSL is that we can use Kotlin code constructs to express our formatting rules.
187193

188-
The template project for this tutorial already includes everything we need to use `kotlin-styled`.
194+
The template project for this tutorial already includes everything we need to use `kotlin-react-css`.
189195
The relevant block in our build configuration is this:
190196

191197
```kotlin
192198
dependencies {
193199
//...
194-
//Kotlin Styled (chapter 3)
195-
implementation("org.jetbrains.kotlin-wrappers:kotlin-styled:5.3.3-pre.264-kotlin-1.5.31")
196-
implementation(npm("styled-components", "~5.3.3"))
200+
//Kotlin React CSS (chapter 3)
201+
implementation("org.jetbrains.kotlin-wrappers:kotlin-react-css:17.0.2-pre.298-kotlin-1.6.10")
197202
//...
198203
}
199204
```
200205

201-
In the previous segment, we used HTML elements like `div` and `h3`. Now, we can use their "styled" counterpart, i.e. `styledDiv` and `styledH3`. These styled components allow us to specify a `css` block, in which we can define our styles.
206+
In the previous segment, we used HTML elements like `div` and `h3`. With `kotlin-react-css` in our project, we can also specify a `css` block inside these elements, where we can define our styles.
202207

203-
Let's use `styledDiv` to move the video player to the top right corner of the page.
208+
Let's use CSS to move the video player to the top right corner of the page.
204209
Adjust the code for our mock video player (the last `div` in our snippet) to look like this:
205210

206211
```kotlin
207-
styledDiv {
212+
div {
208213
css {
209214
position = Position.absolute
210215
top = 10.px
@@ -214,9 +219,7 @@ styledDiv {
214219
+"John Doe: Building and breaking things"
215220
}
216221
img {
217-
attrs {
218-
src = "https://via.placeholder.com/640x360.png?text=Video+Player+Placeholder"
219-
}
222+
src = "https://via.placeholder.com/640x360.png?text=Video+Player+Placeholder"
220223
}
221224
}
222225
```

0 commit comments

Comments
 (0)