Skip to content

Commit 6b8f505

Browse files
committed
Add the icon button composables
1 parent b36f57e commit 6b8f505

File tree

3 files changed

+195
-1
lines changed
  • compose-html-common/src/jsMain/kotlin/com/huanshankeji/compose/web/attributes/ext
  • compose-html-material3/src/jsMain/kotlin/com/huanshankeji/compose/html/material3

3 files changed

+195
-1
lines changed

compose-html-common/src/jsMain/kotlin/com/huanshankeji/compose/web/attributes/ext/Attrs.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,7 @@ fun AttrsScope<*>.autoCapitalizeRequiringValid(value: String) {
137137
fun AttrsScope<*>.enterKeyHintIfValid(value: String) {
138138
if (value in EnterKeyHint.valueSet) enterKeyHint(value)
139139
}
140+
141+
142+
fun AttrsScope<*>.selected(value: Boolean?) =
143+
attrIfNotNull("selected", value)

compose-html-material3/src/jsMain/kotlin/com/huanshankeji/compose/html/material3/MdButton.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.huanshankeji.compose.html.material3
33
import androidx.compose.runtime.Composable
44
import com.huanshankeji.compose.web.attributes.Attrs
55
import com.huanshankeji.compose.web.attributes.ext.*
6+
import com.huanshankeji.compose.web.attributes.slot
67
import org.jetbrains.compose.web.attributes.AttrsScope
78
import org.jetbrains.compose.web.dom.ElementScope
89
import org.jetbrains.compose.web.dom.TagElement
@@ -202,5 +203,5 @@ fun MdTextButton(
202203

203204
class MdButtonScope(val elementScope: ElementScope<HTMLElement>) {
204205
fun AttrsScope<*>.slotEqIcon() =
205-
attr("slot", "icon")
206+
slot("icon")
206207
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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.attr
6+
import com.huanshankeji.compose.web.attributes.ext.*
7+
import com.huanshankeji.compose.web.attributes.slot
8+
import org.jetbrains.compose.web.attributes.AttrsScope
9+
import org.jetbrains.compose.web.dom.ElementScope
10+
import org.jetbrains.compose.web.dom.TagElement
11+
import org.w3c.dom.HTMLElement
12+
13+
14+
/*
15+
https://github.com/material-components/material-web/blob/main/docs/components/icon-button.md
16+
https://material-web.dev/components/icon-button/
17+
https://material-web.dev/components/icon-button/stories/
18+
*/
19+
20+
21+
@Composable
22+
private fun CommonMdIconButton(
23+
tagName: String,
24+
disabled: Boolean?,
25+
flipIconInRtl: Boolean?,
26+
href: String?,
27+
target: String?,
28+
ariaLabelSelected: String?,
29+
toggle: Boolean?,
30+
selected: Boolean?,
31+
type: String?,
32+
value: String?,
33+
attrs: Attrs<HTMLElement>?,
34+
content: @Composable MdIconButtonScope.() -> Unit
35+
) =
36+
@Suppress("RemoveExplicitTypeArguments")
37+
TagElement<HTMLElement>(
38+
tagName,
39+
{
40+
disabled(disabled)
41+
flipIconInRtl?.let { attr("flip-icon-in-rtl", it) }
42+
href(href)
43+
target(target)
44+
ariaLabelSelected?.let { attr("aria-label-selected", it) }
45+
toggle?.let { attr("toggle", it) }
46+
selected(selected)
47+
type(type)
48+
value(value)
49+
50+
attrs?.invoke(this)
51+
}
52+
) {
53+
MdIconButtonScope(this).content()
54+
}
55+
56+
57+
@Composable
58+
fun MdIconButton(
59+
disabled: Boolean? = null,
60+
flipIconInRtl: Boolean? = null,
61+
href: String? = null,
62+
target: String? = null,
63+
ariaLabelSelected: String? = null,
64+
toggle: Boolean? = null,
65+
selected: Boolean? = null,
66+
type: String? = null,
67+
value: String? = null,
68+
attrs: Attrs<HTMLElement>? = null,
69+
content: @Composable MdIconButtonScope.() -> Unit
70+
) {
71+
require("@material/web/iconbutton/icon-button.js")
72+
73+
CommonMdIconButton(
74+
"md-icon-button",
75+
disabled,
76+
flipIconInRtl,
77+
href,
78+
target,
79+
ariaLabelSelected,
80+
toggle,
81+
selected,
82+
type,
83+
value,
84+
attrs,
85+
content
86+
)
87+
}
88+
89+
@Composable
90+
fun MdFilledIconButton(
91+
disabled: Boolean? = null,
92+
flipIconInRtl: Boolean? = null,
93+
href: String? = null,
94+
target: String? = null,
95+
ariaLabelSelected: String? = null,
96+
toggle: Boolean? = null,
97+
selected: Boolean? = null,
98+
type: String? = null,
99+
value: String? = null,
100+
attrs: Attrs<HTMLElement>? = null,
101+
content: @Composable MdIconButtonScope.() -> Unit
102+
) {
103+
require("@material/web/iconbutton/filled-icon-button.js")
104+
105+
CommonMdIconButton(
106+
"md-filled-icon-button",
107+
disabled,
108+
flipIconInRtl,
109+
href,
110+
target,
111+
ariaLabelSelected,
112+
toggle,
113+
selected,
114+
type,
115+
value,
116+
attrs,
117+
content
118+
)
119+
}
120+
121+
@Composable
122+
fun MdFilledTonalIconButton(
123+
disabled: Boolean? = null,
124+
flipIconInRtl: Boolean? = null,
125+
href: String? = null,
126+
target: String? = null,
127+
ariaLabelSelected: String? = null,
128+
toggle: Boolean? = null,
129+
selected: Boolean? = null,
130+
type: String? = null,
131+
value: String? = null,
132+
attrs: Attrs<HTMLElement>? = null,
133+
content: @Composable MdIconButtonScope.() -> Unit
134+
) {
135+
require("@material/web/iconbutton/filled-tonal-icon-button.js")
136+
137+
CommonMdIconButton(
138+
"md-filled-tonal-icon-button",
139+
disabled,
140+
flipIconInRtl,
141+
href,
142+
target,
143+
ariaLabelSelected,
144+
toggle,
145+
selected,
146+
type,
147+
value,
148+
attrs,
149+
content
150+
)
151+
}
152+
153+
@Composable
154+
fun MdOutlinedIconButton(
155+
disabled: Boolean? = null,
156+
flipIconInRtl: Boolean? = null,
157+
href: String? = null,
158+
target: String? = null,
159+
ariaLabelSelected: String? = null,
160+
toggle: Boolean? = null,
161+
selected: Boolean? = null,
162+
type: String? = null,
163+
value: String? = null,
164+
attrs: Attrs<HTMLElement>? = null,
165+
content: @Composable MdIconButtonScope.() -> Unit
166+
) {
167+
require("@material/web/iconbutton/outlined-icon-button.js")
168+
169+
CommonMdIconButton(
170+
"md-outlined-icon-button",
171+
disabled,
172+
flipIconInRtl,
173+
href,
174+
target,
175+
ariaLabelSelected,
176+
toggle,
177+
selected,
178+
type,
179+
value,
180+
attrs,
181+
content
182+
)
183+
}
184+
185+
186+
class MdIconButtonScope(val elementScope: ElementScope<HTMLElement>) {
187+
fun AttrsScope<*>.slotEqSelected() =
188+
slot("selected")
189+
}

0 commit comments

Comments
 (0)