Browse Source

Align StyleSheetBuilder parameter with other @Composable ContentBuilder and add test (#1112)

pull/1274/head
Philip Wedemann 3 years ago committed by GitHub
parent
commit
13620b9dd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      web/core/src/jsMain/kotlin/org/jetbrains/compose/web/elements/Elements.kt
  2. 38
      web/core/src/jsTest/kotlin/elements/StyleTest.kt

4
web/core/src/jsMain/kotlin/org/jetbrains/compose/web/elements/Elements.kt

@ -943,8 +943,8 @@ fun Style(
* @param rulesBuild allows to define the style rules using [StyleSheetBuilder]
*/
@Composable
fun Style(
applyAttrs: (AttrsBuilder<HTMLStyleElement>.() -> Unit)? = null,
inline fun Style(
noinline applyAttrs: (AttrsBuilder<HTMLStyleElement>.() -> Unit)? = null,
rulesBuild: StyleSheetBuilder.() -> Unit
) {
val builder = StyleSheetBuilderImpl()

38
web/core/src/jsTest/kotlin/elements/StyleTest.kt

@ -0,0 +1,38 @@
/*
* Copyright 2020-2021 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
*/
package org.jetbrains.compose.web.core.tests.elements
import androidx.compose.runtime.*
import org.jetbrains.compose.web.css.*
import org.jetbrains.compose.web.dom.*
import org.jetbrains.compose.web.testutils.*
import org.w3c.dom.*
import org.w3c.dom.css.*
import kotlin.test.*
class StyleTest {
@Test
fun testingComposableStyle() = runTest {
var color by mutableStateOf(Color.green)
composition {
Style {
val element = remember { "body" }
element {
backgroundColor(color)
}
}
}
val element = root.firstChild
assertTrue(element is HTMLStyleElement)
val sheet = element.sheet
assertTrue(sheet is CSSStyleSheet)
assertEquals("""body { background-color: green; }""", sheet.cssRules.asList().single().cssText)
color = Color.red
waitForRecompositionComplete()
assertEquals("""body { background-color: red; }""", sheet.cssRules.asList().single().cssText)
}
}
Loading…
Cancel
Save