2022-01-12 10:27:49 +00:00
|
|
|
package dev.inmo.jsuikit.elements
|
2021-12-22 08:38:12 +00:00
|
|
|
|
|
|
|
import androidx.compose.runtime.Composable
|
|
|
|
import androidx.compose.runtime.DisposableEffectResult
|
2022-01-12 13:58:52 +00:00
|
|
|
import dev.inmo.jsuikit.modifiers.UIKitModifier
|
|
|
|
import dev.inmo.jsuikit.modifiers.include
|
2021-12-22 08:38:12 +00:00
|
|
|
import org.jetbrains.compose.web.dom.*
|
2022-01-13 05:55:08 +00:00
|
|
|
import org.jetbrains.compose.web.dom.Text
|
|
|
|
import org.w3c.dom.*
|
2021-12-22 08:38:12 +00:00
|
|
|
import org.w3c.dom.events.Event
|
|
|
|
import kotlin.random.Random
|
|
|
|
import kotlin.random.nextUInt
|
|
|
|
|
|
|
|
private class DialogDisposableEffectResult(
|
|
|
|
private val element: HTMLElement,
|
|
|
|
private val onDispose: (() -> Unit)?,
|
|
|
|
private val onDisposed: (() -> Unit)?
|
|
|
|
) : DisposableEffectResult {
|
|
|
|
override fun dispose() {
|
|
|
|
onDispose?.invoke()
|
|
|
|
js("UIkit").modal("#${element.id}") ?.hide()
|
|
|
|
onDisposed?.invoke()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
fun Dialog(
|
|
|
|
title: String? = null,
|
2022-01-13 05:55:08 +00:00
|
|
|
modifiers: Array<UIKitModifier> = emptyArray(),
|
2021-12-22 08:38:12 +00:00
|
|
|
hide: (() -> Unit)? = null,
|
|
|
|
hidden: (() -> Unit)? = null,
|
|
|
|
footerBuilder: (@Composable () -> Unit)? = null,
|
2022-01-13 05:55:08 +00:00
|
|
|
attributesCustomizer: AttrBuilderContext<HTMLDivElement> = {},
|
2021-12-22 08:38:12 +00:00
|
|
|
bodyBuilder: @Composable () -> Unit
|
|
|
|
) {
|
|
|
|
Div(
|
|
|
|
{
|
|
|
|
attr("uk-modal", "")
|
|
|
|
classes("uk-flex-top", "uk-modal")
|
|
|
|
id("dialog${Random.nextUInt()}")
|
|
|
|
include(*modifiers)
|
2022-01-13 05:55:08 +00:00
|
|
|
attributesCustomizer()
|
2021-12-22 08:38:12 +00:00
|
|
|
}
|
|
|
|
) {
|
|
|
|
Div(
|
|
|
|
{
|
|
|
|
classes("uk-modal-dialog", "uk-margin-auto-vertical")
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
title ?.let {
|
|
|
|
Div(
|
|
|
|
{
|
|
|
|
classes("uk-modal-header")
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
H2({ classes("uk-modal-title") }) {
|
|
|
|
Text(title)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Div(
|
|
|
|
{
|
|
|
|
classes("uk-modal-body")
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
bodyBuilder()
|
|
|
|
}
|
|
|
|
footerBuilder ?.let {
|
|
|
|
Div(
|
|
|
|
{
|
|
|
|
classes("uk-modal-footer", "uk-text-right")
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
footerBuilder()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DisposableRefEffect {
|
|
|
|
DialogDisposableEffectResult(it, hide, hidden)
|
|
|
|
}
|
|
|
|
|
|
|
|
DomSideEffect { htmlElement ->
|
|
|
|
var wrapper: (Event) -> Unit = {}
|
|
|
|
wrapper = { it: Event ->
|
|
|
|
htmlElement.removeEventListener("hidden", wrapper)
|
|
|
|
htmlElement.remove()
|
|
|
|
hidden ?.invoke()
|
|
|
|
}
|
|
|
|
htmlElement.addEventListener("hidden", wrapper)
|
|
|
|
|
|
|
|
val dialog = js("UIkit").modal("#${htmlElement.id}")
|
|
|
|
dialog.show()
|
|
|
|
Unit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|