Browse Source

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

pull/807/head
Shagen Ogandzhanian 4 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. 33
      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")
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")
inline fun <T : Any> jsObject(): T =
js("({})")

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

Loading…
Cancel
Save