Skip to content

Commit cf31532

Browse files
committed
Support FillMax and FitContent and make Card default to FitContent on JS
1 parent 52c3cd7 commit cf31532

File tree

8 files changed

+93
-32
lines changed

8 files changed

+93
-32
lines changed

compose-multiplatform-common/src/commonMain/kotlin/com/huanshankeji/compose/ui/ModifierOrAttrsScope.kt

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,52 @@
11
package com.huanshankeji.compose.ui
22

33
//import androidx.compose.ui.Modifier
4-
import com.huanshankeji.compose.ui.unit.SizeValue
4+
import com.huanshankeji.compose.ui.unit.NumericSize
5+
import com.huanshankeji.compose.ui.unit.Size
6+
7+
typealias NotNullModifierOrAttrs<TElement> = ModifierOrAttrsScope<TElement>.() -> Unit
8+
typealias ModifierOrAttrs<TElement> = NotNullModifierOrAttrs<TElement>?
9+
10+
@Suppress("NOTHING_TO_INLINE")
11+
inline fun <TElement : Element> modifierOrAttrs(noinline modifierOrAttrs: NotNullModifierOrAttrs<TElement>) =
12+
modifierOrAttrs
13+
14+
operator fun <TElement : Element> NotNullModifierOrAttrs<TElement>.plus(other: ModifierOrAttrs<TElement>): ModifierOrAttrs<TElement> =
15+
if (other === null) this
16+
else {
17+
{
18+
this@plus()
19+
other()
20+
}
21+
}
522

6-
typealias ModifierOrAttrs<TElement> = (ModifierOrAttrsScope<TElement>.() -> Unit)?
723

824
expect abstract class Element
925

26+
1027
expect class ModifierOrAttrsScope<out TElement : Element> {
1128
fun style(builder: StyleScope.() -> Unit)
1229
}
1330

1431
expect class StyleScope {
15-
fun margin(value: SizeValue)
16-
fun height(value: SizeValue)
17-
fun width(value: SizeValue)
32+
fun margin(value: NumericSize)
33+
fun height(value: Size)
34+
fun width(value: Size)
1835
}
1936

37+
fun StyleScope.height(value: NumericSize) =
38+
height(Size.Numeric(value))
39+
40+
fun StyleScope.width(value: NumericSize) =
41+
width(Size.Numeric(value))
42+
2043
private const val PADDING_MESSAGE =
2144
"This function is a placeholder for code completion. " +
2245
"Use `margin` to add space around the composable, which is equivalent to `Modifier.padding`. " +
2346
"Set `margin` in the inner composable to add inner padding."
2447

2548
@Deprecated(PADDING_MESSAGE)
26-
fun StyleScope.padding(value: SizeValue): Unit =
49+
fun StyleScope.padding(value: NumericSize): Unit =
2750
throw NotImplementedError(PADDING_MESSAGE)
2851

2952
/*
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package com.huanshankeji.compose.ui.unit
22

3-
expect abstract class SizeValue
4-
expect class DpOrPxValue : SizeValue
3+
expect abstract class NumericSize
4+
expect class DpOrPx : NumericSize
55

6-
expect val Int.dpOrPx: DpOrPxValue
6+
expect val Int.dpOrPx: DpOrPx
7+
8+
9+
sealed class Size {
10+
class Numeric(val value: NumericSize) : Size()
11+
object FillMax : Size()
12+
object FitContent : Size()
13+
}

compose-multiplatform-common/src/jsMain/kotlin/com/huanshankeji/compose/ui/ModifierOrAttrsScope.kt

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.huanshankeji.compose.ui
22

3-
import com.huanshankeji.compose.ui.unit.SizeValue
3+
import com.huanshankeji.compose.ui.unit.NumericSize
4+
import com.huanshankeji.compose.ui.unit.Size
5+
import com.huanshankeji.compose.ui.unit.Size.*
46
import org.jetbrains.compose.web.attributes.AttrsScope
57
import org.jetbrains.compose.web.css.height
68
import org.jetbrains.compose.web.css.margin
9+
import org.jetbrains.compose.web.css.percent
710
import org.jetbrains.compose.web.css.width
811
import org.jetbrains.compose.web.dom.AttrBuilderContext
912
import org.w3c.dom.HTMLElement
@@ -22,12 +25,24 @@ actual class ModifierOrAttrsScope<out TElement : Element>(val attrsScope: AttrsS
2225
}
2326

2427
actual class StyleScope(val styleScope: org.jetbrains.compose.web.css.StyleScope) {
25-
actual fun margin(value: SizeValue) =
28+
actual fun margin(value: NumericSize) =
2629
styleScope.margin(value.platformValue)
2730

28-
actual fun height(value: SizeValue) =
29-
styleScope.height(value.platformValue)
30-
31-
actual fun width(value: SizeValue) =
32-
styleScope.width(value.platformValue)
31+
actual fun height(value: Size) =
32+
styleScope.run {
33+
when (value) {
34+
FitContent -> property("height", "fit-content")
35+
FillMax -> height(100.percent)
36+
is Numeric -> height(value.value.platformValue)
37+
}
38+
}
39+
40+
actual fun width(value: Size) =
41+
styleScope.run {
42+
when (value) {
43+
FitContent -> property("width", "fit-content")
44+
FillMax -> width(100.percent)
45+
is Numeric -> width(value.value.platformValue)
46+
}
47+
}
3348
}

compose-multiplatform-common/src/jsMain/kotlin/com/huanshankeji/compose/ui/unit/Sizes.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import org.jetbrains.compose.web.css.CSSSizeValue
55
import org.jetbrains.compose.web.css.CSSUnit
66
import org.jetbrains.compose.web.css.px
77

8-
actual abstract class SizeValue(val platformValue: CSSLengthOrPercentageValue)
9-
actual class DpOrPxValue(platformValue: CSSSizeValue<CSSUnit.px>) : SizeValue(platformValue)
8+
actual abstract class NumericSize(val platformValue: CSSLengthOrPercentageValue)
9+
actual class DpOrPx(platformValue: CSSSizeValue<CSSUnit.px>) : NumericSize(platformValue)
1010

11-
actual val Int.dpOrPx: DpOrPxValue
12-
get() = DpOrPxValue(this.px)
11+
actual val Int.dpOrPx: DpOrPx
12+
get() = DpOrPx(this.px)

compose-multiplatform-common/src/jvmMain/kotlin/com/huanshankeji/compose/ui/ModifierOrAttrsScopeJvm.kt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.huanshankeji.compose.ui
22

3+
import androidx.compose.foundation.layout.fillMaxHeight
34
import androidx.compose.foundation.layout.height
45
import androidx.compose.foundation.layout.padding
5-
import androidx.compose.foundation.layout.width
66
import androidx.compose.ui.Modifier
7-
import com.huanshankeji.compose.ui.unit.SizeValue
7+
import com.huanshankeji.compose.ui.unit.NumericSize
8+
import com.huanshankeji.compose.ui.unit.Size
9+
import com.huanshankeji.compose.ui.unit.Size.*
810

911
fun <TElement : Element> ModifierOrAttrs<TElement>.toModifier(): Modifier =
1012
this?.let {
@@ -32,15 +34,23 @@ actual class ModifierOrAttrsScope<out TElement : Element>(modifier: Modifier) {
3234
}
3335

3436
actual class StyleScope(val modifierOrAttrsScope: ModifierOrAttrsScope<*>) {
35-
actual fun margin(value: SizeValue) = modifierOrAttrsScope.modify {
37+
actual fun margin(value: NumericSize) = modifierOrAttrsScope.modify {
3638
padding(value.platformValue)
3739
}
3840

39-
actual fun height(value: SizeValue) = modifierOrAttrsScope.modify {
40-
height(value.platformValue)
41+
actual fun height(value: Size) = modifierOrAttrsScope.modify {
42+
when (value) {
43+
FitContent -> this
44+
FillMax -> fillMaxHeight()
45+
is Numeric -> height(value.value.platformValue)
46+
}
4147
}
4248

43-
actual fun width(value: SizeValue) = modifierOrAttrsScope.modify {
44-
width(value.platformValue)
49+
actual fun width(value: Size) = modifierOrAttrsScope.modify {
50+
when (value) {
51+
FitContent -> this
52+
FillMax -> fillMaxHeight()
53+
is Numeric -> height(value.value.platformValue)
54+
}
4555
}
4656
}

compose-multiplatform-common/src/jvmMain/kotlin/com/huanshankeji/compose/ui/unit/Sizes.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package com.huanshankeji.compose.ui.unit
33
import androidx.compose.ui.unit.Dp
44
import androidx.compose.ui.unit.dp
55

6-
actual abstract class SizeValue(val platformValue: Dp)
7-
actual class DpOrPxValue(platformValue: Dp) : SizeValue(platformValue)
6+
actual abstract class NumericSize(val platformValue: Dp)
7+
actual class DpOrPx(platformValue: Dp) : NumericSize(platformValue)
88

9-
actual val Int.dpOrPx: DpOrPxValue
10-
get() = DpOrPxValue(this.dp)
9+
actual val Int.dpOrPx: DpOrPx
10+
get() = DpOrPx(this.dp)

compose-multiplatform-material/src/jsMain/kotlin/com/huanshankeji/compose/material/Card.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ package com.huanshankeji.compose.material
22

33
import androidx.compose.runtime.Composable
44
import com.huanshankeji.compose.ui.ModifierOrAttrs
5+
import com.huanshankeji.compose.ui.modifierOrAttrs
6+
import com.huanshankeji.compose.ui.plus
57
import com.huanshankeji.compose.ui.toAttrs
8+
import com.huanshankeji.compose.ui.unit.Size
69
import dev.petuska.kmdc.card.MDCCard
710
import org.w3c.dom.HTMLDivElement
811

912
actual typealias CardElement = HTMLDivElement
1013

1114
@Composable
1215
actual fun Card(modifierOrAttrs: ModifierOrAttrs<CardElement>, content: @Composable () -> Unit) =
13-
MDCCard(attrs = modifierOrAttrs.toAttrs()) { content() }
16+
MDCCard(attrs = (modifierOrAttrs<CardElement> {
17+
style { width(Size.FitContent) }
18+
} + modifierOrAttrs).toAttrs()) { content() }

demo/src/commonMain/kotlin/com/huanshankeji/compose/material/demo/Composables.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.huanshankeji.compose.layout.Column
77
import com.huanshankeji.compose.layout.Row
88
import com.huanshankeji.compose.material.*
99
import com.huanshankeji.compose.material.icon.MaterialIcons
10+
import com.huanshankeji.compose.ui.height
1011
import com.huanshankeji.compose.ui.unit.dpOrPx
1112

1213
@OptIn(ConfusableTextApi::class)

0 commit comments

Comments
 (0)