You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 17, 2023. It is now read-only.
Copy file name to clipboardExpand all lines: Building Web Applications with React and Kotlin JS/01_Introduction.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ It is supported by a large ecosystem of community-created materials and componen
6
6
7
7
We will get to reuse our knowledge about Kotlin's language paradigms, syntax, and tooling to build front-end applications for modern browsers.
8
8
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.
10
10
We will go through the usual tasks associated with building a typical, simple React application.
11
11
12
12
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
18
18
### What we are going to build
19
19
20
20
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!
22
22
For anyone needing to still catch up, all the videos are available on YouTube!
23
23
24
24
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!
@@ -75,7 +73,7 @@ As is typical [JavaScript convention](https://faqs.skillcrush.com/article/176-wh
75
73
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:
Copy file name to clipboardExpand all lines: Building Web Applications with React and Kotlin JS/03_A_First_Static_Page.md
+34-31Lines changed: 34 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,18 +5,27 @@ Let's start our story with a classic little *Hello World*-type program!
5
5
Change the code inside `src/main/kotlin/Main.kt` to look like this:
6
6
7
7
```kotlin
8
-
importreact.dom.*
9
8
importkotlinx.browser.document
10
-
importkotlinx.css.*
9
+
importreact.*
10
+
importreact.css.css
11
+
importreact.dom.render
12
+
importcsstype.Position
13
+
importcsstype.px
14
+
importreact.dom.html.ReactHTML.h1
15
+
importreact.dom.html.ReactHTML.h3
16
+
importreact.dom.html.ReactHTML.div
17
+
importreact.dom.html.ReactHTML.p
18
+
importreact.dom.html.ReactHTML.img
11
19
importkotlinx.serialization.Serializable
12
-
importstyled.*
13
20
14
21
funmain() {
15
-
render(document.getElementById("root")) {
22
+
val container = document.getElementById("root") ?: error("Couldn't find root container!")
23
+
24
+
render(Fragment.create {
16
25
h1 {
17
26
+"Hello, React+Kotlin/JS!"
18
27
}
19
-
}
28
+
}, container)
20
29
}
21
30
```
22
31
@@ -26,7 +35,7 @@ If you don't have the continuous Gradle build (as described in Chapter 2) still
26
35
27
36
Congratulations, you've just written your first website using pure Kotlin/JS and React!
28
37
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`.
30
39
This container element is defined in `/src/main/resources/index.html`, which was included in the template.
31
40
32
41
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
Video(4, "Creating Internal DSLs in Kotlin", "Venkat Subramaniam", "https://youtu.be/JzTeAM8N1-o")
151
158
)
152
159
```
153
160
@@ -171,40 +178,38 @@ for(video in watchedVideos) {
171
178
}
172
179
```
173
180
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!)
175
182
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.
176
183
177
184
### Writing typesafe CSS
178
185
179
186
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.
181
188
To fix that, we could include a CSS file in our `index.html` template.
182
189
But we can also use this chance to play with another Kotlin DSL - this time for CSS.
183
190
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.
187
193
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`.
189
195
The relevant block in our build configuration is this:
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.
202
207
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.
204
209
Adjust the code for our mock video player (the last `div` in our snippet) to look like this:
0 commit comments