aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/reading/component/widget/Modal.scala
blob: 81d0c7850e7165298298f539d5a88fd30ababae6 (plain)
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 = 250,
      transition = Transition.easeOut,
      animate = (progress, element) => {
      element.style.opacity = s"$progress"
      element.childNodes(2) match {
        case e: HTMLElement => e.style.transform = s"scale(${0.85 + 0.15 * progress})"
      }
    }
    )

    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}"
    )
}