The VirtualDom[Output <: FragT, FragT] trait allows you to use Scalatags' DSL to construct fragments for any virtual DOM implementation (e.g., Scala XML trees, Preact/React VDOM nodes in the browser).
To use it, you must implement a custom VirtualDom instance by providing the following factory methods:
stringToFrag(s: String): FragT: Converts a plain string into your VDOM fragment type.rawToFrag(s: String): FragT: Converts a raw HTML string into your VDOM fragment type.makeBuilder(tag: String): vdom.Builder[Output, FragT]: Creates a builder for a specific HTML tag.
Once instantiated, you can access various DSL namespaces like attrs, tags, styles, and svgTags to build your tree.
// Conceptual implementation pattern
object MyReactDom extends VirtualDom[ReactNode, ReactNode] {
def stringToFrag(s: String): ReactNode = TODO("Convert string to React node")
def rawToFrag(s: String): ReactNode = TODO("Convert raw HTML to React node")
def makeBuilder(tag: String): vdom.Builder[ReactNode, ReactNode] = TODO("Return a builder for React")
// The trait provides namespaces like:
// object attrs, object tags, object styles, etc.
}
// Usage:
val myElement = MyReactDom.tags.div(MyReactDom.attrs.id := "foo")(MyReactDom.stringFrag("hello"))