Skip to content

Commit 94d29a6

Browse files
committed
Improve the Text APIs
1. add `RawText`; 1. add an opt-in requirement for `com.huanshankeji.compose.material.Text`; 1. add `modifierAndAttrs` arguments.
1 parent d93663f commit 94d29a6

File tree

11 files changed

+85
-15
lines changed

11 files changed

+85
-15
lines changed
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.huanshankeji.compose
22

33
import androidx.compose.runtime.Composable
4+
import com.huanshankeji.compose.ui.Element
5+
import com.huanshankeji.compose.ui.ModifierOrAttrs
6+
7+
expect abstract class BasicTextElement : Element
48

59
@Composable
6-
expect fun BasicText(text: String)
10+
expect fun BasicText(text: String, modifierOrAttrs: ModifierOrAttrs<BasicTextElement> = null)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.huanshankeji.compose
2+
3+
import androidx.compose.runtime.Composable
4+
5+
/** A raw text composable which doesn't add any element on web. */
6+
@Composable
7+
expect fun RawText(text: String)
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package com.huanshankeji.compose
22

33
import androidx.compose.runtime.Composable
4+
import com.huanshankeji.compose.ui.ModifierOrAttrs
5+
import com.huanshankeji.compose.ui.toAttrs
6+
import org.jetbrains.compose.web.dom.Span
47
import org.jetbrains.compose.web.dom.Text
8+
import org.w3c.dom.HTMLSpanElement
9+
10+
actual typealias BasicTextElement = HTMLSpanElement
511

612
@Composable
7-
actual fun BasicText(text: String) =
8-
Text(text)
13+
actual fun BasicText(text: String, modifierOrAttrs: ModifierOrAttrs<HTMLSpanElement>) =
14+
Span(modifierOrAttrs.toAttrs()) { Text(text) }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.huanshankeji.compose
2+
3+
import androidx.compose.runtime.Composable
4+
import org.jetbrains.compose.web.dom.Text
5+
6+
@Composable
7+
actual fun RawText(text: String) =
8+
Text(text)
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package com.huanshankeji.compose
22

33
import androidx.compose.runtime.Composable
4+
import com.huanshankeji.compose.ui.Element
5+
import com.huanshankeji.compose.ui.ModifierOrAttrs
6+
import com.huanshankeji.compose.ui.toModifier
7+
8+
actual abstract class BasicTextElement : Element()
49

510
@Composable
6-
actual fun BasicText(text: String) =
7-
androidx.compose.foundation.text.BasicText(text)
11+
actual fun BasicText(text: String, modifierOrAttrs: ModifierOrAttrs<BasicTextElement>) =
12+
androidx.compose.foundation.text.BasicText(text, modifierOrAttrs.toModifier())
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.huanshankeji.compose
2+
3+
import androidx.compose.runtime.Composable
4+
5+
@Composable
6+
actual fun RawText(text: String) =
7+
androidx.compose.foundation.text.BasicText(text)
Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
package com.huanshankeji.compose.material
22

33
import androidx.compose.runtime.Composable
4+
import com.huanshankeji.compose.ui.Element
5+
import com.huanshankeji.compose.ui.ModifierOrAttrs
46

7+
expect abstract class TextElement : Element
8+
9+
@RequiresOptIn(
10+
"The `com.huanshankeji.compose.material.Text` function " +
11+
"can be easily confused with other Composable functions named `Text` " +
12+
"such as `androidx.compose.material.Text` and `org.jetbrains.compose.web.dom.Text` " +
13+
"if not careful. " +
14+
"Opt-in to use this API or use `MaterialText` instead.",
15+
RequiresOptIn.Level.WARNING
16+
)
17+
@Retention(AnnotationRetention.BINARY)
18+
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
19+
annotation class ConfusableTextApi
20+
21+
@ConfusableTextApi
22+
@Composable
23+
expect fun Text(text: String, modifierOrAttrs: ModifierOrAttrs<TextElement> = null)
24+
25+
@Suppress("NOTHING_TO_INLINE")
26+
@OptIn(ConfusableTextApi::class)
527
@Composable
6-
expect fun Text(text: String)
28+
inline fun MaterialText(text: String, noinline modifierOrAttrs: ModifierOrAttrs<TextElement> = null) =
29+
Text(text, modifierOrAttrs)

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

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

33
import androidx.compose.runtime.Composable
44
import com.huanshankeji.compose.BasicText
5+
import com.huanshankeji.compose.ui.ModifierOrAttrs
6+
import org.w3c.dom.HTMLSpanElement
7+
8+
actual typealias TextElement = HTMLSpanElement
59

610
@Composable
7-
actual fun Text(text: String) =
8-
BasicText(text)
11+
actual fun Text(text: String, modifierOrAttrs: ModifierOrAttrs<TextElement>) =
12+
BasicText(text, modifierOrAttrs)

compose-multiplatform-material/src/jvmMain/kotlin/com/huanshankeji/compose/material/Text.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.huanshankeji.compose.material
2+
3+
import androidx.compose.runtime.Composable
4+
import com.huanshankeji.compose.ui.Element
5+
import com.huanshankeji.compose.ui.ModifierOrAttrs
6+
import com.huanshankeji.compose.ui.toModifier
7+
8+
actual abstract class TextElement : Element()
9+
10+
@Composable
11+
actual fun Text(text: String, modifierOrAttrs: ModifierOrAttrs<TextElement>) =
12+
androidx.compose.material.Text(text, modifierOrAttrs.toModifier())

0 commit comments

Comments
 (0)