Skip to content

Instantly share code, notes, and snippets.

@chandu0101
Last active August 29, 2015 14:19
Show Gist options
  • Save chandu0101/98b8e9c3a78310ba4228 to your computer and use it in GitHub Desktop.
Save chandu0101/98b8e9c3a78310ba4228 to your computer and use it in GitHub Desktop.
//style attr ( flex,padding,..)
case class NativeStyle[T](name : String) {
def := (v :T) = new NativeStylePair[T](name,v)
}
// style attr and value pair (flex = 1 , alignItems = center)
case class NativeStylePair[T](key : String ,value : T)
// define supported css attrs with corresponding type
trait NativeAttrs {
val flex = new NativeStyle[Int]("flex")
object alignItems extends NativeStyle[String]("alignItems") {
val center = this := "center"
}
}
//base stylesheet
trait NativeStyleSheet {
object dsl extends NativeAttrs {
}
// i want to accept all pairs in style def method
//as NativeStylePair typed compiler failing here whats the best way to acheive this ..
def style(v: NativeStylePair*) = {
val p = js.Dictionary.empty[Any]
v.foreach(t => p.update(t.key, t.value))
p
}
}
// realworld example
object Styles extends NativeStyleSheet {
import dsl._
val container = style(flex := 1,
alignItems.center)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment