-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: renderDetached method for third party integrations
- Loading branch information
Showing
5 changed files
with
203 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.raquo.laminar.nodes | ||
|
||
/** This class lets you manually manage the lifecycle of a single | ||
* Laminar element. This is useful when you need to pass a DOM | ||
* element to a third party JS library, and you want that element | ||
* to be managed by Laminar. | ||
*/ | ||
class DetachedRoot[+El <: ReactiveElement.Base]( | ||
val node: El, | ||
activateNow: Boolean | ||
) { | ||
|
||
if (activateNow) { | ||
activate() | ||
} | ||
|
||
@inline def ref: node.ref.type = node.ref | ||
|
||
/** Start the element's subscriptions, as if it was mounted. */ | ||
def activate(): Unit = { | ||
node.dynamicOwner.activate() | ||
} | ||
|
||
/** Stop the element's subscriptions, as if it was unmounted. */ | ||
def deactivate(): Unit = { | ||
node.dynamicOwner.deactivate() | ||
} | ||
|
||
/** Returns true if the element's subscriptions are currently active. */ | ||
def isActive: Boolean = { | ||
node.dynamicOwner.isActive | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.raquo.laminar | ||
|
||
import com.raquo.laminar.api.L._ | ||
import com.raquo.laminar.utils.UnitSpec | ||
|
||
class DetachedRootSpec extends UnitSpec { | ||
|
||
it("node lifecycle - activateNow = false") { | ||
|
||
val nameVar = Var("world") | ||
|
||
val root = renderDetached( | ||
div("hello", child.text <-- nameVar), | ||
activateNow = false | ||
) | ||
|
||
expectNode( | ||
root.node.ref, | ||
div of ("hello", sentinel) | ||
) | ||
|
||
// -- | ||
|
||
assertEquals(root.isActive, false) | ||
|
||
root.activate() | ||
|
||
expectNode( | ||
root.node.ref, | ||
div of ("hello", "world") | ||
) | ||
|
||
assertEquals(root.isActive, true) | ||
|
||
// -- | ||
|
||
root.deactivate() | ||
|
||
nameVar.set("ignored-name") | ||
|
||
expectNode( | ||
root.node.ref, | ||
div of("hello", "world") | ||
) | ||
|
||
assertEquals(root.isActive, false) | ||
} | ||
|
||
it("node lifecycle - activateNow = true") { | ||
|
||
val nameVar = Var("world") | ||
|
||
val root = renderDetached( | ||
div("hello", child.text <-- nameVar), | ||
activateNow = true | ||
) | ||
|
||
expectNode( | ||
root.node.ref, | ||
div of ("hello", "world") | ||
) | ||
|
||
assertEquals(root.isActive, true) | ||
|
||
// -- | ||
|
||
nameVar.set("you") | ||
|
||
expectNode( | ||
root.node.ref, | ||
div of("hello", "you") | ||
) | ||
|
||
// -- | ||
|
||
root.deactivate() | ||
|
||
nameVar.set("ignored-name") | ||
|
||
expectNode( | ||
root.node.ref, | ||
div of("hello", "you") | ||
) | ||
} | ||
} |
This file contains 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