Browse Source

Use standard side effects in Elements.kt (#1793)

pull/1788/head
Shagen Ogandzhanian 3 years ago committed by GitHub
parent
commit
6aaeddc4b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 35
      web/core/src/jsMain/kotlin/org/jetbrains/compose/web/elements/Elements.kt
  2. 9
      web/core/src/jsMain/kotlin/org/jetbrains/compose/web/elements/Style.kt

35
web/core/src/jsMain/kotlin/org/jetbrains/compose/web/elements/Elements.kt

@ -697,15 +697,14 @@ fun TextArea(
this.copyFrom(textAreaAttrsBuilder)
},
content = {
DomSideEffect(keyForRestoringControlledState.value, textAreaRestoreControlledStateEffect)
DisposableEffect(keyForRestoringControlledState.value) {
restoreControlledTextAreaState(element = scopeElement)
onDispose { }
}
}
)
}
private val textAreaRestoreControlledStateEffect: DomEffectScope.(HTMLTextAreaElement) -> Unit = {
restoreControlledTextAreaState(element = it)
}
@Composable
fun Nav(
attrs: AttrBuilderContext<HTMLElement>? = null,
@ -933,12 +932,11 @@ fun Style(
}
},
) {
DomSideEffect(cssRules) { style ->
(style.sheet as? CSSStyleSheet)?.let { cssStylesheet ->
setCSSRules(cssStylesheet, cssRules)
onDispose {
clearCSSRules(cssStylesheet)
}
DisposableEffect(cssRules) {
val cssStylesheet = scopeElement.sheet as? CSSStyleSheet
cssStylesheet?.setCSSRules(cssRules)
onDispose {
cssStylesheet?.clearCSSRules()
}
}
}
@ -959,6 +957,12 @@ inline fun Style(
Style(applyAttrs, builder.cssRules)
}
private fun CSSStyleSheet.clearCSSRules() {
repeat(cssRules.length) {
deleteRule(0)
}
}
/**
* Adds <input> element of [type].
*
@ -1017,15 +1021,14 @@ fun <K> Input(
if (type == InputType.Radio) {
DisposeRadioGroupEffect()
}
DomSideEffect(keyForRestoringControlledState.value, inputRestoreControlledStateEffect)
DisposableEffect(keyForRestoringControlledState.value) {
restoreControlledInputState(inputElement = scopeElement)
onDispose { }
}
}
)
}
private val inputRestoreControlledStateEffect: DomEffectScope.(HTMLInputElement) -> Unit = {
restoreControlledInputState(inputElement = it)
}
@Composable
fun <K> Input(type: InputType<K>) {
Input(type) {}

9
web/core/src/jsMain/kotlin/org/jetbrains/compose/web/elements/Style.kt

@ -7,15 +7,10 @@ import org.w3c.dom.css.CSSStyleRule
import org.jetbrains.compose.web.css.*
import org.w3c.dom.css.CSSStyleSheet
internal fun clearCSSRules(sheet: CSSStyleSheet) {
repeat(sheet.cssRules.length) {
sheet.deleteRule(0)
}
}
internal fun setCSSRules(sheet: CSSStyleSheet, cssRules: CSSRuleDeclarationList) {
internal fun CSSStyleSheet.setCSSRules(cssRules: CSSRuleDeclarationList) {
cssRules.forEach { cssRule ->
sheet.addRule(cssRule)
addRule(cssRule)
}
}

Loading…
Cancel
Save