diff --git a/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/CSSMediaRule.kt b/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/CSSMediaRule.kt index 02d5ef1af0..ee038eec93 100644 --- a/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/CSSMediaRule.kt +++ b/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/CSSMediaRule.kt @@ -128,14 +128,33 @@ fun GenericStyleSheetBuilder.not( query: CSSMediaQuery.Invertible ) = CSSMediaQuery.Not(query) +/** + * A mediaQuery selector + * + * Example: + * ``` + * object CombinedMediaQueries : StyleSheet() { + * media(mediaMinWidth(200.px).and(mediaMaxWidth(400.px))) { ... } + * } + * ``` + */ fun GenericStyleSheetBuilder.mediaMinWidth(value: CSSUnitValue) = CSSMediaQuery.MediaFeature("min-width", value) +/** + * See [mediaMinWidth] + */ fun GenericStyleSheetBuilder.mediaMaxWidth(value: CSSUnitValue) = CSSMediaQuery.MediaFeature("max-width", value) +/** + * See [mediaMinWidth] + */ fun GenericStyleSheetBuilder.mediaMinHeight(value: CSSUnitValue) = CSSMediaQuery.MediaFeature("min-height", value) +/** + * See [mediaMinWidth] + */ fun GenericStyleSheetBuilder.madiaMaxHeight(value: CSSUnitValue) = CSSMediaQuery.MediaFeature("max-height", value) diff --git a/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/StyleBuilder.kt b/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/StyleBuilder.kt index 43466c4f33..5d333ec1a2 100644 --- a/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/StyleBuilder.kt +++ b/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/StyleBuilder.kt @@ -18,7 +18,7 @@ interface StyleBuilder { fun variable(variableName: String, value: String) = variable(variableName, StylePropertyValue(value)) fun variable(variableName: String, value: Number) = variable(variableName, StylePropertyValue(value)) - operator fun CSSStyleVariable.invoke(value: TValue) { + operator fun CSSStyleVariable.invoke(value: TValue) { variable(name, value.toString()) } @@ -34,7 +34,7 @@ interface StyleBuilder { inline fun variableValue(variableName: String, fallback: StylePropertyValue? = null) = "var(--$variableName${fallback?.let { ", $it" } ?: ""})" -external interface CSSVariableValueAs +external interface CSSVariableValueAs inline fun CSSVariableValue(value: StylePropertyValue) = value.unsafeCast() @@ -56,9 +56,9 @@ interface CSSVariable { val name: String } -class CSSStyleVariable(override val name: String) : CSSVariable +class CSSStyleVariable(override val name: String) : CSSVariable -fun CSSStyleVariable.value(fallback: TValue? = null) = +fun CSSStyleVariable.value(fallback: TValue? = null) = CSSVariableValue( variableValue( name, @@ -67,9 +67,8 @@ fun CSSStyleVariable.value(fallback: TValue ) fun CSSStyleVariable.value(fallback: TValue? = null) - where TValue : CSSVariableValueAs, - TValue: StylePropertyValue - = + where TValue : CSSVariableValueAs, + TValue : StylePropertyValue = CSSVariableValue( variableValue( name, @@ -77,7 +76,26 @@ fun CSSStyleVariable.value(fallback: TValue? = null) ) ) -fun variable() = +/** + * Introduces CSS variable that can be later referred anywhere in [StyleSheet] + * + * Example: + * ``` + * object AppCSSVariables { + * val width by variable() + * val stringHeight by variable() + * val order by variable() + * } + * + * object AppStylesheet : StyleSheet() { + * val classWithProperties by style { + * AppCSSVariables.width(100.px) + * property("width", AppCSSVariables.width.value()) + * } + *``` + * + */ +fun variable() = ReadOnlyProperty> { _, property -> CSSStyleVariable(property.name) } @@ -104,7 +122,7 @@ open class StyleBuilderImpl : StyleBuilder, StyleHolder { override fun equals(other: Any?): Boolean { return if (other is StyleHolder) { properties.nativeEquals(other.properties) && - variables.nativeEquals(other.variables) + variables.nativeEquals(other.variables) } else false } @@ -131,6 +149,6 @@ fun StylePropertyList.nativeEquals(properties: StylePropertyList): Boolean { return all { prop -> val otherProp = properties[index++] prop.name == otherProp.name && - prop.value.toString() == otherProp.value.toString() + prop.value.toString() == otherProp.value.toString() } } diff --git a/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/StyleSheet.kt b/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/StyleSheet.kt index 2cce399fff..80a90a51c8 100644 --- a/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/StyleSheet.kt +++ b/web/core/src/jsMain/kotlin/org/jetbrains/compose/web/css/StyleSheet.kt @@ -47,6 +47,30 @@ open class StyleSheet( protected fun style(cssRule: CSSBuilder.() -> Unit) = CSSHolder(usePrefix, cssRule) + /** + * Example: + * ``` + * object AppStyleSheet : StyleSheet() { + * val bounce by keyframes { + * from { + * property("transform", "translateX(50%)") + * } + * + * to { + * property("transform", "translateX(-50%)") + * } + * } + * + * val myClass by style { + * animation(bounce) { + * duration(2.s) + * timingFunction(AnimationTimingFunction.EaseIn) + * direction(AnimationDirection.Alternate) + * } + * } + * } + * ``` + */ protected fun keyframes(cssKeyframes: CSSKeyframesBuilder.() -> Unit) = CSSKeyframesHolder(usePrefix, cssKeyframes) companion object { @@ -67,8 +91,8 @@ open class StyleSheet( val (style, newCssRules) = buildCSS(selfSelector, selfSelector, cssBuild) val cssRule = cssRules.find { it is CSSStyleRuleDeclaration && - it.selector is CSSSelector.CSSClass && it.style == style && - (boundClasses[it.selector.className] ?: emptyList()) == newCssRules + it.selector is CSSSelector.CSSClass && it.style == style && + (boundClasses[it.selector.className] ?: emptyList()) == newCssRules }.unsafeCast() return if (cssRule != null) { cssRule.selector.unsafeCast().className @@ -99,7 +123,13 @@ open class StyleSheet( } } - protected class CSSKeyframesHolder(private val usePrefix: Boolean, private val keyframesBuilder: CSSKeyframesBuilder.() -> Unit) { + /** + * See [keyframes] + */ + protected class CSSKeyframesHolder( + private val usePrefix: Boolean, + private val keyframesBuilder: CSSKeyframesBuilder.() -> Unit + ) { operator fun provideDelegate( sheet: StyleSheet, property: KProperty<*>