Created
August 19, 2016 07:59
-
-
Save vovateleport/18e138d8ba1e7293c5a4365b55be3e5e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 japgolly.scalajs.react._ | |
import japgolly.scalajs.react.vdom.prefix_<^._ | |
import org.scalajs.dom._ | |
object Portal { | |
def apply(kidz: ReactElement*) = component(Props(kidz)) | |
case class Props(kidz: Seq[ReactElement]) | |
private val component = ReactComponentB[Props]("Portal") | |
.renderBackend[Backend] | |
.componentDidMount(cdm => cdm.backend.init() >> cdm.backend.update()) | |
.componentDidUpdate(cdu => cdu.$.backend.update()) | |
.componentWillUnmount(cwu => cwu.backend.remove()) | |
.build | |
class Backend($: BackendScope[Props, Unit]) { | |
val nodeWrap: Element = document.createElement("div") | |
def render():ReactElement = <.div() | |
def init() = Callback ( document.body.appendChild(nodeWrap) ) | |
def remove() = Callback( document.body.removeChild(nodeWrap) ) | |
def update() = $.props >>= { p => | |
val aa:ReactElement = p.kidz match { | |
case Seq(x) => x | |
case _ => <.div(p.kidz) | |
} | |
ReactDOM.render(aa, nodeWrap) | |
Callback.empty | |
} | |
} | |
} | |
object ModalDialog { | |
def apply(title: String, body: Seq[ReactElement], footer: Seq[ReactElement], closeCB: Callback) = component(Props(title, body, footer, closeCB)) | |
case class Props(title: String, body: Seq[ReactElement], footer: Seq[ReactElement], closeCB: Callback) | |
private val component = ReactComponentB[Props]("ModalDialog") | |
.render_P{ p => | |
Portal( | |
<.div(^.cls:="container", | |
<.div(^.cls:="modal", ^.display.block, | |
<.div(^.cls:="modal-dialog", | |
<.div(^.cls:="modal-content", | |
<.div(^.cls:="modal-header", | |
<.button(^.cls:="close", ^.tpe:="button", ^.onClick-->p.closeCB, <.span(^.dangerouslySetInnerHtml("×"))), | |
<.h4(^.cls:="modal-title",p.title) | |
), | |
p.body.nonEmpty ?= <.div(^.cls:="modal-body", p.body), | |
p.footer.nonEmpty ?= <.div(^.cls:="modal-footer", p.footer) | |
) | |
) | |
) | |
), | |
<.div(^.cls:="modal-backdrop in") | |
) | |
} | |
.build | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment