Browse Source

Use CSSStyleSheet so that there's no need to define additional functionality on Stylesheet

pull/807/head
Shagen Ogandzhanian 3 years ago committed by GitHub
parent
commit
019cbd2940
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      web/core/src/jsMain/kotlin/androidx/compose/web/css/BrowserAPI.kt
  2. 31
      web/core/src/jsMain/kotlin/androidx/compose/web/elements/Style.kt

24
web/core/src/jsMain/kotlin/androidx/compose/web/css/BrowserAPI.kt

@ -6,30 +6,6 @@
@file:Suppress("UNUSED", "NOTHING_TO_INLINE", "FunctionName") @file:Suppress("UNUSED", "NOTHING_TO_INLINE", "FunctionName")
package org.jetbrains.compose.web.css package org.jetbrains.compose.web.css
import org.w3c.dom.css.*
import org.w3c.dom.css.StyleSheet
inline val StyleSheet.cssRules
get() = this.asDynamic().cssRules.unsafeCast<CSSRuleList>()
inline fun StyleSheet.deleteRule(index: Int) {
this.asDynamic().deleteRule(index)
}
fun StyleSheet.insertRule(cssRule: String, index: Int? = null): Int {
return if (index != null) {
this.asDynamic().insertRule(cssRule, index).unsafeCast<Int>()
} else {
this.asDynamic().insertRule(cssRule).unsafeCast<Int>()
}
}
inline operator fun CSSRuleList.get(index: Int): CSSRule {
return this.asDynamic()[index].unsafeCast<CSSRule>()
}
@Suppress("NOTHING_TO_INLINE") @Suppress("NOTHING_TO_INLINE")
inline fun <T : Any> jsObject(): T = inline fun <T : Any> jsObject(): T =
js("({})") js("({})")

31
web/core/src/jsMain/kotlin/androidx/compose/web/elements/Style.kt

@ -9,6 +9,7 @@ import org.w3c.dom.css.CSSStyleDeclaration
import org.w3c.dom.css.CSSStyleRule import org.w3c.dom.css.CSSStyleRule
import org.w3c.dom.css.StyleSheet import org.w3c.dom.css.StyleSheet
import org.jetbrains.compose.web.css.* import org.jetbrains.compose.web.css.*
import org.w3c.dom.css.CSSStyleSheet
/** /**
* Use this function to mount the <style> tag into the DOM tree. * Use this function to mount the <style> tag into the DOM tree.
@ -43,47 +44,49 @@ inline fun Style(
}, },
) { ) {
DomSideEffect(cssRules) { style -> DomSideEffect(cssRules) { style ->
style.sheet?.let { sheet -> (style.sheet as? CSSStyleSheet)?.let { cssStylesheet ->
setCSSRules(sheet, cssRules) setCSSRules(cssStylesheet, cssRules)
onDispose { onDispose {
clearCSSRules(sheet) clearCSSRules(cssStylesheet)
} }
} }
} }
} }
} }
fun clearCSSRules(sheet: StyleSheet) { fun clearCSSRules(sheet: CSSStyleSheet) {
repeat(sheet.cssRules.length) { repeat(sheet.cssRules.length) {
sheet.deleteRule(0) sheet.deleteRule(0)
} }
} }
fun setCSSRules(sheet: StyleSheet, cssRules: CSSRuleDeclarationList) { fun setCSSRules(sheet: CSSStyleSheet, cssRules: CSSRuleDeclarationList) {
cssRules.forEach { cssRule -> cssRules.forEach { cssRule ->
sheet.addRule(cssRule) sheet.addRule(cssRule)
} }
} }
private fun StyleSheet.addRule(cssRule: String): CSSRule { private fun CSSStyleSheet.addRule(cssRule: String): CSSRule? {
val cssRuleIndex = this.insertRule(cssRule, this.cssRules.length) val cssRuleIndex = this.insertRule(cssRule, this.cssRules.length)
return this.cssRules[cssRuleIndex] return this.cssRules.item(cssRuleIndex)
} }
private fun CSSGroupingRule.addRule(cssRule: String): CSSRule { private fun CSSStyleSheet.addRule(cssRuleDeclaration: CSSRuleDeclaration) {
val cssRuleIndex = this.insertRule(cssRule, this.cssRules.length) addRule("${cssRuleDeclaration.header} {}")?.let { cssRule ->
return this.cssRules[cssRuleIndex] fillRule(cssRuleDeclaration, cssRule)
}
} }
private fun StyleSheet.addRule(cssRuleDeclaration: CSSRuleDeclaration) { private fun CSSGroupingRule.addRule(cssRule: String): CSSRule? {
val cssRule = addRule("${cssRuleDeclaration.header} {}") val cssRuleIndex = this.insertRule(cssRule, this.cssRules.length)
fillRule(cssRuleDeclaration, cssRule) return this.cssRules.item(cssRuleIndex)
} }
private fun CSSGroupingRule.addRule(cssRuleDeclaration: CSSRuleDeclaration) { private fun CSSGroupingRule.addRule(cssRuleDeclaration: CSSRuleDeclaration) {
val cssRule = addRule("${cssRuleDeclaration.header} {}") addRule("${cssRuleDeclaration.header} {}")?.let { cssRule ->
fillRule(cssRuleDeclaration, cssRule) fillRule(cssRuleDeclaration, cssRule)
} }
}
private fun fillRule( private fun fillRule(
cssRuleDeclaration: CSSRuleDeclaration, cssRuleDeclaration: CSSRuleDeclaration,

Loading…
Cancel
Save