Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mountInto extension #347

Closed
wants to merge 5 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions calico/src/main/scala/calico/syntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package syntax

import calico.html.Modifier
import cats.Functor
import cats.Monad
import cats.effect.kernel.Resource
import fs2.concurrent.SignallingRef
import fs2.dom.Dom
import fs2.dom.Element
import monocle.Lens

extension [F[_]](component: Resource[F, fs2.dom.Node[F]])
Expand All @@ -35,3 +37,11 @@ extension [F[_], A](sigRef: SignallingRef[F, A])
extension [E](e: E)
inline def modify[F[_], A](a: A)(using m: Modifier[F, E, A]): Resource[F, Unit] =
m.modify(a, e)

extension [F[_]](component: Resource[F, Element[F]])
def mountInto(rootElement: F[Element[F]])(using Monad[F], Dom[F]): Resource[F, Element[F]] = {
gmkumar2005 marked this conversation as resolved.
Show resolved Hide resolved
for {
root <- Resource.eval(rootElement)
gmkumar2005 marked this conversation as resolved.
Show resolved Hide resolved
_ <- component.flatMap(e => Resource.make(root.appendChild(e))(_ => root.removeChild(e)))
} yield root
}
Loading