Skip to content

Commit 0224d46

Browse files
committed
Implement theme switching
1 parent 48cd4cc commit 0224d46

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

src/jsMain/kotlin/Application.kt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,50 @@ import components.Layout
66
import components.PageContent
77
import components.PageFooter
88
import components.PageHeader
9-
import org.jetbrains.compose.web.css.*
9+
import org.jetbrains.compose.web.css.Color
10+
import org.jetbrains.compose.web.css.Style
1011
import org.jetbrains.compose.web.dom.*
1112
import org.jetbrains.compose.web.renderComposableInBody
1213

14+
enum class Theme { Dark, Light }
15+
1316
fun main() {
1417
renderComposableInBody {
18+
var theme by remember { mutableStateOf(Theme.Light) }
1519
Style {
20+
root {
21+
when (theme) {
22+
Theme.Light -> {
23+
ThemeVariables.mainColor(Color("#333"))
24+
ThemeVariables.accentColor(Color("#6200ea"))
25+
ThemeVariables.backgroundColor(Color("#f0f0f0"))
26+
}
27+
Theme.Dark -> {
28+
ThemeVariables.mainColor(Color("#f0f0f0"))
29+
ThemeVariables.accentColor(Color("#6200ea"))
30+
ThemeVariables.backgroundColor(Color("#333"))
31+
}
32+
}
33+
}
1634
}
1735

1836
Style(BaseStyles)
1937

2038
Layout {
2139
PageHeader {
2240
H1 { Text("My delicious site") }
41+
Div {
42+
Button(attrs = {
43+
onClick {
44+
theme = when (theme) {
45+
Theme.Light -> Theme.Dark
46+
Theme.Dark -> Theme.Light
47+
}
48+
}
49+
}) {
50+
Text("Switch theme")
51+
}
52+
}
2353
}
2454
PageContent {
2555
var counter: Int by remember { mutableStateOf(0) }

src/jsMain/kotlin/Styles.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ object ThemeVariables {
88

99
object BaseStyles : StyleSheet() {
1010
init {
11-
root {
12-
ThemeVariables.mainColor(Color("#333"))
13-
ThemeVariables.accentColor(Color("#6200ea"))
14-
ThemeVariables.backgroundColor(Color("#f0f0f0"))
15-
}
16-
1711
"#root" style {
1812
display(DisplayStyle.Flex)
1913
flexDirection(FlexDirection.Column)
@@ -28,8 +22,9 @@ object BaseStyles : StyleSheet() {
2822
flex(0, 0, 60.px)
2923
display(DisplayStyle.Flex)
3024
alignItems(AlignItems.Center)
31-
justifyContent(JustifyContent.Center)
25+
justifyContent(JustifyContent.SpaceBetween)
3226
fontSize(1.5.cssRem)
27+
padding(10.px)
3328
}
3429

3530
"main" style {

0 commit comments

Comments
 (0)