Skip to content

Commit 8746b57

Browse files
committed
Add the MdList and MdListItem composables
1 parent 8f3692e commit 8746b57

File tree

1 file changed

+91
-0
lines changed
  • compose-html-material3/src/jsMain/kotlin/com/huanshankeji/compose/html/material3

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.huanshankeji.compose.html.material3
2+
3+
import androidx.compose.runtime.Composable
4+
import com.huanshankeji.compose.web.attributes.Attrs
5+
import com.huanshankeji.compose.web.attributes.ext.disabled
6+
import com.huanshankeji.compose.web.attributes.ext.href
7+
import com.huanshankeji.compose.web.attributes.ext.target
8+
import com.huanshankeji.compose.web.attributes.ext.type
9+
import com.huanshankeji.compose.web.attributes.slot
10+
import org.jetbrains.compose.web.attributes.AttrsScope
11+
import org.jetbrains.compose.web.dom.ElementScope
12+
import org.jetbrains.compose.web.dom.TagElement
13+
import org.w3c.dom.HTMLElement
14+
15+
/*
16+
https://github.com/material-components/material-web/blob/main/docs/components/list.md
17+
https://material-web.dev/components/list/
18+
https://material-web.dev/components/list/stories/
19+
*/
20+
21+
@Composable
22+
fun MdList(attrs: Attrs<HTMLElement>? = null, content: @Composable MdListScope.() -> Unit) {
23+
require("@material/web/list/list.js")
24+
25+
TagElement("md-list", attrs) {
26+
MdListScope(this).content()
27+
}
28+
}
29+
30+
class MdListScope(val elementScope: ElementScope<HTMLElement>) {
31+
@Composable
32+
fun MdListItem(
33+
disabled: Boolean? = null,
34+
type: MdListItemType? = null,
35+
href: String? = null,
36+
target: String? = null,
37+
attrs: Attrs<HTMLElement>? = null,
38+
content: @Composable MdListItemScope.() -> Unit
39+
) =
40+
@OptIn(ExposedMdListApi::class)
41+
com.huanshankeji.compose.html.material3.MdListItem(disabled, type, href, target, attrs, content)
42+
}
43+
44+
45+
@RequiresOptIn(
46+
"An `MdListItem` is usually in an `MdList`. This component is exposed and should be used carefully. In most cases try to use the one in `MdListScope` instead.",
47+
RequiresOptIn.Level.WARNING
48+
)
49+
@Retention(AnnotationRetention.BINARY)
50+
annotation class ExposedMdListApi
51+
52+
@ExposedMdListApi
53+
@Composable
54+
fun MdListItem(
55+
disabled: Boolean? = null,
56+
type: MdListItemType? = null,
57+
href: String? = null,
58+
target: String? = null,
59+
attrs: Attrs<HTMLElement>? = null,
60+
content: @Composable MdListItemScope.() -> Unit
61+
) {
62+
require("@material/web/list/list-item.js")
63+
64+
//@Suppress("RemoveExplicitTypeArguments")
65+
TagElement<HTMLElement>("md-list-item", {
66+
disabled(disabled)
67+
type(type?.stringValue)
68+
href(href)
69+
target(target)
70+
71+
attrs?.invoke(this)
72+
}) {
73+
MdListItemScope(this).content()
74+
}
75+
}
76+
77+
enum class MdListItemType(val stringValue: String) {
78+
Text("text"), Link("link"), Button("button")
79+
}
80+
81+
class MdListItemScope(val elementScope: ElementScope<HTMLElement>) {
82+
enum class Slot(val stringValue: String) {
83+
Headline("headline"),
84+
Start("start"), End("end"),
85+
SupportingText("supporting-text"), TrailingSupportingText("trailing-supporting-text"),
86+
Overline("overline")
87+
}
88+
89+
fun AttrsScope<*>.slot(value: Slot) =
90+
slot(value.stringValue)
91+
}

0 commit comments

Comments
 (0)