diff --git a/CHANGELOG.md b/CHANGELOG.md index ed475d6..dd647b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.0.39 +* Add support of `Notifications` + ## 0.0.38 * Add support of `Toggle` diff --git a/src/jsMain/kotlin/dev/inmo/jsuikit/elements/Notification.kt b/src/jsMain/kotlin/dev/inmo/jsuikit/elements/Notification.kt new file mode 100644 index 0000000..6e95fcf --- /dev/null +++ b/src/jsMain/kotlin/dev/inmo/jsuikit/elements/Notification.kt @@ -0,0 +1,15 @@ +package dev.inmo.jsuikit.elements + +import dev.inmo.jsuikit.modifiers.UIKit +import dev.inmo.jsuikit.types.UIKitNotificationParameter +import dev.inmo.jsuikit.types.NotificationsGroup +import dev.inmo.jsuikit.types.invoke +import dev.inmo.jsuikit.utils.Milliseconds + +fun Notification( + message: String, + status: UIKitNotificationParameter.Style? = null, + timeout: Milliseconds? = null, + group: NotificationsGroup? = null, + position: UIKitNotificationParameter.Position? = null +) = UIKit.notification.invoke(message, status, timeout, group, position) diff --git a/src/jsMain/kotlin/dev/inmo/jsuikit/modifiers/UIKit.kt b/src/jsMain/kotlin/dev/inmo/jsuikit/modifiers/UIKit.kt index 96882df..f39df8b 100644 --- a/src/jsMain/kotlin/dev/inmo/jsuikit/modifiers/UIKit.kt +++ b/src/jsMain/kotlin/dev/inmo/jsuikit/modifiers/UIKit.kt @@ -1,4 +1,6 @@ package dev.inmo.jsuikit.modifiers -inline val UIKit - get() = js("UIkit") +import dev.inmo.jsuikit.types.UIKit + +inline val UIKit: UIKit + get() = js("UIkit").unsafeCast() diff --git a/src/jsMain/kotlin/dev/inmo/jsuikit/types/UIKit.kt b/src/jsMain/kotlin/dev/inmo/jsuikit/types/UIKit.kt new file mode 100644 index 0000000..dafeed0 --- /dev/null +++ b/src/jsMain/kotlin/dev/inmo/jsuikit/types/UIKit.kt @@ -0,0 +1,5 @@ +package dev.inmo.jsuikit.types + +external interface UIKit { + val notification: UIKitNotifications +} diff --git a/src/jsMain/kotlin/dev/inmo/jsuikit/types/UIKitNotificationParameter.kt b/src/jsMain/kotlin/dev/inmo/jsuikit/types/UIKitNotificationParameter.kt new file mode 100644 index 0000000..15ab125 --- /dev/null +++ b/src/jsMain/kotlin/dev/inmo/jsuikit/types/UIKitNotificationParameter.kt @@ -0,0 +1,39 @@ +package dev.inmo.jsuikit.types + +sealed class UIKitNotificationParameter { + abstract val parameterName: String + abstract val parameterValue: String + + sealed class Style(override val parameterValue: String) : UIKitNotificationParameter() { + override val parameterName: String + get() = "status" + + object Primary : Style("primary") + object Success : Style("success") + object Warning : Style("warning") + object Danger : Style("danger") + + } + + sealed class Position(override val parameterValue: String) : UIKitNotificationParameter() { + override val parameterName: String + get() = "pos" + + sealed class Top(parameterValue: String) : Position("top-$parameterValue") { + + object Left : Top("left") + object Center : Top("center") + object Right : Top("right") + + } + + sealed class Bottom(parameterValue: String) : Position("bottom-$parameterValue") { + + object Left : Bottom("left") + object Center : Bottom("center") + object Right : Bottom("right") + + } + + } +} diff --git a/src/jsMain/kotlin/dev/inmo/jsuikit/types/UIKitNotifications.kt b/src/jsMain/kotlin/dev/inmo/jsuikit/types/UIKitNotifications.kt new file mode 100644 index 0000000..9aef644 --- /dev/null +++ b/src/jsMain/kotlin/dev/inmo/jsuikit/types/UIKitNotifications.kt @@ -0,0 +1,50 @@ +package dev.inmo.jsuikit.types + +import dev.inmo.jsuikit.utils.Milliseconds +import org.w3c.dom.Element +import kotlin.js.Json +import kotlin.js.json + +typealias NotificationsGroup = String + +external interface UIKitNotifications { + + fun closeAll(group: NotificationsGroup) + + operator fun invoke(message: String, parameters: Json) + + operator fun invoke(element: Element): UIKitNotification? +} + +external interface UIKitNotification { + fun close(immediate: Boolean) +} + +data class UIKitNotificationsParameters( + val status: UIKitNotificationParameter.Style? = null, + val timeout: Milliseconds? = null, + val group: NotificationsGroup? = null, + val position: UIKitNotificationParameter.Position? = null +) { + fun parametersJson() = json( + *listOfNotNull( + status ?.let { it.parameterName to it.parameterValue }, + timeout ?.let { "timeout" to timeout.toString() }, + group ?.let { "group" to it }, + position ?.let { it.parameterName to it.parameterValue }, + ).toTypedArray() + ) +} + +operator fun UIKitNotifications.invoke( + message: String, + parameters: UIKitNotificationsParameters +) = invoke(message, parameters.parametersJson()) + +operator fun UIKitNotifications.invoke( + message: String, + status: UIKitNotificationParameter.Style? = null, + timeout: Milliseconds? = null, + group: NotificationsGroup? = null, + position: UIKitNotificationParameter.Position? = null +) = invoke(message, UIKitNotificationsParameters(status, timeout, group, position))