Overlays
Modal dialogs, toast notifications, popup menus, and context menus.
Kraft provides three overlay systems that share the same pattern: a manager you access from any component, a
stage that renders the overlays, and handles for controlling individual overlays. All three are registered
automatically when you call semanticUI() in your app setup.
val app = kraftApp {
semanticUI() // registers modals, toasts, and popups with SemanticUI styling
} Each system is accessible from any component via a delegated property:
class MyComponent(ctx: Ctx<Props>) : Component<Props>(ctx) {
// modals, toasts, and popups are available on every component, e.g. in an event handler
private fun onAction(event: MouseEvent) {
modals.show<Unit> { handle -> /* ... */ }
toasts.info("Saved")
popups.showContextMenu(event) { handle -> /* ... */ }
}
} Modals
Show a modal by calling modals.show with a render function. You receive a typed
Handle<RESULT>: resolve(result) decides the answer, close() removes
the modal. Every modal answers exactly once, after it is gone: the decided result, or null when it
was dismissed without one. RESULT is inferred when the content takes a typed handle, like
OkCancelModal; for your own modal class, spell it out: modals.show<Unit> { }.
modals.show<Boolean> { handle ->
div {
h2 { +"Confirm deletion" }
ui.red.button {
onClick {
handle.resolve(true)
handle.close()
}
+"Delete"
}
ui.button {
onClick { handle.close() } // no resolve: answers null
+"Cancel"
}
}
}.onResult { confirmed ->
if (confirmed == true) performDelete()
}.onClose {
// runs after onResult, however the modal went away
} Never register handlers inside the render function: it runs on every render of the modal
stage. Register onResult and onClose on the handle that show returns, or
once in a component's init.
The first resolve wins. modals.closeAll() closes every modal; each one answers what it
resolved, else null. In a coroutine, showAndAwait returns the answer directly;
cancelling the coroutine closes the modal:
launch {
val result = modals.showAndAwait { handle ->
OkCancelModal { mini(handle = handle, header = { +"Delete?" }, content = {}) }
}
if (result == OkCancelModal.Result.Ok) performDelete()
} OkCancelModal
For the common confirm/cancel pattern, use the built-in OkCancelModal. It answers
Ok, Cancel, or null when dismissed (backdrop, back navigation,
closeAll()). Its buttons are real <button>s and Cancel has the focus, so Enter
never confirms by accident. It comes in three sizes:
modals.show { handle ->
OkCancelModal {
mini(
handle = handle,
header = { ui.header { +"Are you sure?" } },
content = { +"This action cannot be undone." },
okText = { +"Delete" },
cancelText = { +"Cancel" },
) { result ->
when (result) {
OkCancelModal.Result.Ok -> performDelete()
OkCancelModal.Result.Cancel, null -> { /* nothing */ }
}
}
}
}
// Also available inside OkCancelModal { }: tiny(...), small(...) Treat null like Cancel: if (result == Cancel) ... else delete()
compiles, and deletes on a dismissal.
Your own modal
Extend FadingModal<Props, RESULT> for a Semantic UI modal with fade transitions:
close(result) answers and closes, close() dismisses. The backdrop and back navigation
dismiss too.
class RenameModal(ctx: Ctx<Props>) : FadingModal<RenameModal.Props, String>(ctx) {
class Props(override val handle: ModalsManager.Handle<String>) : FadingModal.Props<String>()
private var name by value("")
override fun FlowContent.renderContent() {
ui.mini.modal.transition.visible.active.front {
ui.content { UiInputField(name, { name = it }) }
noui.actions {
ui.button Button { onClick { close() }; +"Cancel" }
ui.primary.button Button { onClick { close(name) }; +"Rename" }
}
}
}
} Toasts
Toast notifications appear in the top-right corner and auto-dismiss after a configurable duration (default: 7 seconds). Three convenience methods cover the common types:
toasts.info("Changes saved")
toasts.warning("Session expires in 5 minutes")
toasts.error("Failed to save — check your connection") Control the duration per toast, or pass null to keep it visible until clicked:
import kotlin.time.Duration.Companion.seconds
toasts.info("Quick note", duration = 3.seconds)
toasts.error("Read this carefully", duration = null) // stays until clicked Custom toast settings
Configure defaults when setting up the app:
val app = kraftApp {
semanticUI {
// this block configures the ToastsManager.Builder
defaultDuration = 5.seconds
}
} Appending messages
For API responses that return typed Message objects, append them directly:
// Single message
toasts.append(Message.Type.info, "Item created")
// From a Messages collection (e.g. from an API response)
toasts.append(apiResponse.messages) Popups & Context Menus
Context menus appear near the triggering element. Pass a UIEvent and the menu renders at the event
position:
ui.button {
onClick { evt ->
popups.showContextMenu(evt, PopupsManager.Positioning.BottomLeft) { handle ->
ui.vertical.menu {
noui.item A {
onClick { handle.close() }
+"Edit"
}
noui.item A {
onClick { handle.close() }
+"Delete"
}
}
}
}
+"Actions"
} Positioning
Six positioning options control where the popup appears relative to the anchor:
| Value | Position |
|---|---|
Positioning.TopLeft | Above, left-aligned |
Positioning.TopCenter | Above, centered |
Positioning.TopRight | Above, right-aligned |
Positioning.BottomLeft | Below, left-aligned |
Positioning.BottomCenter | Below, centered |
Positioning.BottomRight | Below, right-aligned |
Hover popups
For tooltips that appear on hover, use the showHoverPopup helpers. These attach to an element and
manage show/hide automatically:
ui.blue.label {
popups.showHoverPopup.topCenter(this) {
+"This is a tooltip"
}
+"Hover me"
} Close all open popups programmatically:
popups.closeAll()