-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
html interop wasm support (untested) (#211)
- Loading branch information
Showing
8 changed files
with
131 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 12 additions & 24 deletions
36
lib/compose-html-interop/src/commonMain/kotlin/dev/sargunv/composehtmlinterop/util.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,23 @@ | ||
package dev.sargunv.composehtmlinterop | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.layout.LayoutCoordinates | ||
import androidx.compose.ui.layout.boundsInWindow | ||
import androidx.compose.ui.unit.Density | ||
import androidx.compose.ui.unit.Dp | ||
import org.w3c.dom.HTMLElement | ||
import org.w3c.dom.Node | ||
|
||
public expect abstract class HTMLElement { | ||
public fun focus() | ||
} | ||
|
||
internal fun Dp.toCssValue(): String = "${value}px" | ||
|
||
internal fun HTMLElement.matchLayout(layoutCoordinates: LayoutCoordinates, density: Density) { | ||
with(density) { | ||
style.apply { | ||
val rect = layoutCoordinates.boundsInWindow() | ||
width = rect.width.toDp().toCssValue() | ||
height = rect.height.toDp().toCssValue() | ||
left = rect.left.toDp().toCssValue() | ||
top = rect.top.toDp().toCssValue() | ||
} | ||
} | ||
} | ||
@Composable internal expect fun rememberContainerNode(): HTMLElement | ||
|
||
internal expect fun HTMLElement.matchLayout(layoutCoordinates: LayoutCoordinates, density: Density) | ||
|
||
@Composable | ||
internal fun <T : Node> rememberDomNode(parent: Node, factory: () -> T): T { | ||
return remember(key1 = parent, calculation = factory).also { child -> | ||
DisposableEffect(parent, child) { | ||
parent.insertBefore(child, parent.firstChild) | ||
onDispose { parent.removeChild(child) } | ||
} | ||
} | ||
} | ||
internal expect fun <T : HTMLElement> rememberDomNode(parent: HTMLElement, factory: () -> T): T | ||
|
||
internal expect val HTMLElement.headChild: HTMLElement? | ||
|
||
internal expect val HTMLElement.tailChild: HTMLElement? |
51 changes: 51 additions & 0 deletions
51
lib/compose-html-interop/src/jsMain/kotlin/dev/sargunv/composehtmlinterop/util.js.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package dev.sargunv.composehtmlinterop | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.layout.LayoutCoordinates | ||
import androidx.compose.ui.layout.boundsInWindow | ||
import androidx.compose.ui.unit.Density | ||
import kotlinx.browser.document | ||
|
||
public actual typealias HTMLElement = org.w3c.dom.HTMLElement | ||
|
||
@Composable | ||
internal actual fun rememberContainerNode(): HTMLElement = | ||
rememberDomNode(parent = document.body!!) { | ||
document.createElement("div").unsafeCast<HTMLElement>().apply { | ||
style.position = "absolute" | ||
style.margin = "0px" | ||
} | ||
} | ||
|
||
internal actual fun HTMLElement.matchLayout( | ||
layoutCoordinates: LayoutCoordinates, | ||
density: Density, | ||
) { | ||
with(density) { | ||
style.apply { | ||
val rect = layoutCoordinates.boundsInWindow() | ||
width = rect.width.toDp().toCssValue() | ||
height = rect.height.toDp().toCssValue() | ||
left = rect.left.toDp().toCssValue() | ||
top = rect.top.toDp().toCssValue() | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
internal actual fun <T : HTMLElement> rememberDomNode(parent: HTMLElement, factory: () -> T): T { | ||
return remember(key1 = parent, calculation = factory).also { child -> | ||
DisposableEffect(parent, child) { | ||
parent.insertBefore(child, parent.firstChild) | ||
onDispose { parent.removeChild(child) } | ||
} | ||
} | ||
} | ||
|
||
internal actual val HTMLElement.headChild | ||
get() = firstElementChild?.unsafeCast<HTMLElement>() | ||
|
||
internal actual val HTMLElement.tailChild | ||
get() = lastElementChild?.unsafeCast<HTMLElement>() |
51 changes: 51 additions & 0 deletions
51
lib/compose-html-interop/src/wasmJsMain/kotlin/dev/sargunv/composehtmlinterop/util.wasmJs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package dev.sargunv.composehtmlinterop | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.layout.LayoutCoordinates | ||
import androidx.compose.ui.layout.boundsInWindow | ||
import androidx.compose.ui.unit.Density | ||
import kotlinx.browser.document | ||
|
||
public actual typealias HTMLElement = org.w3c.dom.HTMLElement | ||
|
||
@Composable | ||
internal actual fun rememberContainerNode() = | ||
rememberDomNode(parent = document.body!!) { | ||
document.createElement("div").unsafeCast<HTMLElement>().apply { | ||
style.position = "absolute" | ||
style.margin = "0px" | ||
} | ||
} | ||
|
||
internal actual fun HTMLElement.matchLayout( | ||
layoutCoordinates: LayoutCoordinates, | ||
density: Density, | ||
) { | ||
with(density) { | ||
style.apply { | ||
val rect = layoutCoordinates.boundsInWindow() | ||
width = rect.width.toDp().toCssValue() | ||
height = rect.height.toDp().toCssValue() | ||
left = rect.left.toDp().toCssValue() | ||
top = rect.top.toDp().toCssValue() | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
internal actual fun <T : HTMLElement> rememberDomNode(parent: HTMLElement, factory: () -> T): T { | ||
return remember(key1 = parent, calculation = factory).also { child -> | ||
DisposableEffect(parent, child) { | ||
parent.insertBefore(child, parent.firstChild) | ||
onDispose { parent.removeChild(child) } | ||
} | ||
} | ||
} | ||
|
||
internal actual val HTMLElement.headChild | ||
get() = firstElementChild?.unsafeCast<HTMLElement>() | ||
|
||
internal actual val HTMLElement.tailChild | ||
get() = lastElementChild?.unsafeCast<HTMLElement>() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# Module maplibre-gl-js-kotlin | ||
# Module kotlin-maplibre-js | ||
|
||
Kotlin bindings for [MapLibre GL JS](https://www.npmjs.com/package/maplibre-gl). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters