From 6048760a1a7e3a259784f64323034fbe0d621b5f Mon Sep 17 00:00:00 2001 From: Shagen Ogandzhanian Date: Mon, 7 Jun 2021 18:41:29 +0200 Subject: [PATCH] CSS units API is CSSOM-agnostic and supports homogenous arithmetic operations --- .../kotlin/com/sample/style/Stylesheet.kt | 24 +- .../jsMain/kotlin/com/sample/style/WtCol.kt | 4 +- .../kotlin/com/sample/style/Stylesheet.kt | 28 +- .../jsMain/kotlin/com/sample/style/WtCol.kt | 8 +- .../kotlin/androidx/compose/web/css/CSS.kt | 105 +------ .../androidx/compose/web/css/CSSMediaRule.kt | 8 +- .../androidx/compose/web/css/CSSProperties.kt | 77 ++--- .../androidx/compose/web/css/CSSUnits.kt | 285 ++++++++++++----- .../kotlin/androidx/compose/web/css/Color.kt | 8 +- web/core/src/jsTest/kotlin/CSSUnitApiTests.kt | 291 ++++++++++++++---- 10 files changed, 523 insertions(+), 315 deletions(-) diff --git a/examples/web_landing/src/jsMain/kotlin/com/sample/style/Stylesheet.kt b/examples/web_landing/src/jsMain/kotlin/com/sample/style/Stylesheet.kt index 4acc137b0d..7940b0e2d2 100644 --- a/examples/web_landing/src/jsMain/kotlin/com/sample/style/Stylesheet.kt +++ b/examples/web_landing/src/jsMain/kotlin/com/sample/style/Stylesheet.kt @@ -7,18 +7,18 @@ object AppCSSVariables : CSSVariables { val wtColorGreyLight by variable() val wtColorGreyDark by variable() - val wtOffsetTopUnit by variable() - val wtHorizontalLayoutGutter by variable() - val wtFlowUnit by variable() - - val wtHeroFontSize by variable() - val wtHeroLineHeight by variable() - val wtSubtitle2FontSize by variable() - val wtSubtitle2LineHeight by variable() - val wtH2FontSize by variable() - val wtH2LineHeight by variable() - val wtH3FontSize by variable() - val wtH3LineHeight by variable() + val wtOffsetTopUnit by variable() + val wtHorizontalLayoutGutter by variable() + val wtFlowUnit by variable() + + val wtHeroFontSize by variable() + val wtHeroLineHeight by variable() + val wtSubtitle2FontSize by variable() + val wtSubtitle2LineHeight by variable() + val wtH2FontSize by variable() + val wtH2LineHeight by variable() + val wtH3FontSize by variable() + val wtH3LineHeight by variable() val wtColCount by variable() } diff --git a/examples/web_landing/src/jsMain/kotlin/com/sample/style/WtCol.kt b/examples/web_landing/src/jsMain/kotlin/com/sample/style/WtCol.kt index 7d5fea4aae..df91abb2ec 100644 --- a/examples/web_landing/src/jsMain/kotlin/com/sample/style/WtCol.kt +++ b/examples/web_landing/src/jsMain/kotlin/com/sample/style/WtCol.kt @@ -4,7 +4,7 @@ import org.jetbrains.compose.web.css.* import org.jetbrains.compose.web.css.selectors.CSSSelector fun GenericStyleSheetBuilder.mediaMaxWidth( - value: CSSSizeValue, + value: CSSUnitValue, cssSelector: CSSSelector, rulesBuild: TBuilder.() -> Unit ) { @@ -13,7 +13,7 @@ fun GenericStyleSheetBuilder.mediaMaxWidth( } } -fun CSSBuilder.forMaxWidth(value: CSSSizeValue, builder: CSSBuilder.() -> Unit) { +fun CSSBuilder.forMaxWidth(value: CSSUnitValue, builder: CSSBuilder.() -> Unit) { mediaMaxWidth(value, self, builder) } diff --git a/web/benchmark-core/src/jsMain/kotlin/com/sample/style/Stylesheet.kt b/web/benchmark-core/src/jsMain/kotlin/com/sample/style/Stylesheet.kt index 4b1286c8b3..abfd7baae1 100644 --- a/web/benchmark-core/src/jsMain/kotlin/com/sample/style/Stylesheet.kt +++ b/web/benchmark-core/src/jsMain/kotlin/com/sample/style/Stylesheet.kt @@ -1,28 +1,24 @@ package com.sample.style -import androidx.compose.runtime.Composable import org.jetbrains.compose.web.css.* import org.jetbrains.compose.web.css.selectors.* -import org.jetbrains.compose.web.attributes.* -import org.jetbrains.compose.web.dom.* -import org.jetbrains.compose.web.* object AppCSSVariables : CSSVariables { val wtColorGreyLight by variable() val wtColorGreyDark by variable() - val wtOffsetTopUnit by variable() - val wtHorizontalLayoutGutter by variable() - val wtFlowUnit by variable() - - val wtHeroFontSize by variable() - val wtHeroLineHeight by variable() - val wtSubtitle2FontSize by variable() - val wtSubtitle2LineHeight by variable() - val wtH2FontSize by variable() - val wtH2LineHeight by variable() - val wtH3FontSize by variable() - val wtH3LineHeight by variable() + val wtOffsetTopUnit by variable() + val wtHorizontalLayoutGutter by variable() + val wtFlowUnit by variable() + + val wtHeroFontSize by variable() + val wtHeroLineHeight by variable() + val wtSubtitle2FontSize by variable() + val wtSubtitle2LineHeight by variable() + val wtH2FontSize by variable() + val wtH2LineHeight by variable() + val wtH3FontSize by variable() + val wtH3LineHeight by variable() val wtColCount by variable() } diff --git a/web/benchmark-core/src/jsMain/kotlin/com/sample/style/WtCol.kt b/web/benchmark-core/src/jsMain/kotlin/com/sample/style/WtCol.kt index de2ab412b4..ff2d88504f 100644 --- a/web/benchmark-core/src/jsMain/kotlin/com/sample/style/WtCol.kt +++ b/web/benchmark-core/src/jsMain/kotlin/com/sample/style/WtCol.kt @@ -1,14 +1,10 @@ package com.sample.style -import androidx.compose.runtime.Composable import org.jetbrains.compose.web.css.* import org.jetbrains.compose.web.css.selectors.* -import org.jetbrains.compose.web.attributes.* -import org.jetbrains.compose.web.dom.* -import org.jetbrains.compose.web.* fun GenericStyleSheetBuilder.mediaMaxWidth( - value: CSSSizeValue, + value: CSSUnitValue, cssSelector: CSSSelector, rulesBuild: TBuilder.() -> Unit ) { @@ -17,7 +13,7 @@ fun GenericStyleSheetBuilder.mediaMaxWidth( } } -fun CSSBuilder.forMaxWidth(value: CSSSizeValue, builder: CSSBuilder.() -> Unit) { +fun CSSBuilder.forMaxWidth(value: CSSUnitValue, builder: CSSBuilder.() -> Unit) { mediaMaxWidth(value, self, builder) } diff --git a/web/core/src/jsMain/kotlin/androidx/compose/web/css/CSS.kt b/web/core/src/jsMain/kotlin/androidx/compose/web/css/CSS.kt index 975cd4c5e6..a9e1d4766c 100644 --- a/web/core/src/jsMain/kotlin/androidx/compose/web/css/CSS.kt +++ b/web/core/src/jsMain/kotlin/androidx/compose/web/css/CSS.kt @@ -87,7 +87,7 @@ interface CSSNumberish { } fun CSSNumberish.asNumber() = this.asDynamic() as? Number -fun CSSNumberish.asCSSNumericValue(): CSSNumericValue? = this.asDynamic() as? CSSNumericValueJS +fun CSSNumberish.asCSSNumericValue(): CSSNumericValue? = this.asDynamic() as? CSSNumericValue // declare enum CSSNumericBaseType { // 'length', @@ -124,47 +124,7 @@ val CSSNumericType.percentHint get() = CSSNumericBaseType.valueOf(this.asDynamic().percentHint) // set(value) { this.asDynamic().percentHint = value.value } -external interface CSSNumericValue : CSSStyleValue { - fun add(vararg values: CSSNumberish): CSSNumericValue - fun sub(vararg values: CSSNumberish): CSSNumericValue - fun mul(vararg values: CSSNumberish): CSSNumericValue - fun div(vararg values: CSSNumberish): CSSNumericValue - fun min(vararg values: CSSNumberish): CSSNumericValue - fun max(vararg values: CSSNumberish): CSSNumericValue - - fun to(unit: String): CSSUnitValue - fun toSum(vararg units: String): CSSMathSum - fun type(): CSSNumericType - - @Suppress("NESTED_CLASS_IN_EXTERNAL_INTERFACE") - companion object { - fun parse(cssText: String): CSSNumericValue - } -} - -abstract external class CSSNumericValueJS : CSSNumericValue { - override fun add(vararg values: CSSNumberish): CSSNumericValue - override fun sub(vararg values: CSSNumberish): CSSNumericValue - override fun mul(vararg values: CSSNumberish): CSSNumericValue - override fun div(vararg values: CSSNumberish): CSSNumericValue - override fun min(vararg values: CSSNumberish): CSSNumericValue - override fun max(vararg values: CSSNumberish): CSSNumericValue - - override fun to(unit: String): CSSUnitValue - override fun toSum(vararg units: String): CSSMathSum - override fun type(): CSSNumericType -} - -external interface CSSUnitValue : CSSNumericValue { - val value: Number - val unit: String -} - -@JsName("CSSUnitValue") -external class CSSUnitValueJS(value: Number, unit: String) : CSSNumericValueJS, CSSUnitValue { - override val value: Number - override val unit: String -} +external interface CSSNumericValue : CSSStyleValue // declare enum CSSMathOperator { // 'sum', @@ -185,9 +145,7 @@ enum class CSSMathOperator(val value: String) { clamp("clamp") } -open external class CSSMathValue : CSSNumericValueJS { - // readonly operator: CSSMathOperator -} +open external class CSSMathValue : CSSNumericValue val CSSMathValue.operator get() = CSSMathOperator.valueOf(this.asDynamic().operator) @@ -217,16 +175,6 @@ external class CSSMathMax(vararg args: CSSNumberish) : CSSMathValue { val values: CSSNumericArray } -// TODO(yavanosta) : conflict with base class properties -// Since there is no support for this class in any browser, it's better -// wait for the implementation. -// declare class CSSMathClamp extends CSSMathValue { -// constructor(min: CSSNumberish, val: CSSNumberish, max: CSSNumberish) -// readonly min: CSSNumericValue -// readonly val: CSSNumericValue -// readonly max: CSSNumericValue -// } - external class CSSNumericArray { // TODO: [Symbol.iterator]() : IterableIterator fun forEach(handler: (CSSNumericValue) -> Unit) @@ -340,6 +288,7 @@ external interface StylePropertyValue fun String.asStylePropertyValue() = unsafeCast() fun Number.asStylePropertyValue() = unsafeCast() fun CSSStyleValue.asStylePropertyValue() = unsafeCast() +fun CSSUnitValue.asStylePropertyValue() = asString().asStylePropertyValue() external class StylePropertyMap : StylePropertyMapReadOnly { fun set(property: String, vararg values: StylePropertyValue) @@ -351,51 +300,5 @@ external class StylePropertyMap : StylePropertyMapReadOnly { inline fun Element.computedStyleMap(): StylePropertyMapReadOnly = this.asDynamic().computedStyleMap().unsafeCast() -external class CSS { - companion object { - fun number(value: Number): CSSUnitValue - fun percent(value: Number): CSSpercentValue - - // - fun em(value: Number): CSSemValue - fun ex(value: Number): CSSexValue - fun ch(value: Number): CSSchValue - fun rem(value: Number): CSSremValue - fun vw(value: Number): CSSvwValue - fun vh(value: Number): CSSvhValue - fun vmin(value: Number): CSSvminValue - fun vmax(value: Number): CSSvmaxValue - fun cm(value: Number): CSScmValue - fun mm(value: Number): CSSmmValue - fun Q(value: Number): CSSQValue - - fun pt(value: Number): CSSptValue - fun pc(value: Number): CSSpcValue - fun px(value: Number): CSSpxValue - - // - fun deg(value: Number): CSSdegValue - fun grad(value: Number): CSSgradValue - fun rad(value: Number): CSSradValue - fun turn(value: Number): CSSturnValue - - //