Created
December 22, 2021 21:11
-
-
Save psttf/dec7017b162431f54f669956088c7821 to your computer and use it in GitHub Desktop.
Sharing scala-dom-types based markup between JVM (scalatags) and JS (OutWatch)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import outwatch.helpers.AttributeBuilder | |
import outwatch.{Attr, HtmlVNode, VDomModifier} | |
import scala.language.implicitConversions | |
object TestComponent extends TestViews(outwatch.dsl) { | |
type TModifier = VDomModifier | |
type TRes = VDomModifier | |
type AtPr = Attr | |
implicit def tTagOps[A](x: HtmlVNode): TagOps = new TagOps { | |
override def apply(t: VDomModifier*) = x(t: _*) | |
} | |
implicit def stringT(x: String): VDomModifier = | |
VDomModifier.renderToVDomModifier(x) | |
implicit def raStringAttrOps( | |
x: AttributeBuilder[String, Attr], | |
): AttrOps[String] = | |
(string: String) => x.assign(string) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.raquo.domtypes.generic.builders._ | |
import com.raquo.domtypes.generic.codecs.Codec | |
import com.raquo.domtypes.generic.defs._ | |
import com.raquo.domtypes.generic.defs.complex.canonical.CanonicalComplexHtmlKeys | |
import scalatags.Text.all._ | |
import scalatags.Text.{Aggregate, Modifier, TypedTag} | |
import scala.language.implicitConversions | |
object DomTypes | |
extends HtmlTagBuilder[TypedTag, String] | |
with ReflectedHtmlAttrBuilder[SCAttr2] | |
with HtmlAttrBuilder[SCAttr1] | |
with PropBuilder[SCAttr2] | |
with sameRefTags.DocumentTags[TypedTag, String] | |
with sameRefTags.TextTags[TypedTag, String] | |
with CanonicalComplexHtmlKeys[SCAttr2, SCAttr1, SCAttr2] | |
with Aggregate { | |
def htmlTag[Ref <: String](tagName: String, void: Boolean): TypedTag[Ref] = | |
TypedTag(tagName, Nil) | |
def reflectedAttr[V, DomPropV]( | |
attrKey: String, | |
propKey: String, | |
attrCodec: Codec[V, String], | |
propCodec: Codec[V, DomPropV], | |
): SCAttr2[V, DomPropV] = | |
scalatags.generic.Attr(attrKey) | |
def htmlAttr[V](key: String, codec: Codec[V, String]): SCAttr1[V] = | |
scalatags.generic.Attr(key) | |
def prop[V, DomV](key: String, codec: Codec[V, DomV]): SCAttr2[V, DomV] = | |
scalatags.generic.Attr(key) | |
} | |
object DTView extends TestViews(DomTypes) { | |
type TModifier = Modifier | |
type TRes = Frag | |
type AtPr = AttrPair | |
implicit def tTagOps[A <: String](x: TypedTag[A]): DTView.TagOps = | |
new DTView.TagOps { | |
override def apply(t: Modifier*) = x(t: _*) | |
} | |
implicit def stringT(x: String): Frag = | |
stringFrag(x) | |
implicit def raStringAttrOps( | |
x: SCAttr2[String, String], | |
): DTView.AttrOps[String] = | |
(string: String) => x := string | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment