|
|
@ -11,9 +11,10 @@ import androidx.compose.runtime.SideEffect |
|
|
|
import androidx.compose.runtime.currentComposer |
|
|
|
import androidx.compose.runtime.currentComposer |
|
|
|
import androidx.compose.runtime.remember |
|
|
|
import androidx.compose.runtime.remember |
|
|
|
import org.w3c.dom.Element |
|
|
|
import org.w3c.dom.Element |
|
|
|
import org.w3c.dom.HTMLElement |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface DOMScope<out TElement : Element> |
|
|
|
interface DOMScope<out TElement : Element> { |
|
|
|
|
|
|
|
val DisposableEffectScope.scopeElement: TElement |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* ElementScope allows adding effects to the Composable representing html element. |
|
|
|
* ElementScope allows adding effects to the Composable representing html element. |
|
|
@ -35,9 +36,11 @@ interface ElementScope<out TElement : Element> : DOMScope<TElement> { |
|
|
|
* and must be reversed or cleaned up if [key] changes or if the DisposableRefEffect leaves the composition. |
|
|
|
* and must be reversed or cleaned up if [key] changes or if the DisposableRefEffect leaves the composition. |
|
|
|
* [effect] lambda provides a reference to a native element represented by Composable. |
|
|
|
* [effect] lambda provides a reference to a native element represented by Composable. |
|
|
|
* Adding [DisposableEffectScope.onDispose] to [effect] is mandatory. |
|
|
|
* Adding [DisposableEffectScope.onDispose] to [effect] is mandatory. |
|
|
|
|
|
|
|
* DisposableRefEffect is deprecated, use regular DisposableEffect instead and access element via [DOMScope.scopeElement] if needed |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Composable |
|
|
|
@Composable |
|
|
|
@NonRestartableComposable |
|
|
|
@NonRestartableComposable |
|
|
|
|
|
|
|
@Deprecated("DisposableRefEffect is deprecated, use regular DisposableEffect instead and access element via scopeElement() if needed") |
|
|
|
fun DisposableRefEffect( |
|
|
|
fun DisposableRefEffect( |
|
|
|
key: Any?, |
|
|
|
key: Any?, |
|
|
|
effect: DisposableEffectScope.(TElement) -> DisposableEffectResult |
|
|
|
effect: DisposableEffectScope.(TElement) -> DisposableEffectResult |
|
|
@ -48,9 +51,11 @@ interface ElementScope<out TElement : Element> : DOMScope<TElement> { |
|
|
|
* and must be reversed or cleaned up if element or the DisposableRefEffect leaves the composition. |
|
|
|
* and must be reversed or cleaned up if element or the DisposableRefEffect leaves the composition. |
|
|
|
* [effect] lambda provides a reference to a native element represented by Composable. |
|
|
|
* [effect] lambda provides a reference to a native element represented by Composable. |
|
|
|
* Adding [DisposableEffectScope.onDispose] to [effect] is mandatory. |
|
|
|
* Adding [DisposableEffectScope.onDispose] to [effect] is mandatory. |
|
|
|
|
|
|
|
* DisposableRefEffect is deprecated, use regular DisposableEffect instead and access element via [DOMScope.scopeElement] if needed |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Composable |
|
|
|
@Composable |
|
|
|
@NonRestartableComposable |
|
|
|
@NonRestartableComposable |
|
|
|
|
|
|
|
@Deprecated("DisposableRefEffect is deprecated, use regular DisposableEffect instead and access element via scopeElement() if needed") |
|
|
|
fun DisposableRefEffect( |
|
|
|
fun DisposableRefEffect( |
|
|
|
effect: DisposableEffectScope.(TElement) -> DisposableEffectResult |
|
|
|
effect: DisposableEffectScope.(TElement) -> DisposableEffectResult |
|
|
|
) { |
|
|
|
) { |
|
|
@ -61,25 +66,28 @@ interface ElementScope<out TElement : Element> : DOMScope<TElement> { |
|
|
|
* A side effect of composition that runs on every successful recomposition if [key] changes. |
|
|
|
* A side effect of composition that runs on every successful recomposition if [key] changes. |
|
|
|
* Also see [SideEffect]. |
|
|
|
* Also see [SideEffect]. |
|
|
|
* Same as other effects in [ElementScope], it provides a reference to a native element in [effect] lambda. |
|
|
|
* Same as other effects in [ElementScope], it provides a reference to a native element in [effect] lambda. |
|
|
|
|
|
|
|
* DomSideEffect is deprecated, use [SideEffect] instead. If, for some reason, you need to access the scope element, use DisposableEffect and call [DOMScope.scopeElement] |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Composable |
|
|
|
@Composable |
|
|
|
@NonRestartableComposable |
|
|
|
@NonRestartableComposable |
|
|
|
|
|
|
|
@Deprecated("DomSideEffect is deprecated, use SideEffect instead. If, for some reason, you need to access the scope element, use DisposableEffect") |
|
|
|
fun DomSideEffect(key: Any?, effect: DomEffectScope.(TElement) -> Unit) |
|
|
|
fun DomSideEffect(key: Any?, effect: DomEffectScope.(TElement) -> Unit) |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* A side effect of composition that runs on every successful recomposition. |
|
|
|
* A side effect of composition that runs on every successful recomposition. |
|
|
|
* Also see [SideEffect]. |
|
|
|
* Also see [SideEffect]. |
|
|
|
* Same as other effects in [ElementScope], it provides a reference to a native element in [effect] lambda. |
|
|
|
* Same as other effects in [ElementScope], it provides a reference to a native element in [effect] lambda. |
|
|
|
|
|
|
|
* DomSideEffect is deprecated, use [SideEffect] instead. If, for some reason, you need to access the scope element, use DisposableEffect and call [DOMScope.scopeElement] |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Composable |
|
|
|
@Composable |
|
|
|
@NonRestartableComposable |
|
|
|
@NonRestartableComposable |
|
|
|
|
|
|
|
@Deprecated("DomSideEffect is deprecated, use SideEffect instead. If, for some reason, you need to access the scope element, use DisposableEffect") |
|
|
|
fun DomSideEffect(effect: DomEffectScope.(TElement) -> Unit) |
|
|
|
fun DomSideEffect(effect: DomEffectScope.(TElement) -> Unit) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
abstract class ElementScopeBase<out TElement : Element> : ElementScope<TElement> { |
|
|
|
abstract class ElementScopeBase<out TElement : Element> : ElementScope<TElement> { |
|
|
|
abstract val element: TElement |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private var nextDisposableDomEffectKey = 0 |
|
|
|
private var nextDisposableDomEffectKey = 0 |
|
|
|
|
|
|
|
abstract val element: TElement |
|
|
|
|
|
|
|
|
|
|
|
@Composable |
|
|
|
@Composable |
|
|
|
@NonRestartableComposable |
|
|
|
@NonRestartableComposable |
|
|
@ -114,7 +122,10 @@ abstract class ElementScopeBase<out TElement : Element> : ElementScope<TElement> |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
internal open class ElementScopeImpl<TElement : Element> : ElementScopeBase<TElement>() { |
|
|
|
internal open class ElementScopeImpl<TElement : Element> : ElementScopeBase<TElement>() { |
|
|
|
public override lateinit var element: TElement |
|
|
|
override lateinit var element: TElement |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override val DisposableEffectScope.scopeElement: TElement |
|
|
|
|
|
|
|
get() = element |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
interface DomEffectScope { |
|
|
|
interface DomEffectScope { |
|
|
|