Browse Source

Update compose-web examples and readme about effects (#2395)

Co-authored-by: Oleksandr Karpovich <oleksandr.karpovich@jetbrains.com>
pull/2396/head
Oleksandr Karpovich 2 years ago committed by GitHub
parent
commit
5936fd8063
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      examples/web-landing/src/jsMain/kotlin/com/sample/content/CodeSnippets.kt
  2. 16
      examples/web-with-react/src/jsMain/kotlin/ReactInComposeApp.kt
  3. 20
      tutorials/Web/Using_Effects/README.md

11
examples/web-landing/src/jsMain/kotlin/com/sample/content/CodeSnippets.kt

@ -1,9 +1,6 @@
package com.sample.content package com.sample.content
import androidx.compose.runtime.Composable import androidx.compose.runtime.*
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.runtime.mutableStateOf
import org.jetbrains.compose.web.css.* import org.jetbrains.compose.web.css.*
import org.jetbrains.compose.web.dom.* import org.jetbrains.compose.web.dom.*
import com.sample.HighlightJs import com.sample.HighlightJs
@ -263,9 +260,9 @@ fun FormattedCodeSnippet(code: String, language: String = "kotlin") {
backgroundColor(Color("transparent")) backgroundColor(Color("transparent"))
} }
}) { }) {
@Suppress("DEPRECATION") DisposableEffect(code) {
DomSideEffect(code) { scopeElement.setHighlightedCode(code)
it.setHighlightedCode(code) onDispose { }
} }
} }
} }

16
examples/web-with-react/src/jsMain/kotlin/ReactInComposeApp.kt

@ -1,7 +1,4 @@
import androidx.compose.runtime.Composable import androidx.compose.runtime.*
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import org.jetbrains.compose.web.css.margin import org.jetbrains.compose.web.css.margin
import org.jetbrains.compose.web.css.percent import org.jetbrains.compose.web.css.percent
import org.jetbrains.compose.web.css.px import org.jetbrains.compose.web.css.px
@ -23,15 +20,16 @@ private fun ElementScope<HTMLElement>.UseReactEffect(
key: Any?, key: Any?,
content: RBuilder.() -> Unit content: RBuilder.() -> Unit
) { ) {
DomSideEffect(key = key) { htmlElement -> DisposableEffect(key) {
render(htmlElement) { render(scopeElement) {
content() content()
} }
onDispose { }
} }
DisposableRefEffect { htmlElement -> DisposableEffect(Unit) {
onDispose { onDispose {
unmountComponentAtNode(htmlElement) unmountComponentAtNode(scopeElement)
} }
} }
} }
@ -91,4 +89,4 @@ fun reactInComposeAppExample() {
YoutubeReactPlayerWrapper(videoUrl) YoutubeReactPlayerWrapper(videoUrl)
} }
} }
} }

20
tutorials/Web/Using_Effects/README.md

@ -28,8 +28,18 @@ Only one `ref` can be used per element. Calling it more than once will dismiss e
For example, `ref` can be used to add and remove some event listeners not provided by compose-web from the box. For example, `ref` can be used to add and remove some event listeners not provided by compose-web from the box.
### DisposableRefEffect ### DisposableRefEffect (deprecated)
**Deprecated:**
Consider using `DisposableEffect`. Its scope provides `scopeElement` - a reference to the underlying HTML element:
``` kotlin
DisposableEffect(key) {
scopeElement.innerText = key
onDispose { scopeElement.innerText = "" }
}
```
---
Under the hood, `DisposableRefEffect` uses [DisposableEffect](https://developer.android.com/jetpack/compose/side-effects#disposableeffect) Under the hood, `DisposableRefEffect` uses [DisposableEffect](https://developer.android.com/jetpack/compose/side-effects#disposableeffect)
`DisposableRefEffect` is similar to `ref`, since it also provides a reference to an element. At the same time it has few differences. `DisposableRefEffect` is similar to `ref`, since it also provides a reference to an element. At the same time it has few differences.
@ -65,8 +75,14 @@ Div {
} }
``` ```
### DomSideEffect ### DomSideEffect (deprecated)
**Deprecated:**
Consider using [SideEffect](https://developer.android.com/jetpack/compose/side-effects#sideeffect-publish).
If a reference to an underlying HTML element is needed, consider using `DisposableEffect` and `scopeElement` within its scope.
---
Under the hood, `DomSideEffect` uses [SideEffect](https://developer.android.com/jetpack/compose/side-effects#sideeffect-publish) Under the hood, `DomSideEffect` uses [SideEffect](https://developer.android.com/jetpack/compose/side-effects#sideeffect-publish)
`DomSideEffect` as well as `DisposableRefEffect` can be used with a key and without it. `DomSideEffect` as well as `DisposableRefEffect` can be used with a key and without it.

Loading…
Cancel
Save