Skip to content

Commit edd02bb

Browse files
committed
Change the first argument from styles to attrs for better customizability for the layouts
1 parent 4d9b047 commit edd02bb

File tree

4 files changed

+85
-50
lines changed

4 files changed

+85
-50
lines changed

compose-web-common/src/jsMain/kotlin/com/huanshankeji/compose/web/Layouts.kt

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

33
import androidx.compose.runtime.Composable
4+
import com.huanshankeji.compose.web.attributes.attrs
5+
import com.huanshankeji.compose.web.attributes.plus
6+
import com.huanshankeji.compose.web.css.Styles
7+
import com.huanshankeji.compose.web.css.wrapInAttrs
48
import org.jetbrains.compose.web.css.*
9+
import org.jetbrains.compose.web.dom.AttrBuilderContext
510
import org.jetbrains.compose.web.dom.ContentBuilder
611
import org.jetbrains.compose.web.dom.Div
712
import org.w3c.dom.HTMLDivElement
@@ -10,102 +15,125 @@ import org.w3c.dom.HTMLDivElement
1015

1116
@Composable
1217
fun Flexbox(
13-
styles: Styles? = null,
18+
attrs: AttrBuilderContext<HTMLDivElement>? = null,
1419
content: ContentBuilder<HTMLDivElement>
1520
) =
16-
Div({
21+
Div(attrs<HTMLDivElement> {
1722
style {
1823
display(DisplayStyle.Flex)
19-
styles?.invoke(this)
2024
}
21-
}, content)
25+
} + attrs, content)
26+
27+
@Composable
28+
fun Flexbox(styles: Styles? = null, content: ContentBuilder<HTMLDivElement>) =
29+
Flexbox(styles.wrapInAttrs(), content)
2230

2331
@Composable
2432
fun Column(
25-
styles: Styles? = null,
33+
attrs: AttrBuilderContext<HTMLDivElement>? = null,
2634
fitContent: Boolean = true,
2735
content: ContentBuilder<HTMLDivElement>
2836
) =
29-
Flexbox({
30-
flexDirection(FlexDirection.Column)
31-
if (fitContent) property("width", "fit-content")
32-
styles?.invoke(this)
33-
}, content)
37+
Flexbox(attrs<HTMLDivElement> {
38+
style {
39+
flexDirection(FlexDirection.Column)
40+
if (fitContent) property("width", "fit-content")
41+
}
42+
} + attrs, content)
43+
44+
@Composable
45+
fun Column(styles: Styles? = null, fitContent: Boolean = true, content: ContentBuilder<HTMLDivElement>) =
46+
Column(styles.wrapInAttrs(), fitContent, content)
3447

3548
@Composable
3649
fun ColumnWithSpaceBetween(
37-
styles: Styles? = null,
50+
attrs: AttrBuilderContext<HTMLDivElement>? = null,
3851
fitContent: Boolean = true,
3952
content: ContentBuilder<HTMLDivElement>
4053
) =
41-
Column({
42-
justifyContent(JustifyContent.SpaceBetween)
43-
styles?.invoke(this)
44-
}, fitContent, content)
54+
Column(attrs<HTMLDivElement> {
55+
style {
56+
justifyContent(JustifyContent.SpaceBetween)
57+
}
58+
} + attrs, fitContent, content)
59+
60+
61+
@Composable
62+
fun Row(
63+
attrs: AttrBuilderContext<HTMLDivElement>? = null,
64+
content: ContentBuilder<HTMLDivElement>
65+
) =
66+
Flexbox(attrs<HTMLDivElement> {
67+
style {
68+
flexDirection(FlexDirection.Row)
69+
}
70+
} + attrs, content)
4571

4672
@Composable
4773
fun Row(
4874
styles: Styles? = null,
4975
content: ContentBuilder<HTMLDivElement>
5076
) =
51-
Flexbox({
52-
flexDirection(FlexDirection.Row)
53-
styles?.invoke(this)
54-
}, content)
77+
Row(styles.wrapInAttrs(), content)
5578

5679
@Composable
5780
fun RowWithSpaceBetween(
58-
styles: Styles? = null,
81+
attrs: AttrBuilderContext<HTMLDivElement>? = null,
5982
content: ContentBuilder<HTMLDivElement>
6083
) =
61-
Row({
62-
justifyContent(JustifyContent.SpaceBetween)
63-
styles?.invoke(this)
64-
}, content)
84+
Row(attrs<HTMLDivElement> {
85+
style {
86+
justifyContent(JustifyContent.SpaceBetween)
87+
}
88+
} + attrs, content)
6589

6690
@Composable
6791
fun ColumnWithGaps(
68-
styles: Styles? = null,
92+
attrs: AttrBuilderContext<HTMLDivElement>? = null,
6993
gap: CSSNumeric,
7094
fitContent: Boolean = true,
7195
content: ContentBuilder<HTMLDivElement>
7296
) =
73-
Column({
74-
gap(gap)
75-
styles?.invoke(this)
76-
}, fitContent, content)
97+
Column(attrs<HTMLDivElement> {
98+
style {
99+
gap(gap)
100+
}
101+
} + attrs, fitContent, content)
77102

78103
@Composable
79104
fun RowWithGaps(
80-
styles: Styles? = null,
105+
attrs: AttrBuilderContext<HTMLDivElement>? = null,
81106
gap: CSSNumeric,
82107
content: ContentBuilder<HTMLDivElement>
83108
) =
84-
Row({
85-
gap(gap)
86-
styles?.invoke(this)
87-
}, content)
109+
Row(attrs<HTMLDivElement> {
110+
style {
111+
gap(gap)
112+
}
113+
} + attrs, content)
88114

89115
@Composable
90116
fun Centered(
91-
styles: Styles? = null,
117+
attrs: AttrBuilderContext<HTMLDivElement>? = null,
92118
content: ContentBuilder<HTMLDivElement>
93119
) =
94-
Flexbox({
95-
alignItems(AlignItems.Center)
96-
justifyContent(JustifyContent.Center)
97-
styles?.invoke(this)
98-
}, content)
120+
Flexbox(attrs<HTMLDivElement> {
121+
style {
122+
alignItems(AlignItems.Center)
123+
justifyContent(JustifyContent.Center)
124+
}
125+
} + attrs, content)
99126

100127
@Composable
101128
fun CenteredInViewport(
102-
styles: Styles? = null,
129+
attrs: AttrBuilderContext<HTMLDivElement>? = null,
103130
content: ContentBuilder<HTMLDivElement>
104131
) =
105-
Centered({
106-
minHeight(100.vh)
107-
styles?.invoke(this)
108-
}, content)
132+
Centered(attrs<HTMLDivElement> {
133+
style {
134+
minHeight(100.vh)
135+
}
136+
} + attrs, content)
109137

110138
@Composable
111139
fun FrGrid(

compose-web-common/src/jsMain/kotlin/com/huanshankeji/compose/web/Types.kt

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

33
import androidx.compose.runtime.Composable
4-
import org.jetbrains.compose.web.css.StyleScope
54
import org.jetbrains.compose.web.dom.AttrBuilderContext
65
import org.jetbrains.compose.web.dom.ContentBuilder
76
import org.jetbrains.compose.web.dom.Div
@@ -19,6 +18,4 @@ typealias DivElementComposable = ElementComposable<HTMLDivElement>
1918
// `::Div` cannot be used directly because "Function References of @Composable functions are not currently supported"
2019
val DivComposable: ElementComposable<HTMLDivElement> = { attrs, content -> Div(attrs, content) }
2120

22-
typealias Styles = StyleScope.() -> Unit
23-
2421
typealias HTMLElementContent = (@Composable ElementScope<HTMLElement>.() -> Unit)?
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.huanshankeji.compose.web.css
2+
3+
import org.jetbrains.compose.web.css.StyleScope
4+
import org.jetbrains.compose.web.dom.AttrBuilderContext
5+
import org.w3c.dom.Element
6+
7+
typealias Styles = StyleScope.() -> Unit
8+
9+
fun <TElement : Element> Styles?.wrapInAttrs(): AttrBuilderContext<TElement>? =
10+
this?.let { { style { it() } } }

compose-web-material/src/jsMain/kotlin/com/huanshankeji/compose/web/material/Layouts.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.huanshankeji.compose.web.material
22

33
import androidx.compose.runtime.Composable
44
import com.huanshankeji.compose.web.CenteredInViewport
5-
import com.huanshankeji.compose.web.Styles
5+
import com.huanshankeji.compose.web.css.Styles
66
import org.jetbrains.compose.web.attributes.AttrsScope
77
import org.jetbrains.compose.web.dom.ContentBuilder
88
import org.w3c.dom.HTMLDivElement
@@ -13,6 +13,6 @@ fun CenteredCardInViewport(
1313
cardAttrs: (AttrsScope<HTMLDivElement>.() -> Unit)? = null,
1414
content: ContentBuilder<HTMLDivElement>
1515
) =
16-
CenteredInViewport(viewportDivStyles) {
16+
CenteredInViewport({ viewportDivStyles?.let { style { it() } } }) {
1717
MdcCard(cardAttrs, content = content)
1818
}

0 commit comments

Comments
 (0)