1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
package reading.component.widget
import scala.util.Random
import org.scalajs.dom.raw.HTMLElement
import rx._
import rx.Ctx.Owner.Unsafe._
import scalacss.Defaults._
import scalacss.ScalatagsCss._
import scalatags.JsDom.all._
import reading.component.widget.style.{ Modal => ModalStyle }
import reading.utils.{ RxAttr }
object Modal {
def apply(onClose: => Unit)(content: HtmlTag): HtmlTag = {
val modalId = s"modal${Random.nextInt}"
Animate(
id = modalId,
duration = 300,
transition = Transition.easeOut,
animate = (progress, element) => {
element.style.opacity = s"$progress"
element.childNodes(2) match {
case e: HTMLElement => e.style.transform = s"translateY(${40 * (progress - 1)}px)"
}
}
)
div(
ModalStyle.render,
ModalStyle.modal,
id := modalId,
div(
ModalStyle.curtain,
RxAttr(onclick, Rx(() => close(modalId, onClose)))
),
div(
ModalStyle.content,
content,
button(
ModalStyle.close,
RxAttr(onclick, Rx(() => close(modalId, onClose))),
"Fermer"
)
)
)
}
private def close(modalId: String, onClose: => Unit): Unit =
Animate(
id = modalId,
duration = 300,
transition = Transition.linear,
onEnd = onClose,
animate = (progress, element) => element.style.opacity = s"${1 - progress}"
)
}
|