From 13620b9dd7b0ecaa0b844b52227deb0ef1082b18 Mon Sep 17 00:00:00 2001 From: Philip Wedemann <22521688+hfhbd@users.noreply.github.com> Date: Thu, 14 Oct 2021 11:56:41 +0200 Subject: [PATCH] Align StyleSheetBuilder parameter with other @Composable ContentBuilder and add test (#1112) --- .../compose/web/elements/Elements.kt | 4 +- .../src/jsTest/kotlin/elements/StyleTest.kt | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 web/core/src/jsTest/kotlin/elements/StyleTest.kt diff --git a/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/elements/Elements.kt b/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/elements/Elements.kt index afab3a5d8c..66c3049e08 100644 --- a/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/elements/Elements.kt +++ b/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.() -> Unit)? = null, +inline fun Style( + noinline applyAttrs: (AttrsBuilder.() -> Unit)? = null, rulesBuild: StyleSheetBuilder.() -> Unit ) { val builder = StyleSheetBuilderImpl() diff --git a/web/core/src/jsTest/kotlin/elements/StyleTest.kt b/web/core/src/jsTest/kotlin/elements/StyleTest.kt new file mode 100644 index 0000000000..ad6c499588 --- /dev/null +++ b/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) + } +}