Skip to content

Commit 32fb9ad

Browse files
committed
Make it possible to make a Material list match its parent size on JS DOM, adding a matchPositionRelativeParentJsOnly modifier and improving TopAppBarScaffold on JS again
1 parent ef2d4d9 commit 32fb9ad

File tree

5 files changed

+48
-11
lines changed

5 files changed

+48
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.huanshankeji.compose.foundation.ext
2+
3+
import com.huanshankeji.compose.ui.Modifier
4+
5+
actual fun Modifier.matchPositionRelativeParentJsOnly(): Modifier =
6+
this
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.huanshankeji.compose.foundation.ext
2+
3+
import com.huanshankeji.compose.ui.Modifier
4+
5+
expect fun Modifier.matchPositionRelativeParentJsOnly(): Modifier
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.huanshankeji.compose.foundation.ext
2+
3+
import com.huanshankeji.compose.ui.Modifier
4+
import com.varabyte.kobweb.compose.ui.attrsModifier
5+
import org.jetbrains.compose.web.css.*
6+
7+
actual fun Modifier.matchPositionRelativeParentJsOnly(): Modifier =
8+
platformModify {
9+
attrsModifier {
10+
style {
11+
position(Position.Absolute)
12+
left(0.px)
13+
top(0.px)
14+
right(0.px)
15+
bottom(0.px)
16+
// Also considering using `inset` (https://developer.mozilla.org/en-US/docs/Web/CSS/inset)
17+
}
18+
}
19+
}

compose-multiplatform-material2/src/jsMain/kotlin/com/huanshankeji/compose/material2/ext/TopAppBarScaffold.js.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,22 @@ actual fun TopAppBarScaffold(
113113
}
114114

115115
Column(Modifier.fillMaxSize()) {
116-
Div(Modifier.weight(1f).fillMaxWidth().toAttrs {
117-
style { position(Position.Relative) }
118-
}) {
119-
PrimitiveTopAppBarScaffold(
120-
title,
121-
topAppBarModifier,
122-
navigationIcon,
123-
actions,
124-
Modifier
125-
) {
116+
PrimitiveTopAppBarScaffold(
117+
title,
118+
topAppBarModifier,
119+
navigationIcon,
120+
actions,
121+
Modifier.weight(1f).fillMaxWidth()
122+
) {
123+
Div(Modifier.fillMaxSize().toAttrs {
124+
style { position(Position.Relative) }
125+
}) {
126126
// see `ScaffoldLayoutWithMeasureFix`
127127
val innerPadding = PaddingValues()
128128
content(innerPadding)
129+
130+
floatingActionButton?.let { fabWithPosition(it) }
129131
}
130-
floatingActionButton?.let { fabWithPosition(it) }
131132
}
132133

133134
bottomBar?.invoke()

compose-multiplatform-material3/src/commonMain/kotlin/com/huanshankeji/compose/material3/lazy/ext/List.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.huanshankeji.compose.material3.lazy.ext
22

33
import androidx.compose.runtime.Composable
4+
import com.huanshankeji.compose.foundation.ext.matchPositionRelativeParentJsOnly
45
import com.huanshankeji.compose.material.icons.Icon
56
import com.huanshankeji.compose.material3.ext.toNullableContentWithModifier
67
import com.huanshankeji.compose.material3.ext.toNullableTextWithModifier
@@ -67,6 +68,11 @@ class ListItemComponents(
6768
)
6869
}
6970

71+
/**
72+
* On JS DOM, if there isn't a parent with a fixed height
73+
* and you want the list height to be constraint to the available space of the parent instead of expanding the parent,
74+
* set "position:relative;" on the parent element and use [Modifier.matchPositionRelativeParentJsOnly].
75+
*/
7076
@Composable
7177
expect fun List(modifier: Modifier = Modifier, content: ListScope.() -> Unit)
7278

0 commit comments

Comments
 (0)