invariant
支持有关如何使基于 SRI 的组件正常工作的正确答案。其他japgolly
基于的在处理第三方 JS 时不那么友好。
以下是我如何让该库react-mt-svg-lines
(一个动画库)工作:
克拉兹
case class MtSvgLines(
key: js.UndefOr[String] = js.undefined,
ref: js.UndefOr[String] = js.undefined,
animate: js.Any = true,
duration: Double = 10.0,
stagger: js.UndefOr[Double] = js.undefined,
timing: js.UndefOr[String] = js.undefined,
playback: js.UndefOr[String] = js.undefined,
fade: js.UndefOr[String] = js.undefined
)
{
def apply(children: ReactNode*): ReactComponentU_ = {
val props = JSMacro[MtSvgLines](this)
val f = React.asInstanceOf[js.Dynamic].createFactory(js.Dynamic.global.SvgLines.default)
if (children.isEmpty)
f(props).asInstanceOf[ReactComponentU_]
else if (children.size == 1)
f(props, children.head).asInstanceOf[ReactComponentU_]
else
f(props, children.toJsArray).asInstanceOf[ReactComponentU_]
}
}
值得注意的是,请确保其中的论点createFactory
是global.Whatever
你export
要的。考虑到孩子们在数字上的差异。用于js.UndefOr
每个可选。
客户端
def render(S: State, C: PropsChildren) = {
<.header(^.key := UUID.randomUUID().toString, Styles.Header_TextIsCentered, Styles.Header_BackgroundIsGrayBlue_WithWhiteText)(
<.div(^.className := "container")(
<.h1()("justin shin, tampa fl"),
MtSvgLines(animate = true, duration = 500) {
import japgolly.scalajs.react.vdom.svg.all._
svg(SvgStyles.svg,
defs(
maskTag(id := "mask", x := "0", y := "0", width := "100%", height := "100%",
rect(id := "alpha", x := "0", y := "0", width := "100%", height := "100%"),
text(id := "title", x := "50%",y := "0", dy := "1.58em")("Justin", transform := "translate(50, 0)"),
text(id := "title", x := "50%",y := "0", dy := "1.58em")("Tampa")
)
),
rect(id := "base", x := "0", y := "0", width := "100%", height := "100%"),
path(stroke := "green", strokeWidth := "10", fill := "none", d := "M20.8,51c0,0,20.8,18.2,21.5,18.2c0.6,0,33.3-38.5,33.3-38.5"),
)
}
)
)
}