Skip to content

Commit c5717a1

Browse files
rqcaiJakeWharton
andauthored
Adjust snapshot dimensions (#2787)
* Adjust snapshot dimensions Setting the dimensions using CSS style on the element before taking a snapshot. * Update redwood-dom-testing/src/commonMain/kotlin/app/cash/redwood/dom/testing/DomSnapshotter.kt Co-authored-by: Jake Wharton <[email protected]> * apiDump --------- Co-authored-by: Jake Wharton <[email protected]>
1 parent 2744716 commit c5717a1

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

redwood-dom-testing/api/redwood-dom-testing.klib.api

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
final class app.cash.redwood.dom.testing/DomSnapshotter { // app.cash.redwood.dom.testing/DomSnapshotter|null[0]
1010
constructor <init>(kotlin/String) // app.cash.redwood.dom.testing/DomSnapshotter.<init>|<init>(kotlin.String){}[0]
1111

12-
final suspend fun snapshot(org.w3c.dom/Element, kotlin/String = ..., kotlin/Boolean = ...) // app.cash.redwood.dom.testing/DomSnapshotter.snapshot|snapshot(org.w3c.dom.Element;kotlin.String;kotlin.Boolean){}[0]
12+
final suspend fun snapshot(org.w3c.dom/Element, kotlin/String = ..., kotlin/Boolean = ..., kotlin/Int? = ..., kotlin/Int? = ...) // app.cash.redwood.dom.testing/DomSnapshotter.snapshot|snapshot(org.w3c.dom.Element;kotlin.String;kotlin.Boolean;kotlin.Int?;kotlin.Int?){}[0]
1313

1414
final object Companion { // app.cash.redwood.dom.testing/DomSnapshotter.Companion|null[0]
1515
final inline fun invoke(): app.cash.redwood.dom.testing/DomSnapshotter // app.cash.redwood.dom.testing/DomSnapshotter.Companion.invoke|invoke(){}[0]

redwood-dom-testing/src/commonMain/kotlin/app/cash/redwood/dom/testing/DomSnapshotter.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package app.cash.redwood.dom.testing
1717

1818
import kotlin.coroutines.resume
1919
import kotlin.coroutines.resumeWithException
20+
import kotlin.math.ceil
2021
import kotlinx.browser.document
2122
import kotlinx.coroutines.await
2223
import kotlinx.coroutines.suspendCancellableCoroutine
@@ -37,16 +38,24 @@ public class DomSnapshotter @PublishedApi internal constructor(
3738
element: Element,
3839
name: String = "snapshot",
3940
scrolling: Boolean = false,
41+
width: Int? = null,
42+
height: Int? = null,
4043
) {
44+
element.setAttribute(
45+
"style",
46+
"width: ${width?.let { "${it}px" } ?: "max-content"}; " +
47+
"height: ${height?.let { "${it}px" } ?: "max-content"};",
48+
)
49+
4150
val image = HtmlToImage.toBlob(
4251
element = element,
4352
options = Options().apply {
4453
this.backgroundColor = "#ffff66"
45-
this.width = 300
46-
this.height = 100
47-
this.canvasWidth = width
48-
this.canvasHeight = height
49-
this.pixelRatio = 3.0
54+
this.width = ceil(element.getBoundingClientRect().width).toInt()
55+
this.height = ceil(element.getBoundingClientRect().height).toInt()
56+
this.canvasWidth = this.width
57+
this.canvasHeight = this.height
58+
this.pixelRatio = 1.0
5059
},
5160
).await()
5261

0 commit comments

Comments
 (0)