在 Kotlin React 应用程序中导入外部 css

Pra*_*kar 5 kotlin kotlin-frontend kotlin-js

我想在我的 Kotlin-React 应用程序中使用类似 bootstrap/material 的 css 库。有没有办法导入这些外部 css 库?有一个Kotlin-Styled包装器,但不确定如何使用它来导入 css。

Pio*_*trK 1

这不是如何导入外部 CSS 的直接答案,但让我向您展示我如何成功地将Material UI库与 Kotlin 和 React 结合使用。这是该项目的演示: https: //krzema12.github.io/fsynth/

请参阅 Material UI 组件的 Kotlin 键入示例:

@file:JsModule("@material-ui/core/ListItem")

package it.krzeminski.fsynth.typings.materialui.widgets

import react.RProps
import react.RState
import react.ReactElement

@JsName("default")
external class ListItem : react.Component<RProps, RState> {
    override fun render(): ReactElement?
}
Run Code Online (Sandbox Code Playgroud)

(来源:https://github.com/krzema12/fsynth/blob/master/web/src/main/kotlin/it/krzeminski/fsynth/typings/materialui/widgets/ListItem.kt

和一个方便的包装:

fun RBuilder.materialListItem(handler: RHandler<RProps>) = child(ListItem::class) {
    handler()
}
Run Code Online (Sandbox Code Playgroud)

(来源:https://github.com/krzema12/fsynth/blob/master/web/src/main/kotlin/it/krzeminski/fsynth/typings/MaterialUiBuilders.kt#L42

然后我可以通过以下方式使用这个组件:

materialListItem {
...children...
}
Run Code Online (Sandbox Code Playgroud)

(来源: https: //github.com/krzema12/fsynth/blob/master/web/src/main/kotlin/it/krzeminski/fsynth/App.kt

据我从Material UI 文档页面的了解,它有效并且足够,因为 CSS 嵌入在 JavaScript 中。